1 /* BasicToolBarUI.java --
2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package javax.swing.plaf.basic;
41 import gnu.classpath.NotImplementedException;
43 import java.awt.BorderLayout;
44 import java.awt.Color;
45 import java.awt.Component;
46 import java.awt.Container;
47 import java.awt.Dimension;
48 import java.awt.Graphics;
49 import java.awt.Insets;
50 import java.awt.Point;
51 import java.awt.Rectangle;
52 import java.awt.Window;
53 import java.awt.event.ContainerEvent;
54 import java.awt.event.ContainerListener;
55 import java.awt.event.FocusEvent;
56 import java.awt.event.FocusListener;
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.Hashtable;
65 import javax.swing.AbstractButton;
66 import javax.swing.JButton;
67 import javax.swing.JComponent;
68 import javax.swing.JDialog;
69 import javax.swing.JFrame;
70 import javax.swing.JToolBar;
71 import javax.swing.KeyStroke;
72 import javax.swing.LookAndFeel;
73 import javax.swing.RootPaneContainer;
74 import javax.swing.SwingConstants;
75 import javax.swing.SwingUtilities;
76 import javax.swing.UIManager;
77 import javax.swing.border.Border;
78 import javax.swing.border.CompoundBorder;
79 import javax.swing.event.MouseInputListener;
80 import javax.swing.plaf.ComponentUI;
81 import javax.swing.plaf.ToolBarUI;
82 import javax.swing.plaf.UIResource;
83 import javax.swing.plaf.basic.BasicBorders.ButtonBorder;
86 * This is the Basic Look and Feel UI class for JToolBar.
88 public class BasicToolBarUI extends ToolBarUI implements SwingConstants
90 /** Static owner of all DragWindows.
91 * This is package-private to avoid an accessor method. */
92 static JFrame owner = new JFrame();
94 /** The border used when the JToolBar is in nonrollover mode. */
95 private static Border nonRolloverBorder;
97 /** The border used when the JToolBar is in rollover mode. */
98 private static Border rolloverBorder;
100 /** The last known BorderLayout constraint before floating. */
101 protected String constraintBeforeFloating;
103 /** The last known orientation of the JToolBar before floating.
104 * This is package-private to avoid an accessor method. */
105 int lastGoodOrientation;
107 /** The color of the border when it is dockable. */
108 protected Color dockingBorderColor;
110 /** The background color of the JToolBar when it is dockable. */
111 protected Color dockingColor;
113 /** The docking listener responsible for mouse events on the JToolBar. */
114 protected MouseInputListener dockingListener;
116 /** The window used for dragging the JToolBar. */
117 protected BasicToolBarUI.DragWindow dragWindow;
119 /** The color of the border when it is not dockable. */
120 protected Color floatingBorderColor;
122 /** The background color of the JToolBar when it is not dockable. */
123 protected Color floatingColor;
125 /** The index of the focused component. */
126 protected int focusedCompIndex;
128 /** The PropertyChangeListener for the JToolBar. */
129 protected PropertyChangeListener propertyListener;
131 /** The JToolBar this UI delegate is responsible for. */
132 protected JToolBar toolBar;
134 /** The Container listener for the JToolBar. */
135 protected ContainerListener toolBarContListener;
137 /** The Focus listener for the JToolBar. */
138 protected FocusListener toolBarFocusListener;
141 * @deprecated since JDK1.3.
143 protected KeyStroke leftKey;
146 * @deprecated since JDK1.3.
148 protected KeyStroke rightKey;
151 * @deprecated since JDK1.3.
153 protected KeyStroke upKey;
156 * @deprecated since JDK1.3.
158 protected KeyStroke downKey;
161 * The floating window that is responsible for holding the JToolBar when it
162 * is dragged outside of its original parent.
164 private transient Window floatFrame;
166 /** The original parent of the JToolBar.
167 * This is package-private to avoid an accessor method. */
168 transient Container origParent;
170 /** A hashtable of components and their original borders.
171 * This is package-private to avoid an accessor method. */
172 transient Hashtable borders;
174 /** A window listener for the floatable frame. */
175 private transient WindowListener windowListener;
177 /** A set of cached bounds of the JToolBar.
178 * This is package-private to avoid an accessor method. */
179 transient Dimension cachedBounds;
181 /** The cached orientation of the JToolBar.
182 * This is package-private to avoid an accessor method. */
183 transient int cachedOrientation;
186 * This method creates a new <code>BasicToolBarUI</code> object for the given JToolBar.
188 public BasicToolBarUI()
194 * This method returns whether the JToolBar can dock at the given position.
196 * @param c The component to try to dock in.
197 * @param p The position of the mouse cursor relative to the given
200 * @return Whether the JToolBar can dock.
202 public boolean canDock(Component c, Point p)
204 return areaOfClick(c, p) != -1;
208 * This helper method returns the position of the JToolBar if it can dock.
210 * @param c The component to try to dock in.
211 * @param p The position of the mouse cursor relative to the given
214 * @return One of the SwingConstants directions or -1 if the JToolBar can't
217 private int areaOfClick(Component c, Point p)
219 // Has to dock in immediate parent, not eventual root container.
220 Rectangle pBounds = c.getBounds();
222 // XXX: In Sun's implementation, the space the toolbar has to dock is dependent on the size it had last.
223 Dimension d = toolBar.getSize();
224 int limit = Math.min(d.width, d.height);
226 // The order of checking is 1. top 2. bottom 3. left 4. right
227 if (! pBounds.contains(p))
231 return SwingConstants.NORTH;
233 if (p.y > (pBounds.height - limit))
234 return SwingConstants.SOUTH;
237 return SwingConstants.WEST;
239 if (p.x > (pBounds.width - limit))
240 return SwingConstants.EAST;
246 * This method creates a new DockingListener for the JToolBar.
248 * @return A new DockingListener for the JToolBar.
250 protected MouseInputListener createDockingListener()
252 return new DockingListener(toolBar);
256 * This method creates a new DragWindow for the given JToolBar.
258 * @param toolbar The JToolBar to create a DragWindow for.
260 * @return A new DragWindow.
262 protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
264 return new DragWindow();
268 * This method creates a new floating frame for the JToolBar. By default,
269 * this UI uses createFloatingWindow instead. This method of creating a
270 * floating frame is deprecated.
272 * @param toolbar The JToolBar to create a floating frame for.
274 * @return A new floating frame.
276 protected JFrame createFloatingFrame(JToolBar toolbar)
278 // FIXME: Though deprecated, this should still work.
283 * This method creates a new floating window for the JToolBar. This is the
284 * method used by default to create a floating container for the JToolBar.
286 * @param toolbar The JToolBar to create a floating window for.
288 * @return A new floating window.
290 protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
292 // This one is used by default though.
293 return new ToolBarDialog();
297 * This method creates a new WindowListener for the JToolBar.
299 * @return A new WindowListener.
301 protected WindowListener createFrameListener()
303 return new FrameListener();
307 * This method creates a new nonRolloverBorder for JButtons when the
308 * JToolBar's rollover property is set to false.
310 * @return A new NonRolloverBorder.
312 protected Border createNonRolloverBorder()
314 Border b = UIManager.getBorder("ToolBar.nonrolloverBorder");
318 b = new CompoundBorder(
319 new ButtonBorder(UIManager.getColor("Button.shadow"),
320 UIManager.getColor("Button.darkShadow"),
321 UIManager.getColor("Button.light"),
322 UIManager.getColor("Button.highlight")),
323 BasicBorders.getMarginBorder());
329 * This method creates a new PropertyChangeListener for the JToolBar.
331 * @return A new PropertyChangeListener.
333 protected PropertyChangeListener createPropertyListener()
335 return new PropertyListener();
339 * This method creates a new rollover border for JButtons when the
340 * JToolBar's rollover property is set to true.
342 * @return A new rollover border.
344 protected Border createRolloverBorder()
346 Border b = UIManager.getBorder("ToolBar.rolloverBorder");
350 b = new CompoundBorder(
351 new ButtonBorder(UIManager.getColor("Button.shadow"),
352 UIManager.getColor("Button.darkShadow"),
353 UIManager.getColor("Button.light"),
354 UIManager.getColor("Button.highlight")),
355 BasicBorders.getMarginBorder());
362 * This method creates a new Container listener for the JToolBar.
364 * @return A new Container listener.
366 protected ContainerListener createToolBarContListener()
368 return new ToolBarContListener();
372 * This method creates a new FocusListener for the JToolBar.
374 * @return A new FocusListener for the JToolBar.
376 protected FocusListener createToolBarFocusListener()
378 return new ToolBarFocusListener();
382 * This method creates a new UI delegate for the given JComponent.
384 * @param c The JComponent to create a UI delegate for.
386 * @return A new UI delegate.
388 public static ComponentUI createUI(JComponent c)
390 return new BasicToolBarUI();
394 * This method is called to drag the DragWindow around when the JToolBar is
395 * being dragged around.
397 * @param position The mouse cursor coordinates relative to the JToolBar.
398 * @param origin The screen position of the JToolBar.
400 protected void dragTo(Point position, Point origin)
402 int loc = areaOfClick(origParent,
403 SwingUtilities.convertPoint(toolBar, position,
408 dragWindow.setBorderColor(dockingBorderColor);
409 dragWindow.setBackground(dockingColor);
413 dragWindow.setBorderColor(floatingBorderColor);
414 dragWindow.setBackground(floatingColor);
420 boolean tmp = ((loc == SwingConstants.NORTH)
421 || (loc == SwingConstants.SOUTH) || (loc == -1));
423 cachedOrientation = toolBar.getOrientation();
424 cachedBounds = toolBar.getSize();
425 if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
426 || ((cachedOrientation == VERTICAL) && ! tmp))
428 w = cachedBounds.width;
429 h = cachedBounds.height;
433 w = cachedBounds.height;
434 h = cachedBounds.width;
437 Point p = dragWindow.getOffset();
438 Insets insets = toolBar.getInsets();
440 dragWindow.setBounds((origin.x + position.x) - p.x
441 - ((insets.left + insets.right) / 2),
442 (origin.y + position.y) - p.y
443 - ((insets.top + insets.bottom) / 2), w, h);
445 if (! dragWindow.isVisible())
450 * This method is used at the end of a drag session to place the frame in
451 * either its original parent as a docked JToolBar or in its floating
454 * @param position The position of the mouse cursor relative to the
456 * @param origin The screen position of the JToolBar before the drag session
459 protected void floatAt(Point position, Point origin)
461 Point p = new Point(position);
462 int aoc = areaOfClick(origParent,
463 SwingUtilities.convertPoint(toolBar, p, origParent));
465 Container oldParent = toolBar.getParent();
467 oldParent.remove(toolBar);
468 oldParent.doLayout();
474 newParent = ((RootPaneContainer) floatFrame).getContentPane();
478 newParent = origParent;
484 case SwingConstants.EAST:
485 constraint = BorderLayout.EAST;
487 case SwingConstants.NORTH:
488 constraint = BorderLayout.NORTH;
490 case SwingConstants.SOUTH:
491 constraint = BorderLayout.SOUTH;
493 case SwingConstants.WEST:
494 constraint = BorderLayout.WEST;
497 constraint = BorderLayout.CENTER;
501 int newOrientation = SwingConstants.HORIZONTAL;
503 && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
504 newOrientation = SwingConstants.VERTICAL;
508 constraintBeforeFloating = constraint;
509 lastGoodOrientation = newOrientation;
512 newParent.add(toolBar, constraint);
514 setFloating(aoc == -1, null);
515 toolBar.setOrientation(newOrientation);
517 Insets insets = floatFrame.getInsets();
518 Dimension dims = toolBar.getPreferredSize();
519 p = dragWindow.getOffset();
520 setFloatingLocation((position.x + origin.x) - p.x
521 - ((insets.left + insets.right) / 2),
522 (position.y + origin.y) - p.y
523 - ((insets.top + insets.bottom) / 2));
528 floatFrame.setSize(dims.width + insets.left + insets.right,
529 dims.height + insets.top + insets.bottom);
533 newParent.invalidate();
534 newParent.validate();
539 * This method returns the docking color.
541 * @return The docking color.
543 public Color getDockingColor()
549 * This method returns the Color which is displayed when over a floating
552 * @return The color which is displayed when over a floating area.
554 public Color getFloatingColor()
556 return floatingColor;
560 * This method returns the maximum size of the given JComponent for this UI.
562 * @param c The JComponent to find the maximum size for.
564 * @return The maximum size for this UI.
566 public Dimension getMaximumSize(JComponent c)
568 return getPreferredSize(c);
572 * This method returns the minimum size of the given JComponent for this UI.
574 * @param c The JComponent to find a minimum size for.
576 * @return The minimum size for this UI.
578 public Dimension getMinimumSize(JComponent c)
580 return getPreferredSize(c);
584 * This method installs the needed components for the JToolBar.
586 protected void installComponents()
588 floatFrame = (Window) createFloatingWindow(toolBar);
590 dragWindow = createDragWindow(toolBar);
592 nonRolloverBorder = createNonRolloverBorder();
593 rolloverBorder = createRolloverBorder();
595 borders = new Hashtable();
596 setRolloverBorders(toolBar.isRollover());
602 * This method installs the defaults as specified by the look and feel.
604 protected void installDefaults()
606 LookAndFeel.installBorder(toolBar, "ToolBar.border");
607 LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
608 "ToolBar.foreground", "ToolBar.font");
610 dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
611 dockingColor = UIManager.getColor("ToolBar.dockingBackground");
613 floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
614 floatingColor = UIManager.getColor("ToolBar.floatingBackground");
618 * This method installs the keyboard actions for the JToolBar as specified
619 * by the look and feel.
621 protected void installKeyboardActions()
622 throws NotImplementedException
628 * This method installs listeners for the JToolBar.
630 protected void installListeners()
632 dockingListener = createDockingListener();
633 toolBar.addMouseListener(dockingListener);
634 toolBar.addMouseMotionListener(dockingListener);
636 propertyListener = createPropertyListener();
637 toolBar.addPropertyChangeListener(propertyListener);
639 toolBarContListener = createToolBarContListener();
640 toolBar.addContainerListener(toolBarContListener);
642 windowListener = createFrameListener();
643 floatFrame.addWindowListener(windowListener);
645 toolBarFocusListener = createToolBarFocusListener();
646 toolBar.addFocusListener(toolBarFocusListener);
650 * This method installs non rollover borders for each component inside the
653 * @param c The JComponent whose children need to have non rollover borders
656 protected void installNonRolloverBorders(JComponent c)
658 Component[] components = toolBar.getComponents();
660 for (int i = 0; i < components.length; i++)
661 setBorderToNonRollover(components[i]);
665 * This method installs normal (or their original) borders for each
666 * component inside the given JComponent.
668 * @param c The JComponent whose children need to have their original
671 protected void installNormalBorders(JComponent c)
673 Component[] components = toolBar.getComponents();
675 for (int i = 0; i < components.length; i++)
676 setBorderToNormal(components[i]);
680 * This method install rollover borders for each component inside the given
683 * @param c The JComponent whose children need to have rollover borders
686 protected void installRolloverBorders(JComponent c)
688 Component[] components = toolBar.getComponents();
690 for (int i = 0; i < components.length; i++)
691 setBorderToRollover(components[i]);
695 * This method fills the borders hashtable with a list of components that
696 * are JButtons and their borders.
698 private void fillHashtable()
700 Component[] c = toolBar.getComponents();
702 for (int i = 0; i < c.length; i++)
704 if (c[i] instanceof JButton)
706 // Don't really care about anything other than JButtons
707 JButton b = (JButton) c[i];
709 if (b.getBorder() != null)
710 borders.put(b, b.getBorder());
716 * This method installs the UI for the given JComponent.
718 * @param c The JComponent to install a UI for.
720 public void installUI(JComponent c)
724 if (c instanceof JToolBar)
726 toolBar = (JToolBar) c;
730 installKeyboardActions();
735 * This method returns whether the JToolBar is floating.
737 * @return Whether the JToolBar is floating.
739 public boolean isFloating()
741 return floatFrame.isVisible();
745 * This method returns whether rollover borders have been set.
747 * @return Whether rollover borders have been set.
749 public boolean isRolloverBorders()
751 return toolBar.isRollover();
755 * This method navigates in the given direction giving focus to the next
756 * component in the given direction.
758 * @param direction The direction to give focus to.
760 protected void navigateFocusedComp(int direction)
761 throws NotImplementedException
767 * This method sets the border of the given component to a non rollover
770 * @param c The Component whose border needs to be set.
772 protected void setBorderToNonRollover(Component c)
774 if (c instanceof AbstractButton)
776 AbstractButton b = (AbstractButton) c;
777 b.setRolloverEnabled(false);
779 // Save old border in hashtable.
780 borders.put(b, b.getBorder());
782 b.setBorder(nonRolloverBorder);
787 * This method sets the border of the given component to its original value.
789 * @param c The Component whose border needs to be set.
791 protected void setBorderToNormal(Component c)
793 if (c instanceof AbstractButton)
795 AbstractButton b = (AbstractButton) c;
796 b.setRolloverEnabled(true);
797 b.setBorder((Border) borders.remove(b));
802 * This method sets the border of the given component to a rollover border.
804 * @param c The Component whose border needs to be set.
806 protected void setBorderToRollover(Component c)
808 if (c instanceof AbstractButton)
810 AbstractButton b = (AbstractButton) c;
811 b.setRolloverEnabled(false);
813 // Save old border in hashtable.
814 borders.put(b, b.getBorder());
816 b.setBorder(rolloverBorder);
821 * This method sets the docking color.
823 * @param c The docking color.
825 public void setDockingColor(Color c)
831 * This method sets the floating property for the JToolBar.
833 * @param b Whether the JToolBar is floating.
836 public void setFloating(boolean b, Point p)
838 // FIXME: use p for something. It's not location
839 // since we already have setFloatingLocation.
840 floatFrame.setVisible(b);
844 * This method sets the color displayed when the JToolBar is not in a
847 * @param c The floating color.
849 public void setFloatingColor(Color c)
855 * This method sets the floating location of the JToolBar.
857 * @param x The x coordinate for the floating frame.
858 * @param y The y coordinate for the floating frame.
860 public void setFloatingLocation(int x, int y)
862 // x,y are the coordinates of the new JFrame created to store the toolbar
863 // XXX: The floating location is bogus is not floating.
864 floatFrame.setLocation(x, y);
865 floatFrame.invalidate();
866 floatFrame.validate();
867 floatFrame.repaint();
871 * This is a convenience method for changing the orientation of the
874 * @param orientation The new orientation.
876 public void setOrientation(int orientation)
878 toolBar.setOrientation(orientation);
882 * This method changes the child components to have rollover borders if the
883 * given parameter is true. Otherwise, the components are set to have non
886 * @param rollover Whether the children will have rollover borders.
888 public void setRolloverBorders(boolean rollover)
891 installRolloverBorders(toolBar);
893 installNonRolloverBorders(toolBar);
897 * This method uninstall UI installed components from the JToolBar.
899 protected void uninstallComponents()
901 installNormalBorders(toolBar);
910 * This method removes the defaults installed by the Look and Feel.
912 protected void uninstallDefaults()
914 toolBar.setBackground(null);
915 toolBar.setForeground(null);
916 toolBar.setFont(null);
918 dockingBorderColor = null;
920 floatingBorderColor = null;
921 floatingColor = null;
925 * This method uninstalls keyboard actions installed by the UI.
927 protected void uninstallKeyboardActions()
928 throws NotImplementedException
934 * This method uninstalls listeners installed by the UI.
936 protected void uninstallListeners()
938 toolBar.removeFocusListener(toolBarFocusListener);
939 toolBarFocusListener = null;
941 floatFrame.removeWindowListener(windowListener);
942 windowListener = null;
944 toolBar.removeContainerListener(toolBarContListener);
945 toolBarContListener = null;
947 toolBar.removeMouseMotionListener(dockingListener);
948 toolBar.removeMouseListener(dockingListener);
949 dockingListener = null;
953 * This method uninstalls the UI.
955 * @param c The JComponent that is having this UI removed.
957 public void uninstallUI(JComponent c)
959 uninstallKeyboardActions();
960 uninstallListeners();
961 uninstallComponents();
967 * This is the MouseHandler class that allows the user to drag the JToolBar
968 * in and out of the parent and dock it if it can.
970 public class DockingListener implements MouseInputListener
972 /** Whether the JToolBar is being dragged. */
973 protected boolean isDragging;
976 * The origin point. This point is saved from the beginning press and is
977 * used until the end of the drag session.
979 protected Point origin;
981 /** The JToolBar being dragged. */
982 protected JToolBar toolBar;
985 * Creates a new DockingListener object.
987 * @param t The JToolBar this DockingListener is being used for.
989 public DockingListener(JToolBar t)
995 * This method is called when the mouse is clicked.
997 * @param e The MouseEvent.
999 public void mouseClicked(MouseEvent e)
1005 * This method is called when the mouse is dragged. It delegates the drag
1006 * painting to the dragTo method.
1008 * @param e The MouseEvent.
1010 public void mouseDragged(MouseEvent e)
1013 dragTo(e.getPoint(), origin);
1017 * This method is called when the mouse enters the JToolBar.
1019 * @param e The MouseEvent.
1021 public void mouseEntered(MouseEvent e)
1023 // Don't care (yet).
1027 * This method is called when the mouse exits the JToolBar.
1029 * @param e The MouseEvent.
1031 public void mouseExited(MouseEvent e)
1033 // Don't care (yet).
1037 * This method is called when the mouse is moved in the JToolBar.
1039 * @param e The MouseEvent.
1041 public void mouseMoved(MouseEvent e)
1043 // TODO: What should be done here, if anything?
1047 * This method is called when the mouse is pressed in the JToolBar. If the
1048 * press doesn't occur in a place where it causes the JToolBar to be
1049 * dragged, it returns. Otherwise, it starts a drag session.
1051 * @param e The MouseEvent.
1053 public void mousePressed(MouseEvent e)
1055 if (! toolBar.isFloatable())
1058 Point ssd = e.getPoint();
1059 Insets insets = toolBar.getInsets();
1061 // Verify that this click occurs in the top inset.
1062 if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1064 if (e.getX() > insets.left)
1069 if (e.getY() > insets.top)
1073 origin = new Point(0, 0);
1074 if (toolBar.isShowing())
1075 SwingUtilities.convertPointToScreen(ssd, toolBar);
1077 if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1078 // Need to know who keeps the toolBar if it gets dragged back into it.
1079 origParent = toolBar.getParent();
1081 if (toolBar.isShowing())
1082 SwingUtilities.convertPointToScreen(origin, toolBar);
1086 if (dragWindow != null)
1087 dragWindow.setOffset(new Point(cachedBounds.width/2, cachedBounds.height/2));
1089 dragTo(e.getPoint(), origin);
1093 * This method is called when the mouse is released from the JToolBar.
1095 * @param e The MouseEvent.
1097 public void mouseReleased(MouseEvent e)
1099 if (! isDragging || ! toolBar.isFloatable())
1103 floatAt(e.getPoint(), origin);
1109 * This is the window that appears when the JToolBar is being dragged
1112 protected class DragWindow extends Window
1115 * The current border color. It changes depending on whether the JToolBar
1116 * is over a place that allows it to dock.
1118 private Color borderColor;
1120 /** The between the mouse and the top left corner of the window. */
1121 private Point offset;
1124 * Creates a new DragWindow object.
1125 * This is package-private to avoid an accessor method.
1133 * The color that the border should be.
1135 * @return The border color.
1137 public Color getBorderColor()
1139 if (borderColor == null)
1146 * This method returns the insets for the DragWindow.
1148 * @return The insets for the DragWindow.
1150 public Insets getInsets()
1152 // This window has no decorations, so insets are empty.
1153 return new Insets(0, 0, 0, 0);
1157 * This method returns the mouse offset from the top left corner of the
1160 * @return The mouse offset.
1162 public Point getOffset()
1168 * This method paints the DragWindow.
1170 * @param g The Graphics object to paint with.
1172 public void paint(Graphics g)
1174 // No visiting children necessary.
1175 Color saved = g.getColor();
1176 Rectangle b = getBounds();
1178 g.setColor(getBorderColor());
1179 g.drawRect(0, 0, b.width - 1, b.height - 1);
1185 * This method changes the border color.
1187 * @param c The new border color.
1189 public void setBorderColor(Color c)
1195 * This method changes the mouse offset.
1197 * @param p The new mouse offset.
1199 public void setOffset(Point p)
1205 * FIXME: Do something.
1207 * @param o DOCUMENT ME!
1209 public void setOrientation(int o)
1211 // FIXME: implement.
1216 * This helper class listens for Window events from the floatable window and
1217 * if it is closed, returns the JToolBar to the last known good location.
1219 protected class FrameListener extends WindowAdapter
1222 * This method is called when the floating window is closed.
1224 * @param e The WindowEvent.
1226 public void windowClosing(WindowEvent e)
1228 Container parent = toolBar.getParent();
1229 parent.remove(toolBar);
1231 if (origParent != null)
1233 origParent.add(toolBar,
1234 (constraintBeforeFloating != null)
1235 ? constraintBeforeFloating : BorderLayout.NORTH);
1236 toolBar.setOrientation(lastGoodOrientation);
1239 origParent.invalidate();
1240 origParent.validate();
1241 origParent.repaint();
1246 * This helper class listens for PropertyChangeEvents from the JToolBar.
1248 protected class PropertyListener implements PropertyChangeListener
1251 * This method is called when a property from the JToolBar is changed.
1253 * @param e The PropertyChangeEvent.
1255 public void propertyChange(PropertyChangeEvent e)
1257 // FIXME: need name properties so can change floatFrame title.
1258 if (e.getPropertyName().equals("rollover") && toolBar != null)
1259 setRolloverBorders(toolBar.isRollover());
1264 * This helper class listens for components added to and removed from the
1267 protected class ToolBarContListener implements ContainerListener
1270 * This method is responsible for setting rollover or non rollover for new
1271 * buttons added to the JToolBar.
1273 * @param e The ContainerEvent.
1275 public void componentAdded(ContainerEvent e)
1277 if (e.getChild() instanceof JButton)
1279 JButton b = (JButton) e.getChild();
1281 if (b.getBorder() != null)
1282 borders.put(b, b.getBorder());
1285 if (isRolloverBorders())
1286 setBorderToRollover(e.getChild());
1288 setBorderToNonRollover(e.getChild());
1290 cachedBounds = toolBar.getPreferredSize();
1291 cachedOrientation = toolBar.getOrientation();
1295 * This method is responsible for giving the child components their
1296 * original borders when they are removed.
1298 * @param e The ContainerEvent.
1300 public void componentRemoved(ContainerEvent e)
1302 setBorderToNormal(e.getChild());
1303 cachedBounds = toolBar.getPreferredSize();
1304 cachedOrientation = toolBar.getOrientation();
1309 * This is the floating window that is returned when getFloatingWindow is
1312 private class ToolBarDialog extends JDialog implements UIResource
1315 * Creates a new ToolBarDialog object with the name given by the JToolBar.
1317 public ToolBarDialog()
1320 setName((toolBar.getName() != null) ? toolBar.getName() : "");
1327 protected class ToolBarFocusListener implements FocusListener
1330 * Creates a new ToolBarFocusListener object.
1332 protected ToolBarFocusListener()
1334 // FIXME: implement.
1340 * @param e DOCUMENT ME!
1342 public void focusGained(FocusEvent e)
1344 // FIXME: implement.
1350 * @param e DOCUMENT ME!
1352 public void focusLost(FocusEvent e)
1354 // FIXME: implement.
1359 * This helper class acts as the border for the JToolBar.
1361 private static class ToolBarBorder implements Border
1363 /** The size of the larger, draggable side of the border. */
1364 private static final int offset = 10;
1366 /** The other sides. */
1367 private static final int regular = 2;
1370 * This method returns the border insets for the JToolBar.
1372 * @param c The Component to find insets for.
1374 * @return The border insets.
1376 public Insets getBorderInsets(Component c)
1378 if (c instanceof JToolBar)
1380 JToolBar tb = (JToolBar) c;
1381 int orientation = tb.getOrientation();
1383 if (! tb.isFloatable())
1384 return new Insets(regular, regular, regular, regular);
1385 else if (orientation == SwingConstants.HORIZONTAL)
1386 return new Insets(regular, offset, regular, regular);
1388 return new Insets(offset, regular, regular, regular);
1391 return new Insets(0, 0, 0, 0);
1395 * This method returns whether the border is opaque.
1397 * @return Whether the border is opaque.
1399 public boolean isBorderOpaque()
1405 * This method paints the ribbed area of the border.
1407 * @param g The Graphics object to paint with.
1408 * @param x The x coordinate of the area.
1409 * @param y The y coordinate of the area.
1410 * @param w The width of the area.
1411 * @param h The height of the area.
1412 * @param size The size of the bump.
1413 * @param c The color of the bumps.
1415 private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1418 Color saved = g.getColor();
1421 int hgap = 2 * size;
1422 int vgap = 4 * size;
1425 for (int i = x; i < (w + x); i += hgap)
1426 for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1428 g.fillRect(i, j, size, size);
1434 * This method paints the border around the given Component.
1436 * @param c The Component whose border is being painted.
1437 * @param g The Graphics object to paint with.
1438 * @param x The x coordinate of the component.
1439 * @param y The y coordinate of the component.
1440 * @param width The width of the component.
1441 * @param height The height of the component.
1443 public void paintBorder(Component c, Graphics g, int x, int y, int width,
1446 if (c instanceof JToolBar)
1448 JToolBar tb = (JToolBar) c;
1450 int orientation = tb.getOrientation();
1452 if (orientation == SwingConstants.HORIZONTAL)
1454 paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1455 paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1459 paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1460 paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);