Source for javax.swing.plaf.basic.BasicDesktopPaneUI

   1: /* BasicDesktopPaneUI.java --
   2:    Copyright (C) 2004 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.plaf.basic;
  40: 
  41: import java.awt.Dimension;
  42: import java.awt.event.ActionEvent;
  43: import java.beans.PropertyVetoException;
  44: 
  45: import javax.swing.AbstractAction;
  46: import javax.swing.DefaultDesktopManager;
  47: import javax.swing.DesktopManager;
  48: import javax.swing.JComponent;
  49: import javax.swing.JDesktopPane;
  50: import javax.swing.JInternalFrame;
  51: import javax.swing.KeyStroke;
  52: import javax.swing.UIDefaults;
  53: import javax.swing.UIManager;
  54: import javax.swing.plaf.ComponentUI;
  55: import javax.swing.plaf.DesktopPaneUI;
  56: 
  57: /**
  58:  * This class is the UI delegate for JDesktopPane for the Basic look and feel.
  59:  */
  60: public class BasicDesktopPaneUI extends DesktopPaneUI
  61: {
  62:   /**
  63:    * This helper class is used to handle key events that cause JInternalFrames
  64:    * to be closed.
  65:    */
  66:   protected class CloseAction extends AbstractAction
  67:   {
  68:     /**
  69:      * This method is called when the action is performed.
  70:      *
  71:      * @param e The ActionEvent.
  72:      */
  73:     public void actionPerformed(ActionEvent e)
  74:     {
  75:       if (desktop.getSelectedFrame() != null)
  76:         {
  77:       try
  78:         {
  79:           desktop.getSelectedFrame().setClosed(true);
  80:         }
  81:       catch (PropertyVetoException pve)
  82:         {
  83:         }
  84:         }
  85:     }
  86: 
  87:     /**
  88:      * This method returns whether the action is enabled.
  89:      *
  90:      * @return Whether the action is enabled.
  91:      */
  92:     public boolean isEnabled()
  93:     {
  94:       if (desktop.getSelectedFrame() != null)
  95:     return desktop.getSelectedFrame().isClosable();
  96:       return false;
  97:     }
  98:   }
  99: 
 100:   /**
 101:    * This helper class is used to handle key events that cause JInternalFrames
 102:    * to be maximized.
 103:    */
 104:   protected class MaximizeAction extends AbstractAction
 105:   {
 106:     /**
 107:      * This method is called when the action is performed.
 108:      *
 109:      * @param e The ActionEvent.
 110:      */
 111:     public void actionPerformed(ActionEvent e)
 112:     {
 113:       if (desktop.getSelectedFrame() != null)
 114:         {
 115:       try
 116:         {
 117:           desktop.getSelectedFrame().setMaximum(true);
 118:         }
 119:       catch (PropertyVetoException pve)
 120:         {
 121:         }
 122:         }
 123:     }
 124: 
 125:     /**
 126:      * This method returns whether the action is enabled.
 127:      *
 128:      * @return Whether the action is enabled.
 129:      */
 130:     public boolean isEnabled()
 131:     {
 132:       if (desktop.getSelectedFrame() != null)
 133:     return desktop.getSelectedFrame().isMaximizable();
 134:       return false;
 135:     }
 136:   }
 137: 
 138:   /**
 139:    * This helper class is used to handle key events that cause JInternalFrames
 140:    * to be minimized.
 141:    */
 142:   protected class MinimizeAction extends AbstractAction
 143:   {
 144:     /**
 145:      * This method is called when the action is performed.
 146:      *
 147:      * @param e The ActionEvent.
 148:      */
 149:     public void actionPerformed(ActionEvent e)
 150:     {
 151:       if (desktop.getSelectedFrame() != null)
 152:         {
 153:       try
 154:         {
 155:           desktop.getSelectedFrame().setIcon(true);
 156:         }
 157:       catch (PropertyVetoException pve)
 158:         {
 159:         }
 160:         }
 161:     }
 162: 
 163:     /**
 164:      * This method returns whether the action is enabled.
 165:      *
 166:      * @return Whether the action is enabled.
 167:      */
 168:     public boolean isEnabled()
 169:     {
 170:       if (desktop.getSelectedFrame() != null)
 171:     return desktop.getSelectedFrame().isIconifiable();
 172:       return false;
 173:     }
 174:   }
 175: 
 176:   /**
 177:    * This helper class is used to handle key events that pass the SELECTED
 178:    * property to the next JInternalFrame in the JDesktopPane's list of
 179:    * children.
 180:    */
 181:   protected class NavigateAction extends AbstractAction
 182:   {
 183:     /**
 184:      * This method is called when the action is performed.
 185:      *
 186:      * @param e The ActionEvent.
 187:      */
 188:     public void actionPerformed(ActionEvent e)
 189:     {
 190:       // This is supposed to set the next selected frame. 
 191:       JInternalFrame[] frames = desktop.getAllFrames();
 192:       if (frames.length == 0)
 193:     return;
 194: 
 195:       JInternalFrame sFrame = frames[0];
 196:       if (desktop.getSelectedFrame() != null)
 197:     sFrame = desktop.getSelectedFrame();
 198: 
 199:       int i = 0;
 200:       for (; i < frames.length; i++)
 201:     if (frames[i] == sFrame)
 202:       break;
 203: 
 204:       // FIXME: Navigate actions go reverse too.      
 205:       if (i == frames.length)
 206:     i = 0;
 207: 
 208:       desktop.setSelectedFrame(frames[i]);
 209:     }
 210: 
 211:     /**
 212:      * This method returns whether the action is enabled.
 213:      *
 214:      * @return Whether this action is enabled.
 215:      */
 216:     public boolean isEnabled()
 217:     {
 218:       // Always true.
 219:       return true;
 220:     }
 221:   }
 222: 
 223:   /**
 224:    * This helper class is used to restore the JInternalFrame to its original
 225:    * size before maximizing or iconifying.
 226:    */
 227:   protected class OpenAction extends AbstractAction
 228:   {
 229:     /**
 230:      * This method is called when the action is performed.
 231:      *
 232:      * @param e The ActionEvent.
 233:      */
 234:     public void actionPerformed(ActionEvent e)
 235:     {
 236:       JInternalFrame frame = desktop.getSelectedFrame();
 237:       if (frame != null)
 238:         {
 239:       try
 240:         {
 241:           if (frame.isIcon())
 242:         frame.setIcon(false);
 243:           else if (frame.isMaximum())
 244:         frame.setMaximum(false);
 245:         }
 246:       catch (PropertyVetoException pve)
 247:         {
 248:         }
 249:         }
 250:     }
 251: 
 252:     /**
 253:      * This method returns whether the action is enabled.
 254:      *
 255:      * @return Whether this action is enabled.
 256:      */
 257:     public boolean isEnabled()
 258:     {
 259:       // JInternalFrames are always restorable.
 260:       return true;
 261:     }
 262:   }
 263: 
 264:   /**
 265:    * The KeyStroke associated with closing JInternalFrames.
 266:    * @deprecated
 267:    */
 268:   protected KeyStroke closeKey;
 269: 
 270:   /**
 271:    * The KeyStroke associated with maximizing JInternalFrames.
 272:    * @deprecated
 273:    */
 274:   protected KeyStroke maximizeKey;
 275: 
 276:   /**
 277:    * The KeyStroke associated with minimizing JInternalFrames.
 278:    * @deprecated
 279:    */
 280:   protected KeyStroke minimizeKey;
 281: 
 282:   /**
 283:    * The KeyStroke associated with navigating (forward?) through
 284:    * JInternalFrames.
 285:    * @deprecated
 286:    */
 287:   protected KeyStroke navigateKey;
 288: 
 289:   /**
 290:    * The KeyStroke associated with navigating (backward?) through
 291:    * JInternalFrames.
 292:    * @deprecated
 293:    */
 294:   protected KeyStroke navigateKey2;
 295: 
 296:   /** The default desktop manager used with JDesktopPane. */
 297:   protected DesktopManager desktopManager;
 298: 
 299:   /** The JDesktopPane this UI is used with. */
 300:   protected JDesktopPane desktop;
 301: 
 302:   /**
 303:    * Creates a new BasicDesktopPaneUI object.
 304:    */
 305:   public BasicDesktopPaneUI()
 306:   {
 307:   }
 308: 
 309:   /**
 310:    * This method creates a BasicDesktopPaneUI for the given JComponent.
 311:    *
 312:    * @param c The JComponent to create a UI for.
 313:    *
 314:    * @return A new BasicDesktopPaneUI.
 315:    */
 316:   public static ComponentUI createUI(JComponent c)
 317:   {
 318:     return new BasicDesktopPaneUI();
 319:   }
 320: 
 321:   /**
 322:    * This method returns the maximum size for the given JComponent.
 323:    *
 324:    * @param c The JComponent to find a maximum size for.
 325:    *
 326:    * @return The maximum size for the given JComponent.
 327:    */
 328:   public Dimension getMaximumSize(JComponent c)
 329:   {
 330:     return getPreferredSize(c);
 331:   }
 332: 
 333:   /**
 334:    * This method returns the minimum size for the given JComponent.
 335:    *
 336:    * @param c The JComponent to find a minimum size for.
 337:    *
 338:    * @return The minimum size for the given JComponent.
 339:    */
 340:   public Dimension getMinimumSize(JComponent c)
 341:   {
 342:     return getPreferredSize(c);
 343:   }
 344: 
 345:   /**
 346:    * This method returns the preferred size for the given JComponent.
 347:    *
 348:    * @param c The JComponent to find a preferred size for.
 349:    *
 350:    * @return The preferred size for the given JComponent.
 351:    */
 352:   public Dimension getPreferredSize(JComponent c)
 353:   {
 354:     // return null because JDesktopPanes don't have preferred sizes.
 355:     return null;
 356:   }
 357: 
 358:   /**
 359:    * This method installs the defaults for the JDesktopPane provided by the
 360:    * current look and feel.
 361:    */
 362:   protected void installDefaults()
 363:   {
 364:     UIDefaults defaults = UIManager.getLookAndFeelDefaults();
 365: 
 366:     desktop.setBackground(defaults.getColor("Desktop.background"));
 367:   }
 368: 
 369:   /**
 370:    * This method installs the desktop manager for the JDesktopPane.
 371:    */
 372:   protected void installDesktopManager()
 373:   {
 374:     desktopManager = new DefaultDesktopManager();
 375:     desktop.setDesktopManager(desktopManager);
 376:   }
 377: 
 378:   /**
 379:    * This method installs the keyboard actions for the JDesktopPane.
 380:    */
 381:   protected void installKeyboardActions()
 382:   {
 383:     // FIXME: create actions and keystrokes.
 384:     registerKeyboardAction();
 385:   }
 386: 
 387:   /**
 388:    * This method installs the UI for the given JComponent.
 389:    *
 390:    * @param c The JComponent to install this UI for.
 391:    */
 392:   public void installUI(JComponent c)
 393:   {
 394:     if (c instanceof JDesktopPane)
 395:       {
 396:     desktop = (JDesktopPane) c;
 397: 
 398:     installDefaults();
 399:     installDesktopManager();
 400:     installKeyboardActions();
 401:       }
 402:   }
 403: 
 404:   /**
 405:    * This method registers the actions to the appropriate Action and Input
 406:    * maps.
 407:    */
 408:   protected void registerKeyboardAction()
 409:   {
 410:     // FIXME: Do the binding.
 411:     // XXX: the gtk windows tend to intercept a lot of the
 412:     // key events for themselves. must figure a way past that
 413:     // before binding
 414:   }
 415: 
 416:   /**
 417:    * This method reverses the work done by the installDefaults method.
 418:    */
 419:   protected void uninstallDefaults()
 420:   {
 421:     desktop.setBackground(null);
 422:   }
 423: 
 424:   /**
 425:    * This method reverses the work done by the installDesktopManager method.
 426:    */
 427:   protected void uninstallDesktopManager()
 428:   {
 429:     desktopManager = null;
 430:     desktop.setDesktopManager(null);
 431:   }
 432: 
 433:   /**
 434:    * This method reverses the work done by the installKeyboardActions method.
 435:    */
 436:   protected void uninstallKeyboardActions()
 437:   {
 438:     unregisterKeyboardActions();
 439:     // FIXME: null the actions and keystrokes.
 440:   }
 441: 
 442:   /**
 443:    * This method reverses the work done by the registerKeyboardActions method.
 444:    */
 445:   protected void unregisterKeyboardActions()
 446:   {
 447:     // FIXME: unmap the keystrokes
 448:   }
 449: 
 450:   /**
 451:    * This method uninstalls the UI for the given JComponent. It should reverse
 452:    * all the work done by the installUI method.
 453:    *
 454:    * @param c The JComponent to uninstall this UI for.
 455:    */
 456:   public void uninstallUI(JComponent c)
 457:   {
 458:     uninstallKeyboardActions();
 459:     uninstallDesktopManager();
 460:     uninstallDefaults();
 461: 
 462:     desktop = null;
 463:   }
 464: }