Source code: com/flexstor/common/awt/KeyWatcher.java
1 /*
2 * KeyWatcher.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:32 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.awt;
12
13 import java.awt.Component;
14 import java.awt.Container;
15 import java.awt.event.ContainerEvent;
16 import java.awt.event.ContainerListener;
17 import java.awt.event.KeyListener;
18 import java.util.Vector;
19
20 /**
21 * Adds the controller as a key listener for all items (and sub items) in a container. This
22 * is done by watching the container and automatically adding and removing key listeners as
23 * other containers or components are added or removed.
24 *
25 * Please Note:
26 * 1. setController() not only sets the controller, but also does an inital pass through the target container
27 * and adds any items already in that container.
28 * 2. The controller MUST implement KeyListener.
29 * 3. Implement canProcessComponent() to controll which components are included or excluded.
30 *
31 * @author Dan Schroeder
32 * @version 2.0
33 */
34 public class KeyWatcher implements ContainerListener
35 {
36 private KeyListener controller;
37 private Container target;
38 private Vector vExcludeVector = new Vector();
39
40 public void addExclusion(Class c)
41 {
42 vExcludeVector.addElement(c);
43 }
44
45 public void removeExclusion(Class c)
46 {
47 vExcludeVector.removeElement(c);
48 }
49
50 /**
51 * Controls if a components is included or excluded from key watching.
52 * Returns true if this component can
53 * be watched, or false to exclude it.
54 * @param c the component to be checked.
55 * @return true to handle this component, false to ignore it.
56 */
57 protected boolean canProcessComponent ( Component c )
58 {
59 // if contained in exclude list return false
60 if ( vExcludeVector.indexOf(c.getClass()) == -1 )
61 return true; // not found
62 else
63 return false;
64 };
65
66 /**
67 * Creates a new key watcher.
68 * @param target The Container to watch.
69 */
70 public KeyWatcher ( Container target )
71 {
72 this.target = target;
73 // for now, Tree and JCTextAreaComponent as in 2.1
74 // Reason: Tree is based on 1.0 event model, adding listener would break it
75 addExclusion(com.flexstor.common.awt.tree.Tree.class);
76 }
77
78 /**
79 * Creates a new key watcher.
80 * @param target The Container to watch.
81 * @param controller the KeyListener
82 */
83 public KeyWatcher ( Container target, KeyListener controller )
84 {
85 this(target);
86 setController(controller);
87 }
88
89 /**
90 * Sets the controller that will be added as a key listener. Also does an inital pass through the target container.
91 * @param controller The controller class that implements a KeyListener.
92 */
93 public void setController ( KeyListener controller )
94 {
95 this.controller = controller;
96 configureListeners ( target );
97 }
98
99 /**
100 * Required for the container lister -- DO NOT CALL THIS METHOD DIRECTLY!
101 */
102 public void componentAdded ( ContainerEvent e )
103 {
104 configureListeners ( e.getChild() );
105 }
106
107 /**
108 * Required for the container lister -- DO NOT CALL THIS METHOD DIRECTLY!
109 */
110 public void componentRemoved ( ContainerEvent e )
111 {
112 unconfigureListeners ( e.getChild() );
113 }
114
115 /**
116 * Adds a keyListener for c and all components within c.
117 */
118 private void configureListeners ( Component c )
119 {
120 if ( controller == null || !canProcessComponent(c) )
121 return;
122
123 if ( c instanceof Container )
124 {
125 ((Container)c).addContainerListener ( this );
126
127 Component children[] = ((Container)c).getComponents();
128 for ( int i = 0; i < children.length; i++ )
129 configureListeners(children[i]);
130 }
131
132 c.addKeyListener ( controller );
133 }
134
135 /**
136 * Removes the keyListener for c and all components within c.
137 */
138 private void unconfigureListeners ( Component c )
139 {
140 if ( controller == null || !canProcessComponent(c) )
141 return;
142
143 if ( c instanceof Container )
144 {
145 ((Container)c).removeContainerListener ( this );
146
147 Component children[] = ((Container)c).getComponents();
148
149 for ( int i = 0; i < children.length; i++ )
150 unconfigureListeners(children[i]);
151 }
152
153 c.removeKeyListener ( controller );
154 }
155 }