1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
|
/* BasicToolBarUI.java --
Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.plaf.basic;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Hashtable;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.RootPaneContainer;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.ActionMapUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.ToolBarUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicBorders.ButtonBorder;
/**
* This is the Basic Look and Feel UI class for JToolBar.
*/
public class BasicToolBarUI extends ToolBarUI implements SwingConstants
{
/**
* Implements the keyboard actions for JToolBar.
*/
static class ToolBarAction
extends AbstractAction
{
/**
* Performs the action.
*/
public void actionPerformed(ActionEvent event)
{
Object cmd = getValue("__command__");
JToolBar toolBar = (JToolBar) event.getSource();
BasicToolBarUI ui = (BasicToolBarUI) toolBar.getUI();
if (cmd.equals("navigateRight"))
ui.navigateFocusedComp(EAST);
else if (cmd.equals("navigateLeft"))
ui.navigateFocusedComp(WEST);
else if (cmd.equals("navigateUp"))
ui.navigateFocusedComp(NORTH);
else if (cmd.equals("navigateDown"))
ui.navigateFocusedComp(SOUTH);
else
assert false : "Shouldn't reach here";
}
}
/** Static owner of all DragWindows.
* This is package-private to avoid an accessor method. */
static JFrame owner = new JFrame();
/** The border used when the JToolBar is in nonrollover mode. */
private static Border nonRolloverBorder;
/** The border used when the JToolBar is in rollover mode. */
private static Border rolloverBorder;
/** The last known BorderLayout constraint before floating. */
protected String constraintBeforeFloating;
/** The last known orientation of the JToolBar before floating.
* This is package-private to avoid an accessor method. */
int lastGoodOrientation;
/** The color of the border when it is dockable. */
protected Color dockingBorderColor;
/** The background color of the JToolBar when it is dockable. */
protected Color dockingColor;
/** The docking listener responsible for mouse events on the JToolBar. */
protected MouseInputListener dockingListener;
/** The window used for dragging the JToolBar. */
protected BasicToolBarUI.DragWindow dragWindow;
/** The color of the border when it is not dockable. */
protected Color floatingBorderColor;
/** The background color of the JToolBar when it is not dockable. */
protected Color floatingColor;
/** The index of the focused component. */
protected int focusedCompIndex;
/** The PropertyChangeListener for the JToolBar. */
protected PropertyChangeListener propertyListener;
/** The JToolBar this UI delegate is responsible for. */
protected JToolBar toolBar;
/** The Container listener for the JToolBar. */
protected ContainerListener toolBarContListener;
/** The Focus listener for the JToolBar. */
protected FocusListener toolBarFocusListener;
/**
* @deprecated since JDK1.3.
*/
protected KeyStroke leftKey;
/**
* @deprecated since JDK1.3.
*/
protected KeyStroke rightKey;
/**
* @deprecated since JDK1.3.
*/
protected KeyStroke upKey;
/**
* @deprecated since JDK1.3.
*/
protected KeyStroke downKey;
/**
* The floating window that is responsible for holding the JToolBar when it
* is dragged outside of its original parent.
*/
private transient Window floatFrame;
/** The original parent of the JToolBar.
* This is package-private to avoid an accessor method. */
transient Container origParent;
/** A hashtable of components and their original borders.
* This is package-private to avoid an accessor method. */
transient Hashtable borders;
/** A window listener for the floatable frame. */
private transient WindowListener windowListener;
/** A set of cached bounds of the JToolBar.
* This is package-private to avoid an accessor method. */
transient Dimension cachedBounds;
/** The cached orientation of the JToolBar.
* This is package-private to avoid an accessor method. */
transient int cachedOrientation;
/**
* This method creates a new <code>BasicToolBarUI</code> object for the given JToolBar.
*/
public BasicToolBarUI()
{
// Do nothing here.
}
/**
* This method returns whether the JToolBar can dock at the given position.
*
* @param c The component to try to dock in.
* @param p The position of the mouse cursor relative to the given
* component.
*
* @return Whether the JToolBar can dock.
*/
public boolean canDock(Component c, Point p)
{
return areaOfClick(c, p) != -1;
}
/**
* This helper method returns the position of the JToolBar if it can dock.
*
* @param c The component to try to dock in.
* @param p The position of the mouse cursor relative to the given
* component.
*
* @return One of the SwingConstants directions or -1 if the JToolBar can't
* dock.
*/
private int areaOfClick(Component c, Point p)
{
// Has to dock in immediate parent, not eventual root container.
Rectangle pBounds = c.getBounds();
// XXX: In Sun's implementation, the space the toolbar has to dock is dependent on the size it had last.
Dimension d = toolBar.getSize();
int limit = Math.min(d.width, d.height);
// The order of checking is 1. top 2. bottom 3. left 4. right
if (! pBounds.contains(p))
return -1;
if (p.y < limit)
return SwingConstants.NORTH;
if (p.y > (pBounds.height - limit))
return SwingConstants.SOUTH;
if (p.x < limit)
return SwingConstants.WEST;
if (p.x > (pBounds.width - limit))
return SwingConstants.EAST;
return -1;
}
/**
* This method creates a new DockingListener for the JToolBar.
*
* @return A new DockingListener for the JToolBar.
*/
protected MouseInputListener createDockingListener()
{
return new DockingListener(toolBar);
}
/**
* This method creates a new DragWindow for the given JToolBar.
*
* @param toolbar The JToolBar to create a DragWindow for.
*
* @return A new DragWindow.
*/
protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar)
{
return new DragWindow();
}
/**
* This method creates a new floating frame for the JToolBar. By default,
* this UI uses createFloatingWindow instead. This method of creating a
* floating frame is deprecated.
*
* @param toolbar The JToolBar to create a floating frame for.
*
* @return A new floating frame.
*/
protected JFrame createFloatingFrame(JToolBar toolbar)
{
// FIXME: Though deprecated, this should still work.
return null;
}
/**
* This method creates a new floating window for the JToolBar. This is the
* method used by default to create a floating container for the JToolBar.
*
* @param toolbar The JToolBar to create a floating window for.
*
* @return A new floating window.
*/
protected RootPaneContainer createFloatingWindow(JToolBar toolbar)
{
// This one is used by default though.
return new ToolBarDialog();
}
/**
* This method creates a new WindowListener for the JToolBar.
*
* @return A new WindowListener.
*/
protected WindowListener createFrameListener()
{
return new FrameListener();
}
/**
* This method creates a new nonRolloverBorder for JButtons when the
* JToolBar's rollover property is set to false.
*
* @return A new NonRolloverBorder.
*/
protected Border createNonRolloverBorder()
{
Border b = UIManager.getBorder("ToolBar.nonrolloverBorder");
if (b == null)
{
b = new CompoundBorder(
new ButtonBorder(UIManager.getColor("Button.shadow"),
UIManager.getColor("Button.darkShadow"),
UIManager.getColor("Button.light"),
UIManager.getColor("Button.highlight")),
BasicBorders.getMarginBorder());
}
return b; }
/**
* This method creates a new PropertyChangeListener for the JToolBar.
*
* @return A new PropertyChangeListener.
*/
protected PropertyChangeListener createPropertyListener()
{
return new PropertyListener();
}
/**
* This method creates a new rollover border for JButtons when the
* JToolBar's rollover property is set to true.
*
* @return A new rollover border.
*/
protected Border createRolloverBorder()
{
Border b = UIManager.getBorder("ToolBar.rolloverBorder");
if (b == null)
{
b = new CompoundBorder(
new ButtonBorder(UIManager.getColor("Button.shadow"),
UIManager.getColor("Button.darkShadow"),
UIManager.getColor("Button.light"),
UIManager.getColor("Button.highlight")),
BasicBorders.getMarginBorder());
}
return b;
}
/**
* This method creates a new Container listener for the JToolBar.
*
* @return A new Container listener.
*/
protected ContainerListener createToolBarContListener()
{
return new ToolBarContListener();
}
/**
* This method creates a new FocusListener for the JToolBar.
*
* @return A new FocusListener for the JToolBar.
*/
protected FocusListener createToolBarFocusListener()
{
return new ToolBarFocusListener();
}
/**
* This method creates a new UI delegate for the given JComponent.
*
* @param c The JComponent to create a UI delegate for.
*
* @return A new UI delegate.
*/
public static ComponentUI createUI(JComponent c)
{
return new BasicToolBarUI();
}
/**
* This method is called to drag the DragWindow around when the JToolBar is
* being dragged around.
*
* @param position The mouse cursor coordinates relative to the JToolBar.
* @param origin The screen position of the JToolBar.
*/
protected void dragTo(Point position, Point origin)
{
int loc = areaOfClick(origParent,
SwingUtilities.convertPoint(toolBar, position,
origParent));
if (loc != -1)
{
dragWindow.setBorderColor(dockingBorderColor);
dragWindow.setBackground(dockingColor);
}
else
{
dragWindow.setBorderColor(floatingBorderColor);
dragWindow.setBackground(floatingColor);
}
int w = 0;
int h = 0;
boolean tmp = (loc == SwingConstants.NORTH)
|| (loc == SwingConstants.SOUTH) || (loc == -1);
cachedOrientation = toolBar.getOrientation();
cachedBounds = toolBar.getSize();
if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp)
|| ((cachedOrientation == VERTICAL) && ! tmp))
{
w = cachedBounds.width;
h = cachedBounds.height;
}
else
{
w = cachedBounds.height;
h = cachedBounds.width;
}
Point p = dragWindow.getOffset();
Insets insets = toolBar.getInsets();
dragWindow.setBounds((origin.x + position.x) - p.x
- ((insets.left + insets.right) / 2),
(origin.y + position.y) - p.y
- ((insets.top + insets.bottom) / 2), w, h);
if (! dragWindow.isVisible())
dragWindow.show();
}
/**
* This method is used at the end of a drag session to place the frame in
* either its original parent as a docked JToolBar or in its floating
* frame.
*
* @param position The position of the mouse cursor relative to the
* JToolBar.
* @param origin The screen position of the JToolBar before the drag session
* started.
*/
protected void floatAt(Point position, Point origin)
{
Point p = new Point(position);
int aoc = areaOfClick(origParent,
SwingUtilities.convertPoint(toolBar, p, origParent));
Container oldParent = toolBar.getParent();
oldParent.remove(toolBar);
oldParent.doLayout();
oldParent.repaint();
Container newParent;
if (aoc == -1)
newParent = ((RootPaneContainer) floatFrame).getContentPane();
else
{
floatFrame.hide();
newParent = origParent;
}
String constraint;
switch (aoc)
{
case SwingConstants.EAST:
constraint = BorderLayout.EAST;
break;
case SwingConstants.NORTH:
constraint = BorderLayout.NORTH;
break;
case SwingConstants.SOUTH:
constraint = BorderLayout.SOUTH;
break;
case SwingConstants.WEST:
constraint = BorderLayout.WEST;
break;
default:
constraint = BorderLayout.CENTER;
break;
}
int newOrientation = SwingConstants.HORIZONTAL;
if ((aoc != -1)
&& ((aoc == SwingConstants.EAST) || (aoc == SwingConstants.WEST)))
newOrientation = SwingConstants.VERTICAL;
if (aoc != -1)
{
constraintBeforeFloating = constraint;
lastGoodOrientation = newOrientation;
}
newParent.add(toolBar, constraint);
setFloating(aoc == -1, null);
toolBar.setOrientation(newOrientation);
Insets insets = floatFrame.getInsets();
Dimension dims = toolBar.getPreferredSize();
p = dragWindow.getOffset();
setFloatingLocation((position.x + origin.x) - p.x
- ((insets.left + insets.right) / 2),
(position.y + origin.y) - p.y
- ((insets.top + insets.bottom) / 2));
if (aoc == -1)
{
floatFrame.pack();
floatFrame.setSize(dims.width + insets.left + insets.right,
dims.height + insets.top + insets.bottom);
floatFrame.show();
}
newParent.invalidate();
newParent.validate();
newParent.repaint();
}
/**
* This method returns the docking color.
*
* @return The docking color.
*/
public Color getDockingColor()
{
return dockingColor;
}
/**
* This method returns the Color which is displayed when over a floating
* area.
*
* @return The color which is displayed when over a floating area.
*/
public Color getFloatingColor()
{
return floatingColor;
}
/**
* This method returns the maximum size of the given JComponent for this UI.
*
* @param c The JComponent to find the maximum size for.
*
* @return The maximum size for this UI.
*/
public Dimension getMaximumSize(JComponent c)
{
return getPreferredSize(c);
}
/**
* This method returns the minimum size of the given JComponent for this UI.
*
* @param c The JComponent to find a minimum size for.
*
* @return The minimum size for this UI.
*/
public Dimension getMinimumSize(JComponent c)
{
return getPreferredSize(c);
}
/**
* This method installs the needed components for the JToolBar.
*/
protected void installComponents()
{
floatFrame = (Window) createFloatingWindow(toolBar);
dragWindow = createDragWindow(toolBar);
nonRolloverBorder = createNonRolloverBorder();
rolloverBorder = createRolloverBorder();
borders = new Hashtable();
setRolloverBorders(toolBar.isRollover());
fillHashtable();
}
/**
* This method installs the defaults as specified by the look and feel.
*/
protected void installDefaults()
{
LookAndFeel.installBorder(toolBar, "ToolBar.border");
LookAndFeel.installColorsAndFont(toolBar, "ToolBar.background",
"ToolBar.foreground", "ToolBar.font");
dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
dockingColor = UIManager.getColor("ToolBar.dockingBackground");
floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
floatingColor = UIManager.getColor("ToolBar.floatingBackground");
}
/**
* This method installs the keyboard actions for the JToolBar as specified
* by the look and feel.
*/
protected void installKeyboardActions()
{
// Install the input map.
InputMap inputMap =
(InputMap) SharedUIDefaults.get("ToolBar.ancestorInputMap");
SwingUtilities.replaceUIInputMap(toolBar,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
inputMap);
// FIXME: The JDK uses a LazyActionMap for parentActionMap
SwingUtilities.replaceUIActionMap(toolBar, getActionMap());
}
/**
* Fetches the action map from the UI defaults, or create a new one
* if the action map hasn't been initialized.
*
* @return the action map
*/
private ActionMap getActionMap()
{
ActionMap am = (ActionMap) UIManager.get("ToolBar.actionMap");
if (am == null)
{
am = createDefaultActions();
UIManager.getLookAndFeelDefaults().put("ToolBar.actionMap", am);
}
return am;
}
private ActionMap createDefaultActions()
{
ActionMapUIResource am = new ActionMapUIResource();
Action action = new ToolBarAction();
am.put("navigateLeft", action);
am.put("navigateRight", action);
am.put("navigateUp", action);
am.put("navigateDown", action);
return am;
}
/**
* This method installs listeners for the JToolBar.
*/
protected void installListeners()
{
dockingListener = createDockingListener();
toolBar.addMouseListener(dockingListener);
toolBar.addMouseMotionListener(dockingListener);
propertyListener = createPropertyListener();
toolBar.addPropertyChangeListener(propertyListener);
toolBarContListener = createToolBarContListener();
toolBar.addContainerListener(toolBarContListener);
windowListener = createFrameListener();
floatFrame.addWindowListener(windowListener);
toolBarFocusListener = createToolBarFocusListener();
if (toolBarFocusListener != null)
{
int count = toolBar.getComponentCount();
for (int i = 0; i < count; i++)
toolBar.getComponent(i).addFocusListener(toolBarFocusListener);
}
}
/**
* This method installs non rollover borders for each component inside the
* given JComponent.
*
* @param c The JComponent whose children need to have non rollover borders
* installed.
*/
protected void installNonRolloverBorders(JComponent c)
{
Component[] components = toolBar.getComponents();
for (int i = 0; i < components.length; i++)
setBorderToNonRollover(components[i]);
}
/**
* This method installs normal (or their original) borders for each
* component inside the given JComponent.
*
* @param c The JComponent whose children need to have their original
* borders installed.
*/
protected void installNormalBorders(JComponent c)
{
Component[] components = toolBar.getComponents();
for (int i = 0; i < components.length; i++)
setBorderToNormal(components[i]);
}
/**
* This method install rollover borders for each component inside the given
* JComponent.
*
* @param c The JComponent whose children need to have rollover borders
* installed.
*/
protected void installRolloverBorders(JComponent c)
{
Component[] components = toolBar.getComponents();
for (int i = 0; i < components.length; i++)
setBorderToRollover(components[i]);
}
/**
* This method fills the borders hashtable with a list of components that
* are JButtons and their borders.
*/
private void fillHashtable()
{
Component[] c = toolBar.getComponents();
for (int i = 0; i < c.length; i++)
{
if (c[i] instanceof JButton)
{
// Don't really care about anything other than JButtons
JButton b = (JButton) c[i];
if (b.getBorder() != null)
borders.put(b, b.getBorder());
}
}
}
/**
* This method installs the UI for the given JComponent.
*
* @param c The JComponent to install a UI for.
*/
public void installUI(JComponent c)
{
super.installUI(c);
if (c instanceof JToolBar)
{
toolBar = (JToolBar) c;
installDefaults();
installComponents();
installListeners();
installKeyboardActions();
}
}
/**
* This method returns whether the JToolBar is floating.
*
* @return Whether the JToolBar is floating.
*/
public boolean isFloating()
{
return floatFrame.isVisible();
}
/**
* This method returns whether rollover borders have been set.
*
* @return Whether rollover borders have been set.
*/
public boolean isRolloverBorders()
{
return toolBar.isRollover();
}
/**
* This method navigates in the given direction giving focus to the next
* component in the given direction.
*
* @param direction The direction to give focus to.
*/
protected void navigateFocusedComp(int direction)
{
int count = toolBar.getComponentCount();
switch (direction)
{
case EAST:
case SOUTH:
if (focusedCompIndex >= 0 && focusedCompIndex < count)
{
int i = focusedCompIndex + 1;
boolean focusRequested = false;
// Find component to focus and request focus on it.
while (i != focusedCompIndex && ! focusRequested)
{
if (i >= count)
i = 0;
Component comp = toolBar.getComponentAtIndex(i++);
if (comp != null && comp.isFocusable()
&& comp.isEnabled())
{
comp.requestFocus();
focusRequested = true;
}
}
}
break;
case WEST:
case NORTH:
if (focusedCompIndex >= 0 && focusedCompIndex < count)
{
int i = focusedCompIndex - 1;
boolean focusRequested = false;
// Find component to focus and request focus on it.
while (i != focusedCompIndex && ! focusRequested)
{
if (i < 0)
i = count - 1;
Component comp = toolBar.getComponentAtIndex(i--);
if (comp != null && comp.isFocusable()
&& comp.isEnabled())
{
comp.requestFocus();
focusRequested = true;
}
}
}
break;
default:
break;
}
}
/**
* This method sets the border of the given component to a non rollover
* border.
*
* @param c The Component whose border needs to be set.
*/
protected void setBorderToNonRollover(Component c)
{
if (c instanceof AbstractButton)
{
AbstractButton b = (AbstractButton) c;
b.setRolloverEnabled(false);
// Save old border in hashtable.
if (b.getBorder() != null)
borders.put(b, b.getBorder());
b.setBorder(nonRolloverBorder);
}
}
/**
* This method sets the border of the given component to its original value.
*
* @param c The Component whose border needs to be set.
*/
protected void setBorderToNormal(Component c)
{
if (c instanceof AbstractButton)
{
AbstractButton b = (AbstractButton) c;
b.setRolloverEnabled(true);
b.setBorder((Border) borders.remove(b));
}
}
/**
* This method sets the border of the given component to a rollover border.
*
* @param c The Component whose border needs to be set.
*/
protected void setBorderToRollover(Component c)
{
if (c instanceof AbstractButton)
{
AbstractButton b = (AbstractButton) c;
b.setRolloverEnabled(false);
// Save old border in hashtable.
if (b.getBorder() != null)
borders.put(b, b.getBorder());
b.setBorder(rolloverBorder);
}
}
/**
* This method sets the docking color.
*
* @param c The docking color.
*/
public void setDockingColor(Color c)
{
dockingColor = c;
}
/**
* This method sets the floating property for the JToolBar.
*
* @param b Whether the JToolBar is floating.
* @param p FIXME
*/
public void setFloating(boolean b, Point p)
{
// FIXME: use p for something. It's not location
// since we already have setFloatingLocation.
floatFrame.setVisible(b);
}
/**
* This method sets the color displayed when the JToolBar is not in a
* dockable area.
*
* @param c The floating color.
*/
public void setFloatingColor(Color c)
{
floatingColor = c;
}
/**
* This method sets the floating location of the JToolBar.
*
* @param x The x coordinate for the floating frame.
* @param y The y coordinate for the floating frame.
*/
public void setFloatingLocation(int x, int y)
{
// x,y are the coordinates of the new JFrame created to store the toolbar
// XXX: The floating location is bogus is not floating.
floatFrame.setLocation(x, y);
floatFrame.invalidate();
floatFrame.validate();
floatFrame.repaint();
}
/**
* This is a convenience method for changing the orientation of the
* JToolBar.
*
* @param orientation The new orientation.
*/
public void setOrientation(int orientation)
{
toolBar.setOrientation(orientation);
}
/**
* This method changes the child components to have rollover borders if the
* given parameter is true. Otherwise, the components are set to have non
* rollover borders.
*
* @param rollover Whether the children will have rollover borders.
*/
public void setRolloverBorders(boolean rollover)
{
if (rollover)
installRolloverBorders(toolBar);
else
installNonRolloverBorders(toolBar);
}
/**
* This method uninstall UI installed components from the JToolBar.
*/
protected void uninstallComponents()
{
installNormalBorders(toolBar);
borders = null;
cachedBounds = null;
floatFrame = null;
dragWindow = null;
}
/**
* This method removes the defaults installed by the Look and Feel.
*/
protected void uninstallDefaults()
{
toolBar.setBackground(null);
toolBar.setForeground(null);
toolBar.setFont(null);
dockingBorderColor = null;
dockingColor = null;
floatingBorderColor = null;
floatingColor = null;
}
/**
* This method uninstalls keyboard actions installed by the UI.
*/
protected void uninstallKeyboardActions()
{
SwingUtilities.replaceUIInputMap(toolBar, JComponent.
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
SwingUtilities.replaceUIActionMap(toolBar, null);
}
/**
* This method uninstalls listeners installed by the UI.
*/
protected void uninstallListeners()
{
if (toolBarFocusListener != null)
{
int count = toolBar.getComponentCount();
for (int i = 0; i < count; i++)
toolBar.getComponent(i).removeFocusListener(toolBarFocusListener);
toolBarFocusListener = null;
}
floatFrame.removeWindowListener(windowListener);
windowListener = null;
toolBar.removeContainerListener(toolBarContListener);
toolBarContListener = null;
toolBar.removeMouseMotionListener(dockingListener);
toolBar.removeMouseListener(dockingListener);
dockingListener = null;
}
/**
* This method uninstalls the UI.
*
* @param c The JComponent that is having this UI removed.
*/
public void uninstallUI(JComponent c)
{
uninstallKeyboardActions();
uninstallListeners();
uninstallComponents();
uninstallDefaults();
toolBar = null;
}
/**
* This is the MouseHandler class that allows the user to drag the JToolBar
* in and out of the parent and dock it if it can.
*/
public class DockingListener implements MouseInputListener
{
/** Whether the JToolBar is being dragged. */
protected boolean isDragging;
/**
* The origin point. This point is saved from the beginning press and is
* used until the end of the drag session.
*/
protected Point origin;
/** The JToolBar being dragged. */
protected JToolBar toolBar;
/**
* Creates a new DockingListener object.
*
* @param t The JToolBar this DockingListener is being used for.
*/
public DockingListener(JToolBar t)
{
toolBar = t;
}
/**
* This method is called when the mouse is clicked.
*
* @param e The MouseEvent.
*/
public void mouseClicked(MouseEvent e)
{
// Nothing to do here.
}
/**
* This method is called when the mouse is dragged. It delegates the drag
* painting to the dragTo method.
*
* @param e The MouseEvent.
*/
public void mouseDragged(MouseEvent e)
{
if (isDragging)
dragTo(e.getPoint(), origin);
}
/**
* This method is called when the mouse enters the JToolBar.
*
* @param e The MouseEvent.
*/
public void mouseEntered(MouseEvent e)
{
// Nothing to do here.
}
/**
* This method is called when the mouse exits the JToolBar.
*
* @param e The MouseEvent.
*/
public void mouseExited(MouseEvent e)
{
// Nothing to do here.
}
/**
* This method is called when the mouse is moved in the JToolBar.
*
* @param e The MouseEvent.
*/
public void mouseMoved(MouseEvent e)
{
// Nothing to do here.
}
/**
* This method is called when the mouse is pressed in the JToolBar. If the
* press doesn't occur in a place where it causes the JToolBar to be
* dragged, it returns. Otherwise, it starts a drag session.
*
* @param e The MouseEvent.
*/
public void mousePressed(MouseEvent e)
{
if (! toolBar.isFloatable())
return;
Point ssd = e.getPoint();
Insets insets = toolBar.getInsets();
// Verify that this click occurs in the top inset.
if (toolBar.getOrientation() == SwingConstants.HORIZONTAL)
{
if (e.getX() > insets.left)
return;
}
else
{
if (e.getY() > insets.top)
return;
}
origin = new Point(0, 0);
if (toolBar.isShowing())
SwingUtilities.convertPointToScreen(ssd, toolBar);
if (! (SwingUtilities.getAncestorOfClass(Window.class, toolBar) instanceof UIResource))
// Need to know who keeps the toolBar if it gets dragged back into it.
origParent = toolBar.getParent();
if (toolBar.isShowing())
SwingUtilities.convertPointToScreen(origin, toolBar);
isDragging = true;
if (dragWindow != null)
dragWindow.setOffset(new Point(cachedBounds.width / 2,
cachedBounds.height / 2));
dragTo(e.getPoint(), origin);
}
/**
* This method is called when the mouse is released from the JToolBar.
*
* @param e The MouseEvent.
*/
public void mouseReleased(MouseEvent e)
{
if (! isDragging || ! toolBar.isFloatable())
return;
isDragging = false;
floatAt(e.getPoint(), origin);
dragWindow.hide();
}
}
/**
* This is the window that appears when the JToolBar is being dragged
* around.
*/
protected class DragWindow extends Window
{
/**
* The current border color. It changes depending on whether the JToolBar
* is over a place that allows it to dock.
*/
private Color borderColor;
/** The between the mouse and the top left corner of the window. */
private Point offset;
/**
* Creates a new DragWindow object.
* This is package-private to avoid an accessor method.
*/
DragWindow()
{
super(owner);
}
/**
* The color that the border should be.
*
* @return The border color.
*/
public Color getBorderColor()
{
if (borderColor == null)
return Color.BLACK;
return borderColor;
}
/**
* This method returns the insets for the DragWindow.
*
* @return The insets for the DragWindow.
*/
public Insets getInsets()
{
// This window has no decorations, so insets are empty.
return new Insets(0, 0, 0, 0);
}
/**
* This method returns the mouse offset from the top left corner of the
* DragWindow.
*
* @return The mouse offset.
*/
public Point getOffset()
{
return offset;
}
/**
* This method paints the DragWindow.
*
* @param g The Graphics object to paint with.
*/
public void paint(Graphics g)
{
// No visiting children necessary.
Color saved = g.getColor();
Rectangle b = getBounds();
g.setColor(getBorderColor());
g.drawRect(0, 0, b.width - 1, b.height - 1);
g.setColor(saved);
}
/**
* This method changes the border color.
*
* @param c The new border color.
*/
public void setBorderColor(Color c)
{
borderColor = c;
}
/**
* This method changes the mouse offset.
*
* @param p The new mouse offset.
*/
public void setOffset(Point p)
{
offset = p;
}
/**
* Sets the orientation of the toolbar and the
* drag window.
*
* @param o - the new orientation of the toolbar and drag
* window.
*/
public void setOrientation(int o)
{
toolBar.setOrientation(o);
if (dragWindow != null)
dragWindow.setOrientation(o);
}
}
/**
* This helper class listens for Window events from the floatable window and
* if it is closed, returns the JToolBar to the last known good location.
*/
protected class FrameListener extends WindowAdapter
{
/**
* This method is called when the floating window is closed.
*
* @param e The WindowEvent.
*/
public void windowClosing(WindowEvent e)
{
Container parent = toolBar.getParent();
parent.remove(toolBar);
if (origParent != null)
{
origParent.add(toolBar,
(constraintBeforeFloating != null)
? constraintBeforeFloating : BorderLayout.NORTH);
toolBar.setOrientation(lastGoodOrientation);
}
origParent.invalidate();
origParent.validate();
origParent.repaint();
}
}
/**
* This helper class listens for PropertyChangeEvents from the JToolBar.
*/
protected class PropertyListener implements PropertyChangeListener
{
/**
* This method is called when a property from the JToolBar is changed.
*
* @param e The PropertyChangeEvent.
*/
public void propertyChange(PropertyChangeEvent e)
{
// FIXME: need name properties so can change floatFrame title.
if (e.getPropertyName().equals("rollover") && toolBar != null)
setRolloverBorders(toolBar.isRollover());
}
}
/**
* This helper class listens for components added to and removed from the
* JToolBar.
*/
protected class ToolBarContListener implements ContainerListener
{
/**
* This method is responsible for setting rollover or non rollover for new
* buttons added to the JToolBar.
*
* @param e The ContainerEvent.
*/
public void componentAdded(ContainerEvent e)
{
if (e.getChild() instanceof JButton)
{
JButton b = (JButton) e.getChild();
if (b.getBorder() != null)
borders.put(b, b.getBorder());
}
if (isRolloverBorders())
setBorderToRollover(e.getChild());
else
setBorderToNonRollover(e.getChild());
cachedBounds = toolBar.getPreferredSize();
cachedOrientation = toolBar.getOrientation();
Component c = e.getChild();
if (toolBarFocusListener != null)
c.addFocusListener(toolBarFocusListener);
}
/**
* This method is responsible for giving the child components their
* original borders when they are removed.
*
* @param e The ContainerEvent.
*/
public void componentRemoved(ContainerEvent e)
{
setBorderToNormal(e.getChild());
cachedBounds = toolBar.getPreferredSize();
cachedOrientation = toolBar.getOrientation();
Component c = e.getChild();
if (toolBarFocusListener != null)
c.removeFocusListener(toolBarFocusListener);
}
}
/**
* This is the floating window that is returned when getFloatingWindow is
* called.
*/
private class ToolBarDialog extends JDialog implements UIResource
{
/**
* Creates a new ToolBarDialog object with the name given by the JToolBar.
*/
public ToolBarDialog()
{
super();
setName((toolBar.getName() != null) ? toolBar.getName() : "");
}
}
/**
* DOCUMENT ME!
*/
protected class ToolBarFocusListener implements FocusListener
{
/**
* Creates a new ToolBarFocusListener object.
*/
protected ToolBarFocusListener()
{
// Nothing to do here.
}
/**
* Receives notification when the toolbar or one of it's component
* receives the keyboard input focus.
*
* @param e the focus event
*/
public void focusGained(FocusEvent e)
{
Component c = e.getComponent();
focusedCompIndex = toolBar.getComponentIndex(c);
}
/**
* Receives notification when the toolbar or one of it's component
* looses the keyboard input focus.
*
* @param e the focus event
*/
public void focusLost(FocusEvent e)
{
// Do nothing here.
}
}
/**
* This helper class acts as the border for the JToolBar.
*/
private static class ToolBarBorder implements Border
{
/** The size of the larger, draggable side of the border. */
private static final int offset = 10;
/** The other sides. */
private static final int regular = 2;
/**
* This method returns the border insets for the JToolBar.
*
* @param c The Component to find insets for.
*
* @return The border insets.
*/
public Insets getBorderInsets(Component c)
{
if (c instanceof JToolBar)
{
JToolBar tb = (JToolBar) c;
int orientation = tb.getOrientation();
if (! tb.isFloatable())
return new Insets(regular, regular, regular, regular);
else if (orientation == SwingConstants.HORIZONTAL)
return new Insets(regular, offset, regular, regular);
else
return new Insets(offset, regular, regular, regular);
}
return new Insets(0, 0, 0, 0);
}
/**
* This method returns whether the border is opaque.
*
* @return Whether the border is opaque.
*/
public boolean isBorderOpaque()
{
return false;
}
/**
* This method paints the ribbed area of the border.
*
* @param g The Graphics object to paint with.
* @param x The x coordinate of the area.
* @param y The y coordinate of the area.
* @param w The width of the area.
* @param h The height of the area.
* @param size The size of the bump.
* @param c The color of the bumps.
*/
private void paintBumps(Graphics g, int x, int y, int w, int h, int size,
Color c)
{
Color saved = g.getColor();
g.setColor(c);
int hgap = 2 * size;
int vgap = 4 * size;
int count = 0;
for (int i = x; i < (w + x); i += hgap)
for (int j = ((count++ % 2) == 0) ? y : (y + (2 * size)); j < (h + y);
j += vgap)
g.fillRect(i, j, size, size);
g.setColor(saved);
}
/**
* This method paints the border around the given Component.
*
* @param c The Component whose border is being painted.
* @param g The Graphics object to paint with.
* @param x The x coordinate of the component.
* @param y The y coordinate of the component.
* @param width The width of the component.
* @param height The height of the component.
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height)
{
if (c instanceof JToolBar)
{
JToolBar tb = (JToolBar) c;
int orientation = tb.getOrientation();
if (orientation == SwingConstants.HORIZONTAL)
{
paintBumps(g, x, y, offset, height, 1, Color.WHITE);
paintBumps(g, x + 1, y + 1, offset - 1, height - 1, 1, Color.GRAY);
}
else
{
paintBumps(g, x, y, width, offset, 1, Color.WHITE);
paintBumps(g, x + 1, y + 1, width - 1, offset - 1, 1, Color.GRAY);
}
}
}
}
}
|