Source for javax.swing.plaf.metal.MetalToolBarUI

   1: /* MetalToolBarUI.java
   2:    Copyright (C) 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.metal;
  40: 
  41: import java.awt.Graphics;
  42: import java.awt.Point;
  43: import java.awt.event.ContainerListener;
  44: import java.awt.event.MouseEvent;
  45: 
  46: import java.beans.PropertyChangeListener;
  47: 
  48: import javax.swing.JComponent;
  49: import javax.swing.JToolBar;
  50: import javax.swing.SwingConstants;
  51: import javax.swing.UIManager;
  52: import javax.swing.border.Border;
  53: import javax.swing.event.MouseInputListener;
  54: import javax.swing.plaf.ComponentUI;
  55: import javax.swing.plaf.basic.BasicToolBarUI;
  56: 
  57: /**
  58:  * A UI delegate for the {@link JToolBar} component.
  59:  */
  60: public class MetalToolBarUI extends BasicToolBarUI
  61: {
  62:   
  63:   /**
  64:    * A listener (no longer used) that responds when components are added to or 
  65:    * removed from the {@link JToolBar}.  The required behaviour is now
  66:    * handled in the super class. 
  67:    * 
  68:    * @see MetalToolBarUI#createContainerListener()
  69:    */
  70:   protected class MetalContainerListener
  71:     extends BasicToolBarUI.ToolBarContListener
  72:   {
  73:     /**
  74:      * Creates a new instance.
  75:      */
  76:     protected MetalContainerListener()
  77:     {
  78:       // Nothing to do here.
  79:     }
  80:   }
  81: 
  82:   /**
  83:    * A listener (no longer used) that responds to property change events in a
  84:    * {@link JToolBar} component.  The required behaviour is now handled in the 
  85:    * super class. 
  86:    * 
  87:    * @see MetalToolBarUI#createRolloverListener()
  88:    */
  89:   protected class MetalRolloverListener
  90:     extends BasicToolBarUI.PropertyListener
  91:   {
  92:     /**
  93:      * Creates a new instance.
  94:      */
  95:     protected MetalRolloverListener()
  96:     {
  97:       // Nothing to do here.
  98:     }
  99:   }
 100:   
 101:   /** 
 102:    * The container listener (an implementation specific field, according to the
 103:    * spec, and not used in GNU Classpath).
 104:    */
 105:   protected ContainerListener contListener;
 106:   
 107:   /** 
 108:    * The rollover listener (an implementation specific field, according to the
 109:    * spec, and not used in GNU Classpath). 
 110:    */
 111:   protected PropertyChangeListener rolloverListener;
 112: 
 113:   /**
 114:    * Creates a new instance of this UI delegate.
 115:    */
 116:   public MetalToolBarUI()
 117:   {
 118:     super();
 119:   }
 120: 
 121:   /**
 122:    * Returns a new instance of <code>MetalToolBarUI</code>.
 123:    *
 124:    * @param component  the component for which we return an UI instance
 125:    *
 126:    * @return A new instance of <code>MetalToolBarUI</code>.
 127:    */
 128:   public static ComponentUI createUI(JComponent component)
 129:   {
 130:     return new MetalToolBarUI();
 131:   }
 132:   
 133:   /**
 134:    * Returns <code>null</code> as permitted by recent versions of the API
 135:    * specification.  Originally it seems this method returned a new instance of 
 136:    * {@link MetalRolloverListener}, but this is now redundant.
 137:    * 
 138:    * @return <code>null</code>.
 139:    */
 140:   protected PropertyChangeListener createRolloverListener()
 141:   {
 142:     return null;
 143:   }
 144:   
 145:   /**
 146:    * Returns <code>null</code> as permitted by recent versions of the API
 147:    * specification.  Originally it seems this method returned a new instance of 
 148:    * {@link MetalContainerListener}, but this is now redundant.
 149:    * 
 150:    * @return <code>null</code>.
 151:    */
 152:   protected ContainerListener createContainerListener()
 153:   {
 154:     return null;
 155:   }
 156:   
 157:   /**
 158:    * Returns a border with no rollover effect for buttons in the tool bar.
 159:    * 
 160:    * @return A border.
 161:    * 
 162:    * @see MetalBorders#getToolbarButtonBorder()
 163:    */
 164:   protected Border createNonRolloverBorder()
 165:   {
 166:     return MetalBorders.getToolbarButtonBorder();   
 167:   }
 168:   
 169:   /**
 170:    * Sets the offset for the window used for dragging the toolbar.
 171:    * It is set as long as the window is not null (it has been installed).
 172:    */
 173:   protected void setDragOffset(Point p)
 174:   {
 175:     if (dragWindow != null)
 176:       dragWindow.setOffset(p);
 177:   }
 178:   
 179:   /** 
 180:    * Creates and returns an instance of MetalDockingListener.
 181:    * 
 182:    * @return an instance of MetalDockingListener.
 183:    */
 184:   protected MouseInputListener createDockingListener()
 185:   {
 186:     return new MetalDockingListener(toolBar);
 187:   }
 188:   
 189:   /**
 190:    * This is the MouseHandler class that allows the user to drag the JToolBar
 191:    * in and out of the parent and dock it if it can.
 192:    */
 193:   protected class MetalDockingListener extends BasicToolBarUI.DockingListener
 194:   {    
 195:     /**
 196:      * Creates a new DockingListener object.
 197:      *
 198:      * @param t The JToolBar this DockingListener is being used for.
 199:      */
 200:     public MetalDockingListener(JToolBar t)
 201:     {
 202:       super(t);
 203:     }
 204:     
 205:     /**
 206:      * This method is called when the mouse is pressed in the JToolBar. If the
 207:      * press doesn't occur in a place where it causes the JToolBar to be
 208:      * dragged, it returns. Otherwise, it starts a drag session.
 209:      *
 210:      * @param e The MouseEvent.
 211:      */
 212:     public void mousePressed(MouseEvent e)
 213:     {
 214:       super.mousePressed(e);
 215:       setDragOffset(new Point(e.getX(), e.getY()));
 216:     }
 217:     
 218:     /**
 219:      * This method is called when the mouse is dragged. It delegates the drag
 220:      * painting to the dragTo method.
 221:      *
 222:      * @param e The MouseEvent.
 223:      */
 224:     public void mouseDragged(MouseEvent e)
 225:     {
 226:       // Does not do anything differently than dragging 
 227:       // BasicToolBarUI.DockingListener
 228:       super.mouseDragged(e);
 229:     }
 230:   }
 231: 
 232:   /**
 233:    * Installs the UI on the toolbar. This calls super and sets the rollover
 234:    * property according to the <code>UIManager</code> property
 235:    * &quot;ToolBar.isRollover&quot;.
 236:    *
 237:    * @param c the component to install the UI on
 238:    */
 239:   public void installUI(JComponent c)
 240:   {
 241:     super.installUI(c);
 242:     if (c instanceof JToolBar)
 243:       {
 244:         JToolBar tb = (JToolBar) c;
 245:         tb.setRollover(UIManager.getBoolean("ToolBar.isRollover"));
 246:       }
 247:   }
 248: 
 249:   /**
 250:    * Uninstalls the UI from the toolbar. This calls super and resets the
 251:    * rollover property.
 252:    *
 253:    * @param c the component to uninstall the UI from
 254:    */
 255:   public void uninstallUI(JComponent c)
 256:   {
 257:     if (c instanceof JToolBar)
 258:       {
 259:         JToolBar tb = (JToolBar) c;
 260:         tb.setRollover(false);
 261:       }
 262:     super.uninstallUI(c);
 263:   }
 264: 
 265:   /**
 266:    * Paints the background of the component if necessary and then calls
 267:    * <code>paint(g, c)</code>.
 268:    *
 269:    * This is overridden to implement the OceanTheme gradient when an OceanTheme
 270:    * is installed.
 271:    *
 272:    * @param g the graphics to use
 273:    * @param c the component to paint.
 274:    *
 275:    * @since 1.5
 276:    */
 277:   public void update(Graphics g, JComponent c)
 278:   {
 279:     // TODO: Sun's implementation uses the MenuBar.gradient here.
 280:     // I would consider this a bug, but implement it like this
 281:     // for compatibility.
 282:     if (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme
 283:         && UIManager.get("MenuBar.gradient") != null)
 284:       {
 285:         if (c.isOpaque())
 286:           {
 287:             MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(),
 288:                                      SwingConstants.VERTICAL,
 289:                                      "MenuBar.gradient");
 290:           }
 291:         paint(g, c);
 292:       }
 293:     else
 294:       {
 295:         super.update(g, c);
 296:       }
 297:   }
 298: }