1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.lwawt.macosx;
  26 
  27 import java.awt.AWTKeyStroke;
  28 import java.awt.Toolkit;
  29 import java.lang.reflect.InvocationTargetException;
  30 import sun.awt.EmbeddedFrame;
  31 import sun.lwawt.LWToolkit;
  32 import sun.lwawt.LWWindowPeer;
  33 
  34 public class CViewEmbeddedFrame extends EmbeddedFrame {
  35 
  36     private final long nsViewPtr;
  37 
  38     private boolean isActive = false;
  39 
  40     public CViewEmbeddedFrame(long nsViewPtr) {
  41         this.nsViewPtr = nsViewPtr;
  42     }
  43 
  44     @SuppressWarnings("deprecation")
  45     @Override
  46     public void addNotify() {
  47         if (getPeer() == null) {
  48             LWToolkit toolkit = (LWToolkit) Toolkit.getDefaultToolkit();
  49             setPeer(toolkit.createEmbeddedFrame(this));
  50         }
  51         super.addNotify();
  52     }
  53 
  54     public long getEmbedderHandle() {
  55         return nsViewPtr;
  56     }
  57 
  58     @Override
  59     public void registerAccelerator(AWTKeyStroke awtks) {
  60     }
  61 
  62     @Override
  63     public void unregisterAccelerator(AWTKeyStroke awtks) {
  64     }
  65 
  66     public boolean isParentWindowActive() {
  67         return isActive;
  68     }
  69     
  70     @Override
  71     public void synthesizeWindowActivation(boolean activated) {
  72         if (isActive != activated) {
  73             isActive = activated;
  74             ((LWWindowPeer)getPeer()).notifyActivation(activated);
  75         }
  76     }
  77 
  78     /*
  79      * Initializes the embedded frame bounds and validates a component.
  80      * Designed to be called from the main thread.
  81      */
  82     @SuppressWarnings("deprecation")
  83     public void validateWithBounds(final int x, final int y, final int width, final int height) {
  84         try {
  85             LWCToolkit.invokeAndWait(new Runnable() {
  86                 @Override
  87                 public void run() {
  88                     ((LWWindowPeer) getPeer()).setBoundsPrivate(0, 0, width, height);
  89                     validate();
  90                     setVisible(false);
  91                     setVisible(true);
  92                 }
  93             }, null);
  94         } catch (InterruptedException|InvocationTargetException ex) {}
  95     }
  96 }