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:
547: protected void installComponents()
548: {
549: floatFrame = (Window) createFloatingWindow(toolBar);
550:
551: dragWindow = createDragWindow(toolBar);
552:
553: cachedBounds = toolBar.getPreferredSize();
554: cachedOrientation = toolBar.getOrientation();
555:
556: nonRolloverBorder = createNonRolloverBorder();
557: rolloverBorder = createRolloverBorder();
558:
559: borders = new Hashtable();
560:
561: fillHashtable();
562: }
563:
564:
567: protected void installDefaults()
568: {
569: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
570:
571: toolBar.setBorder(new ToolBarBorder());
572: toolBar.setBackground(defaults.getColor("ToolBar.background"));
573: toolBar.setForeground(defaults.getColor("ToolBar.foreground"));
574: toolBar.setFont(defaults.getFont("ToolBar.font"));
575:
576: dockingBorderColor = defaults.getColor("ToolBar.dockingForeground");
577: dockingColor = defaults.getColor("ToolBar.dockingBackground");
578:
579: floatingBorderColor = defaults.getColor("ToolBar.floatingForeground");
580: floatingColor = defaults.getColor("ToolBar.floatingBackground");
581: }
582:
583:
587: protected void installKeyboardActions()
588: {
589:
590: }
591:
592:
597: protected void installListeners(JToolBar toolbar)
598: {
599: dockingListener = createDockingListener();
600: toolBar.addMouseListener(dockingListener);
601: toolBar.addMouseMotionListener(dockingListener);
602:
603: propertyListener = createPropertyListener();
604: toolBar.addPropertyChangeListener(propertyListener);
605:
606: toolBarContListener = createToolBarContListener();
607: toolBar.addContainerListener(toolBarContListener);
608:
609: windowListener = createFrameListener();
610: floatFrame.addWindowListener(windowListener);
611:
612: toolBarFocusListener = createToolBarFocusListener();
613: toolBar.addFocusListener(toolBarFocusListener);
614: }
615:
616:
623: protected void installNonRolloverBorders(JComponent c)
624: {
625: Component[] components = toolBar.getComponents();
626:
627: for (int i = 0; i < components.length; i++)
628: setBorderToNonRollover(components[i]);
629: }
630:
631:
638: protected void installNormalBorders(JComponent c)
639: {
640: Component[] components = toolBar.getComponents();
641:
642: for (int i = 0; i < components.length; i++)
643: setBorderToNormal(components[i]);
644: }
645:
646:
653: protected void installRolloverBorders(JComponent c)
654: {
655: Component[] components = toolBar.getComponents();
656:
657: for (int i = 0; i < components.length; i++)
658: setBorderToRollover(components[i]);
659: }
660:
661:
665: private void fillHashtable()
666: {
667: Component[] c = toolBar.getComponents();
668:
669: for (int i = 0; i < c.length; i++)
670: {
671: if (c[i] instanceof JButton)
672: {
673:
674: JButton b = (JButton) c[i];
675:
676: if (b.getBorder() != null)
677: borders.put(b, b.getBorder());
678: }
679: }
680: }
681:
682:
687: public void installUI(JComponent c)
688: {
689: super.installUI(c);
690:
691: if (c instanceof JToolBar)
692: {
693: toolBar = (JToolBar) c;
694: toolBar.setOpaque(true);
695: installDefaults();
696: installComponents();
697: installListeners(toolBar);
698: installKeyboardActions();
699: }
700: }
701:
702:
707: public boolean isFloating()
708: {
709: return floatFrame.isVisible();
710: }
711:
712:
717: public boolean isRolloverBorders()
718: {
719: return toolBar.isRollover();
720: }
721:
722:
728: protected void navigateFocusedComp(int direction)
729: {
730:
731: }
732:
733:
739: protected void setBorderToNonRollover(Component c)
740: {
741: if (c instanceof JButton)
742: {
743: JButton b = (JButton) c;
744: b.setRolloverEnabled(false);
745: b.setBorder(nonRolloverBorder);
746: }
747: }
748:
749:
754: protected void setBorderToNormal(Component c)
755: {
756: if (c instanceof JButton)
757: {
758: JButton b = (JButton) c;
759: Border border = (Border) borders.get(b);
760: b.setBorder(border);
761: }
762: }
763:
764:
769: protected void setBorderToRollover(Component c)
770: {
771: if (c instanceof JButton)
772: {
773: JButton b = (JButton) c;
774: b.setRolloverEnabled(true);
775: b.setBorder(rolloverBorder);
776: }
777: }
778:
779:
784: public void setDockingColor(Color c)
785: {
786: dockingColor = c;
787: }
788:
789:
795: public void setFloating(boolean b, Point p)
796: {
797:
798:
799: floatFrame.setVisible(b);
800: }
801:
802:
808: public void setFloatingColor(Color c)
809: {
810: floatingColor = c;
811: }
812:
813:
819: public void setFloatingLocation(int x, int y)
820: {
821:
822:
823: floatFrame.setLocation(x, y);
824: floatFrame.invalidate();
825: floatFrame.validate();
826: floatFrame.repaint();
827: }
828:
829:
835: public void setOrientation(int orientation)
836: {
837: toolBar.setOrientation(orientation);
838: }
839:
840:
847: public void setRolloverBorders(boolean rollover)
848: {
849: if (rollover)
850: installRolloverBorders(toolBar);
851: else
852: installNonRolloverBorders(toolBar);
853: }
854:
855:
858: protected void uninstallComponents()
859: {
860: installNormalBorders(toolBar);
861: borders = null;
862: rolloverBorder = null;
863: nonRolloverBorder = null;
864: cachedBounds = null;
865:
866: floatFrame = null;
867: dragWindow = null;
868: }
869:
870:
873: protected void uninstallDefaults()
874: {
875: toolBar.setBackground(null);
876: toolBar.setForeground(null);
877: toolBar.setFont(null);
878:
879: dockingBorderColor = null;
880: dockingColor = null;
881: floatingBorderColor = null;
882: floatingColor = null;
883: }
884:
885:
888: protected void uninstallKeyboardActions()
889: {
890:
891: }
892:
893:
896: protected void uninstallListeners()
897: {
898: toolBar.removeFocusListener(toolBarFocusListener);
899: toolBarFocusListener = null;
900:
901: floatFrame.removeWindowListener(windowListener);
902: windowListener = null;
903:
904: toolBar.removeContainerListener(toolBarContListener);
905: toolBarContListener = null;
906:
907: toolBar.removeMouseMotionListener(dockingListener);
908: toolBar.removeMouseListener(dockingListener);
909: dockingListener = null;
910: }
911:
912:
917: public void uninstallUI(JComponent c)
918: {
919: uninstallKeyboardActions();
920: uninstallListeners();
921: uninstallComponents();
922: uninstallDefaults();
923: toolBar = null;
924: }
925:
926:
930: public class DockingListener implements MouseInputListener
931: {
932:
933: protected boolean isDragging;
934:
935:
939: protected Point origin;
940:
941:
942: protected JToolBar toolBar;
943:
944:
949: public DockingListener(JToolBar t)
950: {
951: toolBar = t;
952: }
953:
954:
959: public void mouseClicked(MouseEvent e)
960: {
961:
962: }
963:
964:
970: public void mouseDragged(MouseEvent e)
971: {
972: if (isDragging)
973: dragTo(e.getPoint(), origin);
974: }
975:
976:
981: public void mouseEntered(MouseEvent e)
982: {
983:
984: }
985:
986:
991: public void mouseExited(MouseEvent e)
992: {
993:
994: }
995:
996:
1001: public void mouseMoved(MouseEvent e)
1002: {
1003: }
1004:
1005:
1012: public void mousePressed(MouseEvent e)
1013: {
1014: if (! toolBar.isFloatable())
1015: return;
1016:
1017: Point ssd = e.getPoint();
1018: Insets insets = toolBar.getInsets();
1019:
1020:
1021: if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1022: {
1023: if (e.getX() > insets.left)
1024: return;
1025: }
1026: else
1027: {
1028: if (e.getY() > insets.top)
1029: return;
1030: }
1031:
1032: origin = new Point(0, 0);
1033: SwingUtilities.convertPointToScreen(ssd, toolBar);
1034:
1035: if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1036:
1037: origParent = toolBar.getParent();
1038:
1039: SwingUtilities.convertPointToScreen(origin, toolBar);
1040:
1041: isDragging = true;
1042:
1043: if (dragWindow != null)
1044: dragWindow.setOffset(new Point(e.getX(), e.getY()));
1045:
1046: dragTo(e.getPoint(), origin);
1047: }
1048:
1049:
1054: public void mouseReleased(MouseEvent e)
1055: {
1056: if (! isDragging || ! toolBar.isFloatable())
1057: return;
1058:
1059: isDragging = false;
1060: floatAt(e.getPoint(), origin);
1061: dragWindow.hide();
1062: }
1063: }
1064:
1065:
1069: protected class DragWindow extends Window
1070: {
1071:
1075: private Color borderColor;
1076:
1077:
1078: private Point offset;
1079:
1080:
1084: DragWindow()
1085: {
1086: super(owner);
1087: }
1088:
1089:
1094: public Color getBorderColor()
1095: {
1096: if (borderColor == null)
1097: return Color.BLACK;
1098:
1099: return borderColor;
1100: }
1101:
1102:
1107: public Insets getInsets()
1108: {
1109:
1110: return new Insets(0, 0, 0, 0);
1111: }
1112:
1113:
1119: public Point getOffset()
1120: {
1121: return offset;
1122: }
1123:
1124:
1129: public void paint(Graphics g)
1130: {
1131:
1132: Color saved = g.getColor();
1133: Rectangle b = getBounds();
1134:
1135: g.setColor(getBorderColor());
1136: g.drawRect(0, 0, b.width - 1, b.height - 1);
1137:
1138: g.setColor(saved);
1139: }
1140:
1141:
1146: public void setBorderColor(Color c)
1147: {
1148: borderColor = c;
1149: }
1150:
1151:
1156: public void setOffset(Point p)
1157: {
1158: offset = p;
1159: }
1160:
1161:
1166: public void setOrientation(int o)
1167: {
1168:
1169: }
1170: }
1171:
1172:
1176: protected class FrameListener extends WindowAdapter
1177: {
1178:
1183: public void windowClosing(WindowEvent e)
1184: {
1185: Container parent = toolBar.getParent();
1186: parent.remove(toolBar);
1187:
1188: if (origParent != null)
1189: {
1190: origParent.add(toolBar,
1191: (constraintBeforeFloating != null)
1192: ? constraintBeforeFloating : BorderLayout.NORTH);
1193: toolBar.setOrientation(lastGoodOrientation);
1194: }
1195:
1196: origParent.invalidate();
1197: origParent.validate();
1198: origParent.repaint();
1199: }
1200: }
1201:
1202:
1205: protected class PropertyListener implements PropertyChangeListener
1206: {
1207:
1212: public void propertyChange(PropertyChangeEvent e)
1213: {
1214:
1215: if (e.getPropertyName().equals("rollover"))
1216: setRolloverBorders(toolBar.isRollover());
1217: }
1218: }
1219:
1220:
1224: protected class ToolBarContListener implements ContainerListener
1225: {
1226:
1232: public void componentAdded(ContainerEvent e)
1233: {
1234: if (e.getChild() instanceof JButton)
1235: {
1236: JButton b = (JButton) e.getChild();
1237:
1238: if (b.getBorder() != null)
1239: borders.put(b, b.getBorder());
1240: }
1241:
1242: if (isRolloverBorders())
1243: setBorderToRollover(e.getChild());
1244: else
1245: setBorderToNonRollover(e.getChild());
1246:
1247: cachedBounds = toolBar.getPreferredSize();
1248: cachedOrientation = toolBar.getOrientation();
1249: }
1250:
1251:
1257: public void componentRemoved(ContainerEvent e)
1258: {
1259: setBorderToNormal(e.getChild());
1260: cachedBounds = toolBar.getPreferredSize();
1261: cachedOrientation = toolBar.getOrientation();
1262: }
1263: }
1264:
1265:
1269: private class ToolBarDialog extends JDialog implements UIResource
1270: {
1271:
1274: public ToolBarDialog()
1275: {
1276: super();
1277: setName((toolBar.getName() != null) ? toolBar.getName() : "");
1278: }
1279: }
1280:
1281:
1284: protected class ToolBarFocusListener implements FocusListener
1285: {
1286:
1289: protected ToolBarFocusListener()
1290: {
1291:
1292: }
1293:
1294:
1299: public void focusGained(FocusEvent e)
1300: {
1301:
1302: }
1303:
1304:
1309: public void focusLost(FocusEvent e)
1310: {
1311:
1312: }
1313: }
1314:
1315:
1318: private static class ToolBarBorder implements Border
1319: {
1320:
1321: private static final int offset = 10;
1322:
1323:
1324: private static final int regular = 2;
1325:
1326:
1333: public Insets getBorderInsets(Component c)
1334: {
1335: if (c instanceof JToolBar)
1336: {
1337: JToolBar tb = (JToolBar) c;
1338: int orientation = tb.getOrientation();
1339:
1340: if (! tb.isFloatable())
1341: return new Insets(regular, regular, regular, regular);
1342: else if (orientation == SwingConstants.HORIZONTAL)
1343: return new Insets(regular, offset, regular, regular);
1344: else
1345: return new Insets(offset, regular, regular, regular);
1346: }
1347:
1348: return new Insets(0, 0, 0, 0);
1349: }
1350:
1351:
1356: public boolean isBorderOpaque()
1357: {
1358: return false;
1359: }
1360:
1361:
1372: private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1373: Color c)
1374: {
1375: Color saved = g.getColor();
1376: g.setColor(c);
1377:
1378: int hgap = 2 * size;
1379: int vgap = 4 * size;
1380: int count = 0;
1381:
1382: for (int i = x; i < (w + x); i += hgap)
1383: for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1384: j += vgap)
1385: g.fillRect(i, j, size, size);
1386:
1387: g.setColor(saved);
1388: }
1389:
1390:
1400: public void paintBorder(Component c, Graphics g, int x, int y, int width,
1401: int height)
1402: {
1403: if (c instanceof JToolBar)
1404: {
1405: JToolBar tb = (JToolBar) c;
1406:
1407: int orientation = tb.getOrientation();
1408:
1409: if (orientation == SwingConstants.HORIZONTAL)
1410: {
1411: paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1412: paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1413: }
1414: else
1415: {
1416: paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1417: paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1418: }
1419: }
1420: }
1421: }
1422: }