1 /*
   2  * Copyright (c) 2012, 2015, 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 
  26 
  27 package sun.lwawt.macosx;
  28 
  29 import java.awt.AWTKeyStroke;
  30 import java.awt.Toolkit;
  31 import java.lang.reflect.InvocationTargetException;
  32 
  33 import sun.awt.AWTAccessor;
  34 import sun.awt.EmbeddedFrame;
  35 import sun.lwawt.LWWindowPeer;
  36 
  37 /*
  38  * The CViewEmbeddedFrame class is used in the SWT_AWT bridge.
  39  * This is a part of public API and should not be renamed or moved
  40  */
  41 @SuppressWarnings("serial") // JDK implementation class
  42 public class CViewEmbeddedFrame extends EmbeddedFrame {
  43 
  44     private final long nsViewPtr;
  45 
  46     private boolean isActive = false;
  47 
  48     public CViewEmbeddedFrame(long nsViewPtr) {
  49         this.nsViewPtr = nsViewPtr;
  50     }
  51 
  52     @Override
  53     public void addNotify() {
  54         if (!isDisplayable()) {
  55             LWCToolkit toolkit = (LWCToolkit) Toolkit.getDefaultToolkit();
  56             setPeer(toolkit.createEmbeddedFrame(this));
  57         }
  58         super.addNotify();
  59     }
  60 
  61     public long getEmbedderHandle() {
  62         return nsViewPtr;
  63     }
  64 
  65     @Override
  66     public void registerAccelerator(AWTKeyStroke awtks) {
  67     }
  68 
  69     @Override
  70     public void unregisterAccelerator(AWTKeyStroke awtks) {
  71     }
  72 
  73     public boolean isParentWindowActive() {
  74         return isActive;
  75     }
  76 
  77     /*
  78      * Synthetic event delivery for focus management
  79      */
  80     @Override
  81     public void synthesizeWindowActivation(boolean activated) {
  82         if (isActive != activated) {
  83             isActive = activated;
  84             final LWWindowPeer peer = AWTAccessor.getComponentAccessor()
  85                                                  .getPeer(this);
  86             peer.notifyActivation(activated, null);
  87         }
  88     }
  89 
  90     /*
  91      * Initializes the embedded frame bounds and validates a component.
  92      * Designed to be called from the main thread
  93      * This method should be called once from the initialization of the SWT_AWT Bridge
  94      */
  95     public void validateWithBounds(final int x, final int y, final int width, final int height) {
  96         try {
  97             final LWWindowPeer peer = AWTAccessor.getComponentAccessor()
  98                                                  .getPeer(this);
  99             LWCToolkit.invokeAndWait(new Runnable() {
 100                 @Override
 101                 public void run() {
 102                     peer.setBoundsPrivate(0, 0, width, height);
 103                     validate();
 104                     setVisible(true);
 105                 }
 106             }, this);
 107         } catch (InvocationTargetException ex) {}
 108     }
 109 }