Source for javax.swing.plaf.basic.BasicMenuBarUI

   1: /* BasicMenuBarUI.java --
   2:    Copyright (C) 2002, 2004, 2005  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.ContainerEvent;
  43: import java.awt.event.ContainerListener;
  44: import java.beans.PropertyChangeEvent;
  45: import java.beans.PropertyChangeListener;
  46: 
  47: import javax.swing.BoxLayout;
  48: import javax.swing.JComponent;
  49: import javax.swing.JMenuBar;
  50: import javax.swing.UIDefaults;
  51: import javax.swing.UIManager;
  52: import javax.swing.event.ChangeEvent;
  53: import javax.swing.event.ChangeListener;
  54: import javax.swing.plaf.ComponentUI;
  55: import javax.swing.plaf.MenuBarUI;
  56: 
  57: /**
  58:  * UI Delegate for JMenuBar.
  59:  */
  60: public class BasicMenuBarUI extends MenuBarUI
  61: {
  62:   protected ChangeListener changeListener;
  63: 
  64:   /*ContainerListener that listens to the ContainerEvents fired from menu bar*/
  65:   protected ContainerListener containerListener;
  66: 
  67:   /*Property change listeners that listener to PropertyChangeEvent from menu bar*/
  68:   protected PropertyChangeListener propertyChangeListener;
  69: 
  70:   /* menu bar for which this UI delegate is for*/
  71:   protected JMenuBar menuBar;
  72: 
  73:   /**
  74:    * Creates a new BasicMenuBarUI object.
  75:    */
  76:   public BasicMenuBarUI()
  77:   {
  78:     changeListener = createChangeListener();
  79:     containerListener = createContainerListener();
  80:     propertyChangeListener = new PropertyChangeHandler();
  81:   }
  82: 
  83:   /**
  84:    * Creates ChangeListener
  85:    *
  86:    * @return The ChangeListener
  87:    */
  88:   protected ChangeListener createChangeListener()
  89:   {
  90:     return new ChangeHandler();
  91:   }
  92: 
  93:   /**
  94:    * Creates ContainerListener() to listen for ContainerEvents
  95:    * fired by JMenuBar.
  96:    *
  97:    * @return The ContainerListener
  98:    */
  99:   protected ContainerListener createContainerListener()
 100:   {
 101:     return new ContainerHandler();
 102:   }
 103: 
 104:   /**
 105:    * Factory method to create a BasicMenuBarUI for the given {@link
 106:    * JComponent}, which should be a {@link JMenuBar}.
 107:    *
 108:    * @param x The {@link JComponent} a UI is being created for.
 109:    *
 110:    * @return A BasicMenuBarUI for the {@link JComponent}.
 111:    */
 112:   public static ComponentUI createUI(JComponent x)
 113:   {
 114:     return new BasicMenuBarUI();
 115:   }
 116: 
 117:   /**
 118:    * Returns maximum size for the specified menu bar
 119:    *
 120:    * @param c component for which to get maximum size
 121:    *
 122:    * @return  Maximum size for the specified menu bar
 123:    */
 124:   public Dimension getMaximumSize(JComponent c)
 125:   {
 126:     // let layout manager calculate its size
 127:     return null;
 128:   }
 129: 
 130:   /**
 131:    * Returns maximum allowed size of JMenuBar.
 132:    *
 133:    * @param c menuBar for which to return maximum size
 134:    *
 135:    * @return Maximum size of the give menu bar.
 136:    */
 137:   public Dimension getMinimumSize(JComponent c)
 138:   {
 139:     // let layout manager calculate its size
 140:     return null;
 141:   }
 142: 
 143:   /**
 144:    * Returns preferred size of JMenuBar.
 145:    *
 146:    * @param c menuBar for which to return preferred size
 147:    *
 148:    * @return Preferred size of the give menu bar.
 149:    */
 150:   public Dimension getPreferredSize(JComponent c)
 151:   {
 152:     // let layout manager calculate its size
 153:     return null;
 154:   }
 155: 
 156:   /**
 157:    * Initializes any default properties that this UI has from the defaults for
 158:    * the Basic look and feel.
 159:    */
 160:   protected void installDefaults()
 161:   {
 162:     UIDefaults defaults = UIManager.getLookAndFeelDefaults();
 163: 
 164:     menuBar.setBackground(defaults.getColor("MenuBar.background"));
 165:     menuBar.setBorder(defaults.getBorder("MenuBar.border"));
 166:     menuBar.setFont(defaults.getFont("MenuBar.font"));
 167:     menuBar.setForeground(defaults.getColor("MenuBar.foreground"));
 168:     menuBar.setOpaque(true);
 169:   }
 170: 
 171:   /**
 172:    * This method installs the keyboard actions for the JMenuBar.
 173:    */
 174:   protected void installKeyboardActions()
 175:   {
 176:     // FIXME: implement
 177:   }
 178: 
 179:   /**
 180:    * This method installs the listeners needed for this UI to function.
 181:    */
 182:   protected void installListeners()
 183:   {
 184:     menuBar.addContainerListener(containerListener);
 185:     menuBar.addPropertyChangeListener(propertyChangeListener);
 186:   }
 187: 
 188:   /**
 189:   * Installs and initializes all fields for this UI delegate. Any properties
 190:   * of the UI that need to be initialized and/or set to defaults will be
 191:   * done now. It will also install any listeners necessary.
 192:   *
 193:   * @param c The {@link JComponent} that is having this UI installed.
 194:   */
 195:   public void installUI(JComponent c)
 196:   {
 197:     super.installUI(c);
 198:     menuBar = (JMenuBar) c;
 199:     menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.X_AXIS));
 200:     installDefaults();
 201:     installListeners();
 202:     installKeyboardActions();
 203:   }
 204: 
 205:   /**
 206:    * This method uninstalls the defaults and nulls any objects created during
 207:    * install.
 208:    */
 209:   protected void uninstallDefaults()
 210:   {
 211:     menuBar.setBackground(null);
 212:     menuBar.setBorder(null);
 213:     menuBar.setFont(null);
 214:     menuBar.setForeground(null);
 215:   }
 216: 
 217:   /**
 218:    * This method reverses the work done in installKeyboardActions.
 219:    */
 220:   protected void uninstallKeyboardActions()
 221:   {
 222:     // FIXME: implement. 
 223:   }
 224: 
 225:   /**
 226:    * Unregisters all the listeners that this UI delegate was using.
 227:    */
 228:   protected void uninstallListeners()
 229:   {
 230:     menuBar.removeContainerListener(containerListener);
 231:     menuBar.removePropertyChangeListener(propertyChangeListener);
 232:   }
 233: 
 234:   /**
 235:    * Performs the opposite of installUI. Any properties or resources that need
 236:    * to be cleaned up will be done now. It will also uninstall any listeners
 237:    * it has. In addition, any properties of this UI will be nulled.
 238:    *
 239:    * @param c The {@link JComponent} that is having this UI uninstalled.
 240:    */
 241:   public void uninstallUI(JComponent c)
 242:   {
 243:     uninstallDefaults();
 244:     uninstallListeners();
 245:     uninstallKeyboardActions();
 246:     menuBar = null;
 247:   }
 248: 
 249:   protected class ChangeHandler implements ChangeListener
 250:   {
 251:     public void stateChanged(ChangeEvent event)
 252:     {
 253:     }
 254:   }
 255: 
 256:   /**
 257:    * This class handles ContainerEvents fired by JMenuBar. It revalidates
 258:    * and repaints menu bar whenever menu is added or removed from it.
 259:    */
 260:   protected class ContainerHandler implements ContainerListener
 261:   {
 262:     /**
 263:      * This method is called whenever menu is added to the menu bar
 264:      *
 265:      * @param e The ContainerEvent.
 266:      */
 267:     public void componentAdded(ContainerEvent e)
 268:     {
 269:       menuBar.revalidate();
 270:       menuBar.repaint();
 271:     }
 272: 
 273:     /**
 274:      * This method is called whenever menu is removed from the menu bar.
 275:      *
 276:      * @param e The ContainerEvent.
 277:      */
 278:     public void componentRemoved(ContainerEvent e)
 279:     {
 280:       menuBar.revalidate();
 281:       menuBar.repaint();
 282:     }
 283:   }
 284: 
 285:   /**
 286:    * This class handles PropertyChangeEvents fired from the JMenuBar
 287:    */
 288:   protected class PropertyChangeHandler implements PropertyChangeListener
 289:   {
 290:     /**
 291:      * This method is called whenever one of the properties of the MenuBar
 292:      * changes.
 293:      *
 294:      * @param e The PropertyChangeEvent.
 295:      */
 296:     public void propertyChange(PropertyChangeEvent e)
 297:     {
 298:       if (e.getPropertyName().equals("borderPainted"))
 299:         menuBar.repaint();
 300:       if (e.getPropertyName().equals("margin"))
 301:         menuBar.repaint();
 302:     }
 303:   }
 304: }