This repository was archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCSH.java
More file actions
1729 lines (1528 loc) · 59.4 KB
/
CSH.java
File metadata and controls
1729 lines (1528 loc) · 59.4 KB
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
/*
* @(#)CSH.java 1.50 06/10/30
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package javax.help;
import java.lang.reflect.*;
import javax.help.Map.ID;
import java.awt.ActiveEvent;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.MenuComponent;
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRootPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.TreePath;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.applet.Applet;
import java.net.URL;
import java.util.Stack;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.WeakHashMap;
import java.lang.ref.WeakReference;
/**
* A convenience class that provides simple
* access to context-senstive help functionality. It creates a default JavaHelp
* viewer as well as ActionListeners for "Help" keys, on-item help, and
* help buttons.
*
* @author Roger D. Brinkley
* @author Eduardo Pelegri-Llopart
* @author Stepan Marek
* @version 1.50 10/30/06
*
*/
public class CSH {
static private java.util.Map comps;
static private java.util.Map parents;
static private java.util.Vector managers = new Vector();
/**
* Registers the specified manager to maintain dynamic CSH.
* @param m the CSH manager
* @since 2.0
*/
public static void addManager(Manager m) {
managers.add(m);
}
/**
* Registers the specified manager to maintain dynamic CSH at the specified
* position in list of managers.
* @param i index at which specified manager is to be inserted.
* @param m the CSH manager
* @exception ArrayIndexOutOfBoundsException index out of range
* (i < 0 || i >= CSH.getManagerCount()).
* @since 2.0
*/
public static void addManager(int i, Manager m) {
managers.add(i, m);
}
/**
* Removes the first occurrence of the specified manager in manager list.
* @param m manager to be removed from the list, if present.
* @return <code>true</code> if the list contained the specified manager.
* @since 2.0
*/
public static boolean removeManager(Manager m) {
return managers.remove(m);
}
/**
* Removes the manager at the specified position in manager list.
* @param i the index of the manager to removed.
* @exception ArrayIndexOutOfBoundsException index out of range
* (i < 0 || i >= CHS.getManagerCount()).
* @since 2.0
*/
public static void removeManager(int i) {
managers.remove(i);
}
/**
* Removes all managers from manager list.
* @since 2.0
*/
public static void removeAllManagers() {
managers.clear();
}
/**
* Returns the manager at the specified position in manager list.
* @param i index of manager to return.
* @exception ArrayIndexOutOfBoundsException index is out of range
* (i < 0 || i >= CSH.getManagerCount()).
* @since 2.0
*/
public static Manager getManager(int i) {
return (Manager)managers.get(i);
}
/**
* Returns array of managers registered to maintain dynamic CSH.
*
* @return an array containing the managers.
* @exception ArrayStoreException the runtime type of a manager is not a
* <code>CSH.Manager</code>
* @since 2.0
*/
public static Manager[] getManagers() {
return (Manager[])managers.toArray(new Manager[0]);
}
/**
* Returns the number of managers registered to maintain dynamic CSH.
*
* @return the number of managers.
* @since 2.0
*/
public static int getManagerCount() {
return managers.size();
}
/**
* Store HelpID String for an object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static void _setHelpIDString(Object comp, String helpID) {
if (comp instanceof JComponent) {
// For JComponents just use client property
((JComponent)comp).putClientProperty("HelpID", helpID);
} else if ((comp instanceof Component) || (comp instanceof MenuItem)) {
// For MenuItems and Components we have an internal Hashtable of
// components and their properties.
// Initialize as necessary
if (comps == null) {
comps = new WeakHashMap(5);
}
// See if this component has already set some client properties
// If so update.
// If not then create the client props (as needed) and add to
// the internal Hashtable of components and properties
Hashtable clientProps = (Hashtable) comps.get(comp);
if (clientProps != null) {
if (helpID != null) {
clientProps.put("HelpID", helpID);
} else {
clientProps.remove("HelpID");
if (clientProps.isEmpty()) {
comps.remove(comp);
}
}
} else {
// Only create properties if there is a valid helpID
if (helpID != null) {
clientProps = new Hashtable(2);
clientProps.put("HelpID", helpID);
comps.put(comp, clientProps);
}
}
} else {
throw new IllegalArgumentException("Invalid Component");
}
}
/**
* Returns the static HelpID for given object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static String _getHelpIDString(Object comp) {
String helpID = null;
if (comp != null) {
if (comp instanceof JComponent) {
helpID = (String) ((JComponent)comp).getClientProperty("HelpID");
} else if ((comp instanceof Component) || (comp instanceof MenuItem)) {
if (comps != null) {
Hashtable clientProps = (Hashtable)comps.get(comp);
if (clientProps !=null) {
helpID = (String) clientProps.get("HelpID");
}
}
} else {
throw new IllegalArgumentException("Invalid Component");
}
}
return helpID;
}
/**
* Returns the dynamic HelpID for an object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static String _getHelpIDString(Object comp, AWTEvent evt) {
String helpID = null;
if (comp != null) {
Manager managers[] = getManagers();
for (int i = 0; i < managers.length; i++) {
helpID = managers[i].getHelpIDString(comp, evt);
if (helpID != null) {
return helpID;
}
}
}
return null;
}
/**
* Returns ancestor for an object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static Object getParent(Object comp) {
if (comp == null) {
return null;
}
Object parent = null;
if (comp instanceof MenuComponent) {
parent = ((MenuComponent)comp).getParent();
} else if (comp instanceof JPopupMenu) {
parent = ((JPopupMenu)comp).getInvoker();
} else if (comp instanceof Component) {
parent = ((Component)comp).getParent();
} else {
throw new IllegalArgumentException("Invalid Component");
}
if (parent == null && parents != null) {
parent = parents.get(comp);
}
return parent;
}
/**
* Sets the helpID for a Component.
* If helpID is null this method removes the helpID from the component.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
*/
public static void setHelpIDString(Component comp, String helpID) {
_setHelpIDString(comp, helpID);
}
/**
* Sets the helpID for a MenuItem.
* If helpID is null, this method removes the helpID from the component.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
*/
public static void setHelpIDString(MenuItem comp, String helpID) {
_setHelpIDString(comp, helpID);
}
/**
* Returns the dynamic HelpID for a object. The method passes the arguments
* into all registered CSH manageres to obtain dynamic HelpID. If no manager
* provides HelpID for the object, the static HelpID is returned or traverse
* to the component's ancestors for help.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
* @since 2.0
*/
public static String getHelpIDString(Object comp, AWTEvent evt) {
if (comp == null) {
return null;
}
String helpID = _getHelpIDString(comp, evt);
if (helpID == null) {
helpID = _getHelpIDString(comp);
}
if (helpID == null) {
helpID = getHelpIDString(getParent(comp), evt);
}
return helpID;
}
/**
* Returns the static helpID for a component.
* If the component doesn't have associated help, traverse the
* component's ancestors for help.<p>
*
* This method calls <code>CSH.getHelpIDString(comp, null)</code>.
*
* @see #setHelpIDString(Component, String)
*/
public static String getHelpIDString(Component comp) {
return getHelpIDString(comp, null);
/*
if (comp == null) {
return null;
}
String helpID = _getHelpIDString(comp);
// return the helpID if it exists
if (helpID != null) {
return helpID;
}
// loop through the parents to try to find a valid helpID
Component parent = null;
if (comp instanceof JPopupMenu) {
parent = ((JPopupMenu)comp).getInvoker();
} else {
parent = comp.getParent();
}
if (parent == null && parents != null) {
parent = (Component)parents.get(comp);
}
if (parent != null) {
return getHelpIDString(parent);
} else {
return null;
}
*/
}
/**
* Returns the static helpID for a MenuItem.
* If the component doesn't have associated help, traverse the
* component's ancestors for help.<p>
*
* This method calls <code>CSH.getHelpIDString(comp, null)</code>.
*
* @see #setHelpIDString(MenuItem, String)
*/
public static String getHelpIDString(MenuItem comp) {
return getHelpIDString(comp, null);
/*
String helpID = _getHelpIDString(comp);
// return the helpID if it exists
if (helpID != null) {
return helpID;
}
// loop through the parents to try to find a valid helpID
MenuContainer parent = comp.getParent();
if (parent instanceof MenuItem) {
return getHelpIDString((MenuItem)parent);
} else {
return null;
}
*/
}
/**
* Store HelpSet for an object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static void _setHelpSet(Object comp, HelpSet hs) {
if (comp instanceof JComponent) {
// For JComponents just use client property
((JComponent)comp).putClientProperty("HelpSet", hs);
} else if ((comp instanceof Component) || (comp instanceof MenuItem)) {
// For MenuItem and Components we have an internal Hashtable of
// components and their properties.
// Initialize as necessary
if (comps == null) {
comps = new WeakHashMap(5);
}
// See if this component has already set some client properties
// If so update.
// If not then create the client props (as needed) and add to
// the internal Hashtable of components and properties
Hashtable clientProps = (Hashtable) comps.get(comp);
if (clientProps != null) {
if (hs != null) {
clientProps.put("HelpSet", hs);
} else {
clientProps.remove("HelpSet");
if (clientProps.isEmpty()) {
comps.remove(comp);
}
}
} else {
// Only create properties if there is a valid helpID
if (hs != null) {
clientProps = new Hashtable(2);
clientProps.put("HelpSet", hs);
comps.put(comp, clientProps);
}
}
} else {
throw new IllegalArgumentException("Invalid Component");
}
}
/**
* Returns the static HelpSet for an object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static HelpSet _getHelpSet(Object comp) {
HelpSet hs = null;
if (comp != null) {
if (comp instanceof JComponent) {
hs = (HelpSet) ((JComponent)comp).getClientProperty("HelpSet");
} else if ((comp instanceof Component) || (comp instanceof MenuItem)) {
if (comps != null) {
Hashtable clientProps = (Hashtable)comps.get(comp);
if (clientProps !=null) {
hs = (HelpSet) clientProps.get("HelpSet");
}
}
} else {
throw new IllegalArgumentException("Invalid Component");
}
}
return hs;
}
/**
* Returns the dynamic HelpSet for an object.
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*/
private static HelpSet _getHelpSet(Object comp, AWTEvent evt) {
HelpSet hs = null;
if (comp != null) {
Manager[] managers = getManagers();
for (int i = 0; i < managers.length; i++) {
hs = managers[i].getHelpSet(comp, evt);
if (hs != null) {
return hs;
}
}
}
return hs;
}
/**
* Sets the static HelpSet for a Component.
* If HelpSet is null, this method removes the HelpSet
* from the component.
*/
public static void setHelpSet(Component comp, HelpSet hs) {
_setHelpSet(comp, hs);
}
/**
* Sets the static HelpSet for a MenuItem.
* If HelpSet is null, this method removes the HelpSet
* from the component.
*/
public static void setHelpSet(MenuItem comp, HelpSet hs) {
_setHelpSet(comp, hs);
}
/**
* Returns the dynamic HelpSet for an object.
* HelpSets are stored in conjunction with helpIDs. It is possible for a
* object to have a helpID without a HelpSet, but a object cannot have a HelpSet
* without a helpID.
* If the component doesn't have an associated helpID, traverse the
* component's ancestors for a helpID. If the componet has a helpID but
* doesn't have a HelpSet, return null.
*
* @exception IllegalArgumentException comp is neither <code>Component</code> nor
* <code>MenuItem</code>.
*
* @see getHelpID
*/
public static HelpSet getHelpSet(Object comp, AWTEvent evt) {
if (comp == null) {
return null;
}
String helpID = _getHelpIDString(comp, evt);
if (helpID == null) {
helpID = _getHelpIDString(comp);
}
if (helpID != null) {
HelpSet hs = _getHelpSet(comp, evt);
if (hs == null) {
hs = _getHelpSet(comp);
}
return hs;
}
return (getHelpSet(getParent(comp), evt));
}
/**
* Returns the static HelpSet for a Component.
* HelpSets are stored in conjunction with helpIDs. It is possible for a
* Component to have
* a helpID without a HelpSet, but a Component cannot have a HelpSet
* without a helpID.
* If the component doesn't have an associated helpID, traverse the
* component's ancestors for a helpID. If the componet has a helpID but
* doesn't have a HelpSet, return null.<p>
*
* This method calls <code>CSH.getHelpSet(comp, null)</code>.
*
* @see getHelpID
* @exception IllegalArgumentException comp is not <code>Component</code
*/
public static HelpSet getHelpSet(Component comp) {
return getHelpSet(comp, null);
/*
if (comp == null) {
return null;
}
String helpID = _getHelpIDString(comp);
if (helpID != null) {
HelpSet hs = _getHelpSet(comp);
if (hs != null) {
return hs;
}
}
Component parent = null;
if (comp instanceof JPopupMenu) {
parent = ((JPopupMenu)comp).getInvoker();
} else {
parent = comp.getParent();
}
if (parent == null && parents != null) {
parent = (Component)parents.get(comp);
}
if (parent != null) {
return getHelpSet(parent);
} else {
return null;
}
*/
}
/**
* Returns the static HelpSet for a MenuItem.
* HelpSets are stored in conjunction with helpIDs. It is possible for a
* MenuItem to have a helpID without a HelpSet, but a MenuItem
* cannot have a HelpSet without a helpID.
* If the component doesn't have an associated helpID, traverse the
* component's ancestors for a helpID. If the componet has a helpID, but
* doesn't have a HelpSet return null.
*
* This method calls <code>CSH.getHelpIDString(comp, null)</code>.
* @exception IllegalArgumentException comp is not <code>MenuItem</code>
*
* @see getHelpID
*/
public static HelpSet getHelpSet(MenuItem comp) {
return getHelpSet(comp, null);
/*
if (comp == null) {
return null;
}
String helpID = _getHelpIDString(comp);
if (helpID != null) {
HelpSet hs = _getHelpSet(comp);
if (hs != null) {
return hs;
}
}
MenuContainer parent = comp.getParent();
if (parent instanceof MenuItem) {
return getHelpSet((MenuItem)parent);
} else {
return null;
}
*/
}
/**
* Context Sensitive Event Tracking
*
* Creates a new EventDispatchThread from which to dispatch events. This
* method returns when stopModal is invoked.
*
* @return Object The object on which the event occurred. Null if
* cancelled on an undetermined object.
*/
public static Object trackCSEvents() {
MouseEvent e = getMouseEvent();
if (e != null) {
return getDeepestObjectAt(e.getSource(), e.getX(), e.getY());
} else {
return null;
}
}
/*
* Generic displayHelp for all CSH.Display* subclasses
*
* @param hb The HelpBroker to display in. Can be null but hs and
* and presentation must be supplies
* @param hs The HelpSet to display in. Can be null if hb != null
* @param presentation The Presentation class to display the content in
* @param presentationName The named presentation to modify the
* Presentation class with. In some Presenations this is also
* a "named" Presentation.
* @param obj The object for which the help is displayed for
* @param source The Window for focusOwner purposes
* @param event The event that caused this action.
*/
private static void displayHelp(HelpBroker hb, HelpSet hs,
String presentation,
String presentationName, Object obj,
Object source, AWTEvent event) {
Presentation pres=null;
if (hb != null) {
// Start by setting the ownerWindow in the HelpBroker
if (hb instanceof DefaultHelpBroker) {
((DefaultHelpBroker)hb).setActivationObject(source);
}
} else {
// using a Presentation
// Get a Presentation
ClassLoader loader;
Class klass;
Class types[] = { HelpSet.class,
String.class};
Object args[] = { hs,
presentationName};
try {
loader = hs.getLoader();
if (loader == null) {
klass = Class.forName(presentation);
} else {
klass = loader.loadClass(presentation);
}
Method m = klass.getMethod("getPresentation", types);
pres = (Presentation)m.invoke(null, args);
} catch (Exception ex) {
throw new RuntimeException("error invoking presentation" );
}
if (pres == null) {
return;
}
if (pres instanceof WindowPresentation) {
((WindowPresentation)pres).setActivationObject(source);
}
if (pres instanceof Popup && obj instanceof Component) {
((Popup)pres).setInvoker((Component)obj);
}
}
// OK now do the CSH stuff
String helpID = null;
HelpSet objHS = null;
helpID = CSH.getHelpIDString(obj, event);
objHS = CSH.getHelpSet(obj, event);
if (objHS == null) {
if (hb != null) {
objHS = hb.getHelpSet();
} else {
objHS = hs;
}
}
try {
ID id = ID.create(helpID, objHS);
if (id == null) {
id = objHS.getHomeID();
}
if (hb != null) {
hb.setCurrentID(id);
hb.setDisplayed(true);
} else {
pres.setCurrentID(id);
pres.setDisplayed(true);
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
/**
* Context Sensitive Event Tracking
*
* Creates a new EventDispatchThread from which to dispatch events. This
* method returns when stopModal is invoked.
*
* @return MouseEvent The mouse event occurred. Null if
* cancelled on an undetermined object.
*/
private static MouseEvent getMouseEvent() {
// Should the cursor change to a quesiton mark here or
// require the user to change the cursor externally to this method?
// The problem is that each component can have it's own cursor.
// For that reason it might be better to have the user change the
// cusor rather than us.
// To track context-sensitive events get the event queue and process
// the events the same way EventDispatchThread does. Filter out
// ContextSensitiveEvents SelectObject & Cancel (MouseDown & ???).
// Note: This code only handles mouse events. Accessiblity might
// require additional functionality or event trapping
// If the eventQueue can't be retrieved, the thread gets interrupted,
// or the thread isn't a instanceof EventDispatchThread then return
// a null as we won't be able to trap events.
try {
if (EventQueue.isDispatchThread()) {
EventQueue eq = null;
// Find the eventQueue. If we can't get to it then just return
// null since we won't be able to trap any events.
try {
eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
} catch (Exception ee) {
debug(ee);
}
// Safe guard
if (eq == null) {
return null;
}
int eventNumber = -1;
// Process the events until an object has been selected or
// the context-sensitive search has been canceled.
while (true) {
// This is essentially the body of EventDispatchThread
// modified to trap context-senstive events and act
// appropriately
eventNumber++;
AWTEvent event = eq.getNextEvent();
Object src = event.getSource();
// can't call eq.dispatchEvent
// so I pasted it's body here
// debug(event);
// Not sure if I should suppress ActiveEvents or not
// Modal dialogs do. For now we will not suppress the
// ActiveEvent events
if (event instanceof ActiveEvent) {
((ActiveEvent)event).dispatch();
continue;
}
if (src instanceof Component) {
// Trap the context-sensitive events here
if (event instanceof KeyEvent) {
KeyEvent e = (KeyEvent) event;
// if this is the cancel key then exit
// otherwise pass all other keys up
if (e.getKeyCode() == KeyEvent.VK_CANCEL ||
e.getKeyCode() == KeyEvent.VK_ESCAPE) {
e.consume();
return null;
} else {
e.consume();
// dispatchEvent(event);
}
} else if (event instanceof MouseEvent) {
MouseEvent e = (MouseEvent) event;
int eID = e.getID();
if ((eID == MouseEvent.MOUSE_CLICKED ||
eID == MouseEvent.MOUSE_PRESSED ||
eID == MouseEvent.MOUSE_RELEASED) &&
SwingUtilities.isLeftMouseButton(e)) {
if (eID == MouseEvent.MOUSE_CLICKED) {
if (eventNumber == 0) {
dispatchEvent(event);
continue;
}
}
e.consume();
return e;
} else {
e.consume();
}
} else {
dispatchEvent(event);
}
} else if (src instanceof MenuComponent) {
if (event instanceof InputEvent) {
((InputEvent)event).consume();
}
} else {
System.err.println("unable to dispatch event: " + event);
}
}
}
} catch(InterruptedException e) {
debug(e);
}
debug("Fall Through code");
return null;
}
private static void dispatchEvent(AWTEvent event) {
Object src = event.getSource();
if (event instanceof ActiveEvent) {
// This could become the sole method of dispatching in time.
((ActiveEvent)event).dispatch();
} else if (src instanceof Component) {
((Component)src).dispatchEvent(event);
} else if (src instanceof MenuComponent) {
((MenuComponent)src).dispatchEvent(event);
} else {
System.err.println("unable to dispatch event: " + event);
}
}
/**
* Gets the higest visible component in a ancestor hierarchy at
* specific x,y coordinates
*/
private static Object getDeepestObjectAt(Object parent, int x, int y) {
if (parent instanceof Container) {
Container cont = (Container)parent;
// use a copy of 1.3 Container.findComponentAt
Component child = findComponentAt(cont, cont.getWidth(), cont.getHeight(), x, y);
if (child != null && child != cont) {
if (child instanceof JRootPane) {
JLayeredPane lp = ((JRootPane)child).getLayeredPane();
Rectangle b = lp.getBounds();
child = (Component)getDeepestObjectAt(lp, x - b.x, y - b.y);
}
if (child != null) {
return child;
}
}
}
// if the parent is not a Container then it might be a MenuItem.
// But even if it isn't a MenuItem just return the parent because
// that's a close as we can come.
return parent;
}
private static Component findComponentAt(Container cont, int width, int height, int x, int y) {
synchronized (cont.getTreeLock()) {
if (!((x >= 0) && (x < width) && (y >= 0) && (y < height) && cont.isVisible() && cont.isEnabled())) {
return null;
}
Component[] component = cont.getComponents();
int ncomponents = cont.getComponentCount();
// Two passes: see comment in sun.awt.SunGraphicsCallback
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = component[i];
Rectangle rect = null;
if (comp instanceof CellRendererPane) {
Component c = getComponentAt((CellRendererPane)comp, x, y);
if (c != null) {
rect = getRectangleAt((CellRendererPane)comp, x, y);
comp = c;
}
}
if (comp != null && !comp.isLightweight()) {
if (rect == null || rect.width == 0 || rect.height == 0) {
rect = comp.getBounds();
}
if (comp instanceof Container) {
comp = findComponentAt((Container)comp, rect.width, rect.height, x - rect.x, y - rect.y);
} else {
comp = comp.getComponentAt(x - rect.x, y - rect.y);
}
if (comp != null && comp.isVisible() && comp.isEnabled()) {
return comp;
}
}
}
for (int i = 0 ; i < ncomponents ; i++) {
Component comp = component[i];
Rectangle rect = null;
if (comp instanceof CellRendererPane) {
Component c = getComponentAt((CellRendererPane)comp, x, y);
if (c != null) {
rect = getRectangleAt((CellRendererPane)comp, x, y);
comp = c;
}
}
if (comp != null && comp.isLightweight()) {
if (rect == null || rect.width == 0 || rect.height == 0) {
rect = comp.getBounds();
}
if (comp instanceof Container) {
comp = findComponentAt((Container)comp, rect.width, rect.height, x - rect.x, y - rect.y);
} else {
comp = comp.getComponentAt(x - rect.x, y - rect.y);
}
if (comp != null && comp.isVisible() && comp.isEnabled()) {
return comp;
}
}
}
return cont;
}
}
/**
* Returns the Rectangle enclosing component part that the component
* provided by renderer will be draw into.
*/
private static Rectangle getRectangleAt(CellRendererPane cont, int x, int y) {
Rectangle rect = null;
Container c = cont.getParent();