1 2 3 package Jt; 4 import java.util.*; 5 import java.lang.reflect.*; 6 import java.beans.*; 7 import java.io.*; 8 9 12 13 public class JtObservable extends JtComposite { 14 15 16 public JtObservable () { 17 } 18 19 20 30 31 public Object processMessage (Object event) { 32 33 String msgid = null; 34 JtMessage e = (JtMessage) event; 35 Object content; 36 Object data; 37 JtMessage tmp, tmp1; 38 39 if (e == null) 40 return null; 41 42 msgid = (String ) e.getMsgId (); 43 44 if (msgid == null) 45 return null; 46 47 content = e.getMsgContent(); 48 50 51 if (msgid.equals ("JtADD_OBSERVER")) { 52 tmp = new JtMessage ("JtADD_CHILD"); 53 tmp.setMsgData (content); 54 tmp.setMsgContent (content); 55 return (super.processMessage (tmp)); 56 } 57 58 if (msgid.equals ("JtREMOVE_OBSERVER")) { 59 tmp = new JtMessage ("JtREMOVE_CHILD"); 60 tmp.setMsgData (content); 61 tmp.setMsgContent (content); 62 return (super.processMessage (tmp)); 63 } 64 65 66 if (msgid.equals ("JtNOTIFY_OBSERVERS")) { 67 tmp = new JtMessage ("JtBROADCAST"); 68 69 tmp1 = new JtMessage ("JtNOTIFY_OBSERVER"); 70 tmp1.setMsgContent (this); 71 tmp.setMsgContent (tmp1); 72 return (super.processMessage (tmp)); 73 } 74 75 76 if (msgid.equals ("JtREMOVE")) { 77 return (null); 78 } 79 80 81 handleError ("JtObserver.processMessage: invalid message id:" + msgid); 82 return (null); 83 84 } 85 86 87 90 91 public static void main(String [] args) { 92 93 JtFactory main = new JtFactory (); 94 JtObservable observable; 95 JtMessage msg; 96 JtEcho echo1, echo2; 97 98 99 101 observable = (JtObservable) main.createObject ("Jt.JtObservable", "observable"); 102 echo1 = (JtEcho) main.createObject ("Jt.JtEcho", "echo1"); 103 echo2 = (JtEcho) main.createObject ("Jt.JtEcho", "echo2"); 104 105 System.out.println ("JtObservable(JtADD_OBSERVER): adding an observer ..."); 106 107 msg = new JtMessage ("JtADD_OBSERVER"); 108 msg.setMsgContent (echo1); 109 110 main.sendMessage (observable, msg); 111 112 113 System.out.println ("JtObservable(JtADD_OBSERVER): adding an observer ..."); 114 115 msg = new JtMessage ("JtADD_OBSERVER"); 116 msg.setMsgContent (echo2); 117 118 main.sendMessage (observable, msg); 119 120 System.out.println ("JtObservable(JtNOTIFY_OBSERVERS): notifying observers ..."); 121 122 msg = new JtMessage ("JtNOTIFY_OBSERVERS"); 123 main.sendMessage (observable, msg); 124 125 126 main.removeObject (observable); 127 128 129 } 130 131 } 132 133 134
| Popular Tags
|