Source for javax.swing.Popup

   1: /* Popup.java --
   2:    Copyright (C) 2003 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.awt.Component;
  42: import java.awt.FlowLayout;
  43: import java.awt.Point;
  44: import java.awt.Rectangle;
  45: 
  46: 
  47: /**
  48:  * Manages a popup window that displays a Component on top of
  49:  * everything else.
  50:  *
  51:  * <p>To obtain an instance of <code>Popup</code>, use the
  52:  * {@link javax.swing.PopupFactory}.
  53:  *
  54:  * @since 1.4
  55:  *
  56:  * @author Sascha Brawer (brawer@dandelis.ch)
  57:  */
  58: public class Popup
  59: {
  60:   /**
  61:    * Constructs a new <code>Popup</code> given its owner,
  62:    * contents and the screen position where the popup
  63:    * will appear.
  64:    *
  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.
  68:    *
  69:    * @param contents the contents that will be displayed inside
  70:    *        the <code>Popup</code>.
  71:    *
  72:    * @param x the horizontal position where the Popup will appear.
  73:    *
  74:    * @param y the vertical position where the Popup will appear.
  75:    *
  76:    * @throws IllegalArgumentException if <code>contents</code>
  77:    *         is <code>null</code>.
  78:    */
  79:   protected Popup(Component owner, Component contents,
  80:                   int x, int y)
  81:   {
  82:     if (contents == null)
  83:       throw new IllegalArgumentException();
  84: 
  85:     // The real stuff happens in the implementation of subclasses,
  86:     // for instance JWindowPopup.
  87:   }
  88: 
  89: 
  90:   /**
  91:    * Constructs a new <code>Popup</code>.
  92:    */
  93:   protected Popup()
  94:   {
  95:     // Nothing to do here.
  96:   }
  97: 
  98: 
  99:   /**
 100:    * Displays the <code>Popup</code> on the screen.  Nothing happens
 101:    * if it is currently shown.
 102:    */
 103:   public void show()
 104:   {
 105:     // Implemented by subclasses, for instance JWindowPopup.
 106:   }
 107: 
 108: 
 109:   /**
 110:    * Removes the <code>Popup</code> from the screen.  Nothing happens
 111:    * if it is currently hidden.
 112:    */
 113:   public void hide()
 114:   {
 115:     // Implemented by subclasses, for instance JWindowPopup.
 116:   }
 117: 
 118: 
 119:   /**
 120:    * A <code>Popup</code> that uses a <code>JWindow</code> for
 121:    * displaying its contents.
 122:    *
 123:    * @see PopupFactory#getPopup
 124:    *
 125:    * @author Sascha Brawer (brawer@dandelis.ch)
 126:    */
 127:   static class JWindowPopup
 128:     extends Popup
 129:   {
 130:     /**
 131:      * The <code>JWindow</code> used for displaying the contents
 132:      * of the popup.
 133:      */
 134:     JWindow window;
 135: 
 136:     private Component contents;
 137: 
 138:     /**
 139:      * Constructs a new <code>JWindowPopup</code> given its owner,
 140:      * contents and the screen position where the popup
 141:      * will appear.
 142:      *
 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.
 146:      *
 147:      * @param contents the contents that will be displayed inside
 148:      *        the <code>Popup</code>.
 149:      *
 150:      * @param x the horizontal position where the Popup will appear.
 151:      *
 152:      * @param y the vertical position where the Popup will appear.
 153:      *
 154:      * @throws IllegalArgumentException if <code>contents</code>
 155:      *         is <code>null</code>.
 156:      */
 157:     public JWindowPopup(Component owner, Component contents,
 158:                         int x, int y)
 159:     {
 160:       /* Checks whether contents is null. */
 161:       super(owner, contents, x, y);
 162: 
 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);
 168:     }
 169: 
 170: 
 171:     /**
 172:      * Displays the popup's <code>JWindow</code> on the screen.
 173:      * Nothing happens if it is already visible.
 174:      */
 175:     public void show()
 176:     {
 177:       window.setSize(contents.getSize());
 178:       window.show();
 179:     }
 180: 
 181: 
 182:     /**
 183:      * Removes the popup's <code>JWindow</code> from the
 184:      * screen.  Nothing happens if it is currently not visible.
 185:      */
 186:     public void hide()
 187:     {
 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().
 192:        */
 193:       window.dispose();
 194:     }
 195:   }
 196: 
 197:   /**
 198:    * A popup that displays itself within the JLayeredPane of a JRootPane of
 199:    * the containment hierarchy of the owner component.
 200:    *
 201:    * @author Roman Kennke (kennke@aicas.com)
 202:    */
 203:   static class LightweightPopup extends Popup
 204:   {
 205:     /**
 206:      * The owner component for this popup.
 207:      */
 208:     Component owner;
 209: 
 210:     /**
 211:      * The contents that should be shown.
 212:      */
 213:     Component contents;
 214: 
 215:     /**
 216:      * The X location in screen coordinates.
 217:      */
 218:     int x;
 219: 
 220:     /**
 221:      * The Y location in screen coordinates.
 222:      */
 223:     int y;
 224: 
 225:     /**
 226:      * The panel that holds the content.
 227:      */
 228:     private JPanel panel;
 229: 
 230:     /**
 231:      * The layered pane of the owner.
 232:      */
 233:     private JLayeredPane layeredPane;
 234: 
 235:     /**
 236:      * Constructs a new <code>LightweightPopup</code> given its owner,
 237:      * contents and the screen position where the popup
 238:      * will appear.
 239:      *
 240:      * @param owner the component that should own the popup window; this
 241:      *        provides the JRootPane in which we place the popup window
 242:      *
 243:      * @param contents the contents that will be displayed inside
 244:      *        the <code>Popup</code>.
 245:      *
 246:      * @param x the horizontal position where the Popup will appear in screen
 247:      *        coordinates
 248:      *
 249:      * @param y the vertical position where the Popup will appear in screen
 250:      *        coordinates
 251:      *
 252:      * @throws IllegalArgumentException if <code>contents</code>
 253:      *         is <code>null</code>.
 254:      */
 255:     public LightweightPopup(Component owner, Component  contents, int x, int y)
 256:     {
 257:       super(owner, contents, x, y);
 258:       this.owner = owner;
 259:       this.contents = contents;
 260:       this.x = x;
 261:       this.y = y;
 262: 
 263:       JRootPane rootPane = SwingUtilities.getRootPane(owner);
 264:       JLayeredPane layeredPane = rootPane.getLayeredPane();
 265:       this.layeredPane = layeredPane;
 266:     }
 267: 
 268:     /**
 269:      * Places the popup within the JLayeredPane of the owner component and
 270:      * makes it visible.
 271:      */
 272:     public void show()
 273:     {
 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)
 278:         {
 279:           panel = new JPanel();
 280:           panel.setLayout(new FlowLayout(0, 0, 0));
 281:         }
 282: 
 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, 0);
 288:       panel.repaint();
 289:     }
 290: 
 291:     /**
 292:      * Removes the popup from the JLayeredPane thus making it invisible.
 293:      */
 294:     public void hide()
 295:     {
 296:       Rectangle bounds = panel.getBounds();
 297:       layeredPane.remove(panel);
 298:       layeredPane.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
 299:     }
 300:   }
 301: }