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.Component;
29 import java.awt.Container;
30 import java.awt.Frame;
31 import java.awt.Graphics;
32 import java.awt.Image;
33 import java.awt.MenuBar;
34 import java.awt.MenuComponent;
35 import java.awt.Rectangle;
36 import java.awt.Toolkit;
37 import java.awt.dnd.DragGestureEvent;
38 import java.awt.dnd.DragGestureListener;
39 import java.awt.dnd.DragGestureRecognizer;
40 import java.awt.dnd.DragSource;
41 import java.awt.dnd.DropTarget;
42 import java.awt.dnd.InvalidDnDOperationException;
43 import java.awt.dnd.peer.DragSourceContextPeer;
44 import java.awt.peer.FramePeer;
45 
46 /**
47  * The class provides basic functionality for a lightweight frame
48  * implementation. A subclass is expected to provide painting to an
49  * offscreen image and access to it. Thus it can be used for lightweight
50  * embedding.
51  *
52  * @author Artem Ananiev
53  * @author Anton Tarasov
54  */
55 @SuppressWarnings("serial")
56 public abstract class LightweightFrame extends Frame {
57 
58     /**
59      * Constructs a new, initially invisible {@code LightweightFrame}
60      * instance.
61      */
LightweightFrame()62     public LightweightFrame() {
63         setUndecorated(true);
64         setResizable(true);
65         setEnabled(true);
66     }
67 
68     /**
69      * Blocks introspection of a parent window by this child.
70      *
71      * @return null
72      */
getParent()73     @Override public final Container getParent() { return null; }
74 
getGraphics()75     @Override public Graphics getGraphics() { return null; }
76 
isResizable()77     @Override public final boolean isResizable() { return true; }
78 
79     // Block modification of any frame attributes, since they aren't
80     // applicable for a lightweight frame.
81 
setTitle(String title)82     @Override public final void setTitle(String title) {}
setIconImage(Image image)83     @Override public final void setIconImage(Image image) {}
setIconImages(java.util.List<? extends Image> icons)84     @Override public final void setIconImages(java.util.List<? extends Image> icons) {}
setMenuBar(MenuBar mb)85     @Override public final void setMenuBar(MenuBar mb) {}
setResizable(boolean resizable)86     @Override public final void setResizable(boolean resizable) {}
remove(MenuComponent m)87     @Override public final void remove(MenuComponent m) {}
toFront()88     @Override public final void toFront() {}
toBack()89     @Override public final void toBack() {}
90 
addNotify()91     @Override public void addNotify() {
92         synchronized (getTreeLock()) {
93             if (getPeer() == null) {
94                 SunToolkit stk = (SunToolkit)Toolkit.getDefaultToolkit();
95                 try {
96                     setPeer(stk.createLightweightFrame(this));
97                 } catch (Exception e) {
98                     throw new RuntimeException(e);
99                 }
100             }
101             super.addNotify();
102         }
103     }
104 
setPeer(final FramePeer p)105     private void setPeer(final FramePeer p) {
106         AWTAccessor.getComponentAccessor().setPeer(this, p);
107     }
108 
109     /**
110      * Requests the peer to emulate activation or deactivation of the
111      * frame. Peers should override this method if they are to implement
112      * this functionality.
113      *
114      * @param activate if <code>true</code>, activates the frame;
115      *                 otherwise, deactivates the frame
116      */
emulateActivation(boolean activate)117     public void emulateActivation(boolean activate) {
118         ((FramePeer)getPeer()).emulateActivation(activate);
119     }
120 
121     /**
122      * Delegates the focus grab action to the client (embedding) application.
123      * The method is called by the AWT grab machinery.
124      *
125      * @see SunToolkit#grab(java.awt.Window)
126      */
grabFocus()127     public abstract void grabFocus();
128 
129     /**
130      * Delegates the focus ungrab action to the client (embedding) application.
131      * The method is called by the AWT grab machinery.
132      *
133      * @see SunToolkit#ungrab(java.awt.Window)
134      */
ungrabFocus()135     public abstract void ungrabFocus();
136 
137     /**
138      * Returns the scale factor of this frame. The default value is 1.
139      *
140      * @return the scale factor
141      * @see #notifyDisplayChanged(int)
142      */
getScaleFactor()143     public abstract int getScaleFactor();
144 
145     /**
146      * Called when display of the hosted frame is changed.
147      *
148      * @param scaleFactor the scale factor
149      */
notifyDisplayChanged(int scaleFactor)150     public abstract void notifyDisplayChanged(int scaleFactor);
151 
152     /**
153      * Host window absolute bounds.
154      */
155     private int hostX, hostY, hostW, hostH;
156 
157     /**
158      * Returns the absolute bounds of the host (embedding) window.
159      *
160      * @return the host window bounds
161      */
getHostBounds()162     public Rectangle getHostBounds() {
163         if (hostX == 0 && hostY == 0 && hostW == 0 && hostH == 0) {
164             // The client app is probably unaware of the setHostBounds.
165             // A safe fall-back:
166             return getBounds();
167         }
168         return new Rectangle(hostX, hostY, hostW, hostH);
169     }
170 
171     /**
172      * Sets the absolute bounds of the host (embedding) window.
173      */
setHostBounds(int x, int y, int w, int h)174     public void setHostBounds(int x, int y, int w, int h) {
175         hostX = x;
176         hostY = y;
177         hostW = w;
178         hostH = h;
179     }
180 
181     /**
182      * Create a drag gesture recognizer for the lightweight frame.
183      */
createDragGestureRecognizer( Class<T> abstractRecognizerClass, DragSource ds, Component c, int srcActions, DragGestureListener dgl)184     public abstract <T extends DragGestureRecognizer> T createDragGestureRecognizer(
185             Class<T> abstractRecognizerClass,
186             DragSource ds, Component c, int srcActions,
187             DragGestureListener dgl);
188 
189     /**
190      * Create a drag source context peer for the lightweight frame.
191      */
createDragSourceContextPeer(DragGestureEvent dge)192     public abstract DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException;
193 
194     /**
195      * Adds a drop target to the lightweight frame.
196      */
addDropTarget(DropTarget dt)197     public abstract void addDropTarget(DropTarget dt);
198 
199     /**
200      * Removes a drop target from the lightweight frame.
201      */
removeDropTarget(DropTarget dt)202     public abstract void removeDropTarget(DropTarget dt);
203 }
204