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: if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
408: || ((cachedOrientation == VERTICAL) && ! tmp))
409: {
410: w = cachedBounds.width;
411: h = cachedBounds.height;
412: }
413: else
414: {
415: w = cachedBounds.height;
416: h = cachedBounds.width;
417: }
418:
419: Point p = dragWindow.getOffset();
420: Insets insets = toolBar.getInsets();
421:
422: dragWindow.setBounds((origin.x + position.x) - p.x
423: - ((insets.left + insets.right) / 2),
424: (origin.y + position.y) - p.y
425: - ((insets.top + insets.bottom) / 2), w, h);
426:
427: if (! dragWindow.isVisible())
428: dragWindow.show();
429: }
430:
431:
441: protected void floatAt(Point position, Point origin)
442: {
443: Point p = new Point(position);
444: int aoc = areaOfClick(origParent,
445: SwingUtilities.convertPoint(toolBar, p, origParent));
446:
447: Container oldParent = toolBar.getParent();
448:
449: oldParent.remove(toolBar);
450: oldParent.doLayout();
451: oldParent.repaint();
452:
453: Container newParent;
454:
455: if (aoc == -1)
456: newParent = ((RootPaneContainer) floatFrame).getContentPane();
457: else
458: {
459: floatFrame.hide();
460: newParent = origParent;
461: }
462:
463: String constraint;
464: switch (aoc)
465: {
466: case SwingConstants.EAST:
467: constraint = BorderLayout.EAST;
468: break;
469: case SwingConstants.NORTH:
470: constraint = BorderLayout.NORTH;
471: break;
472: case SwingConstants.SOUTH:
473: constraint = BorderLayout.SOUTH;
474: break;
475: case SwingConstants.WEST:
476: constraint = BorderLayout.WEST;
477: break;
478: default:
479: constraint = BorderLayout.CENTER;
480: break;
481: }
482:
483: int newOrientation = SwingConstants.HORIZONTAL;
484: if ((aoc != -1)
485: && ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
486: newOrientation = SwingConstants.VERTICAL;
487:
488: if (aoc != -1)
489: {
490: constraintBeforeFloating = constraint;
491: lastGoodOrientation = newOrientation;
492: }
493:
494: newParent.add(toolBar, constraint);
495:
496: setFloating(aoc == -1, null);
497: toolBar.setOrientation(newOrientation);
498:
499: Insets insets = floatFrame.getInsets();
500: Dimension dims = toolBar.getPreferredSize();
501: p = dragWindow.getOffset();
502: setFloatingLocation((position.x + origin.x) - p.x
503: - ((insets.left + insets.right) / 2),
504: (position.y + origin.y) - p.y
505: - ((insets.top + insets.bottom) / 2));
506:
507: if (aoc == -1)
508: {
509: floatFrame.pack();
510: floatFrame.setSize(dims.width + insets.left + insets.right,
511: dims.height + insets.top + insets.bottom);
512: floatFrame.show();
513: }
514:
515: newParent.invalidate();
516: newParent.validate();
517: newParent.repaint();
518: }
519:
520:
525: public Color getDockingColor()
526: {
527: return dockingColor;
528: }
529:
530:
536: public Color getFloatingColor()
537: {
538: return floatingColor;
539: }
540:
541:
548: public Dimension getMaximumSize(JComponent c)
549: {
550: return getPreferredSize(c);
551: }
552:
553:
560: public Dimension getMinimumSize(JComponent c)
561: {
562: return getPreferredSize(c);
563: }
564:
565:
568: protected void installComponents()
569: {
570: floatFrame = (Window) createFloatingWindow(toolBar);
571:
572: dragWindow = createDragWindow(toolBar);
573:
574: cachedBounds = toolBar.getPreferredSize();
575: cachedOrientation = toolBar.getOrientation();
576:
577: nonRolloverBorder = createNonRolloverBorder();
578: rolloverBorder = createRolloverBorder();
579:
580: borders = new Hashtable();
581:
582: fillHashtable();
583: }
584:
585:
588: protected void installDefaults()
589: {
590: LookAndFeel.installBorder(toolBar, "ToolBar.border");
591: LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
592: "ToolBar.foreground", "ToolBar.font");
593:
594: dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
595: dockingColor = UIManager.getColor("ToolBar.dockingBackground");
596:
597: floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
598: floatingColor = UIManager.getColor("ToolBar.floatingBackground");
599: setRolloverBorders(toolBar.isRollover());
600: }
601:
602:
606: protected void installKeyboardActions()
607: {
608:
609: }
610:
611:
614: protected void installListeners()
615: {
616: dockingListener = createDockingListener();
617: toolBar.addMouseListener(dockingListener);
618: toolBar.addMouseMotionListener(dockingListener);
619:
620: propertyListener = createPropertyListener();
621: toolBar.addPropertyChangeListener(propertyListener);
622:
623: toolBarContListener = createToolBarContListener();
624: toolBar.addContainerListener(toolBarContListener);
625:
626: windowListener = createFrameListener();
627: floatFrame.addWindowListener(windowListener);
628:
629: toolBarFocusListener = createToolBarFocusListener();
630: toolBar.addFocusListener(toolBarFocusListener);
631: }
632:
633:
640: protected void installNonRolloverBorders(JComponent c)
641: {
642: Component[] components = toolBar.getComponents();
643:
644: for (int i = 0; i < components.length; i++)
645: setBorderToNonRollover(components[i]);
646: }
647:
648:
655: protected void installNormalBorders(JComponent c)
656: {
657: Component[] components = toolBar.getComponents();
658:
659: for (int i = 0; i < components.length; i++)
660: setBorderToNormal(components[i]);
661: }
662:
663:
670: protected void installRolloverBorders(JComponent c)
671: {
672: Component[] components = toolBar.getComponents();
673:
674: for (int i = 0; i < components.length; i++)
675: setBorderToRollover(components[i]);
676: }
677:
678:
682: private void fillHashtable()
683: {
684: Component[] c = toolBar.getComponents();
685:
686: for (int i = 0; i < c.length; i++)
687: {
688: if (c[i] instanceof JButton)
689: {
690:
691: JButton b = (JButton) c[i];
692:
693: if (b.getBorder() != null)
694: borders.put(b, b.getBorder());
695: }
696: }
697: }
698:
699:
704: public void installUI(JComponent c)
705: {
706: super.installUI(c);
707:
708: if (c instanceof JToolBar)
709: {
710: toolBar = (JToolBar) c;
711: toolBar.setOpaque(true);
712: installDefaults();
713: installComponents();
714: installListeners();
715: installKeyboardActions();
716: }
717: }
718:
719:
724: public boolean isFloating()
725: {
726: return floatFrame.isVisible();
727: }
728:
729:
734: public boolean isRolloverBorders()
735: {
736: return toolBar.isRollover();
737: }
738:
739:
745: protected void navigateFocusedComp(int direction)
746: {
747:
748: }
749:
750:
756: protected void setBorderToNonRollover(Component c)
757: {
758: if (c instanceof JButton)
759: {
760: JButton b = (JButton) c;
761: b.setRolloverEnabled(false);
762: b.setBorder(nonRolloverBorder);
763: }
764: }
765:
766:
771: protected void setBorderToNormal(Component c)
772: {
773: if (c instanceof JButton)
774: {
775: JButton b = (JButton) c;
776: Border border = (Border) borders.get(b);
777: b.setBorder(border);
778: }
779: }
780:
781:
786: protected void setBorderToRollover(Component c)
787: {
788: if (c instanceof JButton)
789: {
790: JButton b = (JButton) c;
791: b.setRolloverEnabled(true);
792: b.setBorder(rolloverBorder);
793: }
794: }
795:
796:
801: public void setDockingColor(Color c)
802: {
803: dockingColor = c;
804: }
805:
806:
812: public void setFloating(boolean b, Point p)
813: {
814:
815:
816: floatFrame.setVisible(b);
817: }
818:
819:
825: public void setFloatingColor(Color c)
826: {
827: floatingColor = c;
828: }
829:
830:
836: public void setFloatingLocation(int x, int y)
837: {
838:
839:
840: floatFrame.setLocation(x, y);
841: floatFrame.invalidate();
842: floatFrame.validate();
843: floatFrame.repaint();
844: }
845:
846:
852: public void setOrientation(int orientation)
853: {
854: toolBar.setOrientation(orientation);
855: }
856:
857:
864: public void setRolloverBorders(boolean rollover)
865: {
866: if (rollover)
867: installRolloverBorders(toolBar);
868: else
869: installNonRolloverBorders(toolBar);
870: }
871:
872:
875: protected void uninstallComponents()
876: {
877: installNormalBorders(toolBar);
878: borders = null;
879: rolloverBorder = null;
880: nonRolloverBorder = 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: {
907:
908: }
909:
910:
913: protected void uninstallListeners()
914: {
915: toolBar.removeFocusListener(toolBarFocusListener);
916: toolBarFocusListener = null;
917:
918: floatFrame.removeWindowListener(windowListener);
919: windowListener = null;
920:
921: toolBar.removeContainerListener(toolBarContListener);
922: toolBarContListener = null;
923:
924: toolBar.removeMouseMotionListener(dockingListener);
925: toolBar.removeMouseListener(dockingListener);
926: dockingListener = null;
927: }
928:
929:
934: public void uninstallUI(JComponent c)
935: {
936: uninstallKeyboardActions();
937: uninstallListeners();
938: uninstallComponents();
939: uninstallDefaults();
940: toolBar = null;
941: }
942:
943:
947: public class DockingListener implements MouseInputListener
948: {
949:
950: protected boolean isDragging;
951:
952:
956: protected Point origin;
957:
958:
959: protected JToolBar toolBar;
960:
961:
966: public DockingListener(JToolBar t)
967: {
968: toolBar = t;
969: }
970:
971:
976: public void mouseClicked(MouseEvent e)
977: {
978:
979: }
980:
981:
987: public void mouseDragged(MouseEvent e)
988: {
989: if (isDragging)
990: dragTo(e.getPoint(), origin);
991: }
992:
993:
998: public void mouseEntered(MouseEvent e)
999: {
1000:
1001: }
1002:
1003:
1008: public void mouseExited(MouseEvent e)
1009: {
1010:
1011: }
1012:
1013:
1018: public void mouseMoved(MouseEvent e)
1019: {
1020:
1021: }
1022:
1023:
1030: public void mousePressed(MouseEvent e)
1031: {
1032: if (! toolBar.isFloatable())
1033: return;
1034:
1035: Point ssd = e.getPoint();
1036: Insets insets = toolBar.getInsets();
1037:
1038:
1039: if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
1040: {
1041: if (e.getX() > insets.left)
1042: return;
1043: }
1044: else
1045: {
1046: if (e.getY() > insets.top)
1047: return;
1048: }
1049:
1050: origin = new Point(0, 0);
1051: if (toolBar.isShowing())
1052: SwingUtilities.convertPointToScreen(ssd, toolBar);
1053:
1054: if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
1055:
1056: origParent = toolBar.getParent();
1057:
1058: if (toolBar.isShowing())
1059: SwingUtilities.convertPointToScreen(origin, toolBar);
1060:
1061: isDragging = true;
1062:
1063: if (dragWindow != null)
1064: dragWindow.setOffset(new Point(e.getX(), e.getY()));
1065:
1066: dragTo(e.getPoint(), origin);
1067: }
1068:
1069:
1074: public void mouseReleased(MouseEvent e)
1075: {
1076: if (! isDragging || ! toolBar.isFloatable())
1077: return;
1078:
1079: isDragging = false;
1080: floatAt(e.getPoint(), origin);
1081: dragWindow.hide();
1082: }
1083: }
1084:
1085:
1089: protected class DragWindow extends Window
1090: {
1091:
1095: private Color borderColor;
1096:
1097:
1098: private Point offset;
1099:
1100:
1104: DragWindow()
1105: {
1106: super(owner);
1107: }
1108:
1109:
1114: public Color getBorderColor()
1115: {
1116: if (borderColor == null)
1117: return Color.BLACK;
1118:
1119: return borderColor;
1120: }
1121:
1122:
1127: public Insets getInsets()
1128: {
1129:
1130: return new Insets(0, 0, 0, 0);
1131: }
1132:
1133:
1139: public Point getOffset()
1140: {
1141: return offset;
1142: }
1143:
1144:
1149: public void paint(Graphics g)
1150: {
1151:
1152: Color saved = g.getColor();
1153: Rectangle b = getBounds();
1154:
1155: g.setColor(getBorderColor());
1156: g.drawRect(0, 0, b.width - 1, b.height - 1);
1157:
1158: g.setColor(saved);
1159: }
1160:
1161:
1166: public void setBorderColor(Color c)
1167: {
1168: borderColor = c;
1169: }
1170:
1171:
1176: public void setOffset(Point p)
1177: {
1178: offset = p;
1179: }
1180:
1181:
1186: public void setOrientation(int o)
1187: {
1188:
1189: }
1190: }
1191:
1192:
1196: protected class FrameListener extends WindowAdapter
1197: {
1198:
1203: public void windowClosing(WindowEvent e)
1204: {
1205: Container parent = toolBar.getParent();
1206: parent.remove(toolBar);
1207:
1208: if (origParent != null)
1209: {
1210: origParent.add(toolBar,
1211: (constraintBeforeFloating != null)
1212: ? constraintBeforeFloating : BorderLayout.NORTH);
1213: toolBar.setOrientation(lastGoodOrientation);
1214: }
1215:
1216: origParent.invalidate();
1217: origParent.validate();
1218: origParent.repaint();
1219: }
1220: }
1221:
1222:
1225: protected class PropertyListener implements PropertyChangeListener
1226: {
1227:
1232: public void propertyChange(PropertyChangeEvent e)
1233: {
1234:
1235: if (e.getPropertyName().equals("rollover"))
1236: setRolloverBorders(toolBar.isRollover());
1237: }
1238: }
1239:
1240:
1244: protected class ToolBarContListener implements ContainerListener
1245: {
1246:
1252: public void componentAdded(ContainerEvent e)
1253: {
1254: if (e.getChild() instanceof JButton)
1255: {
1256: JButton b = (JButton) e.getChild();
1257:
1258: if (b.getBorder() != null)
1259: borders.put(b, b.getBorder());
1260: }
1261:
1262: if (isRolloverBorders())
1263: setBorderToRollover(e.getChild());
1264: else
1265: setBorderToNonRollover(e.getChild());
1266:
1267: cachedBounds = toolBar.getPreferredSize();
1268: cachedOrientation = toolBar.getOrientation();
1269: }
1270:
1271:
1277: public void componentRemoved(ContainerEvent e)
1278: {
1279: setBorderToNormal(e.getChild());
1280: cachedBounds = toolBar.getPreferredSize();
1281: cachedOrientation = toolBar.getOrientation();
1282: }
1283: }
1284:
1285:
1289: private class ToolBarDialog extends JDialog implements UIResource
1290: {
1291:
1294: public ToolBarDialog()
1295: {
1296: super();
1297: setName((toolBar.getName() != null) ? toolBar.getName() : "");
1298: }
1299: }
1300:
1301:
1304: protected class ToolBarFocusListener implements FocusListener
1305: {
1306:
1309: protected ToolBarFocusListener()
1310: {
1311:
1312: }
1313:
1314:
1319: public void focusGained(FocusEvent e)
1320: {
1321:
1322: }
1323:
1324:
1329: public void focusLost(FocusEvent e)
1330: {
1331:
1332: }
1333: }
1334:
1335:
1338: private static class ToolBarBorder implements Border
1339: {
1340:
1341: private static final int offset = 10;
1342:
1343:
1344: private static final int regular = 2;
1345:
1346:
1353: public Insets getBorderInsets(Component c)
1354: {
1355: if (c instanceof JToolBar)
1356: {
1357: JToolBar tb = (JToolBar) c;
1358: int orientation = tb.getOrientation();
1359:
1360: if (! tb.isFloatable())
1361: return new Insets(regular, regular, regular, regular);
1362: else if (orientation == SwingConstants.HORIZONTAL)
1363: return new Insets(regular, offset, regular, regular);
1364: else
1365: return new Insets(offset, regular, regular, regular);
1366: }
1367:
1368: return new Insets(0, 0, 0, 0);
1369: }
1370:
1371:
1376: public boolean isBorderOpaque()
1377: {
1378: return false;
1379: }
1380:
1381:
1392: private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
1393: Color c)
1394: {
1395: Color saved = g.getColor();
1396: g.setColor(c);
1397:
1398: int hgap = 2 * size;
1399: int vgap = 4 * size;
1400: int count = 0;
1401:
1402: for (int i = x; i < (w + x); i += hgap)
1403: for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
1404: j += vgap)
1405: g.fillRect(i, j, size, size);
1406:
1407: g.setColor(saved);
1408: }
1409:
1410:
1420: public void paintBorder(Component c, Graphics g, int x, int y, int width,
1421: int height)
1422: {
1423: if (c instanceof JToolBar)
1424: {
1425: JToolBar tb = (JToolBar) c;
1426:
1427: int orientation = tb.getOrientation();
1428:
1429: if (orientation == SwingConstants.HORIZONTAL)
1430: {
1431: paintBumps(g, x, y, offset, height, 1, Color.WHITE);
1432: paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
1433: }
1434: else
1435: {
1436: paintBumps(g, x, y, width, offset, 1, Color.WHITE);
1437: paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
1438: }
1439: }
1440: }
1441: }
1442: }