1 /*
   2  * Copyright (c) 2013, 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 package sun.awt;
  27 
  28 import java.awt.Container;
  29 import java.awt.Frame;
  30 import java.awt.Graphics;
  31 import java.awt.Image;
  32 import java.awt.MenuBar;
  33 import java.awt.MenuComponent;
  34 import java.awt.Toolkit;
  35 import java.awt.peer.FramePeer;
  36 import java.awt.peer.WindowPeer;
  37 
  38 /**
  39  * The class provides basic functionality for a lightweight frame implementation.
  40  * A subclass is expected to provide painting to an offscreen image and access to it.
  41  * Thus it can be used for lightweight embedding.
  42  * 
  43  * @author Artem Ananiev
  44  * @author Anton Tarasov
  45  */
  46 @SuppressWarnings("serial")
  47 public abstract class LightweightFrame extends Frame {
  48 
  49     /**
  50      * Constructs a new, initially invisible {@code LightweightFrame} instance.
  51      */
  52     public LightweightFrame() {
  53         setUndecorated(true);
  54         setResizable(true);
  55         setEnabled(true);
  56     }
  57 
  58     /**
  59      * Blocks introspection of a parent window by this child.
  60      * 
  61      * @return null
  62      */    
  63     @Override public Container getParent() { return null; }
  64     
  65     @Override public Graphics getGraphics() { return null; }
  66     
  67     @Override public boolean isResizable() { return true; }
  68     
  69     // Block modification of any frame attributes, since they aren't applicable
  70     // for a lightweight frame.
  71 
  72     @Override public void setTitle(String title) {}
  73     @Override public void setIconImage(Image image) {}
  74     @Override public void setIconImages(java.util.List<? extends Image> icons) {}
  75     @Override public void setMenuBar(MenuBar mb) {}
  76     @Override public void setResizable(boolean resizable) {}
  77     @Override public void remove(MenuComponent m) {}
  78     @Override public void toFront() {}
  79     @Override public void toBack() {}
  80 
  81     @Override public void addNotify() {
  82         synchronized (getTreeLock()) {
  83             if (getPeer() == null) {
  84                 SunToolkit stk = (SunToolkit)Toolkit.getDefaultToolkit();
  85                 try {
  86                 setPeer(stk.createLightweightFrame(this));
  87                 }catch (Exception e) {
  88                     throw new RuntimeException(e);
  89                 }
  90             }
  91             super.addNotify();
  92         }
  93     }
  94     
  95     private void setPeer(final FramePeer p) {
  96         AWTAccessor.getComponentAccessor().setPeer(this, p);
  97     }
  98 
  99     /**
 100      * Requests the peer to emulate activation or deactivation of the frame.
 101      * Peers should override this method if they are to implement this functionality.
 102      * 
 103      * @param activate if <code>true</code>, activates the frame;
 104      *                 otherwise, deactivates the frame
 105      */
 106     public void emulateActivation(boolean activate) {
 107         ((FramePeer)getPeer()).emulateActivation(activate);
 108     }
 109     
 110     /**
 111      * Grabs focus.
 112      */
 113     public void grabFocus() {
 114         ((WindowPeer)getPeer()).grabFocus();
 115     }
 116     
 117     /**
 118      * Ungrabs focus.
 119      * 
 120      * @param postEvent posts {@link UngrabEvent} when {@code true}
 121      */
 122     public void ungrabFocus(boolean postEvent) {
 123         ((WindowPeer)getPeer()).ungrabFocus(postEvent);        
 124     }
 125 }