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: import ;
80:
81:
84: public class BasicToolBarUI extends ToolBarUI implements SwingConstants
85: {
86:
88: static JFrame owner = new JFrame();
89:
90:
91: private static Border nonRolloverBorder;
92:
93:
94: private static Border rolloverBorder;
95:
96:
97: protected String constraintBeforeFloating;
98:
99:
101: int lastGoodOrientation;
102:
103:
104: protected Color dockingBorderColor;
105:
106:
107: protected Color dockingColor;
108:
109:
110: protected MouseInputListener dockingListener;
111:
112:
113: protected BasicToolBarUI.DragWindow dragWindow;
114:
115:
116: protected Color floatingBorderColor;
117:
118:
119: protected Color floatingColor;
120:
121:
122: protected int focusedCompIndex;
123:
124:
125: protected PropertyChangeListener propertyListener;
126:
127:
128: protected JToolBar toolBar;
129:
130:
131: protected ContainerListener toolBarContListener;
132:
133:
134: protected FocusListener toolBarFocusListener;
135:
136:
139: protected KeyStroke leftKey;
140:
141:
144: protected KeyStroke rightKey;
145:
146:
149: protected KeyStroke upKey;
150:
151:
154: protected KeyStroke downKey;
155:
156:
160: private transient Window floatFrame;
161:
162:
164: transient Container origParent;
165:
166:
168: transient Hashtable borders;
169:
170:
171: private transient WindowListener windowListener;
172:
173:
175: transient Dimension cachedBounds;
176:
177:
179: transient int cachedOrientation;
180:
181:
184: public BasicToolBarUI()
185: {
186:
187: }
188:
189:
198: public boolean canDock(Component c, Point p)
199: {
200: return areaOfClick(c, p) != -1;
201: }
202:
203:
213: private int areaOfClick(Component c, Point p)
214: {
215:
216: Rectangle pBounds = c.getBounds();
217:
218:
219: Dimension d = toolBar.getSize();
220: int limit = Math.min(d.width, d.height);
221:
222:
223: if (! pBounds.contains(p))
224: return -1;
225:
226: if (p.y < limit)
227: return SwingConstants.NORTH;
228:
229: if (p.y > (pBounds.height - limit))
230: return SwingConstants.SOUTH;
231:
232: if (p.x < limit)
233: return SwingConstants.WEST;
234:
235: if (p.x > (pBounds.width - limit))
236: return SwingConstants.EAST;
237:
238: return -1;
239: }
240:
241:
246: protected MouseInputListener createDockingListener()
247: {
248: return new DockingListener(toolBar);
249: }
250:
251:
258: protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
259: {
260: return new DragWindow();
261: }
262:
263:
272: protected JFrame createFloatingFrame(JToolBar toolbar)
273: {
274:
275: return null;
276: }
277:
278:
286: protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
287: {
288:
289: return new ToolBarDialog();
290: }
291:
292:
297: protected WindowListener createFrameListener()
298: {
299: return new FrameListener();
300: }
301:
302:
308: protected Border createNonRolloverBorder()
309: {
310: return new EtchedBorderUIResource();
311: }
312:
313:
318: protected PropertyChangeListener createPropertyListener()
319: {
320: return new PropertyListener();
321: }
322:
323:
329: protected Border createRolloverBorder()
330: {
331: return new EtchedBorderUIResource()
332: {
333: public void paintBorder(Component c, Graphics g, int x, int y,
334: int width, int height)
335: {
336: if (c instanceof JButton)
337: {
338: if (((JButton) c).getModel().isRollover())
339: super.paintBorder(c, g, x, y, width, height);
340: }
341: }
342: };
343: }
344:
345:
350: protected ContainerListener createToolBarContListener()
351: {
352: return new ToolBarContListener();
353: }
354:
355:
360: protected FocusListener createToolBarFocusListener()
361: {
362: return new ToolBarFocusListener();
363: }
364:
365:
372: public static ComponentUI createUI(JComponent c)
373: {
374: return new BasicToolBarUI();
375: }
376:
377:
384: protected void dragTo(Point position, Point origin)
385: {
386: int loc = areaOfClick(origParent,
387: SwingUtilities.convertPoint(toolBar, position,
388: origParent));
389:
390: if (loc != -1)
391: {
392: dragWindow.setBorderColor(dockingBorderColor);
393: dragWindow.setBackground(dockingColor);
394: }
395: else
396: {
397: dragWindow.setBorderColor(floatingBorderColor);
398: dragWindow.setBackground(floatingColor);
399: }
400:
401: int w = 0;
402: int h = 0;
403:
404: boolean tmp = ((loc == SwingConstants.NORTH)
405: || (loc == SwingConstants.SOUTH) || (loc == -1));
406:
407: cachedOrientation = toolBar.getOrientation();
408: cachedBounds = toolBar.getSize();
409: if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
410: || ((cachedOrientation == VERTICAL) && ! tmp))
411: {
412: w = cachedBounds.width;
413: h = cachedBounds.height;
414: }
415: else
416: {
417: w = cachedBounds.height;
418: h = cachedBounds.width;
419: }
420:
421: Point p = dragWindow.getOffset();
422: Insets insets = toolBar.getInsets();
423:
424: dragWindow.setBounds((origin.x + position.x) - p.x
425: - ((insets.left + insets.right) / 2),
426: (origin.y + position.y) - p.y
427: - ((insets.top + insets.bottom) / 2), w, h);
428:
429: if (! dragWindow.isVisible())
430: dragWindow.show();
431: }
432:
433:
443: protected void floatAt(Point position, Point origin)
444: {
445: Point p = new Point(position);
446: int aoc = areaOfClick(origParent,
447: SwingUtilities.convertPoint(toolBar, p, origParent));
448:
449: Container oldParent = toolBar.getParent();
450:
451: oldParent.remove(toolBar);
452: oldParent.doLayout();
453: oldParent.repaint();
454:
455: Container newParent;
456:
457: if (aoc == -1)
458: newParent = ((RootPaneContainer) floatFrame).getContentPane();
459: else
460: {
461: floatFrame.hide();
462: newParent = origParent;
463: }
464:
465: String constraint;
466: switch (aoc)
467: {
468: case SwingConstants.EAST:
469: constraint = BorderLayout.EAST;
470: break;
471: case SwingConstants.NORTH:
472: constraint = BorderLayout.NORTH;
473: break;
474: case SwingConstants.SOUTH:
475: constraint = BorderLayout.SOUTH;
476: break;
477: case SwingConstants.WEST:
478: constraint = BorderLayout.WEST;
479: break;
480: default:
481: constraint = BorderLayout.CENTER;
482: break;
483: }
484:
485: int newOrientation = SwingConstants.HORIZONTAL;
486: if ((aoc != -1)
487: && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
488: newOrientation = SwingConstants.VERTICAL;
489:
490: if (aoc != -1)
491: {
492: constraintBeforeFloating = constraint;
493: lastGoodOrientation = newOrientation;
494: }
495:
496: newParent.add(toolBar, constraint);
497:
498: setFloating(aoc == -1, null);
499: toolBar.setOrientation(newOrientation);
500:
501: Insets insets = floatFrame.getInsets();
502: Dimension dims = toolBar.getPreferredSize();
503: p = dragWindow.getOffset();
504: setFloatingLocation((position.x + origin.x) - p.x
505: - ((insets.left + insets.right) / 2),
506: (position.y + origin.y) - p.y
507: - ((insets.top + insets.bottom) / 2));
508:
509: if (aoc == -1)
510: {
511: floatFrame.pack();
512: floatFrame.setSize(dims.width + insets.left + insets.right,
513: dims.height + insets.top + insets.bottom);
514: floatFrame.show();
515: }
516:
517: newParent.invalidate();
518: newParent.validate();
519: newParent.repaint();
520: }
521:
522:
527: public Color getDockingColor()
528: {
529: return dockingColor;
530: }
531:
532:
538: public Color getFloatingColor()
539: {
540: return floatingColor;
541: }
542:
543:
550: public Dimension getMaximumSize(JComponent c)
551: {
552: return getPreferredSize(c);
553: }
554:
555:
562: public Dimension getMinimumSize(JComponent c)
563: {
564: return getPreferredSize(c);
565: }
566:
567:
570: protected void installComponents()
571: {
572: floatFrame = (Window) createFloatingWindow(toolBar);
573:
574: dragWindow = createDragWindow(toolBar);
575:
576: nonRolloverBorder = createNonRolloverBorder();
577: rolloverBorder = createRolloverBorder();
578:
579: borders = new Hashtable();
580:
581: fillHashtable();
582: }
583:
584:
587: protected void installDefaults()
588: {
589: LookAndFeel.installBorder(toolBar, "ToolBar.border");
590: LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
591: "ToolBar.foreground", "ToolBar.font");
592:
593: dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
594: dockingColor = UIManager.getColor("ToolBar.dockingBackground");
595:
596: floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
597: floatingColor = UIManager.getColor("ToolBar.floatingBackground");
598: setRolloverBorders(toolBar.isRollover());
599: }
600:
601:
605: protected void installKeyboardActions()
606: {
607:
608: }
609:
610:
613: protected void installListeners()
614: {
615: dockingListener = createDockingListener();
616: toolBar.addMouseListener(dockingListener);
617: toolBar.addMouseMotionListener(dockingListener);
618:
619: propertyListener = createPropertyListener();
620: toolBar.addPropertyChangeListener(propertyListener);
621:
622: toolBarContListener = createToolBarContListener();
623: toolBar.addContainerListener(toolBarContListener);
624:
625: windowListener = createFrameListener();
626: floatFrame.addWindowListener(windowListener);
627:
628: toolBarFocusListener = createToolBarFocusListener();
629: toolBar.addFocusListener(toolBarFocusListener);
630: }
631:
632:
639: protected void installNonRolloverBorders(JComponent c)
640: {
641: Component[] components = toolBar.getComponents();
642:
643: for (int i = 0; i < components.length; i++)
644: setBorderToNonRollover(components[i]);
645: }
646:
647:
654: protected void installNormalBorders(JComponent c)
655: {
656: Component[] components = toolBar.getComponents();
657:
658: for (int i = 0; i < components.length; i++)
659: setBorderToNormal(components[i]);
660: }
661:
662:
669: protected void installRolloverBorders(JComponent c)
670: {
671: Component[] components = toolBar.getComponents();
672:
673: for (int i = 0; i < components.length; i++)
674: setBorderToRollover(components[i]);
675: }
676:
677:
681: private void fillHashtable()
682: {
683: Component[] c = toolBar.getComponents();
684:
685: for (int i = 0; i < c.length; i++)
686: {
687: if (c[i] instanceof JButton)
688: {
689:
690: JButton b = (JButton) c[i];
691:
692: if (b.getBorder() != null)
693: borders.put(b, b.getBorder());
694: }
695: }
696: }
697:
698:
703: public void installUI(JComponent c)
704: {
705: super.installUI(c);
706:
707: if (c instanceof JToolBar)
708: {
709: toolBar = (JToolBar) c;
710: toolBar.setOpaque(true);
711: installDefaults();
712: installComponents();
713: installListeners();
714: installKeyboardActions();
715: }
716: }
717:
718:
723: public boolean isFloating()
724: {
725: return floatFrame.isVisible();
726: }
727:
728:
733: public boolean isRolloverBorders()
734: {
735: return toolBar.isRollover();
736: }
737:
738:
744: protected void navigateFocusedComp(int direction)
745: {
746:
747: }
748:
749:
755: protected void setBorderToNonRollover(Component c)
756: {
757: if (c instanceof JButton)
758: {
759: JButton b = (JButton) c;
760: b.setRolloverEnabled(false);
761: b.setBorder(nonRolloverBorder);
762: }
763: }
764:
765:
770: protected void setBorderToNormal(Component c)
771: {
772: if (c instanceof JButton)
773: {
774: JButton b = (JButton) c;
775: Border border = (Border) borders.get(b);
776: b.setBorder(border);
777: }
778: }
779:
780:
785: protected void setBorderToRollover(Component c)
786: {
787: if (c instanceof JButton)
788: {
789: JButton b = (JButton) c;
790: b.setRolloverEnabled(true);
791: b.setBorder(rolloverBorder);
792: }
793: }
794:
795:
800: public void setDockingColor(Color c)
801: {
802: dockingColor = c;
803: }
804:
805:
811: public void setFloating(boolean b, Point p)
812: {
813:
814:
815: floatFrame.setVisible(b);
816: }
817:
818:
824: public void setFloatingColor(Color c)
825: {
826: floatingColor = c;
827: }
828:
829:
835: public void setFloatingLocation(int x, int y)
836: {
837:
838:
839: floatFrame.setLocation(x, y);
840: floatFrame.invalidate();
841: floatFrame.validate();
842: floatFrame.repaint();
843: }
844:
845:
851: public void setOrientation(int orientation)
852: {
853: toolBar.setOrientation(orientation);
854: }
855:
856:
863: public void setRolloverBorders(boolean rollover)
864: {
865: if (rollover)
866: installRolloverBorders(toolBar);
867: else
868: installNonRolloverBorders(toolBar);
869: }
870:
871:
874: protected void uninstallComponents()
875: {
876: installNormalBorders(toolBar);
877: borders = null;
878: rolloverBorder = null;
879: nonRolloverBorder = null;
880: cachedBounds = null;
881:
882: floatFrame = null;
883: dragWindow = null;
884: }
885:
886:
889: protected void uninstallDefaults()
890: {
891: toolBar.setBackground(null);
892: toolBar.setForeground(null);
893: toolBar.setFont(null);
894:
895: dockingBorderColor = null;
896: dockingColor = null;
897: floatingBorderColor = null;
898: floatingColor = null;
899: }
900:
901:
904: protected void uninstallKeyboardActions()
905: {
906:
907: }
908:
909:
912: protected void uninstallListeners()
913: {
914: toolBar.removeFocusListener(toolBarFocusListener);
915: toolBarFocusListener = null;
916:
917: floatFrame.removeWindowListener(windowListener);
918: windowListener = null;
919:
920: toolBar.removeContainerListener(toolBarContListener);
921: toolBarContListener = null;
922:
923: toolBar.removeMouseMotionListener(dockingListener);
924: toolBar.removeMouseListener(dockingListener);
925: dockingListener = null;
926: }
927:
928:
933: public void uninstallUI(JComponent c)
934: {
935: uninstallKeyboardActions();
936: uninstallListeners();
937: uninstallComponents();
938: uninstallDefaults();
939: toolBar = null;
940: }
941:
942:
946: public class DockingListener implements MouseInputListener
947: {
948:
949: protected boolean isDragging;
950:
951:
955: protected Point origin;
956:
957:
958: protected JToolBar toolBar;
959:
960:
965: public DockingListener(JToolBar t)
966: {
967: toolBar = t;
968: }
969:
970:
975: public void mouseClicked(MouseEvent e)
976: {
977:
978: }
979:
980:
986: public void mouseDragged(MouseEvent e)
987: {
988: if (isDragging)
989: dragTo(e.getPoint(), origin);
990: }
991:
992:
997: public void mouseEntered(MouseEvent e)
998: {
999:
1000: }
1001:
1002:
1007: public void mouseExited(MouseEvent e)
1008: {
1009:
1010: }
1011:
1012:
1017: public void mouseMoved(MouseEvent e)
1018: {
1019:
1020: }
1021:
1022:
1029: public void mousePressed(MouseEvent e)
1030: {
1031: if (! toolBar.isFloatable())
1032: return;
1033:
1034: Point ssd = e.getPoint();
1035: Insets insets = toolBar.getInsets();
1036:
1037:
1038: if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1039: {
1040: if (e.getX() > insets.left)
1041: return;
1042: }
1043: else
1044: {
1045: if (e.getY() > insets.top)
1046: return;
1047: }
1048:
1049: origin = new Point(0, 0);
1050: if (toolBar.isShowing())
1051: SwingUtilities.convertPointToScreen(ssd, toolBar);
1052:
1053: if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1054:
1055: origParent = toolBar.getParent();
1056:
1057: if (toolBar.isShowing())
1058: SwingUtilities.convertPointToScreen(origin, toolBar);
1059:
1060: isDragging = true;
1061:
1062: if (dragWindow != null)
1063: dragWindow.setOffset(new Point(cachedBounds.width/2, cachedBounds.height/2));
1064:
1065: dragTo(e.getPoint(), origin);
1066: }
1067:
1068:
1073: public void mouseReleased(MouseEvent e)
1074: {
1075: if (! isDragging || ! toolBar.isFloatable())
1076: return;
1077:
1078: isDragging = false;
1079: floatAt(e.getPoint(), origin);
1080: dragWindow.hide();
1081: }
1082: }
1083:
1084:
1088: protected class DragWindow extends Window
1089: {
1090:
1094: private Color borderColor;
1095:
1096:
1097: private Point offset;
1098:
1099:
1103: DragWindow()
1104: {
1105: super(owner);
1106: }
1107:
1108:
1113: public Color getBorderColor()
1114: {
1115: if (borderColor == null)
1116: return Color.BLACK;
1117:
1118: return borderColor;
1119: }
1120:
1121:
1126: public Insets getInsets()
1127: {
1128:
1129: return new Insets(0, 0, 0, 0);
1130: }
1131:
1132:
1138: public Point getOffset()
1139: {
1140: return offset;
1141: }
1142:
1143:
1148: public void paint(Graphics g)
1149: {
1150:
1151: Color saved = g.getColor();
1152: Rectangle b = getBounds();
1153:
1154: g.setColor(getBorderColor());
1155: g.drawRect(0, 0, b.width - 1, b.height - 1);
1156:
1157: g.setColor(saved);
1158: }
1159:
1160:
1165: public void setBorderColor(Color c)
1166: {
1167: borderColor = c;
1168: }
1169:
1170:
1175: public void setOffset(Point p)
1176: {
1177: offset = p;
1178: }
1179:
1180:
1185: public void setOrientation(int o)
1186: {
1187:
1188: }
1189: }
1190:
1191:
1195: protected class FrameListener extends WindowAdapter
1196: {
1197:
1202: public void windowClosing(WindowEvent e)
1203: {
1204: Container parent = toolBar.getParent();
1205: parent.remove(toolBar);
1206:
1207: if (origParent != null)
1208: {
1209: origParent.add(toolBar,
1210: (constraintBeforeFloating != null)
1211: ? constraintBeforeFloating : BorderLayout.NORTH);
1212: toolBar.setOrientation(lastGoodOrientation);
1213: }
1214:
1215: origParent.invalidate();
1216: origParent.validate();
1217: origParent.repaint();
1218: }
1219: }
1220:
1221:
1224: protected class PropertyListener implements PropertyChangeListener
1225: {
1226:
1231: public void propertyChange(PropertyChangeEvent e)
1232: {
1233:
1234: if (e.getPropertyName().equals("rollover"))
1235: setRolloverBorders(toolBar.isRollover());
1236: }
1237: }
1238:
1239:
1243: protected class ToolBarContListener implements ContainerListener
1244: {
1245:
1251: public void componentAdded(ContainerEvent e)
1252: {
1253: if (e.getChild() instanceof JButton)
1254: {
1255: JButton b = (JButton) e.getChild();
1256:
1257: if (b.getBorder() != null)
1258: borders.put(b, b.getBorder());
1259: }
1260:
1261: if (isRolloverBorders())
1262: setBorderToRollover(e.getChild());
1263: else
1264: setBorderToNonRollover(e.getChild());
1265:
1266: cachedBounds = toolBar.getPreferredSize();
1267: cachedOrientation = toolBar.getOrientation();
1268: }
1269:
1270:
1276: public void componentRemoved(ContainerEvent e)
1277: {
1278: setBorderToNormal(e.getChild());
1279: cachedBounds = toolBar.getPreferredSize();
1280: cachedOrientation = toolBar.getOrientation();
1281: }
1282: }
1283:
1284:
1288: private class ToolBarDialog extends JDialog implements UIResource
1289: {
1290:
1293: public ToolBarDialog()
1294: {
1295: super();
1296: setName((toolBar.getName() != null) ? toolBar.getName() : "");
1297: }
1298: }
1299:
1300:
1303: protected class ToolBarFocusListener implements FocusListener
1304: {
1305:
1308: protected ToolBarFocusListener()
1309: {
1310:
1311: }
1312:
1313:
1318: public void focusGained(FocusEvent e)
1319: {
1320:
1321: }
1322:
1323:
1328: public void focusLost(FocusEvent e)
1329: {
1330:
1331: }
1332: }
1333:
1334:
1337: private static class ToolBarBorder implements Border
1338: {
1339:
1340: private static final int offset = 10;
1341:
1342:
1343: private static final int regular = 2;
1344:
1345:
1352: public Insets getBorderInsets(Component c)
1353: {
1354: if (c instanceof JToolBar)
1355: {
1356: JToolBar tb = (JToolBar) c;
1357: int orientation = tb.getOrientation();
1358:
1359: if (! tb.isFloatable())
1360: return new Insets(regular, regular, regular, regular);
1361: else if (orientation == SwingConstants.HORIZONTAL)
1362: return new Insets(regular, offset, regular, regular);
1363: else
1364: return new Insets(offset, regular, regular, regular);
1365: }
1366:
1367: return new Insets(0, 0, 0, 0);
1368: }
1369:
1370:
1375: public boolean isBorderOpaque()
1376: {
1377: return false;
1378: }
1379:
1380:
1391: private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1392: Color c)
1393: {
1394: Color saved = g.getColor();
1395: g.setColor(c);
1396:
1397: int hgap = 2 * size;
1398: int vgap = 4 * size;
1399: int count = 0;
1400:
1401: for (int i = x; i < (w + x); i += hgap)
1402: for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1403: j += vgap)
1404: g.fillRect(i, j, size, size);
1405:
1406: g.setColor(saved);
1407: }
1408:
1409:
1419: public void paintBorder(Component c, Graphics g, int x, int y, int width,
1420: int height)
1421: {
1422: if (c instanceof JToolBar)
1423: {
1424: JToolBar tb = (JToolBar) c;
1425:
1426: int orientation = tb.getOrientation();
1427:
1428: if (orientation == SwingConstants.HORIZONTAL)
1429: {
1430: paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1431: paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1432: }
1433: else
1434: {
1435: paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1436: paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1437: }
1438: }
1439: }
1440: }
1441: }