Popup.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Popup.java --
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import java.awt.Component;
  33. import java.awt.FlowLayout;
  34. import java.awt.Point;
  35. import java.awt.Rectangle;
  36. /**
  37. * Manages a popup window that displays a Component on top of
  38. * everything else.
  39. *
  40. * <p>To obtain an instance of <code>Popup</code>, use the
  41. * {@link javax.swing.PopupFactory}.
  42. *
  43. * @since 1.4
  44. *
  45. * @author Sascha Brawer (brawer@dandelis.ch)
  46. */
  47. public class Popup
  48. {
  49. /**
  50. * Constructs a new <code>Popup</code> given its owner,
  51. * contents and the screen position where the popup
  52. * will appear.
  53. *
  54. * @param owner the Component to which <code>x</code> and
  55. * <code>y</code> are relative, or <code>null</code> for
  56. * placing the popup relative to the origin of the screen.
  57. *
  58. * @param contents the contents that will be displayed inside
  59. * the <code>Popup</code>.
  60. *
  61. * @param x the horizontal position where the Popup will appear.
  62. *
  63. * @param y the vertical position where the Popup will appear.
  64. *
  65. * @throws IllegalArgumentException if <code>contents</code>
  66. * is <code>null</code>.
  67. */
  68. protected Popup(Component owner, Component contents,
  69. int x, int y)
  70. {
  71. if (contents == null)
  72. throw new IllegalArgumentException();
  73. // The real stuff happens in the implementation of subclasses,
  74. // for instance JWindowPopup.
  75. }
  76. /**
  77. * Constructs a new <code>Popup</code>.
  78. */
  79. protected Popup()
  80. {
  81. // Nothing to do here.
  82. }
  83. /**
  84. * Displays the <code>Popup</code> on the screen. Nothing happens
  85. * if it is currently shown.
  86. */
  87. public void show()
  88. {
  89. // Implemented by subclasses, for instance JWindowPopup.
  90. }
  91. /**
  92. * Removes the <code>Popup</code> from the screen. Nothing happens
  93. * if it is currently hidden.
  94. */
  95. public void hide()
  96. {
  97. // Implemented by subclasses, for instance JWindowPopup.
  98. }
  99. /**
  100. * A <code>Popup</code> that uses a <code>JWindow</code> for
  101. * displaying its contents.
  102. *
  103. * @see PopupFactory#getPopup
  104. *
  105. * @author Sascha Brawer (brawer@dandelis.ch)
  106. */
  107. static class JWindowPopup
  108. extends Popup
  109. {
  110. /**
  111. * The <code>JWindow</code> used for displaying the contents
  112. * of the popup.
  113. */
  114. JWindow window;
  115. private Component contents;
  116. /**
  117. * Constructs a new <code>JWindowPopup</code> given its owner,
  118. * contents and the screen position where the popup
  119. * will appear.
  120. *
  121. * @param owner the Component to which <code>x</code> and
  122. * <code>y</code> are relative, or <code>null</code> for
  123. * placing the popup relative to the origin of the screen.
  124. *
  125. * @param contents the contents that will be displayed inside
  126. * the <code>Popup</code>.
  127. *
  128. * @param x the horizontal position where the Popup will appear.
  129. *
  130. * @param y the vertical position where the Popup will appear.
  131. *
  132. * @throws IllegalArgumentException if <code>contents</code>
  133. * is <code>null</code>.
  134. */
  135. public JWindowPopup(Component owner, Component contents,
  136. int x, int y)
  137. {
  138. /* Checks whether contents is null. */
  139. super(owner, contents, x, y);
  140. this.contents = contents;
  141. window = new JWindow(SwingUtilities.getWindowAncestor(owner));
  142. window.getContentPane().add(contents);
  143. window.setLocation(x, y);
  144. window.setFocusableWindowState(false);
  145. }
  146. /**
  147. * Displays the popup's <code>JWindow</code> on the screen.
  148. * Nothing happens if it is already visible.
  149. */
  150. public void show()
  151. {
  152. window.setSize(contents.getSize());
  153. window.show();
  154. }
  155. /**
  156. * Removes the popup's <code>JWindow</code> from the
  157. * screen. Nothing happens if it is currently not visible.
  158. */
  159. public void hide()
  160. {
  161. /* Calling dispose() instead of hide() will conserve native
  162. * system resources, for example memory in an X11 server.
  163. * They will automatically be re-allocated by a call to
  164. * show().
  165. */
  166. window.dispose();
  167. }
  168. }
  169. /**
  170. * A popup that displays itself within the JLayeredPane of a JRootPane of
  171. * the containment hierarchy of the owner component.
  172. *
  173. * @author Roman Kennke (kennke@aicas.com)
  174. */
  175. static class LightweightPopup extends Popup
  176. {
  177. /**
  178. * The owner component for this popup.
  179. */
  180. Component owner;
  181. /**
  182. * The contents that should be shown.
  183. */
  184. Component contents;
  185. /**
  186. * The X location in screen coordinates.
  187. */
  188. int x;
  189. /**
  190. * The Y location in screen coordinates.
  191. */
  192. int y;
  193. /**
  194. * The panel that holds the content.
  195. */
  196. private JPanel panel;
  197. /**
  198. * The layered pane of the owner.
  199. */
  200. private JLayeredPane layeredPane;
  201. /**
  202. * Constructs a new <code>LightweightPopup</code> given its owner,
  203. * contents and the screen position where the popup
  204. * will appear.
  205. *
  206. * @param owner the component that should own the popup window; this
  207. * provides the JRootPane in which we place the popup window
  208. *
  209. * @param contents the contents that will be displayed inside
  210. * the <code>Popup</code>.
  211. *
  212. * @param x the horizontal position where the Popup will appear in screen
  213. * coordinates
  214. *
  215. * @param y the vertical position where the Popup will appear in screen
  216. * coordinates
  217. *
  218. * @throws IllegalArgumentException if <code>contents</code>
  219. * is <code>null</code>.
  220. */
  221. public LightweightPopup(Component owner, Component contents, int x, int y)
  222. {
  223. super(owner, contents, x, y);
  224. this.owner = owner;
  225. this.contents = contents;
  226. this.x = x;
  227. this.y = y;
  228. JRootPane rootPane = SwingUtilities.getRootPane(owner);
  229. JLayeredPane layeredPane = rootPane.getLayeredPane();
  230. this.layeredPane = layeredPane;
  231. }
  232. /**
  233. * Places the popup within the JLayeredPane of the owner component and
  234. * makes it visible.
  235. */
  236. public void show()
  237. {
  238. // We insert a JPanel between the layered pane and the contents so we
  239. // can fiddle with the setLocation() method without disturbing a
  240. // JPopupMenu (which overrides setLocation in an unusual manner).
  241. if (panel == null)
  242. {
  243. panel = new JPanel();
  244. panel.setLayout(new FlowLayout(0, 0, 0));
  245. }
  246. panel.add(contents);
  247. panel.setSize(contents.getSize());
  248. Point layeredPaneLoc = layeredPane.getLocationOnScreen();
  249. panel.setLocation(x - layeredPaneLoc.x, y - layeredPaneLoc.y);
  250. layeredPane.add(panel, JLayeredPane.POPUP_LAYER, 0);
  251. panel.repaint();
  252. }
  253. /**
  254. * Removes the popup from the JLayeredPane thus making it invisible.
  255. */
  256. public void hide()
  257. {
  258. Rectangle bounds = panel.getBounds();
  259. layeredPane.remove(panel);
  260. layeredPane.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
  261. }
  262. }
  263. }