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