/[classpath]/classpath/javax/swing/plaf/basic/BasicToolBarUI.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicToolBarUI.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by mark, Sat Jun 26 16:07:03 2004 UTC revision 1.2 by mark, Thu Jul 22 19:45:39 2004 UTC
# Line 37  exception statement from your version. * Line 37  exception statement from your version. *
37    
38  package javax.swing.plaf.basic;  package javax.swing.plaf.basic;
39    
40    import java.awt.BorderLayout;
41    import java.awt.Color;
42  import java.awt.Component;  import java.awt.Component;
43  import java.awt.ComponentOrientation;  import java.awt.ComponentOrientation;
44  import java.awt.Container;  import java.awt.Container;
 import java.awt.Color;  
45  import java.awt.Dimension;  import java.awt.Dimension;
46    import java.awt.Graphics;
47  import java.awt.GridLayout;  import java.awt.GridLayout;
48    import java.awt.Insets;
49  import java.awt.Point;  import java.awt.Point;
50    import java.awt.Rectangle;
51    import java.awt.Window;
52    import java.awt.event.ContainerEvent;
53    import java.awt.event.ContainerListener;
54    import java.awt.event.FocusEvent;
55    import java.awt.event.FocusListener;
56    import java.awt.event.MouseAdapter;
57    import java.awt.event.MouseEvent;
58    import java.awt.event.WindowAdapter;
59    import java.awt.event.WindowEvent;
60    import java.awt.event.WindowListener;
61    import java.beans.PropertyChangeEvent;
62    import java.beans.PropertyChangeListener;
63  import java.util.Enumeration;  import java.util.Enumeration;
64  import javax.swing.JFrame;  import java.util.Hashtable;
65  import javax.swing.JButton;  import javax.swing.JButton;
66  import javax.swing.JComponent;  import javax.swing.JComponent;
67    import javax.swing.JDialog;
68    import javax.swing.JFrame;
69  import javax.swing.JToolBar;  import javax.swing.JToolBar;
70  import javax.swing.RootPaneContainer;  import javax.swing.RootPaneContainer;
71    import javax.swing.SwingConstants;
72    import javax.swing.SwingUtilities;
73  import javax.swing.UIDefaults;  import javax.swing.UIDefaults;
74  import javax.swing.UIManager;  import javax.swing.UIManager;
75  import javax.swing.border.Border;  import javax.swing.border.Border;
76  import javax.swing.event.MouseInputListener;  import javax.swing.event.MouseInputListener;
77    import javax.swing.plaf.BorderUIResource;
78  import java.beans.PropertyChangeListener;  import javax.swing.plaf.BorderUIResource.EtchedBorderUIResource;
 import java.awt.event.ContainerListener;  
 import java.awt.event.FocusListener;  
 import java.awt.event.MouseAdapter;  
 import java.awt.event.MouseEvent;  
 import java.awt.event.WindowListener;  
   
79  import javax.swing.plaf.ComponentUI;  import javax.swing.plaf.ComponentUI;
80  import javax.swing.plaf.ToolBarUI;  import javax.swing.plaf.ToolBarUI;
81    import javax.swing.plaf.UIResource;
82    
83  public class BasicToolBarUI extends ToolBarUI  
84    /**
85     * This is the Basic Look and Feel UI class for JToolBar.
86     */
87    public class BasicToolBarUI extends ToolBarUI implements SwingConstants
88  {  {
89      /** Static owner of all DragWindows. */
90      private static JFrame owner = new JFrame();
91    
92      /** The border used when the JToolBar is in nonrollover mode. */
93      private static Border nonRolloverBorder;
94    
95    public class DragWindow    /** The border used when the JToolBar is in rollover mode. */
96    {}    private static Border rolloverBorder;
97    
98      /** The last known BorderLayout constraint before floating. */
99      protected String constraintBeforeFloating;      protected String constraintBeforeFloating;
100    
101      /** The last known orientation of the JToolBar before floating. */
102      private int lastGoodOrientation;
103    
104      /** The color of the border when it is dockable. */
105      protected Color dockingBorderColor;      protected Color dockingBorderColor;
106    
107      /** The background color of the JToolBar when it is dockable. */
108      protected Color dockingColor;      protected Color dockingColor;
109    
110      /** The docking listener responsible for mouse events on the JToolBar. */
111      protected MouseInputListener dockingListener;      protected MouseInputListener dockingListener;
112      //protected KeyStroke downKey  
113      //          Deprecated. As of Java 2 platform v1.3.    /** The window used for dragging the JToolBar. */
114      protected BasicToolBarUI.DragWindow dragWindow;      protected BasicToolBarUI.DragWindow dragWindow;
115    
116      /** The color of the border when it is not dockable. */
117      protected Color floatingBorderColor;      protected Color floatingBorderColor;
118    
119      /** The background color of the JToolBar when it is not dockable. */
120      protected Color floatingColor;      protected Color floatingColor;
121    
122      /** The index of the focused component. */
123      protected int focusedCompIndex;      protected int focusedCompIndex;
124      //protected KeyStroke leftKey;  
125      //          Deprecated. As of Java 2 platform v1.3.    /** The PropertyChangeListener for the JToolBar. */
126      protected PropertyChangeListener propertyListener;      protected PropertyChangeListener propertyListener;
127      //protected KeyStroke rightKey;  
128      //          Deprecated. As of Java 2 platform v1.3.    /** The JToolBar this UI delegate is responsible for. */
129      protected JToolBar toolBar;      protected JToolBar toolBar;
130    
131      /** The Container listener for the JToolBar. */
132      protected ContainerListener toolBarContListener;      protected ContainerListener toolBarContListener;
     protected FocusListener toolBarFocusListener;  
     //        protected KeyStroke upKey;  
     //        Deprecated. As of Java 2 platform v1.3.  
   
     private Dimension maximumSize;  
     private Dimension minimumSize;  
     private Dimension preferredSize;  
     private boolean floating;  
     private boolean rolloverBorders;  
133    
134      BasicToolBarUI(JToolBar b)    /** The Focus listener for the JToolBar. */
135      protected FocusListener toolBarFocusListener;
136    
137      /**
138       * The floating window that is responsible for holding the JToolBar when it
139       * is dragged outside of its original parent.
140       */
141      private transient Window floatFrame;
142    
143      /** The original parent of the JToolBar. */
144      private transient Container origParent;
145    
146      /** A hashtable of components and their original borders. */
147      private transient Hashtable borders;
148    
149      /** A window listener for the floatable frame. */
150      private transient WindowListener windowListener;
151    
152      /** A set of cached bounds of the JToolBar. */
153      private transient Dimension cachedBounds;
154    
155      /** The cached orientation of the JToolBar. */
156      private transient int cachedOrientation;
157    
158      /**
159       * This method creates a new BasicToolBarUI object for the given JToolBar.
160       *
161       * @param b The JToolBar to represent with this UI.
162       */
163      public BasicToolBarUI(JToolBar b)
164      {      {
165          super();          super();
166      }      }
167    
168      /* Can Component c dock at Point p? */    /**
169      boolean canDock(Component c, Point p)     * This method returns whether the JToolBar can dock at the given position.
170       *
171       * @param c The component to try to dock in.
172       * @param p The position of the mouse cursor relative to the given
173       *        component.
174       *
175       * @return Whether the JToolBar can dock.
176       */
177      protected boolean canDock(Component c, Point p)
178      {      {
179        if (areaOfClick(c, p) != -1)
180          return true;
181    
182          return false;          return false;
183      }      }
184    
185      /**
186       * This helper method returns the position of the JToolBar if it can dock.
187       *
188       * @param c The component to try to dock in.
189       * @param p The position of the mouse cursor relative to the given
190       *        component.
191       *
192       * @return One of the SwingConstants directions or -1 if the JToolBar can't
193       *         dock.
194       */
195      private int areaOfClick(Component c, Point p)
196      {
197        // Has to dock in immediate parent, not eventual root container.
198        Rectangle pBounds = c.getBounds();
199    
200        // XXX: In Sun's implementation, the space the toolbar has to dock is dependent on the size it had last.
201        Dimension d = toolBar.getSize();
202        int limit = Math.min(d.width, d.height);
203    
204        // The order of checking is 1. top 2. bottom 3. left 4. right
205        if (! pBounds.contains(p))
206          return -1;
207    
208        if (p.y < limit)
209          return SwingConstants.NORTH;
210    
211        if (p.y > (pBounds.height - limit))
212          return SwingConstants.SOUTH;
213    
214        if (p.x < limit)
215          return SwingConstants.WEST;
216    
217        if (p.x > (pBounds.width - limit))
218          return SwingConstants.EAST;
219    
220        return -1;
221      }
222    
223      /**
224       * This method creates a new DockingListener for the JToolBar.
225       *
226       * @return A new DockingListener for the JToolBar.
227       */
228      protected MouseInputListener createDockingListener()      protected MouseInputListener createDockingListener()
229      {      {
230        return null;      return new DockingListener(toolBar);
231      }      }
232    
233      /**
234       * This method creates a new DragWindow for the given JToolBar.
235       *
236       * @param toolbar The JToolBar to create a DragWindow for.
237       *
238       * @return A new DragWindow.
239       */
240      protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)      protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
241      {      {
242        return null;      return new DragWindow();
243      }      }
244    
245      /**
246       * This method creates a new floating frame for the JToolBar. By default,
247       * this UI uses createFloatingWindow instead. This method of creating a
248       * floating frame is deprecated.
249       *
250       * @param toolbar The JToolBar to create a floating frame for.
251       *
252       * @return A new floating frame.
253       */
254      protected JFrame createFloatingFrame(JToolBar toolbar)      protected JFrame createFloatingFrame(JToolBar toolbar)
255      {      {
256        // FIXME: Though deprecated, this should still work.
257        return null;        return null;
258      }      }
259    
260      /**
261       * This method creates a new floating window for the JToolBar. This is the
262       * method used by default to create a floating container for the JToolBar.
263       *
264       * @param toolbar The JToolBar to create a floating window for.
265       *
266       * @return A new floating window.
267       */
268      protected RootPaneContainer createFloatingWindow(JToolBar toolbar)      protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
269      {      {
270        return null;      // This one is used by default though.
271        return new ToolBarDialog();
272      }      }
273    
274      /**
275       * This method creates a new WindowListener for the JToolBar.
276       *
277       * @return A new WindowListener.
278       */
279      protected WindowListener createFrameListener()      protected WindowListener createFrameListener()
280      {      {
281        return null;      return new FrameListener();
282      }      }
283    
284      /**
285       * This method creates a new nonRolloverBorder for JButtons when the
286       * JToolBar's rollover property is set to false.
287       *
288       * @return A new NonRolloverBorder.
289       */
290      protected Border createNonRolloverBorder()      protected Border createNonRolloverBorder()
291      {      {
292        return null;      return new EtchedBorderUIResource();
293      }      }
294    
295      /**
296       * This method creates a new PropertyChangeListener for the JToolBar.
297       *
298       * @return A new PropertyChangeListener.
299       */
300      protected PropertyChangeListener createPropertyListener()      protected PropertyChangeListener createPropertyListener()
301      {      {
302        return null;      return new PropertyListener();
303      }      }
304    
305      /**
306       * This method creates a new rollover border for JButtons when the
307       * JToolBar's rollover property is set to true.
308       *
309       * @return A new rollover border.
310       */
311      protected Border createRolloverBorder()      protected Border createRolloverBorder()
312      {      {
313        return null;      return new EtchedBorderUIResource()
314          {
315            public void paintBorder(Component c, Graphics g, int x, int y,
316                                    int width, int height)
317            {
318              if (c instanceof JButton)
319                {
320                  if (((JButton) c).getModel().isRollover())
321                    super.paintBorder(c, g, x, y, width, height);
322                }
323            }
324          };
325      }      }
326    
327      /**
328       * This method creates a new Container listener for the JToolBar.
329       *
330       * @return A new Container listener.
331       */
332      protected ContainerListener createToolBarContListener()      protected ContainerListener createToolBarContListener()
333      {      {
334        return null;      return new ToolBarContListener();
335      }      }
336    
337      /**
338       * This method creates a new FocusListener for the JToolBar.
339       *
340       * @return A new FocusListener for the JToolBar.
341       */
342      protected FocusListener createToolBarFocusListener()      protected FocusListener createToolBarFocusListener()
343      {      {
344        return null;      return new ToolBarFocusListener();
345      }      }
346    
347      /**
348       * This method creates a new UI delegate for the given JComponent.
349       *
350       * @param c The JComponent to create a UI delegate for.
351       *
352       * @return A new UI delegate.
353       */
354      public static ComponentUI createUI(JComponent c)      public static ComponentUI createUI(JComponent c)
355      {      {
356          return new BasicToolBarUI((JToolBar) c);          return new BasicToolBarUI((JToolBar) c);
357      }      }
358    
359      /**
360       * This method is called to drag the DragWindow around when the JToolBar is
361       * being dragged around.
362       *
363       * @param position The mouse cursor coordinates relative to the JToolBar.
364       * @param origin The screen position of the JToolBar.
365       */
366      protected void dragTo(Point position, Point origin)      protected void dragTo(Point position, Point origin)
367      {      {
368      }      int loc = areaOfClick(origParent,
369                              SwingUtilities.convertPoint(toolBar, position,
370                                                          origParent));
371    
372        if (loc != -1)
373          {
374            dragWindow.setBorderColor(dockingBorderColor);
375            dragWindow.setBackground(dockingColor);
376          }
377        else
378          {
379            dragWindow.setBorderColor(floatingBorderColor);
380            dragWindow.setBackground(floatingColor);
381          }
382    
383        int w = 0;
384        int h = 0;
385    
386        boolean tmp = ((loc == SwingConstants.NORTH)
387                      || (loc == SwingConstants.SOUTH) || (loc == -1));
388    
389        if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
390            || ((cachedOrientation == VERTICAL) && ! tmp))
391          {
392            w = cachedBounds.width;
393            h = cachedBounds.height;
394          }
395        else
396          {
397            w = cachedBounds.height;
398            h = cachedBounds.width;
399        }
400    
401        Point p = dragWindow.getOffset();
402        Insets insets = toolBar.getInsets();
403    
404        dragWindow.setBounds((origin.x + position.x) - p.x
405                             - ((insets.left + insets.right) / 2),
406                             (origin.y + position.y) - p.y
407                             - ((insets.top + insets.bottom) / 2), w, h);
408    
409        if (! dragWindow.isVisible())
410          dragWindow.show();
411      }
412    
413      /**
414       * This method is used at the end of a drag session to place the frame in
415       * either its original parent as a docked JToolBar or in its floating
416       * frame.
417       *
418       * @param position The position of the mouse cursor relative to the
419       *        JToolBar.
420       * @param origin The screen position of the JToolBar before the drag session
421       *        started.
422       */
423      protected void floatAt(Point position, Point origin)      protected void floatAt(Point position, Point origin)
424      {      {
425      }      Point p = new Point(position);
426        int aoc = areaOfClick(origParent,
427      /* Return the Color which is displayed when over a docking area */                            SwingUtilities.convertPoint(toolBar, p, origParent));
428      Color getDockingColor()  
429        Container oldParent = toolBar.getParent();
430    
431        oldParent.remove(toolBar);
432        oldParent.doLayout();
433        oldParent.repaint();
434    
435        Container newParent;
436    
437        if (aoc == -1)
438          newParent = ((RootPaneContainer) floatFrame).getContentPane();
439        else
440          {
441            floatFrame.hide();
442            newParent = origParent;
443        }
444    
445        String constraint;
446        switch (aoc)
447          {
448          case SwingConstants.EAST:
449            constraint = BorderLayout.EAST;
450            break;
451          case SwingConstants.NORTH:
452            constraint = BorderLayout.NORTH;
453            break;
454          case SwingConstants.SOUTH:
455            constraint = BorderLayout.SOUTH;
456            break;
457          case SwingConstants.WEST:
458            constraint = BorderLayout.WEST;
459            break;
460          default:
461            constraint = BorderLayout.CENTER;
462            break;
463          }
464    
465        int newOrientation = SwingConstants.HORIZONTAL;
466        if ((aoc != -1)
467            && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
468          newOrientation = SwingConstants.VERTICAL;
469    
470        if (aoc != -1)
471          {
472            constraintBeforeFloating = constraint;
473            lastGoodOrientation = newOrientation;
474          }
475    
476        newParent.add(toolBar, constraint);
477    
478        setFloating(aoc == -1, null);
479        toolBar.setOrientation(newOrientation);
480    
481        Insets insets = floatFrame.getInsets();
482        Dimension dims = toolBar.getPreferredSize();
483        p = dragWindow.getOffset();
484        setFloatingLocation((position.x + origin.x) - p.x
485                            - ((insets.left + insets.right) / 2),
486                            (position.y + origin.y) - p.y
487                            - ((insets.top + insets.bottom) / 2));
488    
489        if (aoc == -1)
490          {
491            floatFrame.pack();
492            floatFrame.setSize(dims.width + insets.left + insets.right,
493                               dims.height + insets.top + insets.bottom);
494            floatFrame.show();
495          }
496    
497        newParent.invalidate();
498        newParent.validate();
499        newParent.repaint();
500      }
501    
502      /**
503       * This method returns the docking color.
504       *
505       * @return The docking color.
506       */
507      public Color getDockingColor()
508      {      {
509          return dockingColor;          return dockingColor;
510      }      }
511    
512      /* Return the Color which is displayed when over a floating area */    /**
513      Color getFloatingColor()     * This method returns the Color which is displayed when over a floating
514       * area.
515       *
516       * @return The color which is displayed when over a floating area.
517       */
518      public Color getFloatingColor()
519      {      {
520          return floatingColor;          return floatingColor;
521      }      }
522    
523      /* See ComponentUI */    /**
524       * This method returns the maximum size of the given JComponent for this UI.
525       *
526       * @param c The JComponent to find the maximum size for.
527       *
528       * @return The maximum size for this UI.
529       */
530    public Dimension getMaximumSize(JComponent c)    public Dimension getMaximumSize(JComponent c)
531      {      {
532        return null;      return getPreferredSize(c);
533      }      }
534    
535      /* See ComponentUI */    /**
536       * This method returns the minimum size of the given JComponent for this UI.
537       *
538       * @param c The JComponent to find a minimum size for.
539       *
540       * @return The minimum size for this UI.
541       */
542    public Dimension getMinimumSize(JComponent c)    public Dimension getMinimumSize(JComponent c)
543      {      {
544        return null;      return getPreferredSize(c);
545      }      }
546    
547      /* See ComponentUI */    /**
548       * This method returns the preferred size of the given JComponent for this
549       * UI.
550       *
551       * @param c The JComponent to find a preferred size for.
552       *
553       * @return The preferred size for this UI.
554       */
555    public Dimension getPreferredSize(JComponent c)    public Dimension getPreferredSize(JComponent c)
556      {      {
557        return null;      return toolBar.getLayout().preferredLayoutSize(c);
558      }      }
559    
560      /**
561       * This method installs the needed components for the JToolBar.
562       */
563      protected void installComponents()      protected void installComponents()
564      {      {
565        floatFrame = (Window) createFloatingWindow(toolBar);
566    
567        dragWindow = createDragWindow(toolBar);
568    
569        cachedBounds = toolBar.getPreferredSize();
570        cachedOrientation = toolBar.getOrientation();
571    
572        nonRolloverBorder = createNonRolloverBorder();
573        rolloverBorder = createRolloverBorder();
574    
575        borders = new Hashtable();
576    
577        fillHashtable();
578      }      }
579    
580      /**
581       * This method installs the defaults as specified by the look and feel.
582       */
583      protected void installDefaults()      protected void installDefaults()
584      {      {
585        UIDefaults defaults = UIManager.getLookAndFeelDefaults();
586    
587        toolBar.setBorder(new ToolBarBorder());
588        toolBar.setBackground(defaults.getColor("ToolBar.background"));
589        toolBar.setForeground(defaults.getColor("ToolBar.foreground"));
590        toolBar.setFont(defaults.getFont("ToolBar.font"));
591    
592        dockingBorderColor = defaults.getColor("ToolBar.dockingForeground");
593        dockingColor = defaults.getColor("ToolBar.dockingBackground");
594    
595        floatingBorderColor = defaults.getColor("ToolBar.floatingForeground");
596        floatingColor = defaults.getColor("ToolBar.floatingBackground");
597      }      }
598    
599      /**
600       * This method installs the keyboard actions for the JToolBar as specified
601       * by the look and feel.
602       */
603      protected void installKeyboardActions()      protected void installKeyboardActions()
604      {      {
605        // FIXME: implement.
606      }      }
607    
608      /**
609       * This method installs listeners for the JToolBar.
610       *
611       * @param toolbar The JToolBar to register listeners for.
612       */
613      protected void installListeners(JToolBar toolbar)      protected void installListeners(JToolBar toolbar)
614      {      {
615          toolbar.addMouseListener(new MouseAdapter() {      dockingListener = createDockingListener();
616                  public void mouseClicked(MouseEvent event) {      toolBar.addMouseListener(dockingListener);
617                          System.err.println("toolbar clicked");      toolBar.addMouseMotionListener(dockingListener);
618                  }  
619          } );      propertyListener = createPropertyListener();
620      }      toolBar.addPropertyChangeListener(propertyListener);
621    
622      /* Call setBorderToNonRollover for each child component of c */      toolBarContListener = createToolBarContListener();
623        toolBar.addContainerListener(toolBarContListener);
624    
625        windowListener = createFrameListener();
626        floatFrame.addWindowListener(windowListener);
627    
628        toolBarFocusListener = createToolBarFocusListener();
629        toolBar.addFocusListener(toolBarFocusListener);
630      }
631    
632      /**
633       * This method installs non rollover borders for each component inside the
634       * given JComponent.
635       *
636       * @param c The JComponent whose children need to have non rollover borders
637       *        installed.
638       */
639      protected void installNonRolloverBorders(JComponent c)      protected void installNonRolloverBorders(JComponent c)
640      {      {
641        Component[] components = toolBar.getComponents();
642    
643        for (int i = 0; i < components.length; i++)
644          setBorderToNonRollover(components[i]);
645      }      }
646    
647      /* Call setBorderToNormal for each child component of c */    /**
648       * This method installs normal (or their original) borders for each
649       * component inside the given JComponent.
650       *
651       * @param c The JComponent whose children need to have their original
652       *        borders installed.
653       */
654      protected void installNormalBorders(JComponent c)      protected void installNormalBorders(JComponent c)
655      {      {
656        Component[] components = toolBar.getComponents();
657    
658        for (int i = 0; i < components.length; i++)
659          setBorderToNormal(components[i]);
660      }      }
661    
662      /* Call setBorderToRollover for each child component of c */    /**
663       * This method install rollover borders for each component inside the given
664       * JComponent.
665       *
666       * @param c The JComponent whose children need to have rollover borders
667       *        installed.
668       */
669      protected void installRolloverBorders(JComponent c)      protected void installRolloverBorders(JComponent c)
670      {      {
671        Component[] components = toolBar.getComponents();
672    
673        for (int i = 0; i < components.length; i++)
674          setBorderToRollover(components[i]);
675      }      }
676    
677      /**
678       * This method fills the borders hashtable with a list of components that
679       * are JButtons and their borders.
680       */
681      private void fillHashtable()
682      {
683        Component[] c = toolBar.getComponents();
684    
685        for (int i = 0; i < c.length; i++)
686          {
687            if (c[i] instanceof JButton)
688              {
689                // Don't really care about anything other than JButtons
690                JButton b = (JButton) c[i];
691    
692                if (b.getBorder() != null)
693                  borders.put(b, b.getBorder());
694              }
695          }
696      }
697    
698      /**
699       * This method installs the UI for the given JComponent.
700       *
701       * @param c The JComponent to install a UI for.
702       */
703      public void installUI(JComponent c)      public void installUI(JComponent c)
704      {      {
705          super.installUI(c);          super.installUI(c);
706    
707          if (c instanceof JToolBar)          if (c instanceof JToolBar)
708          {          {
709              toolBar = (JToolBar) c;              toolBar = (JToolBar) c;
710              toolBar.setOpaque(true);              toolBar.setOpaque(true);
711              switch (toolBar.getOrientation()) {          installDefaults();
712                  case 0: toolBar.setLayout(new GridLayout(1, 0, 4, 4));          installComponents();
                                  break;  
                 case 1: toolBar.setLayout(new GridLayout(0, 1, 4, 4));  
                                break;  
             }  
713              installListeners(toolBar);              installListeners(toolBar);
714            installKeyboardActions();
715          }          }
716      }      }
717    
718      boolean isFloating()    /**
719      {     * This method returns whether the JToolBar is floating.
720          return false;     *
721      }     * @return Whether the JToolBar is floating.
722       */
723      boolean isRolloverBorders()    public boolean isFloating()
724      {      {
725          return false;      return floatFrame.isVisible();
726      }      }
727    
728      /**
729       * This method returns whether rollover borders have been set.
730       *
731       * @return Whether rollover borders have been set.
732       */
733      public boolean isRolloverBorders()
734        {
735        return toolBar.isRollover();
736        }
737    
738      /**
739       * This method navigates in the given direction giving focus to the next
740       * component in the given direction.
741       *
742       * @param direction The direction to give focus to.
743       */
744      protected void navigateFocusedComp(int direction)      protected void navigateFocusedComp(int direction)
745      {      {
746        // FIXME: Implement.
747      }      }
748    
749      /* Make Component c have a non-rollover border (created by    /**
750         createNonRolloverBorder). */     * This method sets the border of the given component to a non rollover
751       * border.
752       *
753       * @param c The Component whose border needs to be set.
754       */
755      protected void setBorderToNonRollover(Component c)      protected void setBorderToNonRollover(Component c)
756      {      {
757        if (c instanceof JButton)
758          {
759            JButton b = (JButton) c;
760            b.setRolloverEnabled(false);
761            b.setBorder(nonRolloverBorder);
762          }
763      }      }
764    
765      /* Make Component c have the border that it originally had before being    /**
766         added to the toolbar. */     * This method sets the border of the given component to its original value.
767       *
768       * @param c The Component whose border needs to be set.
769       */
770      protected void setBorderToNormal(Component c)      protected void setBorderToNormal(Component c)
771      {      {
772        if (c instanceof JButton)
773          {
774            JButton b = (JButton) c;
775            Border border = (Border) borders.get(b);
776            b.setBorder(border);
777          }
778      }      }
779    
780      /* Make Component c have a rollover border (created by    /**
781         createRolloverBorder). */     * This method sets the border of the given component to a rollover border.
782       *
783       * @param c The Component whose border needs to be set.
784       */
785      protected void setBorderToRollover(Component c)      protected void setBorderToRollover(Component c)
786      {      {
787        if (c instanceof JButton)
788          {
789            JButton b = (JButton) c;
790            b.setRolloverEnabled(true);
791            b.setBorder(rolloverBorder);
792          }
793      }      }
794    
795      /* Display in Color c when over a docking area */    /**
796      void setDockingColor(Color c)     * This method sets the docking color.
797       *
798       * @param c The docking color.
799       */
800      public void setDockingColor(Color c)
801      {      {
802          dockingColor = c;          dockingColor = c;
803      }      }
804    
805      void setFloating(boolean b, Point p)    /**
806      {     * This method sets the floating property for the JToolBar.
807      }     *
808       * @param b Whether the JToolBar is floating.
809      /* Display in Color c when over a floating area */     * @param p FIXME
810      void setFloatingColor(Color c)     */
811      {    public void setFloating(boolean b, Point p)
812          floatingColor = c;    {
813      }      // FIXME: use p for something. It's not location
814        // since we already have setFloatingLocation.
815      void setFloatingLocation(int x, int y)      floatFrame.setVisible(b);
816      {    }
817      }  
818      /**
819      void setOrientation(int orientation)     * This method sets the color displayed when the JToolBar is not in a
820      {     * dockable area.
821      }     *
822       * @param c The floating color.
823      /* Set flag to enable rollover borders for the toolbar */     */
824      void setRolloverBorders(boolean rollover)    public void setFloatingColor(Color c)
825      {      {
826          rolloverBorders = rollover;      floatingColor = c;
827      }      }
828    
829      protected void uninstallComponents()    /**
830      {     * This method sets the floating location of the JToolBar.
831      }     *
832       * @param x The x coordinate for the floating frame.
833      protected void uninstallDefaults()     * @param y The y coordinate for the floating frame.
834       */
835      public void setFloatingLocation(int x, int y)
836      {
837        // x,y are the coordinates of the new JFrame created to store the toolbar
838        // XXX: The floating location is bogus is not floating.
839        floatFrame.setLocation(x, y);
840        floatFrame.invalidate();
841        floatFrame.validate();
842        floatFrame.repaint();
843      }
844    
845      /**
846       * This is a convenience method for changing the orientation of the
847       * JToolBar.
848       *
849       * @param orientation The new orientation.
850       */
851      public void setOrientation(int orientation)
852      {
853        toolBar.setOrientation(orientation);
854      }
855    
856      /**
857       * This method changes the child components to have rollover borders if the
858       * given parameter is true. Otherwise, the components are set to have non
859       * rollover borders.
860       *
861       * @param rollover Whether the children will have rollover borders.
862       */
863      public void setRolloverBorders(boolean rollover)
864      {
865        if (rollover)
866          installRolloverBorders(toolBar);
867        else
868          installNonRolloverBorders(toolBar);
869      }
870    
871      /**
872       * This method uninstall UI installed components from the JToolBar.
873       */
874      protected void uninstallComponents()
875        {
876        installNormalBorders(toolBar);
877        borders = null;
878        rolloverBorder = null;
879        nonRolloverBorder = null;
880        cachedBounds = null;
881    
882        floatFrame = null;
883        dragWindow = null;
884      }
885    
886      /**
887       * This method removes the defaults installed by the Look and Feel.
888       */
889      protected void uninstallDefaults()
890      {
891        toolBar.setBackground(null);
892        toolBar.setForeground(null);
893        toolBar.setFont(null);
894    
895        dockingBorderColor = null;
896        dockingColor = null;
897        floatingBorderColor = null;
898        floatingColor = null;
899        }
900    
901      /**
902       * This method uninstalls keyboard actions installed by the UI.
903       */
904      protected void uninstallKeyboardActions()
905        {
906        // FIXME: implement.
907        }
908    
909      /**
910       * This method uninstalls listeners installed by the UI.
911       */
912      protected void uninstallListeners()
913        {
914        toolBar.removeFocusListener(toolBarFocusListener);
915        toolBarFocusListener = null;
916    
917        floatFrame.removeWindowListener(windowListener);
918        windowListener = null;
919    
920        toolBar.removeContainerListener(toolBarContListener);
921        toolBarContListener = null;
922    
923        toolBar.removeMouseMotionListener(dockingListener);
924        toolBar.removeMouseListener(dockingListener);
925        dockingListener = null;
926        }
927    
928      /**
929       * This method uninstalls the UI.
930       *
931       * @param c The JComponent that is having this UI removed.
932       */
933      public void uninstallUI(JComponent c)
934      {      {
935      }      uninstallKeyboardActions();
936        uninstallListeners();
937        uninstallComponents();
938        uninstallDefaults();
939        toolBar = null;
940      }
941    
942      /**
943       * This is the MouseHandler class that allows the user to drag the JToolBar
944       * in and out of the parent and dock it if it can.
945       */
946      protected class DockingListener implements MouseInputListener
947      {
948        /** Whether the JToolBar is being dragged. */
949        protected boolean isDragging;
950    
951        /**
952         * The origin point. This point is saved from the beginning press and is
953         * used until the end of the drag session.
954         */
955        protected Point origin;
956    
957      protected void uninstallKeyboardActions()      /** The JToolBar being dragged. */
958      {      protected JToolBar toolBar;
     }  
   
     protected void uninstallListeners()  
     {  
     }  
959    
960    public void uninstallUI(JComponent c)      /**
961      {       * Creates a new DockingListener object.
962         *
963         * @param t The JToolBar this DockingListener is being used for.
964         */
965        public DockingListener(JToolBar t)
966        {
967          toolBar = t;
968        }
969    
970        /**
971         * This method is called when the mouse is clicked.
972         *
973         * @param e The MouseEvent.
974         */
975        public void mouseClicked(MouseEvent e)
976        {
977          // Don't care.
978        }
979    
980        /**
981         * This method is called when the mouse is dragged. It delegates the drag
982         * painting to the dragTo method.
983         *
984         * @param e The MouseEvent.
985         */
986        public void mouseDragged(MouseEvent e)
987        {
988          if (isDragging)
989            dragTo(e.getPoint(), origin);
990        }
991    
992        /**
993         * This method is called when the mouse enters the JToolBar.
994         *
995         * @param e The MouseEvent.
996         */
997        public void mouseEntered(MouseEvent e)
998        {
999          // Don't care (yet).
1000        }
1001    
1002        /**
1003         * This method is called when the mouse exits the JToolBar.
1004         *
1005         * @param e The MouseEvent.
1006         */
1007        public void mouseExited(MouseEvent e)
1008        {
1009          // Don't care (yet).
1010        }
1011    
1012        /**
1013         * This method is called when the mouse is moved in the JToolBar.
1014         *
1015         * @param e The MouseEvent.
1016         */
1017        public void mouseMoved(MouseEvent e)
1018        {
1019        }
1020    
1021        /**
1022         * This method is called when the mouse is pressed in the JToolBar. If the
1023         * press doesn't occur in a place where it causes the JToolBar to be
1024         * dragged, it returns. Otherwise, it starts a drag session.
1025         *
1026         * @param e The MouseEvent.
1027         */
1028        public void mousePressed(MouseEvent e)
1029        {
1030          if (! toolBar.isFloatable())
1031            return;
1032    
1033          Point ssd = e.getPoint();
1034          Insets insets = toolBar.getInsets();
1035    
1036          // Verify that this click occurs in the top inset.
1037          if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1038            {
1039              if (e.getX() > insets.left)
1040                return;
1041        }
1042          else
1043            {
1044              if (e.getY() > insets.top)
1045                return;
1046            }
1047    
1048          origin = new Point(0, 0);
1049          SwingUtilities.convertPointToScreen(ssd, toolBar);
1050    
1051          if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1052            // Need to know who keeps the toolBar if it gets dragged back into it.
1053            origParent = toolBar.getParent();
1054    
1055          SwingUtilities.convertPointToScreen(origin, toolBar);
1056    
1057          isDragging = true;
1058    
1059          if (dragWindow != null)
1060            dragWindow.setOffset(new Point(e.getX(), e.getY()));
1061    
1062          dragTo(e.getPoint(), origin);
1063        }
1064    
1065        /**
1066         * This method is called when the mouse is released from the JToolBar.
1067         *
1068         * @param e The MouseEvent.
1069         */
1070        public void mouseReleased(MouseEvent e)
1071        {
1072          if (! isDragging || ! toolBar.isFloatable())
1073            return;
1074    
1075          isDragging = false;
1076          floatAt(e.getPoint(), origin);
1077          dragWindow.hide();
1078        }
1079      }
1080    
1081      /**
1082       * This is the window that appears when the JToolBar is being dragged
1083       * around.
1084       */
1085      protected class DragWindow extends Window
1086      {
1087        /**
1088         * The current border color. It changes depending on whether the JToolBar
1089         * is over a place that allows it to dock.
1090         */
1091        private Color borderColor;
1092    
1093        /** The between the mouse and the top left corner of the window. */
1094        private Point offset;
1095    
1096        /**
1097         * Creates a new DragWindow object.
1098         */
1099        private DragWindow()
1100        {
1101          super(owner);
1102        }
1103    
1104        /**
1105         * The color that the border should be.
1106         *
1107         * @return The border color.
1108         */
1109        public Color getBorderColor()
1110        {
1111          if (borderColor == null)
1112            return Color.BLACK;
1113    
1114          return borderColor;
1115        }
1116    
1117        /**
1118         * This method returns the insets for the DragWindow.
1119         *
1120         * @return The insets for the DragWindow.
1121         */
1122        public Insets getInsets()
1123        {
1124          // This window has no decorations, so insets are empty.
1125          return new Insets(0, 0, 0, 0);
1126        }
1127    
1128        /**
1129         * This method returns the mouse offset from the top left corner of the
1130         * DragWindow.
1131         *
1132         * @return The mouse offset.
1133         */
1134        public Point getOffset()
1135        {
1136          return offset;
1137        }
1138    
1139        /**
1140         * This method paints the DragWindow.
1141         *
1142         * @param g The Graphics object to paint with.
1143         */
1144        public void paint(Graphics g)
1145        {
1146          //  No visiting children necessary.
1147          Color saved = g.getColor();
1148          Rectangle b = getBounds();
1149    
1150          g.setColor(getBorderColor());
1151          g.drawRect(0, 0, b.width - 1, b.height - 1);
1152    
1153          g.setColor(saved);
1154        }
1155    
1156        /**
1157         * This method changes the border color.
1158         *
1159         * @param c The new border color.
1160         */
1161        public void setBorderColor(Color c)
1162        {
1163          borderColor = c;
1164        }
1165    
1166        /**
1167         * This method changes the mouse offset.
1168         *
1169         * @param p The new mouse offset.
1170         */
1171        public void setOffset(Point p)
1172        {
1173          offset = p;
1174        }
1175    
1176        /**
1177         * FIXME: Do something.
1178         *
1179         * @param o DOCUMENT ME!
1180         */
1181        public void setOrientation(int o)
1182        {
1183          // FIXME: implement.
1184        }
1185      }
1186    
1187      /**
1188       * This helper class listens for Window events from the floatable window and
1189       * if it is closed, returns the JToolBar to the last known good location.
1190       */
1191      protected class FrameListener extends WindowAdapter
1192      {
1193        /**
1194         * This method is called when the floating window is closed.
1195         *
1196         * @param e The WindowEvent.
1197         */
1198        public void windowClosing(WindowEvent e)
1199        {
1200          Container parent = toolBar.getParent();
1201          parent.remove(toolBar);
1202    
1203          if (origParent != null)
1204            {
1205              origParent.add(toolBar,
1206                             (constraintBeforeFloating != null)
1207                             ? constraintBeforeFloating : BorderLayout.NORTH);
1208              toolBar.setOrientation(lastGoodOrientation);
1209            }
1210    
1211          origParent.invalidate();
1212          origParent.validate();
1213          origParent.repaint();
1214        }
1215      }
1216    
1217      /**
1218       * This helper class listens for PropertyChangeEvents from the JToolBar.
1219       */
1220      protected class PropertyListener implements PropertyChangeListener
1221      {
1222        /**
1223         * This method is called when a property from the JToolBar is changed.
1224         *
1225         * @param e The PropertyChangeEvent.
1226         */
1227        public void propertyChange(PropertyChangeEvent e)
1228        {
1229          // FIXME: need name properties so can change floatFrame title.
1230          if (e.getPropertyName().equals(JToolBar.ROLLOVER_CHANGED_PROPERTY))
1231            setRolloverBorders(toolBar.isRollover());
1232        }
1233      }
1234    
1235      /**
1236       * This helper class listens for components added to and removed from the
1237       * JToolBar.
1238       */
1239      protected class ToolBarContListener implements ContainerListener
1240      {
1241        /**
1242         * This method is responsible for setting rollover or non rollover for new
1243         * buttons added to the JToolBar.
1244         *
1245         * @param e The ContainerEvent.
1246         */
1247        public void componentAdded(ContainerEvent e)
1248        {
1249          if (e.getChild() instanceof JButton)
1250            {
1251              JButton b = (JButton) e.getChild();
1252    
1253              if (b.getBorder() != null)
1254                borders.put(b, b.getBorder());
1255            }
1256    
1257          if (isRolloverBorders())
1258            setBorderToRollover(e.getChild());
1259          else
1260            setBorderToNonRollover(e.getChild());
1261    
1262          cachedBounds = toolBar.getPreferredSize();
1263          cachedOrientation = toolBar.getOrientation();
1264        }
1265    
1266        /**
1267         * This method is responsible for giving the child components their
1268         * original borders when they are removed.
1269         *
1270         * @param e The ContainerEvent.
1271         */
1272        public void componentRemoved(ContainerEvent e)
1273        {
1274          setBorderToNormal(e.getChild());
1275          cachedBounds = toolBar.getPreferredSize();
1276          cachedOrientation = toolBar.getOrientation();
1277        }
1278      }
1279    
1280      /**
1281       * This is the floating window that is returned when getFloatingWindow is
1282       * called.
1283       */
1284      private class ToolBarDialog extends JDialog implements UIResource
1285      {
1286        /**
1287         * Creates a new ToolBarDialog object with the name given by the JToolBar.
1288         */
1289        public ToolBarDialog()
1290        {
1291          super();
1292          setName((toolBar.getName() != null) ? toolBar.getName() : "");
1293        }
1294      }
1295    
1296      /**
1297       * DOCUMENT ME!
1298       */
1299      protected class ToolBarFocusListener implements FocusListener
1300      {
1301        /**
1302         * Creates a new ToolBarFocusListener object.
1303         */
1304        protected ToolBarFocusListener()
1305        {
1306          // FIXME: implement.
1307        }
1308    
1309        /**
1310         * DOCUMENT ME!
1311         *
1312         * @param e DOCUMENT ME!
1313         */
1314        public void focusGained(FocusEvent e)
1315        {
1316          // FIXME: implement.
1317        }
1318    
1319        /**
1320         * DOCUMENT ME!
1321         *
1322         * @param e DOCUMENT ME!
1323         */
1324        public void focusLost(FocusEvent e)
1325        {
1326          // FIXME: implement.
1327        }
1328      }
1329    
1330      /**
1331       * This helper class acts as the border for the JToolBar.
1332       */
1333      private static class ToolBarBorder implements Border
1334      {
1335        /** The size of the larger, draggable side of the border. */
1336        private static int offset = 10;
1337    
1338        /** The other sides. */
1339        private static int regular = 2;
1340    
1341        /**
1342         * This method returns the border insets for the JToolBar.
1343         *
1344         * @param c The Component to find insets for.
1345         *
1346         * @return The border insets.
1347         */
1348        public Insets getBorderInsets(Component c)
1349        {
1350          if (c instanceof JToolBar)
1351            {
1352              JToolBar tb = (JToolBar) c;
1353              int orientation = tb.getOrientation();
1354    
1355              if (! tb.isFloatable())
1356                return new Insets(regular, regular, regular, regular);
1357              else if (orientation == SwingConstants.HORIZONTAL)
1358                return new Insets(regular, offset, regular, regular);
1359              else
1360                return new Insets(offset, regular, regular, regular);
1361            }
1362    
1363          return new Insets(0, 0, 0, 0);
1364        }
1365    
1366        /**
1367         * This method returns whether the border is opaque.
1368         *
1369         * @return Whether the border is opaque.
1370         */
1371        public boolean isBorderOpaque()
1372        {
1373          return false;
1374        }
1375    
1376        /**
1377         * This method paints the ribbed area of the border.
1378         *
1379         * @param g The Graphics object to paint with.
1380         * @param x The x coordinate of the area.
1381         * @param y The y coordinate of the area.
1382         * @param w The width of the area.
1383         * @param h The height of the area.
1384         * @param size The size of the bump.
1385         * @param c The color of the bumps.
1386         */
1387        private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1388                                Color c)
1389        {
1390          Color saved = g.getColor();
1391          g.setColor(c);
1392    
1393          int hgap = 2 * size;
1394          int vgap = 4 * size;
1395          int count = 0;
1396    
1397          for (int i = x; i < (w + x); i += hgap)
1398            for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1399                 j += vgap)
1400              g.fillRect(i, j, size, size);
1401    
1402          g.setColor(saved);
1403        }
1404    
1405        /**
1406         * This method paints the border around the given Component.
1407         *
1408         * @param c The Component whose border is being painted.
1409         * @param g The Graphics object to paint with.
1410         * @param x The x coordinate of the component.
1411         * @param y The y coordinate of the component.
1412         * @param width The width of the component.
1413         * @param height The height of the component.
1414         */
1415        public void paintBorder(Component c, Graphics g, int x, int y, int width,
1416                                int height)
1417        {
1418          if (c instanceof JToolBar)
1419            {
1420              JToolBar tb = (JToolBar) c;
1421    
1422              int orientation = tb.getOrientation();
1423    
1424              if (orientation == SwingConstants.HORIZONTAL)
1425                {
1426                  paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1427                  paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1428                }
1429              else
1430                {
1431                  paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1432                  paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1433                }
1434            }
1435      }      }
1436      }
1437  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26