KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtPrototype


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 Prototype pattern.
11  */

12
13 public class JtPrototype extends JtObject {
14
15
16
17
18   public JtPrototype () {
19   }
20
21   // Clone the object
22

23   private Object JavaDoc cloneObject () {
24
25
26    JtMessage msg = new JtMessage ("JtENCODE_OBJECT");
27    JtObject tmp = new JtObject();
28     Object JavaDoc aux;
29
30     msg.setMsgContent (this);
31     aux = tmp.processMessage (msg);
32     
33     if (aux == null) {
34       handleError ("cloneObject: Unable to encode the object (XML format");
35       return (null);
36     }
37
38     msg = new JtMessage ("JtDECODE_OBJECT");
39     
40     msg.setMsgContent (aux);
41     return (tmp.processMessage (msg));
42   }
43
44   private Object JavaDoc test () {
45
46     JtObject tmp;
47
48
49     tmp = (JtObject) processMessage (new JtMessage ("JtCLONE"));
50
51     if (tmp == null)
52       return (null);
53
54     return (tmp.processMessage (new JtMessage ("JtPRINT_OBJECT")));
55   }
56
57   /**
58     * Process object messages.
59     * <ul>
60     * <li>JtCLONE - returns a clone of this object.
61     * </ul>
62     */

63
64   public Object JavaDoc processMessage (Object JavaDoc event) {
65
66    String JavaDoc msgid = null;
67    JtMessage e = (JtMessage) event;
68    Object JavaDoc content;
69    Object JavaDoc data;
70
71
72      if (e == null)
73     return null;
74
75      msgid = (String JavaDoc) e.getMsgId ();
76
77      if (msgid == null)
78     return null;
79
80      content = e.getMsgContent();
81      //data = e.getMsgData ();
82

83      //return (super.processMessage (event));
84

85      if (msgid.equals ("JtCLONE")) {
86        return (cloneObject ());
87      }
88  
89
90      if (msgid.equals ("JtPRINT_OBJECT")) {
91        return (super.processMessage (event));
92      }
93
94
95      if (msgid.equals ("JtTEST")) {
96        return (test ());
97      }
98
99
100      if (msgid.equals ("JtREMOVE")) {
101        return (null);
102      }
103
104
105      handleError ("JtPrototype.processMessage: invalid message id:" + msgid);
106      return (null);
107
108
109   }
110
111  
112   /**
113    * Unit tests the messages processed by JtPrototype
114    */

115
116   public static void main(String JavaDoc[] args) {
117
118     JtFactory factory = new JtFactory ();
119     JtPrototype proto;
120
121
122     // Create an instance of JtPrototype
123

124     proto = (JtPrototype) factory.createObject ("Jt.JtPrototype", "proto");
125
126
127     factory.sendMessage (proto, new JtMessage ("JtTEST"));
128
129     // Destroy the object
130

131     factory.removeObject (proto);
132
133
134   }
135
136 }
137
138
139
Popular Tags