KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtComposite


1
2
3 package Jt;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import java.beans.*;
7 import java.io.*;
8
9 /**
10  * Jt Implementation of the Composite pattern.
11  */

12
13 public class JtComposite extends JtObject {
14
15   private Hashtable hashtable = null;
16
17
18   /**
19     * Changes the hastable used to represent this class and store component objects.
20     */

21
22   public void setHashtable (Hashtable hashtable) {
23     this.hashtable = hashtable;
24   }
25
26
27   /**
28     * Returns the hastable used to represent this class and store component objects.
29     */

30
31   public Hashtable getHashtable () {
32     return (hashtable);
33   }
34
35   public JtComposite () {
36   }
37
38
39
40   // Broadcast a message
41

42   private void broadcastMessage (JtMessage msg)
43   {
44     Collection values;
45     Iterator it;
46
47     if (msg == null || hashtable == null)
48       return;
49
50     values = hashtable.values ();
51     if (values == null)
52       return;
53     it = values.iterator ();
54
55     while (it.hasNext()) {
56       sendMessage (it.next (), msg);
57     }
58
59   }
60
61
62   /**
63     * Process object messages.
64     * <ul>
65     * <li> JtADD_CHILD - Adds a component object (msgContent) to this composition. It uses
66     * the name (key) specified by msgData.
67     * <li> JtREMOVE_CHILD - Removes a component object (msgData) from this composition
68     * <li> JtGET_CHILD - Returns the component object specified by msgData or null if it
69     * doesn't exist.
70     * <li> JtBROADCAST - Broadcast the message specified by msgContent to all the objects
71     * in this composition.
72     * </ul>
73     * @param message Jt Message
74     */

75
76   public Object JavaDoc processMessage (Object JavaDoc message) {
77
78    String JavaDoc msgid = null;
79    JtMessage e = (JtMessage) message;
80    Object JavaDoc content;
81    Object JavaDoc data;
82
83
84      if (e == null)
85     return null;
86
87      msgid = (String JavaDoc) e.getMsgId ();
88
89      if (msgid == null)
90     return null;
91
92      content = e.getMsgContent();
93      data = e.getMsgData ();
94
95      // Remove this object
96
if (msgid.equals ("JtREMOVE")) {
97        return (null);
98      }
99
100      if (msgid.equals ("JtADD_CHILD")) {
101         // Add an object to the hash table
102

103         if (content == null) {
104           handleWarning
105             ("JtComposite.processMessage(JtADD_CHILD):invalid value (null)");
106           return (this);
107
108         }
109         if (hashtable == null)
110
111           hashtable = new Hashtable ();
112         
113         hashtable.put (data, content);
114         return (this);
115      }
116
117
118      if (msgid.equals ("JtREMOVE_CHILD")) {
119         // Add an object to the hash table
120

121
122         if (hashtable == null)
123           hashtable = new Hashtable ();
124         
125
126         return (hashtable.remove (data));
127
128      }
129
130      if (msgid.equals ("JtGET_CHILD")) {
131
132
133         if (data == null) {
134           handleWarning
135             ("JtHashTable.processMessage(JtGET):invalid value (null)");
136           return (this);
137
138         }
139         if (hashtable == null)
140           return (null);
141         
142
143         return (hashtable.get (data));
144
145      }
146
147
148      if (msgid.equals ("JtTEST")) {
149        return (test ());
150      }
151
152      // Broadcast a message to all the members
153
// of the hash table
154

155      if (msgid.equals ("JtBROADCAST")) {
156      
157        if (hashtable == null) {
158          return (this);
159        }
160
161        broadcastMessage ((JtMessage) content);
162
163        return (this);
164      }
165
166      if (msgid.equals ("JtPRINT_OBJECT")) {
167        return (super.processMessage (message));
168      }
169           
170      handleError ("JtComposite.processMessage: invalid message id:" + msgid);
171      return (null);
172
173   }
174
175  /**
176    * Unit tests the messages processed by JtComposite and illustrates the use of this class.
177    */

178
179   private Object JavaDoc test() {
180
181     JtObject main = new JtFactory ();
182     JtMessage msg;
183     JtEcho echo1;
184     JtEcho echo2;
185
186
187
188     echo1 = (JtEcho) createObject ("Jt.JtEcho", "echo1");
189     echo2 = (JtEcho) createObject ("Jt.JtEcho", "echo2");
190
191
192     System.out.println ("JtComposite(JtADD_CHILD): adding a child ...");
193
194     msg = new JtMessage ("JtADD_CHILD");
195
196     setValue (msg, "msgContent", echo1);
197     setValue (msg, "msgData", "echo1");
198
199     sendMessage (this, msg);
200
201     System.out.println ("JtComposite(JtADD_CHILD): adding a child ...");
202
203     msg = new JtMessage ("JtADD_CHILD");
204
205     setValue (msg, "msgContent", echo2);
206     setValue (msg, "msgData", "echo2");
207
208
209     sendMessage (this, msg);
210
211
212
213     msg = new JtMessage ("JtGET_CHILD");
214     main.setValue (msg, "msgData", "echo1");
215
216     System.err.println ("JtComposite(JtGET_CHILD):" +
217       sendMessage (this, msg));
218
219     System.out.println ("Printing the composition (XML format) ...");
220
221     sendMessage (this, new JtMessage ("JtPRINT_OBJECT"));
222
223
224     msg = new JtMessage ("JtREMOVE_CHILD");
225
226     main.setValue (msg, "msgData", "echo1");
227
228     System.err.println ("JtComposite(JtREMOVE_CHILD):" +
229       sendMessage (this, msg));
230
231
232     msg = new JtMessage ("JtBROADCAST");
233     msg.setMsgContent (new JtMessage ("JtPRINT_OBJECT"));
234
235     System.out.println
236     ("JtComposite(JtBROADCAST): broadcasting a message to the composition ...");
237
238     sendMessage (this, msg);
239
240
241     return (this);
242
243
244   }
245  
246   /**
247    * Unit tests the messages processed by JtComposite
248    */

249
250   public static void main(String JavaDoc[] args) {
251
252     JtObject main = new JtFactory ();
253     JtMessage msg, msg1;
254     Integer JavaDoc count;
255     JtComposite composite;
256
257     // Create an instance of JtColletion
258

259     composite = (JtComposite) main.createObject ("Jt.JtComposite", "composite");
260
261     main.sendMessage (composite, new JtMessage ("JtTEST"));
262
263
264     main.removeObject ("composite");
265
266
267   }
268
269 }
270
271
272
Popular Tags