Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / Popup.java
blob308cd662d8de7e2deca9e6bc4972a5a79c3a523d
1 /* Popup.java --
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package javax.swing;
41 import java.awt.Component;
42 import java.awt.FlowLayout;
43 import java.awt.Point;
44 import java.awt.Rectangle;
47 /**
48 * Manages a popup window that displays a Component on top of
49 * everything else.
51 * <p>To obtain an instance of <code>Popup</code>, use the
52 * {@link javax.swing.PopupFactory}.
54 * @since 1.4
56 * @author Sascha Brawer (brawer@dandelis.ch)
58 public class Popup
60 /**
61 * Constructs a new <code>Popup</code> given its owner,
62 * contents and the screen position where the popup
63 * will appear.
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,
80 int x, int y)
82 if (contents == null)
83 throw new IllegalArgumentException();
85 // The real stuff happens in the implementation of subclasses,
86 // for instance JWindowPopup.
90 /**
91 * Constructs a new <code>Popup</code>.
93 protected Popup()
95 // Nothing to do here.
99 /**
100 * Displays the <code>Popup</code> on the screen. Nothing happens
101 * if it is currently shown.
103 public void show()
105 // Implemented by subclasses, for instance JWindowPopup.
110 * Removes the <code>Popup</code> from the screen. Nothing happens
111 * if it is currently hidden.
113 public void hide()
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
128 extends Popup
131 * The <code>JWindow</code> used for displaying the contents
132 * of the popup.
134 JWindow window;
136 private Component contents;
139 * Constructs a new <code>JWindowPopup</code> given its owner,
140 * contents and the screen position where the popup
141 * will appear.
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,
158 int x, int y)
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.
175 public void show()
177 window.setSize(contents.getSize());
178 window.show();
183 * Removes the popup's <code>JWindow</code> from the
184 * screen. Nothing happens if it is currently not visible.
186 public void hide()
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
191 * show().
193 window.dispose();
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.
208 Component owner;
211 * The contents that should be shown.
213 Component contents;
216 * The X location in screen coordinates.
218 int x;
221 * The Y location in screen coordinates.
223 int y;
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
238 * will appear.
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
247 * coordinates
249 * @param y the vertical position where the Popup will appear in screen
250 * coordinates
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);
258 this.owner = owner;
259 this.contents = contents;
260 this.x = x;
261 this.y = y;
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
270 * makes it visible.
272 public void show()
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).
277 if (panel == null)
279 panel = new JPanel();
280 panel.setLayout(new FlowLayout(0, 0, 0));
283 panel.add(contents);
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);
288 panel.repaint();
292 * Removes the popup from the JLayeredPane thus making it invisible.
294 public void hide()
296 Rectangle bounds = panel.getBounds();
297 layeredPane.remove(panel);
298 layeredPane.repaint(bounds.x, bounds.y, bounds.width, bounds.height);