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.Point;
  43: 
  44: 
  45: /**
  46:  * Manages a popup window that displays a Component on top of
  47:  * everything else.
  48:  *
  49:  * <p>To obtain an instance of <code>Popup</code>, use the
  50:  * {@link javax.swing.PopupFactory}.
  51:  *
  52:  * @since 1.4
  53:  *
  54:  * @author Sascha Brawer (brawer@dandelis.ch)
  55:  */
  56: public class Popup
  57: {
  58:   /**
  59:    * Constructs a new <code>Popup</code> given its owner,
  60:    * contents and the screen position where the popup
  61:    * will appear.
  62:    *
  63:    * @param owner the Component to which <code>x</code> and
  64:    *        <code>y</code> are relative, or <code>null</code> for
  65:    *        placing the popup relative to the origin of the screen.
  66:    *
  67:    * @param contents the contents that will be displayed inside
  68:    *        the <code>Popup</code>.
  69:    *
  70:    * @param x the horizontal position where the Popup will appear.
  71:    *
  72:    * @param y the vertical position where the Popup will appear.
  73:    *
  74:    * @throws IllegalArgumentException if <code>contents</code>
  75:    *         is <code>null</code>.
  76:    */
  77:   protected Popup(Component owner, Component contents,
  78:                   int x, int y)
  79:   {
  80:     if (contents == null)
  81:       throw new IllegalArgumentException();
  82: 
  83:     // The real stuff happens in the implementation of subclasses,
  84:     // for instance JWindowPopup.
  85:   }
  86:   
  87:   
  88:   /**
  89:    * Constructs a new <code>Popup</code>.
  90:    */
  91:   protected Popup()
  92:   {
  93:     // Nothing to do here.
  94:   }
  95: 
  96: 
  97:   /**
  98:    * Displays the <code>Popup</code> on the screen.  Nothing happens
  99:    * if it is currently shown.
 100:    */
 101:   public void show()
 102:   {
 103:     // Implemented by subclasses, for instance JWindowPopup.
 104:   }
 105: 
 106: 
 107:   /**
 108:    * Removes the <code>Popup</code> from the screen.  Nothing happens
 109:    * if it is currently hidden.
 110:    */
 111:   public void hide()
 112:   {
 113:     // Implemented by subclasses, for instance JWindowPopup.
 114:   }
 115: 
 116: 
 117:   /**
 118:    * A <code>Popup</code> that uses a <code>JWindow</code> for
 119:    * displaying its contents.
 120:    *
 121:    * @see PopupFactory#getPopup
 122:    *
 123:    * @author Sascha Brawer (brawer@dandelis.ch)
 124:    */
 125:   static class JWindowPopup
 126:     extends Popup
 127:   {
 128:     /**
 129:      * The <code>JWindow</code> used for displaying the contents
 130:      * of the popup.
 131:      */
 132:     JWindow window;
 133: 
 134:     private Component contents;
 135: 
 136:     /**
 137:      * Constructs a new <code>JWindowPopup</code> given its owner,
 138:      * contents and the screen position where the popup
 139:      * will appear.
 140:      *
 141:      * @param owner the Component to which <code>x</code> and
 142:      *        <code>y</code> are relative, or <code>null</code> for
 143:      *        placing the popup relative to the origin of the screen.
 144:      *
 145:      * @param contents the contents that will be displayed inside
 146:      *        the <code>Popup</code>.
 147:      *
 148:      * @param x the horizontal position where the Popup will appear.
 149:      *
 150:      * @param y the vertical position where the Popup will appear.
 151:      *
 152:      * @throws IllegalArgumentException if <code>contents</code>
 153:      *         is <code>null</code>.
 154:      */
 155:     public JWindowPopup(Component owner, Component contents,
 156:                         int x, int y)
 157:     {
 158:       /* Checks whether contents is null. */
 159:       super(owner, contents, x, y);
 160: 
 161:       this.contents = contents;
 162:       window = new JWindow();
 163:       window.getContentPane().add(contents);
 164:       window.setLocation(x, y);
 165:       window.setFocusableWindowState(false);
 166:     }
 167: 
 168: 
 169:     /**
 170:      * Displays the popup's <code>JWindow</code> on the screen.
 171:      * Nothing happens if it is already visible.
 172:      */
 173:     public void show()
 174:     {
 175:       window.setSize(contents.getSize());
 176:       window.show();
 177:     }
 178:     
 179:     
 180:     /**
 181:      * Removes the popup's <code>JWindow</code> from the
 182:      * screen.  Nothing happens if it is currently not visible.
 183:      */
 184:     public void hide()
 185:     {
 186:       /* Calling dispose() instead of hide() will conserve native
 187:        * system resources, for example memory in an X11 server.
 188:        * They will automatically be re-allocated by a call to
 189:        * show().
 190:        */
 191:       window.dispose();
 192:     }
 193:   }
 194: 
 195:   /**
 196:    * A popup that displays itself within the JLayeredPane of a JRootPane of
 197:    * the containment hierarchy of the owner component.
 198:    *
 199:    * @author Roman Kennke (kennke@aicas.com)
 200:    */
 201:   static class LightweightPopup extends Popup
 202:   {
 203:     /**
 204:      * The owner component for this popup.
 205:      */
 206:     Component owner;
 207: 
 208:     /**
 209:      * The contents that should be shown.
 210:      */
 211:     Component contents;
 212: 
 213:     /**
 214:      * The X location in screen coordinates.
 215:      */
 216:     int x;
 217: 
 218:     /**
 219:      * The Y location in screen coordinates.
 220:      */
 221:     int y;
 222: 
 223:     /**
 224:      * The panel that holds the content.
 225:      */
 226:     private JPanel panel;
 227: 
 228:     /**
 229:      * Constructs a new <code>LightweightPopup</code> given its owner,
 230:      * contents and the screen position where the popup
 231:      * will appear.
 232:      *
 233:      * @param owner the component that should own the popup window; this
 234:      *        provides the JRootPane in which we place the popup window
 235:      *
 236:      * @param contents the contents that will be displayed inside
 237:      *        the <code>Popup</code>.
 238:      *
 239:      * @param x the horizontal position where the Popup will appear in screen
 240:      *        coordinates
 241:      *
 242:      * @param y the vertical position where the Popup will appear in screen
 243:      *        coordinates
 244:      *
 245:      * @throws IllegalArgumentException if <code>contents</code>
 246:      *         is <code>null</code>.
 247:      */
 248:     public LightweightPopup(Component owner, Component  contents, int x, int y)
 249:     {
 250:       super(owner, contents, x, y);
 251:       this.owner = owner;
 252:       this.contents = contents;
 253:       this.x = x;
 254:       this.y = y;
 255:     }
 256: 
 257:     /**
 258:      * Places the popup within the JLayeredPane of the owner component and
 259:      * makes it visible.
 260:      */
 261:     public void show()
 262:     {
 263:       JRootPane rootPane = SwingUtilities.getRootPane(owner);
 264:       JLayeredPane layeredPane = rootPane.getLayeredPane();
 265:       // We insert a JPanel between the layered pane and the contents so we
 266:       // can fiddle with the setLocation() method without disturbing a
 267:       // JPopupMenu (which overrides setLocation in an unusual manner).
 268:       if (panel == null)
 269:         {
 270:           panel = new JPanel();
 271:           panel.setLayout(null);
 272:         }
 273:       panel.add(contents);
 274:       panel.setSize(contents.getSize());
 275:       Point layeredPaneLoc = layeredPane.getLocationOnScreen();
 276:       panel.setLocation(x - layeredPaneLoc.x, y - layeredPaneLoc.y);
 277:       layeredPane.add(panel, JLayeredPane.POPUP_LAYER);
 278:     }
 279: 
 280:     /**
 281:      * Removes the popup from the JLayeredPane thus making it invisible.
 282:      */
 283:     public void hide()
 284:     {
 285:       JRootPane rootPane = SwingUtilities.getRootPane(owner);
 286:       JLayeredPane layeredPane = rootPane.getLayeredPane();
 287:       layeredPane.remove(panel);
 288:     }
 289:   }
 290: }