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:
84:
87: public class BasicToolBarUI extends ToolBarUI implements SwingConstants
88: {
89:
91: static JFrame owner = new JFrame();
92:
93:
94: private static Border nonRolloverBorder;
95:
96:
97: private static Border rolloverBorder;
98:
99:
100: protected String constraintBeforeFloating;
101:
102:
104: int lastGoodOrientation;
105:
106:
107: protected Color dockingBorderColor;
108:
109:
110: protected Color dockingColor;
111:
112:
113: protected MouseInputListener dockingListener;
114:
115:
116: protected BasicToolBarUI.DragWindow dragWindow;
117:
118:
119: protected Color floatingBorderColor;
120:
121:
122: protected Color floatingColor;
123:
124:
125: protected int focusedCompIndex;
126:
127:
128: protected PropertyChangeListener propertyListener;
129:
130:
131: protected JToolBar toolBar;
132:
133:
134: protected ContainerListener toolBarContListener;
135:
136:
137: protected FocusListener toolBarFocusListener;
138:
139:
142: protected KeyStroke leftKey;
143:
144:
147: protected KeyStroke rightKey;
148:
149:
152: protected KeyStroke upKey;
153:
154:
157: protected KeyStroke downKey;
158:
159:
163: private transient Window floatFrame;
164:
165:
167: transient Container origParent;
168:
169:
171: transient Hashtable borders;
172:
173:
174: private transient WindowListener windowListener;
175:
176:
178: transient Dimension cachedBounds;
179:
180:
182: transient int cachedOrientation;
183:
184:
187: public BasicToolBarUI()
188: {
189:
190: }
191:
192:
201: public boolean canDock(Component c, Point p)
202: {
203: return areaOfClick(c, p) != -1;
204: }
205:
206:
216: private int areaOfClick(Component c, Point p)
217: {
218:
219: Rectangle pBounds = c.getBounds();
220:
221:
222: Dimension d = toolBar.getSize();
223: int limit = Math.min(d.width, d.height);
224:
225:
226: if (! pBounds.contains(p))
227: return -1;
228:
229: if (p.y < limit)
230: return SwingConstants.NORTH;
231:
232: if (p.y > (pBounds.height - limit))
233: return SwingConstants.SOUTH;
234:
235: if (p.x < limit)
236: return SwingConstants.WEST;
237:
238: if (p.x > (pBounds.width - limit))
239: return SwingConstants.EAST;
240:
241: return -1;
242: }
243:
244:
249: protected MouseInputListener createDockingListener()
250: {
251: return new DockingListener(toolBar);
252: }
253:
254:
261: protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
262: {
263: return new DragWindow();
264: }
265:
266:
275: protected JFrame createFloatingFrame(JToolBar toolbar)
276: {
277:
278: return null;
279: }
280:
281:
289: protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
290: {
291:
292: return new ToolBarDialog();
293: }
294:
295:
300: protected WindowListener createFrameListener()
301: {
302: return new FrameListener();
303: }
304:
305:
311: protected Border createNonRolloverBorder()
312: {
313: return new EtchedBorder();
314: }
315:
316:
321: protected PropertyChangeListener createPropertyListener()
322: {
323: return new PropertyListener();
324: }
325:
326:
332: protected Border createRolloverBorder()
333: {
334: return new EtchedBorder()
335: {
336: public void paintBorder(Component c, Graphics g, int x, int y,
337: int width, int height)
338: {
339: if (c instanceof JButton)
340: {
341: if (((JButton) c).getModel().isRollover())
342: super.paintBorder(c, g, x, y, width, height);
343: }
344: }
345: };
346: }
347:
348:
353: protected ContainerListener createToolBarContListener()
354: {
355: return new ToolBarContListener();
356: }
357:
358:
363: protected FocusListener createToolBarFocusListener()
364: {
365: return new ToolBarFocusListener();
366: }
367:
368:
375: public static ComponentUI createUI(JComponent c)
376: {
377: return new BasicToolBarUI();
378: }
379:
380:
387: protected void dragTo(Point position, Point origin)
388: {
389: int loc = areaOfClick(origParent,
390: SwingUtilities.convertPoint(toolBar, position,
391: origParent));
392:
393: if (loc != -1)
394: {
395: dragWindow.setBorderColor(dockingBorderColor);
396: dragWindow.setBackground(dockingColor);
397: }
398: else
399: {
400: dragWindow.setBorderColor(floatingBorderColor);
401: dragWindow.setBackground(floatingColor);
402: }
403:
404: int w = 0;
405: int h = 0;
406:
407: boolean tmp = ((loc == SwingConstants.NORTH)
408: || (loc == SwingConstants.SOUTH) || (loc == -1));
409:
410: cachedOrientation = toolBar.getOrientation();
411: cachedBounds = toolBar.getSize();
412: if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
413: || ((cachedOrientation == VERTICAL) && ! tmp))
414: {
415: w = cachedBounds.width;
416: h = cachedBounds.height;
417: }
418: else
419: {
420: w = cachedBounds.height;
421: h = cachedBounds.width;
422: }
423:
424: Point p = dragWindow.getOffset();
425: Insets insets = toolBar.getInsets();
426:
427: dragWindow.setBounds((origin.x + position.x) - p.x
428: - ((insets.left + insets.right) / 2),
429: (origin.y + position.y) - p.y
430: - ((insets.top + insets.bottom) / 2), w, h);
431:
432: if (! dragWindow.isVisible())
433: dragWindow.show();
434: }
435:
436:
446: protected void floatAt(Point position, Point origin)
447: {
448: Point p = new Point(position);
449: int aoc = areaOfClick(origParent,
450: SwingUtilities.convertPoint(toolBar, p, origParent));
451:
452: Container oldParent = toolBar.getParent();
453:
454: oldParent.remove(toolBar);
455: oldParent.doLayout();
456: oldParent.repaint();
457:
458: Container newParent;
459:
460: if (aoc == -1)
461: newParent = ((RootPaneContainer) floatFrame).getContentPane();
462: else
463: {
464: floatFrame.hide();
465: newParent = origParent;
466: }
467:
468: String constraint;
469: switch (aoc)
470: {
471: case SwingConstants.EAST:
472: constraint = BorderLayout.EAST;
473: break;
474: case SwingConstants.NORTH:
475: constraint = BorderLayout.NORTH;
476: break;
477: case SwingConstants.SOUTH:
478: constraint = BorderLayout.SOUTH;
479: break;
480: case SwingConstants.WEST:
481: constraint = BorderLayout.WEST;
482: break;
483: default:
484: constraint = BorderLayout.CENTER;
485: break;
486: }
487:
488: int newOrientation = SwingConstants.HORIZONTAL;
489: if ((aoc != -1)
490: && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
491: newOrientation = SwingConstants.VERTICAL;
492:
493: if (aoc != -1)
494: {
495: constraintBeforeFloating = constraint;
496: lastGoodOrientation = newOrientation;
497: }
498:
499: newParent.add(toolBar, constraint);
500:
501: setFloating(aoc == -1, null);
502: toolBar.setOrientation(newOrientation);
503:
504: Insets insets = floatFrame.getInsets();
505: Dimension dims = toolBar.getPreferredSize();
506: p = dragWindow.getOffset();
507: setFloatingLocation((position.x + origin.x) - p.x
508: - ((insets.left + insets.right) / 2),
509: (position.y + origin.y) - p.y
510: - ((insets.top + insets.bottom) / 2));
511:
512: if (aoc == -1)
513: {
514: floatFrame.pack();
515: floatFrame.setSize(dims.width + insets.left + insets.right,
516: dims.height + insets.top + insets.bottom);
517: floatFrame.show();
518: }
519:
520: newParent.invalidate();
521: newParent.validate();
522: newParent.repaint();
523: }
524:
525:
530: public Color getDockingColor()
531: {
532: return dockingColor;
533: }
534:
535:
541: public Color getFloatingColor()
542: {
543: return floatingColor;
544: }
545:
546:
553: public Dimension getMaximumSize(JComponent c)
554: {
555: return getPreferredSize(c);
556: }
557:
558:
565: public Dimension getMinimumSize(JComponent c)
566: {
567: return getPreferredSize(c);
568: }
569:
570:
573: protected void installComponents()
574: {
575: floatFrame = (Window) createFloatingWindow(toolBar);
576:
577: dragWindow = createDragWindow(toolBar);
578:
579: nonRolloverBorder = createNonRolloverBorder();
580: rolloverBorder = createRolloverBorder();
581:
582: borders = new Hashtable();
583: setRolloverBorders(toolBar.isRollover());
584:
585: fillHashtable();
586: }
587:
588:
591: protected void installDefaults()
592: {
593: LookAndFeel.installBorder(toolBar, "ToolBar.border");
594: LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
595: "ToolBar.foreground", "ToolBar.font");
596:
597: dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
598: dockingColor = UIManager.getColor("ToolBar.dockingBackground");
599:
600: floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
601: floatingColor = UIManager.getColor("ToolBar.floatingBackground");
602: }
603:
604:
608: protected void installKeyboardActions()
609: throws NotImplementedException
610: {
611:
612: }
613:
614:
617: protected void installListeners()
618: {
619: dockingListener = createDockingListener();
620: toolBar.addMouseListener(dockingListener);
621: toolBar.addMouseMotionListener(dockingListener);
622:
623: propertyListener = createPropertyListener();
624: toolBar.addPropertyChangeListener(propertyListener);
625:
626: toolBarContListener = createToolBarContListener();
627: toolBar.addContainerListener(toolBarContListener);
628:
629: windowListener = createFrameListener();
630: floatFrame.addWindowListener(windowListener);
631:
632: toolBarFocusListener = createToolBarFocusListener();
633: toolBar.addFocusListener(toolBarFocusListener);
634: }
635:
636:
643: protected void installNonRolloverBorders(JComponent c)
644: {
645: Component[] components = toolBar.getComponents();
646:
647: for (int i = 0; i < components.length; i++)
648: setBorderToNonRollover(components[i]);
649: }
650:
651:
658: protected void installNormalBorders(JComponent c)
659: {
660: Component[] components = toolBar.getComponents();
661:
662: for (int i = 0; i < components.length; i++)
663: setBorderToNormal(components[i]);
664: }
665:
666:
673: protected void installRolloverBorders(JComponent c)
674: {
675: Component[] components = toolBar.getComponents();
676:
677: for (int i = 0; i < components.length; i++)
678: setBorderToRollover(components[i]);
679: }
680:
681:
685: private void fillHashtable()
686: {
687: Component[] c = toolBar.getComponents();
688:
689: for (int i = 0; i < c.length; i++)
690: {
691: if (c[i] instanceof JButton)
692: {
693:
694: JButton b = (JButton) c[i];
695:
696: if (b.getBorder() != null)
697: borders.put(b, b.getBorder());
698: }
699: }
700: }
701:
702:
707: public void installUI(JComponent c)
708: {
709: super.installUI(c);
710:
711: if (c instanceof JToolBar)
712: {
713: toolBar = (JToolBar) c;
714: installDefaults();
715: installComponents();
716: installListeners();
717: installKeyboardActions();
718: }
719: }
720:
721:
726: public boolean isFloating()
727: {
728: return floatFrame.isVisible();
729: }
730:
731:
736: public boolean isRolloverBorders()
737: {
738: return toolBar.isRollover();
739: }
740:
741:
747: protected void navigateFocusedComp(int direction)
748: {
749:
750: }
751:
752:
758: protected void setBorderToNonRollover(Component c)
759: {
760: if (c instanceof AbstractButton)
761: {
762: AbstractButton b = (AbstractButton) c;
763: b.setRolloverEnabled(false);
764: b.setBorder(nonRolloverBorder);
765: }
766: }
767:
768:
773: protected void setBorderToNormal(Component c)
774: {
775: if (c instanceof JButton)
776: {
777: JButton b = (JButton) c;
778: Border border = (Border) borders.get(b);
779: b.setBorder(border);
780: }
781: }
782:
783:
788: protected void setBorderToRollover(Component c)
789: {
790: if (c instanceof JButton)
791: {
792: JButton b = (JButton) c;
793: b.setRolloverEnabled(true);
794: b.setBorder(rolloverBorder);
795: }
796: }
797:
798:
803: public void setDockingColor(Color c)
804: {
805: dockingColor = c;
806: }
807:
808:
814: public void setFloating(boolean b, Point p)
815: {
816:
817:
818: floatFrame.setVisible(b);
819: }
820:
821:
827: public void setFloatingColor(Color c)
828: {
829: floatingColor = c;
830: }
831:
832:
838: public void setFloatingLocation(int x, int y)
839: {
840:
841:
842: floatFrame.setLocation(x, y);
843: floatFrame.invalidate();
844: floatFrame.validate();
845: floatFrame.repaint();
846: }
847:
848:
854: public void setOrientation(int orientation)
855: {
856: toolBar.setOrientation(orientation);
857: }
858:
859:
866: public void setRolloverBorders(boolean rollover)
867: {
868: if (rollover)
869: installRolloverBorders(toolBar);
870: else
871: installNonRolloverBorders(toolBar);
872: }
873:
874:
877: protected void uninstallComponents()
878: {
879: installNormalBorders(toolBar);
880: borders = null;
881: cachedBounds = null;
882:
883: floatFrame = null;
884: dragWindow = null;
885: }
886:
887:
890: protected void uninstallDefaults()
891: {
892: toolBar.setBackground(null);
893: toolBar.setForeground(null);
894: toolBar.setFont(null);
895:
896: dockingBorderColor = null;
897: dockingColor = null;
898: floatingBorderColor = null;
899: floatingColor = null;
900: }
901:
902:
905: protected void uninstallKeyboardActions()
906: throws NotImplementedException
907: {
908:
909: }
910:
911:
914: protected void uninstallListeners()
915: {
916: toolBar.removeFocusListener(toolBarFocusListener);
917: toolBarFocusListener = null;
918:
919: floatFrame.removeWindowListener(windowListener);
920: windowListener = null;
921:
922: toolBar.removeContainerListener(toolBarContListener);
923: toolBarContListener = null;
924:
925: toolBar.removeMouseMotionListener(dockingListener);
926: toolBar.removeMouseListener(dockingListener);
927: dockingListener = null;
928: }
929:
930:
935: public void uninstallUI(JComponent c)
936: {
937: uninstallKeyboardActions();
938: uninstallListeners();
939: uninstallComponents();
940: uninstallDefaults();
941: toolBar = null;
942: }
943:
944:
948: public class DockingListener implements MouseInputListener
949: {
950:
951: protected boolean isDragging;
952:
953:
957: protected Point origin;
958:
959:
960: protected JToolBar toolBar;
961:
962:
967: public DockingListener(JToolBar t)
968: {
969: toolBar = t;
970: }
971:
972:
977: public void mouseClicked(MouseEvent e)
978: {
979:
980: }
981:
982:
988: public void mouseDragged(MouseEvent e)
989: {
990: if (isDragging)
991: dragTo(e.getPoint(), origin);
992: }
993:
994:
999: public void mouseEntered(MouseEvent e)
1000: {
1001:
1002: }
1003:
1004:
1009: public void mouseExited(MouseEvent e)
1010: {
1011:
1012: }
1013:
1014:
1019: public void mouseMoved(MouseEvent e)
1020: {
1021:
1022: }
1023:
1024:
1031: public void mousePressed(MouseEvent e)
1032: {
1033: if (! toolBar.isFloatable())
1034: return;
1035:
1036: Point ssd = e.getPoint();
1037: Insets insets = toolBar.getInsets();
1038:
1039:
1040: if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1041: {
1042: if (e.getX() > insets.left)
1043: return;
1044: }
1045: else
1046: {
1047: if (e.getY() > insets.top)
1048: return;
1049: }
1050:
1051: origin = new Point(0, 0);
1052: if (toolBar.isShowing())
1053: SwingUtilities.convertPointToScreen(ssd, toolBar);
1054:
1055: if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1056:
1057: origParent = toolBar.getParent();
1058:
1059: if (toolBar.isShowing())
1060: SwingUtilities.convertPointToScreen(origin, toolBar);
1061:
1062: isDragging = true;
1063:
1064: if (dragWindow != null)
1065: dragWindow.setOffset(new Point(cachedBounds.width/2, cachedBounds.height/2));
1066:
1067: dragTo(e.getPoint(), origin);
1068: }
1069:
1070:
1075: public void mouseReleased(MouseEvent e)
1076: {
1077: if (! isDragging || ! toolBar.isFloatable())
1078: return;
1079:
1080: isDragging = false;
1081: floatAt(e.getPoint(), origin);
1082: dragWindow.hide();
1083: }
1084: }
1085:
1086:
1090: protected class DragWindow extends Window
1091: {
1092:
1096: private Color borderColor;
1097:
1098:
1099: private Point offset;
1100:
1101:
1105: DragWindow()
1106: {
1107: super(owner);
1108: }
1109:
1110:
1115: public Color getBorderColor()
1116: {
1117: if (borderColor == null)
1118: return Color.BLACK;
1119:
1120: return borderColor;
1121: }
1122:
1123:
1128: public Insets getInsets()
1129: {
1130:
1131: return new Insets(0, 0, 0, 0);
1132: }
1133:
1134:
1140: public Point getOffset()
1141: {
1142: return offset;
1143: }
1144:
1145:
1150: public void paint(Graphics g)
1151: {
1152:
1153: Color saved = g.getColor();
1154: Rectangle b = getBounds();
1155:
1156: g.setColor(getBorderColor());
1157: g.drawRect(0, 0, b.width - 1, b.height - 1);
1158:
1159: g.setColor(saved);
1160: }
1161:
1162:
1167: public void setBorderColor(Color c)
1168: {
1169: borderColor = c;
1170: }
1171:
1172:
1177: public void setOffset(Point p)
1178: {
1179: offset = p;
1180: }
1181:
1182:
1187: public void setOrientation(int o)
1188: {
1189:
1190: }
1191: }
1192:
1193:
1197: protected class FrameListener extends WindowAdapter
1198: {
1199:
1204: public void windowClosing(WindowEvent e)
1205: {
1206: Container parent = toolBar.getParent();
1207: parent.remove(toolBar);
1208:
1209: if (origParent != null)
1210: {
1211: origParent.add(toolBar,
1212: (constraintBeforeFloating != null)
1213: ? constraintBeforeFloating : BorderLayout.NORTH);
1214: toolBar.setOrientation(lastGoodOrientation);
1215: }
1216:
1217: origParent.invalidate();
1218: origParent.validate();
1219: origParent.repaint();
1220: }
1221: }
1222:
1223:
1226: protected class PropertyListener implements PropertyChangeListener
1227: {
1228:
1233: public void propertyChange(PropertyChangeEvent e)
1234: {
1235:
1236: if (e.getPropertyName().equals("rollover") && toolBar != null)
1237: setRolloverBorders(toolBar.isRollover());
1238: }
1239: }
1240:
1241:
1245: protected class ToolBarContListener implements ContainerListener
1246: {
1247:
1253: public void componentAdded(ContainerEvent e)
1254: {
1255: if (e.getChild() instanceof JButton)
1256: {
1257: JButton b = (JButton) e.getChild();
1258:
1259: if (b.getBorder() != null)
1260: borders.put(b, b.getBorder());
1261: }
1262:
1263: if (isRolloverBorders())
1264: setBorderToRollover(e.getChild());
1265: else
1266: setBorderToNonRollover(e.getChild());
1267:
1268: cachedBounds = toolBar.getPreferredSize();
1269: cachedOrientation = toolBar.getOrientation();
1270: }
1271:
1272:
1278: public void componentRemoved(ContainerEvent e)
1279: {
1280: setBorderToNormal(e.getChild());
1281: cachedBounds = toolBar.getPreferredSize();
1282: cachedOrientation = toolBar.getOrientation();
1283: }
1284: }
1285:
1286:
1290: private class ToolBarDialog extends JDialog implements UIResource
1291: {
1292:
1295: public ToolBarDialog()
1296: {
1297: super();
1298: setName((toolBar.getName() != null) ? toolBar.getName() : "");
1299: }
1300: }
1301:
1302:
1305: protected class ToolBarFocusListener implements FocusListener
1306: {
1307:
1310: protected ToolBarFocusListener()
1311: {
1312:
1313: }
1314:
1315:
1320: public void focusGained(FocusEvent e)
1321: {
1322:
1323: }
1324:
1325:
1330: public void focusLost(FocusEvent e)
1331: {
1332:
1333: }
1334: }
1335:
1336:
1339: private static class ToolBarBorder implements Border
1340: {
1341:
1342: private static final int offset = 10;
1343:
1344:
1345: private static final int regular = 2;
1346:
1347:
1354: public Insets getBorderInsets(Component c)
1355: {
1356: if (c instanceof JToolBar)
1357: {
1358: JToolBar tb = (JToolBar) c;
1359: int orientation = tb.getOrientation();
1360:
1361: if (! tb.isFloatable())
1362: return new Insets(regular, regular, regular, regular);
1363: else if (orientation == SwingConstants.HORIZONTAL)
1364: return new Insets(regular, offset, regular, regular);
1365: else
1366: return new Insets(offset, regular, regular, regular);
1367: }
1368:
1369: return new Insets(0, 0, 0, 0);
1370: }
1371:
1372:
1377: public boolean isBorderOpaque()
1378: {
1379: return false;
1380: }
1381:
1382:
1393: private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1394: Color c)
1395: {
1396: Color saved = g.getColor();
1397: g.setColor(c);
1398:
1399: int hgap = 2 * size;
1400: int vgap = 4 * size;
1401: int count = 0;
1402:
1403: for (int i = x; i < (w + x); i += hgap)
1404: for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1405: j += vgap)
1406: g.fillRect(i, j, size, size);
1407:
1408: g.setColor(saved);
1409: }
1410:
1411:
1421: public void paintBorder(Component c, Graphics g, int x, int y, int width,
1422: int height)
1423: {
1424: if (c instanceof JToolBar)
1425: {
1426: JToolBar tb = (JToolBar) c;
1427:
1428: int orientation = tb.getOrientation();
1429:
1430: if (orientation == SwingConstants.HORIZONTAL)
1431: {
1432: paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1433: paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1434: }
1435: else
1436: {
1437: paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1438: paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1439: }
1440: }
1441: }
1442: }
1443: }