1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64:
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84:
85:
88: public class BasicToolBarUI extends ToolBarUI implements SwingConstants
89: {
90:
92: static JFrame owner = new JFrame();
93:
94:
95: private static Border nonRolloverBorder;
96:
97:
98: private static Border rolloverBorder;
99:
100:
101: protected String constraintBeforeFloating;
102:
103:
105: int lastGoodOrientation;
106:
107:
108: protected Color dockingBorderColor;
109:
110:
111: protected Color dockingColor;
112:
113:
114: protected MouseInputListener dockingListener;
115:
116:
117: protected BasicToolBarUI.DragWindow dragWindow;
118:
119:
120: protected Color floatingBorderColor;
121:
122:
123: protected Color floatingColor;
124:
125:
126: protected int focusedCompIndex;
127:
128:
129: protected PropertyChangeListener propertyListener;
130:
131:
132: protected JToolBar toolBar;
133:
134:
135: protected ContainerListener toolBarContListener;
136:
137:
138: protected FocusListener toolBarFocusListener;
139:
140:
143: protected KeyStroke leftKey;
144:
145:
148: protected KeyStroke rightKey;
149:
150:
153: protected KeyStroke upKey;
154:
155:
158: protected KeyStroke downKey;
159:
160:
164: private transient Window floatFrame;
165:
166:
168: transient Container origParent;
169:
170:
172: transient Hashtable borders;
173:
174:
175: private transient WindowListener windowListener;
176:
177:
179: transient Dimension cachedBounds;
180:
181:
183: transient int cachedOrientation;
184:
185:
188: public BasicToolBarUI()
189: {
190:
191: }
192:
193:
202: public boolean canDock(Component c, Point p)
203: {
204: return areaOfClick(c, p) != -1;
205: }
206:
207:
217: private int areaOfClick(Component c, Point p)
218: {
219:
220: Rectangle pBounds = c.getBounds();
221:
222:
223: Dimension d = toolBar.getSize();
224: int limit = Math.min(d.width, d.height);
225:
226:
227: if (! pBounds.contains(p))
228: return -1;
229:
230: if (p.y < limit)
231: return SwingConstants.NORTH;
232:
233: if (p.y > (pBounds.height - limit))
234: return SwingConstants.SOUTH;
235:
236: if (p.x < limit)
237: return SwingConstants.WEST;
238:
239: if (p.x > (pBounds.width - limit))
240: return SwingConstants.EAST;
241:
242: return -1;
243: }
244:
245:
250: protected MouseInputListener createDockingListener()
251: {
252: return new DockingListener(toolBar);
253: }
254:
255:
262: protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
263: {
264: return new DragWindow();
265: }
266:
267:
276: protected JFrame createFloatingFrame(JToolBar toolbar)
277: {
278:
279: return null;
280: }
281:
282:
290: protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
291: {
292:
293: return new ToolBarDialog();
294: }
295:
296:
301: protected WindowListener createFrameListener()
302: {
303: return new FrameListener();
304: }
305:
306:
312: protected Border createNonRolloverBorder()
313: {
314: Border b = UIManager.getBorder("ToolBar.nonrolloverBorder");
315:
316: if (b == null)
317: {
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());
324: }
325:
326: return b; }
327:
328:
333: protected PropertyChangeListener createPropertyListener()
334: {
335: return new PropertyListener();
336: }
337:
338:
344: protected Border createRolloverBorder()
345: {
346: Border b = UIManager.getBorder("ToolBar.rolloverBorder");
347:
348: if (b == null)
349: {
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());
356: }
357:
358: return b;
359: }
360:
361:
366: protected ContainerListener createToolBarContListener()
367: {
368: return new ToolBarContListener();
369: }
370:
371:
376: protected FocusListener createToolBarFocusListener()
377: {
378: return new ToolBarFocusListener();
379: }
380:
381:
388: public static ComponentUI createUI(JComponent c)
389: {
390: return new BasicToolBarUI();
391: }
392:
393:
400: protected void dragTo(Point position, Point origin)
401: {
402: int loc = areaOfClick(origParent,
403: SwingUtilities.convertPoint(toolBar, position,
404: origParent));
405:
406: if (loc != -1)
407: {
408: dragWindow.setBorderColor(dockingBorderColor);
409: dragWindow.setBackground(dockingColor);
410: }
411: else
412: {
413: dragWindow.setBorderColor(floatingBorderColor);
414: dragWindow.setBackground(floatingColor);
415: }
416:
417: int w = 0;
418: int h = 0;
419:
420: boolean tmp = (loc == SwingConstants.NORTH)
421: || (loc == SwingConstants.SOUTH) || (loc == -1);
422:
423: cachedOrientation = toolBar.getOrientation();
424: cachedBounds = toolBar.getSize();
425: if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
426: || ((cachedOrientation == VERTICAL) && ! tmp))
427: {
428: w = cachedBounds.width;
429: h = cachedBounds.height;
430: }
431: else
432: {
433: w = cachedBounds.height;
434: h = cachedBounds.width;
435: }
436:
437: Point p = dragWindow.getOffset();
438: Insets insets = toolBar.getInsets();
439:
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);
444:
445: if (! dragWindow.isVisible())
446: dragWindow.show();
447: }
448:
449:
459: protected void floatAt(Point position, Point origin)
460: {
461: Point p = new Point(position);
462: int aoc = areaOfClick(origParent,
463: SwingUtilities.convertPoint(toolBar, p, origParent));
464:
465: Container oldParent = toolBar.getParent();
466:
467: oldParent.remove(toolBar);
468: oldParent.doLayout();
469: oldParent.repaint();
470:
471: Container newParent;
472:
473: if (aoc == -1)
474: newParent = ((RootPaneContainer) floatFrame).getContentPane();
475: else
476: {
477: floatFrame.hide();
478: newParent = origParent;
479: }
480:
481: String constraint;
482: switch (aoc)
483: {
484: case SwingConstants.EAST:
485: constraint = BorderLayout.EAST;
486: break;
487: case SwingConstants.NORTH:
488: constraint = BorderLayout.NORTH;
489: break;
490: case SwingConstants.SOUTH:
491: constraint = BorderLayout.SOUTH;
492: break;
493: case SwingConstants.WEST:
494: constraint = BorderLayout.WEST;
495: break;
496: default:
497: constraint = BorderLayout.CENTER;
498: break;
499: }
500:
501: int newOrientation = SwingConstants.HORIZONTAL;
502: if ((aoc != -1)
503: && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
504: newOrientation = SwingConstants.VERTICAL;
505:
506: if (aoc != -1)
507: {
508: constraintBeforeFloating = constraint;
509: lastGoodOrientation = newOrientation;
510: }
511:
512: newParent.add(toolBar, constraint);
513:
514: setFloating(aoc == -1, null);
515: toolBar.setOrientation(newOrientation);
516:
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));
524:
525: if (aoc == -1)
526: {
527: floatFrame.pack();
528: floatFrame.setSize(dims.width + insets.left + insets.right,
529: dims.height + insets.top + insets.bottom);
530: floatFrame.show();
531: }
532:
533: newParent.invalidate();
534: newParent.validate();
535: newParent.repaint();
536: }
537:
538:
543: public Color getDockingColor()
544: {
545: return dockingColor;
546: }
547:
548:
554: public Color getFloatingColor()
555: {
556: return floatingColor;
557: }
558:
559:
566: public Dimension getMaximumSize(JComponent c)
567: {
568: return getPreferredSize(c);
569: }
570:
571:
578: public Dimension getMinimumSize(JComponent c)
579: {
580: return getPreferredSize(c);
581: }
582:
583:
586: protected void installComponents()
587: {
588: floatFrame = (Window) createFloatingWindow(toolBar);
589:
590: dragWindow = createDragWindow(toolBar);
591:
592: nonRolloverBorder = createNonRolloverBorder();
593: rolloverBorder = createRolloverBorder();
594:
595: borders = new Hashtable();
596: setRolloverBorders(toolBar.isRollover());
597:
598: fillHashtable();
599: }
600:
601:
604: protected void installDefaults()
605: {
606: LookAndFeel.installBorder(toolBar, "ToolBar.border");
607: LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
608: "ToolBar.foreground", "ToolBar.font");
609:
610: dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
611: dockingColor = UIManager.getColor("ToolBar.dockingBackground");
612:
613: floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
614: floatingColor = UIManager.getColor("ToolBar.floatingBackground");
615: }
616:
617:
621: protected void installKeyboardActions()
622: throws NotImplementedException
623: {
624:
625: }
626:
627:
630: protected void installListeners()
631: {
632: dockingListener = createDockingListener();
633: toolBar.addMouseListener(dockingListener);
634: toolBar.addMouseMotionListener(dockingListener);
635:
636: propertyListener = createPropertyListener();
637: toolBar.addPropertyChangeListener(propertyListener);
638:
639: toolBarContListener = createToolBarContListener();
640: toolBar.addContainerListener(toolBarContListener);
641:
642: windowListener = createFrameListener();
643: floatFrame.addWindowListener(windowListener);
644:
645: toolBarFocusListener = createToolBarFocusListener();
646: toolBar.addFocusListener(toolBarFocusListener);
647: }
648:
649:
656: protected void installNonRolloverBorders(JComponent c)
657: {
658: Component[] components = toolBar.getComponents();
659:
660: for (int i = 0; i < components.length; i++)
661: setBorderToNonRollover(components[i]);
662: }
663:
664:
671: protected void installNormalBorders(JComponent c)
672: {
673: Component[] components = toolBar.getComponents();
674:
675: for (int i = 0; i < components.length; i++)
676: setBorderToNormal(components[i]);
677: }
678:
679:
686: protected void installRolloverBorders(JComponent c)
687: {
688: Component[] components = toolBar.getComponents();
689:
690: for (int i = 0; i < components.length; i++)
691: setBorderToRollover(components[i]);
692: }
693:
694:
698: private void fillHashtable()
699: {
700: Component[] c = toolBar.getComponents();
701:
702: for (int i = 0; i < c.length; i++)
703: {
704: if (c[i] instanceof JButton)
705: {
706:
707: JButton b = (JButton) c[i];
708:
709: if (b.getBorder() != null)
710: borders.put(b, b.getBorder());
711: }
712: }
713: }
714:
715:
720: public void installUI(JComponent c)
721: {
722: super.installUI(c);
723:
724: if (c instanceof JToolBar)
725: {
726: toolBar = (JToolBar) c;
727: installDefaults();
728: installComponents();
729: installListeners();
730: installKeyboardActions();
731: }
732: }
733:
734:
739: public boolean isFloating()
740: {
741: return floatFrame.isVisible();
742: }
743:
744:
749: public boolean isRolloverBorders()
750: {
751: return toolBar.isRollover();
752: }
753:
754:
760: protected void navigateFocusedComp(int direction)
761: throws NotImplementedException
762: {
763:
764: }
765:
766:
772: protected void setBorderToNonRollover(Component c)
773: {
774: if (c instanceof AbstractButton)
775: {
776: AbstractButton b = (AbstractButton) c;
777: b.setRolloverEnabled(false);
778:
779:
780: borders.put(b, b.getBorder());
781:
782: b.setBorder(nonRolloverBorder);
783: }
784: }
785:
786:
791: protected void setBorderToNormal(Component c)
792: {
793: if (c instanceof AbstractButton)
794: {
795: AbstractButton b = (AbstractButton) c;
796: b.setRolloverEnabled(true);
797: b.setBorder((Border) borders.remove(b));
798: }
799: }
800:
801:
806: protected void setBorderToRollover(Component c)
807: {
808: if (c instanceof AbstractButton)
809: {
810: AbstractButton b = (AbstractButton) c;
811: b.setRolloverEnabled(false);
812:
813:
814: borders.put(b, b.getBorder());
815:
816: b.setBorder(rolloverBorder);
817: }
818: }
819:
820:
825: public void setDockingColor(Color c)
826: {
827: dockingColor = c;
828: }
829:
830:
836: public void setFloating(boolean b, Point p)
837: {
838:
839:
840: floatFrame.setVisible(b);
841: }
842:
843:
849: public void setFloatingColor(Color c)
850: {
851: floatingColor = c;
852: }
853:
854:
860: public void setFloatingLocation(int x, int y)
861: {
862:
863:
864: floatFrame.setLocation(x, y);
865: floatFrame.invalidate();
866: floatFrame.validate();
867: floatFrame.repaint();
868: }
869:
870:
876: public void setOrientation(int orientation)
877: {
878: toolBar.setOrientation(orientation);
879: }
880:
881:
888: public void setRolloverBorders(boolean rollover)
889: {
890: if (rollover)
891: installRolloverBorders(toolBar);
892: else
893: installNonRolloverBorders(toolBar);
894: }
895:
896:
899: protected void uninstallComponents()
900: {
901: installNormalBorders(toolBar);
902: borders = null;
903: cachedBounds = null;
904:
905: floatFrame = null;
906: dragWindow = null;
907: }
908:
909:
912: protected void uninstallDefaults()
913: {
914: toolBar.setBackground(null);
915: toolBar.setForeground(null);
916: toolBar.setFont(null);
917:
918: dockingBorderColor = null;
919: dockingColor = null;
920: floatingBorderColor = null;
921: floatingColor = null;
922: }
923:
924:
927: protected void uninstallKeyboardActions()
928: throws NotImplementedException
929: {
930:
931: }
932:
933:
936: protected void uninstallListeners()
937: {
938: toolBar.removeFocusListener(toolBarFocusListener);
939: toolBarFocusListener = null;
940:
941: floatFrame.removeWindowListener(windowListener);
942: windowListener = null;
943:
944: toolBar.removeContainerListener(toolBarContListener);
945: toolBarContListener = null;
946:
947: toolBar.removeMouseMotionListener(dockingListener);
948: toolBar.removeMouseListener(dockingListener);
949: dockingListener = null;
950: }
951:
952:
957: public void uninstallUI(JComponent c)
958: {
959: uninstallKeyboardActions();
960: uninstallListeners();
961: uninstallComponents();
962: uninstallDefaults();
963: toolBar = null;
964: }
965:
966:
970: public class DockingListener implements MouseInputListener
971: {
972:
973: protected boolean isDragging;
974:
975:
979: protected Point origin;
980:
981:
982: protected JToolBar toolBar;
983:
984:
989: public DockingListener(JToolBar t)
990: {
991: toolBar = t;
992: }
993:
994:
999: public void mouseClicked(MouseEvent e)
1000: {
1001:
1002: }
1003:
1004:
1010: public void mouseDragged(MouseEvent e)
1011: {
1012: if (isDragging)
1013: dragTo(e.getPoint(), origin);
1014: }
1015:
1016:
1021: public void mouseEntered(MouseEvent e)
1022: {
1023:
1024: }
1025:
1026:
1031: public void mouseExited(MouseEvent e)
1032: {
1033:
1034: }
1035:
1036:
1041: public void mouseMoved(MouseEvent e)
1042: {
1043:
1044: }
1045:
1046:
1053: public void mousePressed(MouseEvent e)
1054: {
1055: if (! toolBar.isFloatable())
1056: return;
1057:
1058: Point ssd = e.getPoint();
1059: Insets insets = toolBar.getInsets();
1060:
1061:
1062: if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1063: {
1064: if (e.getX() > insets.left)
1065: return;
1066: }
1067: else
1068: {
1069: if (e.getY() > insets.top)
1070: return;
1071: }
1072:
1073: origin = new Point(0, 0);
1074: if (toolBar.isShowing())
1075: SwingUtilities.convertPointToScreen(ssd, toolBar);
1076:
1077: if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1078:
1079: origParent = toolBar.getParent();
1080:
1081: if (toolBar.isShowing())
1082: SwingUtilities.convertPointToScreen(origin, toolBar);
1083:
1084: isDragging = true;
1085:
1086: if (dragWindow != null)
1087: dragWindow.setOffset(new Point(cachedBounds.width / 2,
1088: cachedBounds.height / 2));
1089:
1090: dragTo(e.getPoint(), origin);
1091: }
1092:
1093:
1098: public void mouseReleased(MouseEvent e)
1099: {
1100: if (! isDragging || ! toolBar.isFloatable())
1101: return;
1102:
1103: isDragging = false;
1104: floatAt(e.getPoint(), origin);
1105: dragWindow.hide();
1106: }
1107: }
1108:
1109:
1113: protected class DragWindow extends Window
1114: {
1115:
1119: private Color borderColor;
1120:
1121:
1122: private Point offset;
1123:
1124:
1128: DragWindow()
1129: {
1130: super(owner);
1131: }
1132:
1133:
1138: public Color getBorderColor()
1139: {
1140: if (borderColor == null)
1141: return Color.BLACK;
1142:
1143: return borderColor;
1144: }
1145:
1146:
1151: public Insets getInsets()
1152: {
1153:
1154: return new Insets(0, 0, 0, 0);
1155: }
1156:
1157:
1163: public Point getOffset()
1164: {
1165: return offset;
1166: }
1167:
1168:
1173: public void paint(Graphics g)
1174: {
1175:
1176: Color saved = g.getColor();
1177: Rectangle b = getBounds();
1178:
1179: g.setColor(getBorderColor());
1180: g.drawRect(0, 0, b.width - 1, b.height - 1);
1181:
1182: g.setColor(saved);
1183: }
1184:
1185:
1190: public void setBorderColor(Color c)
1191: {
1192: borderColor = c;
1193: }
1194:
1195:
1200: public void setOffset(Point p)
1201: {
1202: offset = p;
1203: }
1204:
1205:
1210: public void setOrientation(int o)
1211: {
1212:
1213: }
1214: }
1215:
1216:
1220: protected class FrameListener extends WindowAdapter
1221: {
1222:
1227: public void windowClosing(WindowEvent e)
1228: {
1229: Container parent = toolBar.getParent();
1230: parent.remove(toolBar);
1231:
1232: if (origParent != null)
1233: {
1234: origParent.add(toolBar,
1235: (constraintBeforeFloating != null)
1236: ? constraintBeforeFloating : BorderLayout.NORTH);
1237: toolBar.setOrientation(lastGoodOrientation);
1238: }
1239:
1240: origParent.invalidate();
1241: origParent.validate();
1242: origParent.repaint();
1243: }
1244: }
1245:
1246:
1249: protected class PropertyListener implements PropertyChangeListener
1250: {
1251:
1256: public void propertyChange(PropertyChangeEvent e)
1257: {
1258:
1259: if (e.getPropertyName().equals("rollover") && toolBar != null)
1260: setRolloverBorders(toolBar.isRollover());
1261: }
1262: }
1263:
1264:
1268: protected class ToolBarContListener implements ContainerListener
1269: {
1270:
1276: public void componentAdded(ContainerEvent e)
1277: {
1278: if (e.getChild() instanceof JButton)
1279: {
1280: JButton b = (JButton) e.getChild();
1281:
1282: if (b.getBorder() != null)
1283: borders.put(b, b.getBorder());
1284: }
1285:
1286: if (isRolloverBorders())
1287: setBorderToRollover(e.getChild());
1288: else
1289: setBorderToNonRollover(e.getChild());
1290:
1291: cachedBounds = toolBar.getPreferredSize();
1292: cachedOrientation = toolBar.getOrientation();
1293: }
1294:
1295:
1301: public void componentRemoved(ContainerEvent e)
1302: {
1303: setBorderToNormal(e.getChild());
1304: cachedBounds = toolBar.getPreferredSize();
1305: cachedOrientation = toolBar.getOrientation();
1306: }
1307: }
1308:
1309:
1313: private class ToolBarDialog extends JDialog implements UIResource
1314: {
1315:
1318: public ToolBarDialog()
1319: {
1320: super();
1321: setName((toolBar.getName() != null) ? toolBar.getName() : "");
1322: }
1323: }
1324:
1325:
1328: protected class ToolBarFocusListener implements FocusListener
1329: {
1330:
1333: protected ToolBarFocusListener()
1334: {
1335:
1336: }
1337:
1338:
1343: public void focusGained(FocusEvent e)
1344: {
1345:
1346: }
1347:
1348:
1353: public void focusLost(FocusEvent e)
1354: {
1355:
1356: }
1357: }
1358:
1359:
1362: private static class ToolBarBorder implements Border
1363: {
1364:
1365: private static final int offset = 10;
1366:
1367:
1368: private static final int regular = 2;
1369:
1370:
1377: public Insets getBorderInsets(Component c)
1378: {
1379: if (c instanceof JToolBar)
1380: {
1381: JToolBar tb = (JToolBar) c;
1382: int orientation = tb.getOrientation();
1383:
1384: if (! tb.isFloatable())
1385: return new Insets(regular, regular, regular, regular);
1386: else if (orientation == SwingConstants.HORIZONTAL)
1387: return new Insets(regular, offset, regular, regular);
1388: else
1389: return new Insets(offset, regular, regular, regular);
1390: }
1391:
1392: return new Insets(0, 0, 0, 0);
1393: }
1394:
1395:
1400: public boolean isBorderOpaque()
1401: {
1402: return false;
1403: }
1404:
1405:
1416: private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1417: Color c)
1418: {
1419: Color saved = g.getColor();
1420: g.setColor(c);
1421:
1422: int hgap = 2 * size;
1423: int vgap = 4 * size;
1424: int count = 0;
1425:
1426: for (int i = x; i < (w + x); i += hgap)
1427: for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1428: j += vgap)
1429: g.fillRect(i, j, size, size);
1430:
1431: g.setColor(saved);
1432: }
1433:
1434:
1444: public void paintBorder(Component c, Graphics g, int x, int y, int width,
1445: int height)
1446: {
1447: if (c instanceof JToolBar)
1448: {
1449: JToolBar tb = (JToolBar) c;
1450:
1451: int orientation = tb.getOrientation();
1452:
1453: if (orientation == SwingConstants.HORIZONTAL)
1454: {
1455: paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1456: paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1457: }
1458: else
1459: {
1460: paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1461: paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1462: }
1463: }
1464: }
1465: }
1466: }