KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > patterns > Visitable


1 package Jt.examples.patterns;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6 import Jt.*;
7
8
9 /**
10  * Demonstrates the use of the Visitor pattern.
11  */

12
13
14 public class Visitable extends JtObject {
15
16
17   public Visitable() {
18   }
19
20
21   /**
22     * Process object messages.
23     * <ul>
24     * <li>JtACCEPT - Accept operation
25     * </ul>
26     * @param event Jt Message
27     */

28
29   public Object JavaDoc processMessage (Object JavaDoc event) {
30
31    String JavaDoc msgid = null;
32    JtMessage e = (JtMessage) event;
33    Object JavaDoc content;
34    Object JavaDoc data;
35    JtMessage aux;
36
37      if (e == null)
38     return null;
39
40      msgid = (String JavaDoc) e.getMsgId ();
41
42      if (msgid == null)
43     return null;
44
45      content = e.getMsgContent();
46
47      if (msgid.equals ("JtREMOVE")) {
48        return (this);
49      }
50
51      if (msgid.equals ("JtACCEPT")) {
52
53        if (content == null) {
54          handleError ("processMessage: invalid ACCEPT message; visitor reference is null");
55          return (null);
56        }
57
58        // Send a JtVISIT message to the visitor.
59
// Include a reference to this object
60

61        aux = new JtMessage ("JtVISIT");
62        aux.setMsgContent (this);
63        return (sendMessage (content, aux));
64      }
65
66
67      handleError ("Visitable.processMessage: invalid message id:" + msgid);
68      return (null);
69
70   }
71
72
73 }
74
Popular Tags