2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.Component;
42 import java.awt.FlowLayout;
43 import java.awt.Point;
44 import java.awt.Rectangle;
48 * Manages a popup window that displays a Component on top of
51 * <p>To obtain an instance of <code>Popup</code>, use the
52 * {@link javax.swing.PopupFactory}.
56 * @author Sascha Brawer (brawer@dandelis.ch)
61 * Constructs a new <code>Popup</code> given its owner,
62 * contents and the screen position where the popup
65 * @param owner the Component to which <code>x</code> and
66 * <code>y</code> are relative, or <code>null</code> for
67 * placing the popup relative to the origin of the screen.
69 * @param contents the contents that will be displayed inside
70 * the <code>Popup</code>.
72 * @param x the horizontal position where the Popup will appear.
74 * @param y the vertical position where the Popup will appear.
76 * @throws IllegalArgumentException if <code>contents</code>
77 * is <code>null</code>.
79 protected Popup(Component owner, Component contents,
83 throw new IllegalArgumentException();
85 // The real stuff happens in the implementation of subclasses,
86 // for instance JWindowPopup.
91 * Constructs a new <code>Popup</code>.
95 // Nothing to do here.
100 * Displays the <code>Popup</code> on the screen. Nothing happens
101 * if it is currently shown.
105 // Implemented by subclasses, for instance JWindowPopup.
110 * Removes the <code>Popup</code> from the screen. Nothing happens
111 * if it is currently hidden.
115 // Implemented by subclasses, for instance JWindowPopup.
120 * A <code>Popup</code> that uses a <code>JWindow</code> for
121 * displaying its contents.
123 * @see PopupFactory#getPopup
125 * @author Sascha Brawer (brawer@dandelis.ch)
127 static class JWindowPopup
131 * The <code>JWindow</code> used for displaying the contents
136 private Component contents;
139 * Constructs a new <code>JWindowPopup</code> given its owner,
140 * contents and the screen position where the popup
143 * @param owner the Component to which <code>x</code> and
144 * <code>y</code> are relative, or <code>null</code> for
145 * placing the popup relative to the origin of the screen.
147 * @param contents the contents that will be displayed inside
148 * the <code>Popup</code>.
150 * @param x the horizontal position where the Popup will appear.
152 * @param y the vertical position where the Popup will appear.
154 * @throws IllegalArgumentException if <code>contents</code>
155 * is <code>null</code>.
157 public JWindowPopup(Component owner, Component contents,
160 /* Checks whether contents is null. */
161 super(owner, contents, x, y);
163 this.contents = contents;
164 window = new JWindow(SwingUtilities.getWindowAncestor(owner));
165 window.getContentPane().add(contents);
166 window.setLocation(x, y);
167 window.setFocusableWindowState(false);
172 * Displays the popup's <code>JWindow</code> on the screen.
173 * Nothing happens if it is already visible.
177 window.setSize(contents.getSize());
183 * Removes the popup's <code>JWindow</code> from the
184 * screen. Nothing happens if it is currently not visible.
188 /* Calling dispose() instead of hide() will conserve native
189 * system resources, for example memory in an X11 server.
190 * They will automatically be re-allocated by a call to
198 * A popup that displays itself within the JLayeredPane of a JRootPane of
199 * the containment hierarchy of the owner component.
201 * @author Roman Kennke (kennke@aicas.com)
203 static class LightweightPopup extends Popup
206 * The owner component for this popup.
211 * The contents that should be shown.
216 * The X location in screen coordinates.
221 * The Y location in screen coordinates.
226 * The panel that holds the content.
228 private JPanel panel;
231 * The layered pane of the owner.
233 private JLayeredPane layeredPane;
236 * Constructs a new <code>LightweightPopup</code> given its owner,
237 * contents and the screen position where the popup
240 * @param owner the component that should own the popup window; this
241 * provides the JRootPane in which we place the popup window
243 * @param contents the contents that will be displayed inside
244 * the <code>Popup</code>.
246 * @param x the horizontal position where the Popup will appear in screen
249 * @param y the vertical position where the Popup will appear in screen
252 * @throws IllegalArgumentException if <code>contents</code>
253 * is <code>null</code>.
255 public LightweightPopup(Component owner, Component contents, int x, int y)
257 super(owner, contents, x, y);
259 this.contents = contents;
263 JRootPane rootPane = SwingUtilities.getRootPane(owner);
264 JLayeredPane layeredPane = rootPane.getLayeredPane();
265 this.layeredPane = layeredPane;
269 * Places the popup within the JLayeredPane of the owner component and
274 // We insert a JPanel between the layered pane and the contents so we
275 // can fiddle with the setLocation() method without disturbing a
276 // JPopupMenu (which overrides setLocation in an unusual manner).
279 panel = new JPanel();
280 panel.setLayout(new FlowLayout(0, 0, 0));
284 panel.setSize(contents.getSize());
285 Point layeredPaneLoc = layeredPane.getLocationOnScreen();
286 panel.setLocation(x - layeredPaneLoc.x, y - layeredPaneLoc.y);
287 layeredPane.add(panel, JLayeredPane.POPUP_LAYER, 0);
292 * Removes the popup from the JLayeredPane thus making it invisible.
296 Rectangle bounds = panel.getBounds();
297 layeredPane.remove(panel);
298 layeredPane.repaint(bounds.x, bounds.y, bounds.width, bounds.height);