1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
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:
63: import ;
64: import ;
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:
80:
83: public class BasicToolBarUI extends ToolBarUI implements SwingConstants
84: {
85:
87: static JFrame owner = new JFrame();
88:
89:
90: private static Border nonRolloverBorder;
91:
92:
93: private static Border rolloverBorder;
94:
95:
96: protected String constraintBeforeFloating;
97:
98:
100: int lastGoodOrientation;
101:
102:
103: protected Color dockingBorderColor;
104:
105:
106: protected Color dockingColor;
107:
108:
109: protected MouseInputListener dockingListener;
110:
111:
112: protected BasicToolBarUI.DragWindow dragWindow;
113:
114:
115: protected Color floatingBorderColor;
116:
117:
118: protected Color floatingColor;
119:
120:
121: protected int focusedCompIndex;
122:
123:
124: protected PropertyChangeListener propertyListener;
125:
126:
127: protected JToolBar toolBar;
128:
129:
130: protected ContainerListener toolBarContListener;
131:
132:
133: protected FocusListener toolBarFocusListener;
134:
135:
139: private transient Window floatFrame;
140:
141:
143: transient Container origParent;
144:
145:
147: transient Hashtable borders;
148:
149:
150: private transient WindowListener windowListener;
151:
152:
154: transient Dimension cachedBounds;
155:
156:
158: transient int cachedOrientation;
159:
160:
163: public BasicToolBarUI()
164: {
165:
166: }
167:
168:
177: public boolean canDock(Component c, Point p)
178: {
179: return areaOfClick(c, p) != -1;
180: }
181:
182:
192: private int areaOfClick(Component c, Point p)
193: {
194:
195: Rectangle pBounds = c.getBounds();
196:
197:
198: Dimension d = toolBar.getSize();
199: int limit = Math.min(d.width, d.height);
200:
201:
202: if (! pBounds.contains(p))
203: return -1;
204:
205: if (p.y < limit)
206: return SwingConstants.NORTH;
207:
208: if (p.y > (pBounds.height - limit))
209: return SwingConstants.SOUTH;
210:
211: if (p.x < limit)
212: return SwingConstants.WEST;
213:
214: if (p.x > (pBounds.width - limit))
215: return SwingConstants.EAST;
216:
217: return -1;
218: }
219:
220:
225: protected MouseInputListener createDockingListener()
226: {
227: return new DockingListener(toolBar);
228: }
229:
230:
237: protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
238: {
239: return new DragWindow();
240: }
241:
242:
251: protected JFrame createFloatingFrame(JToolBar toolbar)
252: {
253:
254: return null;
255: }
256:
257:
265: protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
266: {
267:
268: return new ToolBarDialog();
269: }
270:
271:
276: protected WindowListener createFrameListener()
277: {
278: return new FrameListener();
279: }
280:
281:
287: protected Border createNonRolloverBorder()
288: {
289: return new EtchedBorderUIResource();
290: }
291:
292:
297: protected PropertyChangeListener createPropertyListener()
298: {
299: return new PropertyListener();
300: }
301:
302:
308: protected Border createRolloverBorder()
309: {
310: return new EtchedBorderUIResource()
311: {
312: public void paintBorder(Component c, Graphics g, int x, int y,
313: int width, int height)
314: {
315: if (c instanceof JButton)
316: {
317: if (((JButton) c).getModel().isRollover())
318: super.paintBorder(c, g, x, y, width, height);
319: }
320: }
321: };
322: }
323:
324:
329: protected ContainerListener createToolBarContListener()
330: {
331: return new ToolBarContListener();
332: }
333:
334:
339: protected FocusListener createToolBarFocusListener()
340: {
341: return new ToolBarFocusListener();
342: }
343:
344:
351: public static ComponentUI createUI(JComponent c)
352: {
353: return new BasicToolBarUI();
354: }
355:
356:
363: protected void dragTo(Point position, Point origin)
364: {
365: int loc = areaOfClick(origParent,
366: SwingUtilities.convertPoint(toolBar, position,
367: origParent));
368:
369: if (loc != -1)
370: {
371: dragWindow.setBorderColor(dockingBorderColor);
372: dragWindow.setBackground(dockingColor);
373: }
374: else
375: {
376: dragWindow.setBorderColor(floatingBorderColor);
377: dragWindow.setBackground(floatingColor);
378: }
379:
380: int w = 0;
381: int h = 0;
382:
383: boolean tmp = ((loc == SwingConstants.NORTH)
384: || (loc == SwingConstants.SOUTH) || (loc == -1));
385:
386: if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
387: || ((cachedOrientation == VERTICAL) && ! tmp))
388: {
389: w = cachedBounds.width;
390: h = cachedBounds.height;
391: }
392: else
393: {
394: w = cachedBounds.height;
395: h = cachedBounds.width;
396: }
397:
398: Point p = dragWindow.getOffset();
399: Insets insets = toolBar.getInsets();
400:
401: dragWindow.setBounds((origin.x + position.x) - p.x
402: - ((insets.left + insets.right) / 2),
403: (origin.y + position.y) - p.y
404: - ((insets.top + insets.bottom) / 2), w, h);
405:
406: if (! dragWindow.isVisible())
407: dragWindow.show();
408: }
409:
410:
420: protected void floatAt(Point position, Point origin)
421: {
422: Point p = new Point(position);
423: int aoc = areaOfClick(origParent,
424: SwingUtilities.convertPoint(toolBar, p, origParent));
425:
426: Container oldParent = toolBar.getParent();
427:
428: oldParent.remove(toolBar);
429: oldParent.doLayout();
430: oldParent.repaint();
431:
432: Container newParent;
433:
434: if (aoc == -1)
435: newParent = ((RootPaneContainer) floatFrame).getContentPane();
436: else
437: {
438: floatFrame.hide();
439: newParent = origParent;
440: }
441:
442: String constraint;
443: switch (aoc)
444: {
445: case SwingConstants.EAST:
446: constraint = BorderLayout.EAST;
447: break;
448: case SwingConstants.NORTH:
449: constraint = BorderLayout.NORTH;
450: break;
451: case SwingConstants.SOUTH:
452: constraint = BorderLayout.SOUTH;
453: break;
454: case SwingConstants.WEST:
455: constraint = BorderLayout.WEST;
456: break;
457: default:
458: constraint = BorderLayout.CENTER;
459: break;
460: }
461:
462: int newOrientation = SwingConstants.HORIZONTAL;
463: if ((aoc != -1)
464: && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
465: newOrientation = SwingConstants.VERTICAL;
466:
467: if (aoc != -1)
468: {
469: constraintBeforeFloating = constraint;
470: lastGoodOrientation = newOrientation;
471: }
472:
473: newParent.add(toolBar, constraint);
474:
475: setFloating(aoc == -1, null);
476: toolBar.setOrientation(newOrientation);
477:
478: Insets insets = floatFrame.getInsets();
479: Dimension dims = toolBar.getPreferredSize();
480: p = dragWindow.getOffset();
481: setFloatingLocation((position.x + origin.x) - p.x
482: - ((insets.left + insets.right) / 2),
483: (position.y + origin.y) - p.y
484: - ((insets.top + insets.bottom) / 2));
485:
486: if (aoc == -1)
487: {
488: floatFrame.pack();
489: floatFrame.setSize(dims.width + insets.left + insets.right,
490: dims.height + insets.top + insets.bottom);
491: floatFrame.show();
492: }
493:
494: newParent.invalidate();
495: newParent.validate();
496: newParent.repaint();
497: }
498:
499:
504: public Color getDockingColor()
505: {
506: return dockingColor;
507: }
508:
509:
515: public Color getFloatingColor()
516: {
517: return floatingColor;
518: }
519:
520:
527: public Dimension getMaximumSize(JComponent c)
528: {
529: return getPreferredSize(c);
530: }
531:
532:
539: public Dimension getMinimumSize(JComponent c)
540: {
541: return getPreferredSize(c);
542: }
543:
544:
552: public Dimension getPreferredSize(JComponent c)
553: {
554: return toolBar.getLayout().preferredLayoutSize(c);
555: }
556:
557:
560: protected void installComponents()
561: {
562: floatFrame = (Window) createFloatingWindow(toolBar);
563:
564: dragWindow = createDragWindow(toolBar);
565:
566: cachedBounds = toolBar.getPreferredSize();
567: cachedOrientation = toolBar.getOrientation();
568:
569: nonRolloverBorder = createNonRolloverBorder();
570: rolloverBorder = createRolloverBorder();
571:
572: borders = new Hashtable();
573:
574: fillHashtable();
575: }
576:
577:
580: protected void installDefaults()
581: {
582: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
583:
584: toolBar.setBorder(new ToolBarBorder());
585: toolBar.setBackground(defaults.getColor("ToolBar.background"));
586: toolBar.setForeground(defaults.getColor("ToolBar.foreground"));
587: toolBar.setFont(defaults.getFont("ToolBar.font"));
588:
589: dockingBorderColor = defaults.getColor("ToolBar.dockingForeground");
590: dockingColor = defaults.getColor("ToolBar.dockingBackground");
591:
592: floatingBorderColor = defaults.getColor("ToolBar.floatingForeground");
593: floatingColor = defaults.getColor("ToolBar.floatingBackground");
594: }
595:
596:
600: protected void installKeyboardActions()
601: {
602:
603: }
604:
605:
610: protected void installListeners(JToolBar toolbar)
611: {
612: dockingListener = createDockingListener();
613: toolBar.addMouseListener(dockingListener);
614: toolBar.addMouseMotionListener(dockingListener);
615:
616: propertyListener = createPropertyListener();
617: toolBar.addPropertyChangeListener(propertyListener);
618:
619: toolBarContListener = createToolBarContListener();
620: toolBar.addContainerListener(toolBarContListener);
621:
622: windowListener = createFrameListener();
623: floatFrame.addWindowListener(windowListener);
624:
625: toolBarFocusListener = createToolBarFocusListener();
626: toolBar.addFocusListener(toolBarFocusListener);
627: }
628:
629:
636: protected void installNonRolloverBorders(JComponent c)
637: {
638: Component[] components = toolBar.getComponents();
639:
640: for (int i = 0; i < components.length; i++)
641: setBorderToNonRollover(components[i]);
642: }
643:
644:
651: protected void installNormalBorders(JComponent c)
652: {
653: Component[] components = toolBar.getComponents();
654:
655: for (int i = 0; i < components.length; i++)
656: setBorderToNormal(components[i]);
657: }
658:
659:
666: protected void installRolloverBorders(JComponent c)
667: {
668: Component[] components = toolBar.getComponents();
669:
670: for (int i = 0; i < components.length; i++)
671: setBorderToRollover(components[i]);
672: }
673:
674:
678: private void fillHashtable()
679: {
680: Component[] c = toolBar.getComponents();
681:
682: for (int i = 0; i < c.length; i++)
683: {
684: if (c[i] instanceof JButton)
685: {
686:
687: JButton b = (JButton) c[i];
688:
689: if (b.getBorder() != null)
690: borders.put(b, b.getBorder());
691: }
692: }
693: }
694:
695:
700: public void installUI(JComponent c)
701: {
702: super.installUI(c);
703:
704: if (c instanceof JToolBar)
705: {
706: toolBar = (JToolBar) c;
707: toolBar.setOpaque(true);
708: installDefaults();
709: installComponents();
710: installListeners(toolBar);
711: installKeyboardActions();
712: }
713: }
714:
715:
720: public boolean isFloating()
721: {
722: return floatFrame.isVisible();
723: }
724:
725:
730: public boolean isRolloverBorders()
731: {
732: return toolBar.isRollover();
733: }
734:
735:
741: protected void navigateFocusedComp(int direction)
742: {
743:
744: }
745:
746:
752: protected void setBorderToNonRollover(Component c)
753: {
754: if (c instanceof JButton)
755: {
756: JButton b = (JButton) c;
757: b.setRolloverEnabled(false);
758: b.setBorder(nonRolloverBorder);
759: }
760: }
761:
762:
767: protected void setBorderToNormal(Component c)
768: {
769: if (c instanceof JButton)
770: {
771: JButton b = (JButton) c;
772: Border border = (Border) borders.get(b);
773: b.setBorder(border);
774: }
775: }
776:
777:
782: protected void setBorderToRollover(Component c)
783: {
784: if (c instanceof JButton)
785: {
786: JButton b = (JButton) c;
787: b.setRolloverEnabled(true);
788: b.setBorder(rolloverBorder);
789: }
790: }
791:
792:
797: public void setDockingColor(Color c)
798: {
799: dockingColor = c;
800: }
801:
802:
808: public void setFloating(boolean b, Point p)
809: {
810:
811:
812: floatFrame.setVisible(b);
813: }
814:
815:
821: public void setFloatingColor(Color c)
822: {
823: floatingColor = c;
824: }
825:
826:
832: public void setFloatingLocation(int x, int y)
833: {
834:
835:
836: floatFrame.setLocation(x, y);
837: floatFrame.invalidate();
838: floatFrame.validate();
839: floatFrame.repaint();
840: }
841:
842:
848: public void setOrientation(int orientation)
849: {
850: toolBar.setOrientation(orientation);
851: }
852:
853:
860: public void setRolloverBorders(boolean rollover)
861: {
862: if (rollover)
863: installRolloverBorders(toolBar);
864: else
865: installNonRolloverBorders(toolBar);
866: }
867:
868:
871: protected void uninstallComponents()
872: {
873: installNormalBorders(toolBar);
874: borders = null;
875: rolloverBorder = null;
876: nonRolloverBorder = null;
877: cachedBounds = null;
878:
879: floatFrame = null;
880: dragWindow = null;
881: }
882:
883:
886: protected void uninstallDefaults()
887: {
888: toolBar.setBackground(null);
889: toolBar.setForeground(null);
890: toolBar.setFont(null);
891:
892: dockingBorderColor = null;
893: dockingColor = null;
894: floatingBorderColor = null;
895: floatingColor = null;
896: }
897:
898:
901: protected void uninstallKeyboardActions()
902: {
903:
904: }
905:
906:
909: protected void uninstallListeners()
910: {
911: toolBar.removeFocusListener(toolBarFocusListener);
912: toolBarFocusListener = null;
913:
914: floatFrame.removeWindowListener(windowListener);
915: windowListener = null;
916:
917: toolBar.removeContainerListener(toolBarContListener);
918: toolBarContListener = null;
919:
920: toolBar.removeMouseMotionListener(dockingListener);
921: toolBar.removeMouseListener(dockingListener);
922: dockingListener = null;
923: }
924:
925:
930: public void uninstallUI(JComponent c)
931: {
932: uninstallKeyboardActions();
933: uninstallListeners();
934: uninstallComponents();
935: uninstallDefaults();
936: toolBar = null;
937: }
938:
939:
943: public class DockingListener implements MouseInputListener
944: {
945:
946: protected boolean isDragging;
947:
948:
952: protected Point origin;
953:
954:
955: protected JToolBar toolBar;
956:
957:
962: public DockingListener(JToolBar t)
963: {
964: toolBar = t;
965: }
966:
967:
972: public void mouseClicked(MouseEvent e)
973: {
974:
975: }
976:
977:
983: public void mouseDragged(MouseEvent e)
984: {
985: if (isDragging)
986: dragTo(e.getPoint(), origin);
987: }
988:
989:
994: public void mouseEntered(MouseEvent e)
995: {
996:
997: }
998:
999:
1004: public void mouseExited(MouseEvent e)
1005: {
1006:
1007: }
1008:
1009:
1014: public void mouseMoved(MouseEvent e)
1015: {
1016: }
1017:
1018:
1025: public void mousePressed(MouseEvent e)
1026: {
1027: if (! toolBar.isFloatable())
1028: return;
1029:
1030: Point ssd = e.getPoint();
1031: Insets insets = toolBar.getInsets();
1032:
1033:
1034: if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1035: {
1036: if (e.getX() > insets.left)
1037: return;
1038: }
1039: else
1040: {
1041: if (e.getY() > insets.top)
1042: return;
1043: }
1044:
1045: origin = new Point(0, 0);
1046: SwingUtilities.convertPointToScreen(ssd, toolBar);
1047:
1048: if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1049:
1050: origParent = toolBar.getParent();
1051:
1052: SwingUtilities.convertPointToScreen(origin, toolBar);
1053:
1054: isDragging = true;
1055:
1056: if (dragWindow != null)
1057: dragWindow.setOffset(new Point(e.getX(), e.getY()));
1058:
1059: dragTo(e.getPoint(), origin);
1060: }
1061:
1062:
1067: public void mouseReleased(MouseEvent e)
1068: {
1069: if (! isDragging || ! toolBar.isFloatable())
1070: return;
1071:
1072: isDragging = false;
1073: floatAt(e.getPoint(), origin);
1074: dragWindow.hide();
1075: }
1076: }
1077:
1078:
1082: protected class DragWindow extends Window
1083: {
1084:
1088: private Color borderColor;
1089:
1090:
1091: private Point offset;
1092:
1093:
1097: DragWindow()
1098: {
1099: super(owner);
1100: }
1101:
1102:
1107: public Color getBorderColor()
1108: {
1109: if (borderColor == null)
1110: return Color.BLACK;
1111:
1112: return borderColor;
1113: }
1114:
1115:
1120: public Insets getInsets()
1121: {
1122:
1123: return new Insets(0, 0, 0, 0);
1124: }
1125:
1126:
1132: public Point getOffset()
1133: {
1134: return offset;
1135: }
1136:
1137:
1142: public void paint(Graphics g)
1143: {
1144:
1145: Color saved = g.getColor();
1146: Rectangle b = getBounds();
1147:
1148: g.setColor(getBorderColor());
1149: g.drawRect(0, 0, b.width - 1, b.height - 1);
1150:
1151: g.setColor(saved);
1152: }
1153:
1154:
1159: public void setBorderColor(Color c)
1160: {
1161: borderColor = c;
1162: }
1163:
1164:
1169: public void setOffset(Point p)
1170: {
1171: offset = p;
1172: }
1173:
1174:
1179: public void setOrientation(int o)
1180: {
1181:
1182: }
1183: }
1184:
1185:
1189: protected class FrameListener extends WindowAdapter
1190: {
1191:
1196: public void windowClosing(WindowEvent e)
1197: {
1198: Container parent = toolBar.getParent();
1199: parent.remove(toolBar);
1200:
1201: if (origParent != null)
1202: {
1203: origParent.add(toolBar,
1204: (constraintBeforeFloating != null)
1205: ? constraintBeforeFloating : BorderLayout.NORTH);
1206: toolBar.setOrientation(lastGoodOrientation);
1207: }
1208:
1209: origParent.invalidate();
1210: origParent.validate();
1211: origParent.repaint();
1212: }
1213: }
1214:
1215:
1218: protected class PropertyListener implements PropertyChangeListener
1219: {
1220:
1225: public void propertyChange(PropertyChangeEvent e)
1226: {
1227:
1228: if (e.getPropertyName().equals("rollover"))
1229: setRolloverBorders(toolBar.isRollover());
1230: }
1231: }
1232:
1233:
1237: protected class ToolBarContListener implements ContainerListener
1238: {
1239:
1245: public void componentAdded(ContainerEvent e)
1246: {
1247: if (e.getChild() instanceof JButton)
1248: {
1249: JButton b = (JButton) e.getChild();
1250:
1251: if (b.getBorder() != null)
1252: borders.put(b, b.getBorder());
1253: }
1254:
1255: if (isRolloverBorders())
1256: setBorderToRollover(e.getChild());
1257: else
1258: setBorderToNonRollover(e.getChild());
1259:
1260: cachedBounds = toolBar.getPreferredSize();
1261: cachedOrientation = toolBar.getOrientation();
1262: }
1263:
1264:
1270: public void componentRemoved(ContainerEvent e)
1271: {
1272: setBorderToNormal(e.getChild());
1273: cachedBounds = toolBar.getPreferredSize();
1274: cachedOrientation = toolBar.getOrientation();
1275: }
1276: }
1277:
1278:
1282: private class ToolBarDialog extends JDialog implements UIResource
1283: {
1284:
1287: public ToolBarDialog()
1288: {
1289: super();
1290: setName((toolBar.getName() != null) ? toolBar.getName() : "");
1291: }
1292: }
1293:
1294:
1297: protected class ToolBarFocusListener implements FocusListener
1298: {
1299:
1302: protected ToolBarFocusListener()
1303: {
1304:
1305: }
1306:
1307:
1312: public void focusGained(FocusEvent e)
1313: {
1314:
1315: }
1316:
1317:
1322: public void focusLost(FocusEvent e)
1323: {
1324:
1325: }
1326: }
1327:
1328:
1331: private static class ToolBarBorder implements Border
1332: {
1333:
1334: private static final int offset = 10;
1335:
1336:
1337: private static final int regular = 2;
1338:
1339:
1346: public Insets getBorderInsets(Component c)
1347: {
1348: if (c instanceof JToolBar)
1349: {
1350: JToolBar tb = (JToolBar) c;
1351: int orientation = tb.getOrientation();
1352:
1353: if (! tb.isFloatable())
1354: return new Insets(regular, regular, regular, regular);
1355: else if (orientation == SwingConstants.HORIZONTAL)
1356: return new Insets(regular, offset, regular, regular);
1357: else
1358: return new Insets(offset, regular, regular, regular);
1359: }
1360:
1361: return new Insets(0, 0, 0, 0);
1362: }
1363:
1364:
1369: public boolean isBorderOpaque()
1370: {
1371: return false;
1372: }
1373:
1374:
1385: private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1386: Color c)
1387: {
1388: Color saved = g.getColor();
1389: g.setColor(c);
1390:
1391: int hgap = 2 * size;
1392: int vgap = 4 * size;
1393: int count = 0;
1394:
1395: for (int i = x; i < (w + x); i += hgap)
1396: for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1397: j += vgap)
1398: g.fillRect(i, j, size, size);
1399:
1400: g.setColor(saved);
1401: }
1402:
1403:
1413: public void paintBorder(Component c, Graphics g, int x, int y, int width,
1414: int height)
1415: {
1416: if (c instanceof JToolBar)
1417: {
1418: JToolBar tb = (JToolBar) c;
1419:
1420: int orientation = tb.getOrientation();
1421:
1422: if (orientation == SwingConstants.HORIZONTAL)
1423: {
1424: paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1425: paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1426: }
1427: else
1428: {
1429: paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1430: paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1431: }
1432: }
1433: }
1434: }
1435: }