KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > ButtonRegister


1 package com.ca.directory.jxplorer;
2
3 import java.awt.*;
4 import java.util.*;
5
6
7
8 /**
9  * This class should be used to register the common buttons and menu items in JXplorer.
10  * JXplorer has three sets of the same thing; edit menu items, tool bar buttons and the
11  * tree's popup menu items. Once registered with this class, a group of components can be
12  * enabled/disabled in one operation.<br><br>
13  * <b>The way it works</b><br>
14  * A component (button, menu item etc) is registered via registerItem using a key. The
15  * key is one of CONNECT, DISCONNECT...etc. The key is added the ButtonContainer and its
16  * value is an ArrayList of the component that is registered. If more than one component
17  * is registered against the key (which is the point of this class), that component is just
18  * added to the key's ArrayList of components rather than making a new entry in the
19  * ButtonContainer.<br><br>
20  * To set a component's state use setItemEnabled. Pass in the key and whether you want it enabled
21  * or not. This will set the state over all the components registered against the given key.
22  * @author Trudi.
23  */

24
25 public class ButtonRegister
26 {
27     ButtonContainer bc = null;
28
29     //TE: keys...
30
public final String JavaDoc CONNECT = "CONNECT";
31     public final String JavaDoc DISCONNECT = "DISCONNECT";
32     public final String JavaDoc PRINT = "PRINT";
33     public final String JavaDoc CUT = "CUT";
34     public final String JavaDoc COPY = "COPY";
35     public final String JavaDoc COPY_DN = "COPY_DN";
36     public final String JavaDoc PASTE = "PASTE";
37     public final String JavaDoc PASTE_ALIAS = "PASTE_ALIAS";
38     public final String JavaDoc DELETE = "DELETE";
39     public final String JavaDoc NEW = "NEW";
40     public final String JavaDoc RENAME = "RENAME";
41     public final String JavaDoc REFRESH = "REFRESH";
42     public final String JavaDoc REFRESH_TREE = "REFRESH_TREE";
43     public final String JavaDoc BOOKMARKS = "BOOKMARKS";
44     public final String JavaDoc SEARCH = "SEARCH";
45     public final String JavaDoc STOP = "STOP";
46     public final String JavaDoc LDIF = "LDIF";
47
48     private final String JavaDoc[] components = new String JavaDoc[]{CONNECT,
49                                                    DISCONNECT,
50                                                    PRINT,
51                                                    CUT,
52                                                    COPY,
53                                                    COPY_DN,
54                                                    PASTE,
55                                                    PASTE_ALIAS,
56                                                    DELETE,
57                                                    NEW,
58                                                    RENAME,
59                                                    REFRESH,
60                                                    REFRESH_TREE,
61                                                    BOOKMARKS,
62                                                    SEARCH,
63                                                    STOP,
64                                                    LDIF};
65
66
67
68     /**
69      * Empty constructor.
70      */

71     public ButtonRegister()
72     {
73     }
74
75     /**
76      * Registers a component against a key in the ButtonContainer.
77      * @param key the key of the component (this.CONNECT, this.DISCONNECT...etc).
78      * @param comp the component (button/menu item) to register.
79      */

80     public void registerItem(String JavaDoc key, Component comp)
81     {
82         if(bc == null)
83             bc = new ButtonContainer();
84
85         bc.put(key, comp);
86     }
87
88     /**
89      * Sets a component's state. This will set the state over all the components
90      * registered against the given key.
91      * @param key the key of the component (this.CONNECT, this.DISCONNECT...etc).
92      * @param enabled the state to set the component to (true = enabled, false = disabled).
93      */

94     public void setItemEnabled(String JavaDoc key, boolean enabled)
95     {
96         if(bc != null)
97         {
98             ArrayList temp = bc.get(key);
99             if(temp != null)
100                 for (int i = 0; i < temp.size(); i++)
101                     ((Component)temp.get(i)).setEnabled(enabled);
102         }
103     }
104
105     /**
106      * Sets the state of common commonents (COPY, CUT, DELETE, NEW,
107      * RENAME, PASTE_ALIAS (only if state is false), PASTE (only if state is false)).
108      * @param state true to enable, false to disable.
109      */

110     public void setCommonState(boolean state)
111     {
112         setItemEnabled(COPY, state);
113         setItemEnabled(CUT, state);
114         setItemEnabled(DELETE, state);
115         setItemEnabled(NEW, state);
116         setItemEnabled(RENAME, state);
117         setItemEnabled(BOOKMARKS, state);
118         setItemEnabled(SEARCH, state);
119         setItemEnabled(LDIF, state);
120
121         if(state == false)
122         {
123             setItemEnabled(PASTE_ALIAS, state);
124             setItemEnabled(PASTE, state);
125         }
126     }
127
128     /**
129      * Sets all the components to disabled except for CONNECT.
130      */

131     public void setDisconnectState()
132     {
133         for(int i=0;i<components.length; i++)
134             if(i==0)
135                 setItemEnabled(components[i], true); //TE: enable connection.
136
else
137                 setItemEnabled(components[i], false);
138     }
139
140     /**
141      * Sets all the components to enabled except STOP, PASTE and PASTE_ALIAS.
142      */

143     public void setConnectedState()
144     {
145         for(int i=0;i<components.length; i++)
146             if(i==6 || i==7 || i==15)
147                 setItemEnabled(components[i], false); //TE: disable stop, paste & paste alias.
148
else
149                 setItemEnabled(components[i], true);
150     }
151
152     /**
153      * Makes a new kind of hash table: one that allows
154      * multiple objects to be stored against one key.
155      * It does this by using String=ArrayList.
156      */

157     class ButtonContainer extends Hashtable
158     {
159         /**
160          * Empty constructor.
161          */

162         public ButtonContainer() {}
163
164         /**
165          * Registers a component against a key in the ButtonContainer. It creates a
166          * new ArrayList if one does exist for the key, otherwise it uses the existing one.
167          * @param key the key of the component (this.CONNECT, this.DISCONNECT...etc).
168          * @param comp the component (button/menu item) to register.
169          */

170         public void put(String JavaDoc key, Component comp)
171         {
172             ArrayList temp = (ArrayList)super.get(key);
173             if(temp == null)
174                 temp = new ArrayList();
175
176             if(!temp.contains(comp))
177                 temp.add(comp);
178
179             super.put(key, temp);
180         }
181
182         /**
183          * Returns the ArrayList of components that is stored against
184          * the key.
185          * @param key the key of the component (this.CONNECT, this.DISCONNECT...etc).
186          * @return the ArrayList of components that is stored against
187          * the key.
188          */

189         public ArrayList get(String JavaDoc key)
190         {
191             return (ArrayList)super.get(key);
192         }
193     }
194 }
195
Popular Tags