Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicToolBarUI.java
blobfad0108e096b89a82c461f65b16da089b05577e4
1 /* BasicToolBarUI.java --
2 Copyright (C) 2004, 2005 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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 java.awt.BorderLayout;
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.awt.Container;
45 import java.awt.Dimension;
46 import java.awt.Graphics;
47 import java.awt.Insets;
48 import java.awt.Point;
49 import java.awt.Rectangle;
50 import java.awt.Window;
51 import java.awt.event.ContainerEvent;
52 import java.awt.event.ContainerListener;
53 import java.awt.event.FocusEvent;
54 import java.awt.event.FocusListener;
55 import java.awt.event.MouseEvent;
56 import java.awt.event.WindowAdapter;
57 import java.awt.event.WindowEvent;
58 import java.awt.event.WindowListener;
59 import java.beans.PropertyChangeEvent;
60 import java.beans.PropertyChangeListener;
61 import java.util.Hashtable;
63 import javax.swing.JButton;
64 import javax.swing.JComponent;
65 import javax.swing.JDialog;
66 import javax.swing.JFrame;
67 import javax.swing.JToolBar;
68 import javax.swing.RootPaneContainer;
69 import javax.swing.SwingConstants;
70 import javax.swing.SwingUtilities;
71 import javax.swing.UIDefaults;
72 import javax.swing.UIManager;
73 import javax.swing.border.Border;
74 import javax.swing.event.MouseInputListener;
75 import javax.swing.plaf.BorderUIResource.EtchedBorderUIResource;
76 import javax.swing.plaf.ComponentUI;
77 import javax.swing.plaf.ToolBarUI;
78 import javax.swing.plaf.UIResource;
80 /**
81 * This is the Basic Look and Feel UI class for JToolBar.
83 public class BasicToolBarUI extends ToolBarUI implements SwingConstants
85 /** Static owner of all DragWindows. */
86 private static JFrame owner = new JFrame();
88 /** The border used when the JToolBar is in nonrollover mode. */
89 private static Border nonRolloverBorder;
91 /** The border used when the JToolBar is in rollover mode. */
92 private static Border rolloverBorder;
94 /** The last known BorderLayout constraint before floating. */
95 protected String constraintBeforeFloating;
97 /** The last known orientation of the JToolBar before floating. */
98 private int lastGoodOrientation;
100 /** The color of the border when it is dockable. */
101 protected Color dockingBorderColor;
103 /** The background color of the JToolBar when it is dockable. */
104 protected Color dockingColor;
106 /** The docking listener responsible for mouse events on the JToolBar. */
107 protected MouseInputListener dockingListener;
109 /** The window used for dragging the JToolBar. */
110 protected BasicToolBarUI.DragWindow dragWindow;
112 /** The color of the border when it is not dockable. */
113 protected Color floatingBorderColor;
115 /** The background color of the JToolBar when it is not dockable. */
116 protected Color floatingColor;
118 /** The index of the focused component. */
119 protected int focusedCompIndex;
121 /** The PropertyChangeListener for the JToolBar. */
122 protected PropertyChangeListener propertyListener;
124 /** The JToolBar this UI delegate is responsible for. */
125 protected JToolBar toolBar;
127 /** The Container listener for the JToolBar. */
128 protected ContainerListener toolBarContListener;
130 /** The Focus listener for the JToolBar. */
131 protected FocusListener toolBarFocusListener;
134 * The floating window that is responsible for holding the JToolBar when it
135 * is dragged outside of its original parent.
137 private transient Window floatFrame;
139 /** The original parent of the JToolBar. */
140 private transient Container origParent;
142 /** A hashtable of components and their original borders. */
143 private transient Hashtable borders;
145 /** A window listener for the floatable frame. */
146 private transient WindowListener windowListener;
148 /** A set of cached bounds of the JToolBar. */
149 private transient Dimension cachedBounds;
151 /** The cached orientation of the JToolBar. */
152 private transient int cachedOrientation;
155 * This method creates a new <code>BasicToolBarUI</code> object for the given JToolBar.
157 public BasicToolBarUI()
159 // Do nothing here.
163 * This method returns whether the JToolBar can dock at the given position.
165 * @param c The component to try to dock in.
166 * @param p The position of the mouse cursor relative to the given
167 * component.
169 * @return Whether the JToolBar can dock.
171 public boolean canDock(Component c, Point p)
173 return areaOfClick(c, p) != -1;
177 * This helper method returns the position of the JToolBar if it can dock.
179 * @param c The component to try to dock in.
180 * @param p The position of the mouse cursor relative to the given
181 * component.
183 * @return One of the SwingConstants directions or -1 if the JToolBar can't
184 * dock.
186 private int areaOfClick(Component c, Point p)
188 // Has to dock in immediate parent, not eventual root container.
189 Rectangle pBounds = c.getBounds();
191 // XXX: In Sun's implementation, the space the toolbar has to dock is dependent on the size it had last.
192 Dimension d = toolBar.getSize();
193 int limit = Math.min(d.width, d.height);
195 // The order of checking is 1. top 2. bottom 3. left 4. right
196 if (! pBounds.contains(p))
197 return -1;
199 if (p.y < limit)
200 return SwingConstants.NORTH;
202 if (p.y > (pBounds.height - limit))
203 return SwingConstants.SOUTH;
205 if (p.x < limit)
206 return SwingConstants.WEST;
208 if (p.x > (pBounds.width - limit))
209 return SwingConstants.EAST;
211 return -1;
215 * This method creates a new DockingListener for the JToolBar.
217 * @return A new DockingListener for the JToolBar.
219 protected MouseInputListener createDockingListener()
221 return new DockingListener(toolBar);
225 * This method creates a new DragWindow for the given JToolBar.
227 * @param toolbar The JToolBar to create a DragWindow for.
229 * @return A new DragWindow.
231 protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
233 return new DragWindow();
237 * This method creates a new floating frame for the JToolBar. By default,
238 * this UI uses createFloatingWindow instead. This method of creating a
239 * floating frame is deprecated.
241 * @param toolbar The JToolBar to create a floating frame for.
243 * @return A new floating frame.
245 protected JFrame createFloatingFrame(JToolBar toolbar)
247 // FIXME: Though deprecated, this should still work.
248 return null;
252 * This method creates a new floating window for the JToolBar. This is the
253 * method used by default to create a floating container for the JToolBar.
255 * @param toolbar The JToolBar to create a floating window for.
257 * @return A new floating window.
259 protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
261 // This one is used by default though.
262 return new ToolBarDialog();
266 * This method creates a new WindowListener for the JToolBar.
268 * @return A new WindowListener.
270 protected WindowListener createFrameListener()
272 return new FrameListener();
276 * This method creates a new nonRolloverBorder for JButtons when the
277 * JToolBar's rollover property is set to false.
279 * @return A new NonRolloverBorder.
281 protected Border createNonRolloverBorder()
283 return new EtchedBorderUIResource();
287 * This method creates a new PropertyChangeListener for the JToolBar.
289 * @return A new PropertyChangeListener.
291 protected PropertyChangeListener createPropertyListener()
293 return new PropertyListener();
297 * This method creates a new rollover border for JButtons when the
298 * JToolBar's rollover property is set to true.
300 * @return A new rollover border.
302 protected Border createRolloverBorder()
304 return new EtchedBorderUIResource()
306 public void paintBorder(Component c, Graphics g, int x, int y,
307 int width, int height)
309 if (c instanceof JButton)
311 if (((JButton) c).getModel().isRollover())
312 super.paintBorder(c, g, x, y, width, height);
319 * This method creates a new Container listener for the JToolBar.
321 * @return A new Container listener.
323 protected ContainerListener createToolBarContListener()
325 return new ToolBarContListener();
329 * This method creates a new FocusListener for the JToolBar.
331 * @return A new FocusListener for the JToolBar.
333 protected FocusListener createToolBarFocusListener()
335 return new ToolBarFocusListener();
339 * This method creates a new UI delegate for the given JComponent.
341 * @param c The JComponent to create a UI delegate for.
343 * @return A new UI delegate.
345 public static ComponentUI createUI(JComponent c)
347 return new BasicToolBarUI();
351 * This method is called to drag the DragWindow around when the JToolBar is
352 * being dragged around.
354 * @param position The mouse cursor coordinates relative to the JToolBar.
355 * @param origin The screen position of the JToolBar.
357 protected void dragTo(Point position, Point origin)
359 int loc = areaOfClick(origParent,
360 SwingUtilities.convertPoint(toolBar, position,
361 origParent));
363 if (loc != -1)
365 dragWindow.setBorderColor(dockingBorderColor);
366 dragWindow.setBackground(dockingColor);
368 else
370 dragWindow.setBorderColor(floatingBorderColor);
371 dragWindow.setBackground(floatingColor);
374 int w = 0;
375 int h = 0;
377 boolean tmp = ((loc == SwingConstants.NORTH)
378 || (loc == SwingConstants.SOUTH) || (loc == -1));
380 if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
381 || ((cachedOrientation == VERTICAL) && ! tmp))
383 w = cachedBounds.width;
384 h = cachedBounds.height;
386 else
388 w = cachedBounds.height;
389 h = cachedBounds.width;
392 Point p = dragWindow.getOffset();
393 Insets insets = toolBar.getInsets();
395 dragWindow.setBounds((origin.x + position.x) - p.x
396 - ((insets.left + insets.right) / 2),
397 (origin.y + position.y) - p.y
398 - ((insets.top + insets.bottom) / 2), w, h);
400 if (! dragWindow.isVisible())
401 dragWindow.show();
405 * This method is used at the end of a drag session to place the frame in
406 * either its original parent as a docked JToolBar or in its floating
407 * frame.
409 * @param position The position of the mouse cursor relative to the
410 * JToolBar.
411 * @param origin The screen position of the JToolBar before the drag session
412 * started.
414 protected void floatAt(Point position, Point origin)
416 Point p = new Point(position);
417 int aoc = areaOfClick(origParent,
418 SwingUtilities.convertPoint(toolBar, p, origParent));
420 Container oldParent = toolBar.getParent();
422 oldParent.remove(toolBar);
423 oldParent.doLayout();
424 oldParent.repaint();
426 Container newParent;
428 if (aoc == -1)
429 newParent = ((RootPaneContainer) floatFrame).getContentPane();
430 else
432 floatFrame.hide();
433 newParent = origParent;
436 String constraint;
437 switch (aoc)
439 case SwingConstants.EAST:
440 constraint = BorderLayout.EAST;
441 break;
442 case SwingConstants.NORTH:
443 constraint = BorderLayout.NORTH;
444 break;
445 case SwingConstants.SOUTH:
446 constraint = BorderLayout.SOUTH;
447 break;
448 case SwingConstants.WEST:
449 constraint = BorderLayout.WEST;
450 break;
451 default:
452 constraint = BorderLayout.CENTER;
453 break;
456 int newOrientation = SwingConstants.HORIZONTAL;
457 if ((aoc != -1)
458 && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
459 newOrientation = SwingConstants.VERTICAL;
461 if (aoc != -1)
463 constraintBeforeFloating = constraint;
464 lastGoodOrientation = newOrientation;
467 newParent.add(toolBar, constraint);
469 setFloating(aoc == -1, null);
470 toolBar.setOrientation(newOrientation);
472 Insets insets = floatFrame.getInsets();
473 Dimension dims = toolBar.getPreferredSize();
474 p = dragWindow.getOffset();
475 setFloatingLocation((position.x + origin.x) - p.x
476 - ((insets.left + insets.right) / 2),
477 (position.y + origin.y) - p.y
478 - ((insets.top + insets.bottom) / 2));
480 if (aoc == -1)
482 floatFrame.pack();
483 floatFrame.setSize(dims.width + insets.left + insets.right,
484 dims.height + insets.top + insets.bottom);
485 floatFrame.show();
488 newParent.invalidate();
489 newParent.validate();
490 newParent.repaint();
494 * This method returns the docking color.
496 * @return The docking color.
498 public Color getDockingColor()
500 return dockingColor;
504 * This method returns the Color which is displayed when over a floating
505 * area.
507 * @return The color which is displayed when over a floating area.
509 public Color getFloatingColor()
511 return floatingColor;
515 * This method returns the maximum size of the given JComponent for this UI.
517 * @param c The JComponent to find the maximum size for.
519 * @return The maximum size for this UI.
521 public Dimension getMaximumSize(JComponent c)
523 return getPreferredSize(c);
527 * This method returns the minimum size of the given JComponent for this UI.
529 * @param c The JComponent to find a minimum size for.
531 * @return The minimum size for this UI.
533 public Dimension getMinimumSize(JComponent c)
535 return getPreferredSize(c);
539 * This method returns the preferred size of the given JComponent for this
540 * UI.
542 * @param c The JComponent to find a preferred size for.
544 * @return The preferred size for this UI.
546 public Dimension getPreferredSize(JComponent c)
548 return toolBar.getLayout().preferredLayoutSize(c);
552 * This method installs the needed components for the JToolBar.
554 protected void installComponents()
556 floatFrame = (Window) createFloatingWindow(toolBar);
558 dragWindow = createDragWindow(toolBar);
560 cachedBounds = toolBar.getPreferredSize();
561 cachedOrientation = toolBar.getOrientation();
563 nonRolloverBorder = createNonRolloverBorder();
564 rolloverBorder = createRolloverBorder();
566 borders = new Hashtable();
568 fillHashtable();
572 * This method installs the defaults as specified by the look and feel.
574 protected void installDefaults()
576 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
578 toolBar.setBorder(new ToolBarBorder());
579 toolBar.setBackground(defaults.getColor("ToolBar.background"));
580 toolBar.setForeground(defaults.getColor("ToolBar.foreground"));
581 toolBar.setFont(defaults.getFont("ToolBar.font"));
583 dockingBorderColor = defaults.getColor("ToolBar.dockingForeground");
584 dockingColor = defaults.getColor("ToolBar.dockingBackground");
586 floatingBorderColor = defaults.getColor("ToolBar.floatingForeground");
587 floatingColor = defaults.getColor("ToolBar.floatingBackground");
591 * This method installs the keyboard actions for the JToolBar as specified
592 * by the look and feel.
594 protected void installKeyboardActions()
596 // FIXME: implement.
600 * This method installs listeners for the JToolBar.
602 * @param toolbar The JToolBar to register listeners for.
604 protected void installListeners(JToolBar toolbar)
606 dockingListener = createDockingListener();
607 toolBar.addMouseListener(dockingListener);
608 toolBar.addMouseMotionListener(dockingListener);
610 propertyListener = createPropertyListener();
611 toolBar.addPropertyChangeListener(propertyListener);
613 toolBarContListener = createToolBarContListener();
614 toolBar.addContainerListener(toolBarContListener);
616 windowListener = createFrameListener();
617 floatFrame.addWindowListener(windowListener);
619 toolBarFocusListener = createToolBarFocusListener();
620 toolBar.addFocusListener(toolBarFocusListener);
624 * This method installs non rollover borders for each component inside the
625 * given JComponent.
627 * @param c The JComponent whose children need to have non rollover borders
628 * installed.
630 protected void installNonRolloverBorders(JComponent c)
632 Component[] components = toolBar.getComponents();
634 for (int i = 0; i < components.length; i++)
635 setBorderToNonRollover(components[i]);
639 * This method installs normal (or their original) borders for each
640 * component inside the given JComponent.
642 * @param c The JComponent whose children need to have their original
643 * borders installed.
645 protected void installNormalBorders(JComponent c)
647 Component[] components = toolBar.getComponents();
649 for (int i = 0; i < components.length; i++)
650 setBorderToNormal(components[i]);
654 * This method install rollover borders for each component inside the given
655 * JComponent.
657 * @param c The JComponent whose children need to have rollover borders
658 * installed.
660 protected void installRolloverBorders(JComponent c)
662 Component[] components = toolBar.getComponents();
664 for (int i = 0; i < components.length; i++)
665 setBorderToRollover(components[i]);
669 * This method fills the borders hashtable with a list of components that
670 * are JButtons and their borders.
672 private void fillHashtable()
674 Component[] c = toolBar.getComponents();
676 for (int i = 0; i < c.length; i++)
678 if (c[i] instanceof JButton)
680 // Don't really care about anything other than JButtons
681 JButton b = (JButton) c[i];
683 if (b.getBorder() != null)
684 borders.put(b, b.getBorder());
690 * This method installs the UI for the given JComponent.
692 * @param c The JComponent to install a UI for.
694 public void installUI(JComponent c)
696 super.installUI(c);
698 if (c instanceof JToolBar)
700 toolBar = (JToolBar) c;
701 toolBar.setOpaque(true);
702 installDefaults();
703 installComponents();
704 installListeners(toolBar);
705 installKeyboardActions();
710 * This method returns whether the JToolBar is floating.
712 * @return Whether the JToolBar is floating.
714 public boolean isFloating()
716 return floatFrame.isVisible();
720 * This method returns whether rollover borders have been set.
722 * @return Whether rollover borders have been set.
724 public boolean isRolloverBorders()
726 return toolBar.isRollover();
730 * This method navigates in the given direction giving focus to the next
731 * component in the given direction.
733 * @param direction The direction to give focus to.
735 protected void navigateFocusedComp(int direction)
737 // FIXME: Implement.
741 * This method sets the border of the given component to a non rollover
742 * border.
744 * @param c The Component whose border needs to be set.
746 protected void setBorderToNonRollover(Component c)
748 if (c instanceof JButton)
750 JButton b = (JButton) c;
751 b.setRolloverEnabled(false);
752 b.setBorder(nonRolloverBorder);
757 * This method sets the border of the given component to its original value.
759 * @param c The Component whose border needs to be set.
761 protected void setBorderToNormal(Component c)
763 if (c instanceof JButton)
765 JButton b = (JButton) c;
766 Border border = (Border) borders.get(b);
767 b.setBorder(border);
772 * This method sets the border of the given component to a rollover border.
774 * @param c The Component whose border needs to be set.
776 protected void setBorderToRollover(Component c)
778 if (c instanceof JButton)
780 JButton b = (JButton) c;
781 b.setRolloverEnabled(true);
782 b.setBorder(rolloverBorder);
787 * This method sets the docking color.
789 * @param c The docking color.
791 public void setDockingColor(Color c)
793 dockingColor = c;
797 * This method sets the floating property for the JToolBar.
799 * @param b Whether the JToolBar is floating.
800 * @param p FIXME
802 public void setFloating(boolean b, Point p)
804 // FIXME: use p for something. It's not location
805 // since we already have setFloatingLocation.
806 floatFrame.setVisible(b);
810 * This method sets the color displayed when the JToolBar is not in a
811 * dockable area.
813 * @param c The floating color.
815 public void setFloatingColor(Color c)
817 floatingColor = c;
821 * This method sets the floating location of the JToolBar.
823 * @param x The x coordinate for the floating frame.
824 * @param y The y coordinate for the floating frame.
826 public void setFloatingLocation(int x, int y)
828 // x,y are the coordinates of the new JFrame created to store the toolbar
829 // XXX: The floating location is bogus is not floating.
830 floatFrame.setLocation(x, y);
831 floatFrame.invalidate();
832 floatFrame.validate();
833 floatFrame.repaint();
837 * This is a convenience method for changing the orientation of the
838 * JToolBar.
840 * @param orientation The new orientation.
842 public void setOrientation(int orientation)
844 toolBar.setOrientation(orientation);
848 * This method changes the child components to have rollover borders if the
849 * given parameter is true. Otherwise, the components are set to have non
850 * rollover borders.
852 * @param rollover Whether the children will have rollover borders.
854 public void setRolloverBorders(boolean rollover)
856 if (rollover)
857 installRolloverBorders(toolBar);
858 else
859 installNonRolloverBorders(toolBar);
863 * This method uninstall UI installed components from the JToolBar.
865 protected void uninstallComponents()
867 installNormalBorders(toolBar);
868 borders = null;
869 rolloverBorder = null;
870 nonRolloverBorder = null;
871 cachedBounds = null;
873 floatFrame = null;
874 dragWindow = null;
878 * This method removes the defaults installed by the Look and Feel.
880 protected void uninstallDefaults()
882 toolBar.setBackground(null);
883 toolBar.setForeground(null);
884 toolBar.setFont(null);
886 dockingBorderColor = null;
887 dockingColor = null;
888 floatingBorderColor = null;
889 floatingColor = null;
893 * This method uninstalls keyboard actions installed by the UI.
895 protected void uninstallKeyboardActions()
897 // FIXME: implement.
901 * This method uninstalls listeners installed by the UI.
903 protected void uninstallListeners()
905 toolBar.removeFocusListener(toolBarFocusListener);
906 toolBarFocusListener = null;
908 floatFrame.removeWindowListener(windowListener);
909 windowListener = null;
911 toolBar.removeContainerListener(toolBarContListener);
912 toolBarContListener = null;
914 toolBar.removeMouseMotionListener(dockingListener);
915 toolBar.removeMouseListener(dockingListener);
916 dockingListener = null;
920 * This method uninstalls the UI.
922 * @param c The JComponent that is having this UI removed.
924 public void uninstallUI(JComponent c)
926 uninstallKeyboardActions();
927 uninstallListeners();
928 uninstallComponents();
929 uninstallDefaults();
930 toolBar = null;
934 * This is the MouseHandler class that allows the user to drag the JToolBar
935 * in and out of the parent and dock it if it can.
937 public class DockingListener implements MouseInputListener
939 /** Whether the JToolBar is being dragged. */
940 protected boolean isDragging;
943 * The origin point. This point is saved from the beginning press and is
944 * used until the end of the drag session.
946 protected Point origin;
948 /** The JToolBar being dragged. */
949 protected JToolBar toolBar;
952 * Creates a new DockingListener object.
954 * @param t The JToolBar this DockingListener is being used for.
956 public DockingListener(JToolBar t)
958 toolBar = t;
962 * This method is called when the mouse is clicked.
964 * @param e The MouseEvent.
966 public void mouseClicked(MouseEvent e)
968 // Don't care.
972 * This method is called when the mouse is dragged. It delegates the drag
973 * painting to the dragTo method.
975 * @param e The MouseEvent.
977 public void mouseDragged(MouseEvent e)
979 if (isDragging)
980 dragTo(e.getPoint(), origin);
984 * This method is called when the mouse enters the JToolBar.
986 * @param e The MouseEvent.
988 public void mouseEntered(MouseEvent e)
990 // Don't care (yet).
994 * This method is called when the mouse exits the JToolBar.
996 * @param e The MouseEvent.
998 public void mouseExited(MouseEvent e)
1000 // Don't care (yet).
1004 * This method is called when the mouse is moved in the JToolBar.
1006 * @param e The MouseEvent.
1008 public void mouseMoved(MouseEvent e)
1013 * This method is called when the mouse is pressed in the JToolBar. If the
1014 * press doesn't occur in a place where it causes the JToolBar to be
1015 * dragged, it returns. Otherwise, it starts a drag session.
1017 * @param e The MouseEvent.
1019 public void mousePressed(MouseEvent e)
1021 if (! toolBar.isFloatable())
1022 return;
1024 Point ssd = e.getPoint();
1025 Insets insets = toolBar.getInsets();
1027 // Verify that this click occurs in the top inset.
1028 if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1030 if (e.getX() > insets.left)
1031 return;
1033 else
1035 if (e.getY() > insets.top)
1036 return;
1039 origin = new Point(0, 0);
1040 SwingUtilities.convertPointToScreen(ssd, toolBar);
1042 if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1043 // Need to know who keeps the toolBar if it gets dragged back into it.
1044 origParent = toolBar.getParent();
1046 SwingUtilities.convertPointToScreen(origin, toolBar);
1048 isDragging = true;
1050 if (dragWindow != null)
1051 dragWindow.setOffset(new Point(e.getX(), e.getY()));
1053 dragTo(e.getPoint(), origin);
1057 * This method is called when the mouse is released from the JToolBar.
1059 * @param e The MouseEvent.
1061 public void mouseReleased(MouseEvent e)
1063 if (! isDragging || ! toolBar.isFloatable())
1064 return;
1066 isDragging = false;
1067 floatAt(e.getPoint(), origin);
1068 dragWindow.hide();
1073 * This is the window that appears when the JToolBar is being dragged
1074 * around.
1076 protected class DragWindow extends Window
1079 * The current border color. It changes depending on whether the JToolBar
1080 * is over a place that allows it to dock.
1082 private Color borderColor;
1084 /** The between the mouse and the top left corner of the window. */
1085 private Point offset;
1088 * Creates a new DragWindow object.
1090 private DragWindow()
1092 super(owner);
1096 * The color that the border should be.
1098 * @return The border color.
1100 public Color getBorderColor()
1102 if (borderColor == null)
1103 return Color.BLACK;
1105 return borderColor;
1109 * This method returns the insets for the DragWindow.
1111 * @return The insets for the DragWindow.
1113 public Insets getInsets()
1115 // This window has no decorations, so insets are empty.
1116 return new Insets(0, 0, 0, 0);
1120 * This method returns the mouse offset from the top left corner of the
1121 * DragWindow.
1123 * @return The mouse offset.
1125 public Point getOffset()
1127 return offset;
1131 * This method paints the DragWindow.
1133 * @param g The Graphics object to paint with.
1135 public void paint(Graphics g)
1137 // No visiting children necessary.
1138 Color saved = g.getColor();
1139 Rectangle b = getBounds();
1141 g.setColor(getBorderColor());
1142 g.drawRect(0, 0, b.width - 1, b.height - 1);
1144 g.setColor(saved);
1148 * This method changes the border color.
1150 * @param c The new border color.
1152 public void setBorderColor(Color c)
1154 borderColor = c;
1158 * This method changes the mouse offset.
1160 * @param p The new mouse offset.
1162 public void setOffset(Point p)
1164 offset = p;
1168 * FIXME: Do something.
1170 * @param o DOCUMENT ME!
1172 public void setOrientation(int o)
1174 // FIXME: implement.
1179 * This helper class listens for Window events from the floatable window and
1180 * if it is closed, returns the JToolBar to the last known good location.
1182 protected class FrameListener extends WindowAdapter
1185 * This method is called when the floating window is closed.
1187 * @param e The WindowEvent.
1189 public void windowClosing(WindowEvent e)
1191 Container parent = toolBar.getParent();
1192 parent.remove(toolBar);
1194 if (origParent != null)
1196 origParent.add(toolBar,
1197 (constraintBeforeFloating != null)
1198 ? constraintBeforeFloating : BorderLayout.NORTH);
1199 toolBar.setOrientation(lastGoodOrientation);
1202 origParent.invalidate();
1203 origParent.validate();
1204 origParent.repaint();
1209 * This helper class listens for PropertyChangeEvents from the JToolBar.
1211 protected class PropertyListener implements PropertyChangeListener
1214 * This method is called when a property from the JToolBar is changed.
1216 * @param e The PropertyChangeEvent.
1218 public void propertyChange(PropertyChangeEvent e)
1220 // FIXME: need name properties so can change floatFrame title.
1221 if (e.getPropertyName().equals("rollover"))
1222 setRolloverBorders(toolBar.isRollover());
1227 * This helper class listens for components added to and removed from the
1228 * JToolBar.
1230 protected class ToolBarContListener implements ContainerListener
1233 * This method is responsible for setting rollover or non rollover for new
1234 * buttons added to the JToolBar.
1236 * @param e The ContainerEvent.
1238 public void componentAdded(ContainerEvent e)
1240 if (e.getChild() instanceof JButton)
1242 JButton b = (JButton) e.getChild();
1244 if (b.getBorder() != null)
1245 borders.put(b, b.getBorder());
1248 if (isRolloverBorders())
1249 setBorderToRollover(e.getChild());
1250 else
1251 setBorderToNonRollover(e.getChild());
1253 cachedBounds = toolBar.getPreferredSize();
1254 cachedOrientation = toolBar.getOrientation();
1258 * This method is responsible for giving the child components their
1259 * original borders when they are removed.
1261 * @param e The ContainerEvent.
1263 public void componentRemoved(ContainerEvent e)
1265 setBorderToNormal(e.getChild());
1266 cachedBounds = toolBar.getPreferredSize();
1267 cachedOrientation = toolBar.getOrientation();
1272 * This is the floating window that is returned when getFloatingWindow is
1273 * called.
1275 private class ToolBarDialog extends JDialog implements UIResource
1278 * Creates a new ToolBarDialog object with the name given by the JToolBar.
1280 public ToolBarDialog()
1282 super();
1283 setName((toolBar.getName() != null) ? toolBar.getName() : "");
1288 * DOCUMENT ME!
1290 protected class ToolBarFocusListener implements FocusListener
1293 * Creates a new ToolBarFocusListener object.
1295 protected ToolBarFocusListener()
1297 // FIXME: implement.
1301 * DOCUMENT ME!
1303 * @param e DOCUMENT ME!
1305 public void focusGained(FocusEvent e)
1307 // FIXME: implement.
1311 * DOCUMENT ME!
1313 * @param e DOCUMENT ME!
1315 public void focusLost(FocusEvent e)
1317 // FIXME: implement.
1322 * This helper class acts as the border for the JToolBar.
1324 private static class ToolBarBorder implements Border
1326 /** The size of the larger, draggable side of the border. */
1327 private static final int offset = 10;
1329 /** The other sides. */
1330 private static final int regular = 2;
1333 * This method returns the border insets for the JToolBar.
1335 * @param c The Component to find insets for.
1337 * @return The border insets.
1339 public Insets getBorderInsets(Component c)
1341 if (c instanceof JToolBar)
1343 JToolBar tb = (JToolBar) c;
1344 int orientation = tb.getOrientation();
1346 if (! tb.isFloatable())
1347 return new Insets(regular, regular, regular, regular);
1348 else if (orientation == SwingConstants.HORIZONTAL)
1349 return new Insets(regular, offset, regular, regular);
1350 else
1351 return new Insets(offset, regular, regular, regular);
1354 return new Insets(0, 0, 0, 0);
1358 * This method returns whether the border is opaque.
1360 * @return Whether the border is opaque.
1362 public boolean isBorderOpaque()
1364 return false;
1368 * This method paints the ribbed area of the border.
1370 * @param g The Graphics object to paint with.
1371 * @param x The x coordinate of the area.
1372 * @param y The y coordinate of the area.
1373 * @param w The width of the area.
1374 * @param h The height of the area.
1375 * @param size The size of the bump.
1376 * @param c The color of the bumps.
1378 private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1379 Color c)
1381 Color saved = g.getColor();
1382 g.setColor(c);
1384 int hgap = 2 * size;
1385 int vgap = 4 * size;
1386 int count = 0;
1388 for (int i = x; i < (w + x); i += hgap)
1389 for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1390 j += vgap)
1391 g.fillRect(i, j, size, size);
1393 g.setColor(saved);
1397 * This method paints the border around the given Component.
1399 * @param c The Component whose border is being painted.
1400 * @param g The Graphics object to paint with.
1401 * @param x The x coordinate of the component.
1402 * @param y The y coordinate of the component.
1403 * @param width The width of the component.
1404 * @param height The height of the component.
1406 public void paintBorder(Component c, Graphics g, int x, int y, int width,
1407 int height)
1409 if (c instanceof JToolBar)
1411 JToolBar tb = (JToolBar) c;
1413 int orientation = tb.getOrientation();
1415 if (orientation == SwingConstants.HORIZONTAL)
1417 paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1418 paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1420 else
1422 paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1423 paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);