001: // $Id: DrawRepl.java,v 1.7 2005/07/17 11:36:42 chrislott Exp $
002:
003: package org.jgroups.demos;
004:
005: import org.jgroups.Channel;
006: import org.jgroups.JChannel;
007: import org.jgroups.blocks.GroupRequest;
008: import org.jgroups.blocks.MethodCall;
009: import org.jgroups.blocks.RpcDispatcher;
010:
011: import java.awt.*;
012: import java.awt.event.*;
013: import java.io.ByteArrayInputStream;
014: import java.io.ByteArrayOutputStream;
015: import java.io.DataInputStream;
016: import java.io.DataOutputStream;
017: import java.util.Hashtable;
018: import java.util.Random;
019:
020: /**
021: *
022: * Replicates the whiteboard demo by intercepting central AWT event queue and mcasting events to
023: * all members. Not very useful in all cases, e.g. when the "Leave" button is pressed, and this event
024: * is broadcast to all members, all members will leave ! This demo would clearly benefit from more work !
025: * NOT SUPPORTED !
026: */
027: public class DrawRepl implements MouseMotionListener, WindowListener,
028: ActionListener, Runnable {
029: private Graphics graphics = null;
030: private Frame mainFrame = null;
031: private Panel panel = null, sub_panel = null;
032: private final byte[] buf = new byte[128];
033: private final ByteArrayOutputStream out = new ByteArrayOutputStream();
034: private DataOutputStream outstream;
035: private ByteArrayInputStream inp;
036: private DataInputStream instream;
037: private int x, y;
038: private final Hashtable colors = new Hashtable();
039: private final Random random = new Random(System.currentTimeMillis());
040: private int col_val = 1;
041: private Color current_color = Color.red;
042: private Button clear_button, leave_button;
043: private final String groupname = "DrawReplGroup";
044: private final Font default_font = new Font("Helvetica", Font.PLAIN,
045: 12);
046:
047: private EventQueue event_queue = null;
048: private Thread mythread = null;
049: private RpcDispatcher dispatcher;
050: private Channel channel;
051:
052: public DrawRepl() {
053: colors.put(new Integer(1), Color.white);
054: colors.put(new Integer(2), Color.black);
055: colors.put(new Integer(3), Color.red);
056: colors.put(new Integer(4), Color.orange);
057: colors.put(new Integer(5), Color.green);
058: colors.put(new Integer(6), Color.magenta);
059: colors.put(new Integer(7), Color.cyan);
060: colors.put(new Integer(8), Color.blue);
061: mythread = new Thread(this );
062: try {
063: channel = new JChannel();
064: dispatcher = new RpcDispatcher(channel, null, null, this );
065: channel.connect(groupname);
066: } catch (Exception e) {
067: System.err.println(e);
068: System.exit(0);
069: }
070: }
071:
072: public static void main(String[] args) {
073: DrawRepl draw = new DrawRepl();
074: draw.go();
075: }
076:
077: private Color SelectColor() {
078: col_val = (Math.abs(random.nextInt()) % 8) + 1;
079: Color ret = (Color) colors.get(new Integer(col_val));
080: if (ret == null)
081: ret = Color.red;
082: return ret;
083: }
084:
085: AWTEvent copyEvent(Component src, AWTEvent evt) {
086:
087: if (evt instanceof MouseEvent) {
088: MouseEvent mev = (MouseEvent) evt;
089: return new MouseEvent(src, evt.getID(), mev.getWhen(), mev
090: .getModifiers(), mev.getX(), mev.getY(), mev
091: .getClickCount(), mev.isPopupTrigger());
092: }
093:
094: if (evt instanceof KeyEvent) {
095: KeyEvent kev = (KeyEvent) evt;
096: return new KeyEvent(src, evt.getID(), kev.getWhen(), kev
097: .getModifiers(), kev.getKeyCode(), kev.getKeyChar());
098: }
099:
100: if (evt instanceof ActionEvent)
101: return new ActionEvent(src, evt.getID(),
102: ((ActionEvent) evt).getActionCommand(),
103: ((ActionEvent) evt).getModifiers());
104:
105: if (evt instanceof PaintEvent)
106: return new PaintEvent(src, evt.getID(), ((PaintEvent) evt)
107: .getUpdateRect());
108:
109: if (evt instanceof FocusEvent)
110: return new FocusEvent(src, evt.getID(), ((FocusEvent) evt)
111: .isTemporary());
112:
113: if (evt instanceof ComponentEvent)
114: return new ComponentEvent(src, evt.getID());
115:
116: return null;
117: }
118:
119: void dispatch(Object src, AWTEvent evt) {
120: if (src instanceof Component)
121: ((Component) src).dispatchEvent(evt);
122: else if (src instanceof MenuComponent)
123: ((MenuComponent) src).dispatchEvent(evt);
124: else
125: System.err.println("++++++++++");
126: }
127:
128: public Component findComponent(Container parent, String comp_name) {
129: Component retval = null;
130:
131: if (comp_name != null && comp_name.equals(parent.getName()))
132: return parent;
133:
134: int ncomponents = parent.getComponentCount();
135: Component components[] = parent.getComponents();
136: for (int i = ncomponents - 1; i >= 0; i--) {
137: Component comp = components[i], tmp;
138: if (comp != null) {
139: if (comp instanceof Container) {
140: retval = findComponent((Container) comp, comp_name);
141: if (retval != null)
142: return retval;
143: } else if (comp_name.equals(comp.getName()))
144: return comp;
145: }
146: }
147: return retval;
148: }
149:
150: // public void setSize(Integer x, Integer y) {
151: // mainFrame.setSize(new Dimension(x.intValue(), y.intValue()));
152: // }
153:
154: /* Called by Dispatcher */
155: public void processEvent(String comp_name, AWTEvent evt) {
156: AWTEvent copy_evt = null;
157: Component src = findComponent(mainFrame, comp_name);
158: if (src == null) {
159: System.err.println("processEvent(): src is null");
160: return;
161: }
162:
163: System.out.println("Received " + evt.getClass().getName());
164:
165: copy_evt = copyEvent(src, evt);
166: if (copy_evt == null) {
167: System.err.println("copy_evt is NULL");
168: return;
169: }
170: dispatch(src, copy_evt);
171:
172: // if(evt instanceof ComponentEvent && evt.getID() == ComponentEvent.COMPONENT_RESIZED) {
173: // Dimension dim=mainFrame.getSize();
174: // try {
175: // dispatcher.sendGetN(groupname, "setSize", new Integer(dim.height),
176: // new Integer(dim.width), 0, 0);
177: // }
178: // catch(Exception e) {
179: // System.err.println(e);
180: // }
181: // }
182: }
183:
184: void processLocally(AWTEvent evt) {
185: dispatch(evt.getSource(), evt);
186: }
187:
188: public void run() {
189: String comp_name;
190:
191: while (true) {
192: try {
193: AWTEvent evt = event_queue.getNextEvent();
194: Object obj = evt.getSource();
195: if (obj == null) {
196: System.err.println("src is NULL");
197: continue;
198: }
199:
200: if (obj instanceof Component)
201: comp_name = ((Component) obj).getName();
202: else if (obj instanceof MenuComponent)
203: comp_name = ((MenuComponent) obj).getName();
204: else {
205: System.err.println("src is of type "
206: + obj.getClass().getName());
207: continue;
208: }
209:
210: if (evt instanceof FocusEvent
211: || evt instanceof PaintEvent) {
212: System.out.println(evt.getClass().getName()
213: + " not copied");
214: processLocally(evt);
215: continue;
216: }
217: System.out.println("MCasting "
218: + evt.getClass().getName() + " event...");
219: MethodCall call = new MethodCall("processEvent",
220: new Object[] { comp_name, evt }, new String[] {
221: String.class.getName(),
222: AWTEvent.class.getName() });
223: dispatcher.callRemoteMethods(null, call,
224: GroupRequest.GET_NONE, 0);
225: } catch (Exception e) {
226: System.err.println(e);
227: }
228: }
229: }
230:
231: public void go() {
232: mainFrame = new Frame();
233: panel = new Panel();
234: sub_panel = new Panel();
235:
236: event_queue = mainFrame.getToolkit().getSystemEventQueue();
237: mythread.start();
238:
239: mainFrame.setSize(200, 200);
240: mainFrame.add("Center", panel);
241: clear_button = new Button("Clear");
242: clear_button.setFont(default_font);
243: clear_button.addActionListener(this );
244: leave_button = new Button("Exit");
245: leave_button.setFont(default_font);
246: leave_button.addActionListener(this );
247: sub_panel.add("South", clear_button);
248: sub_panel.add("South", leave_button);
249: mainFrame.add("South", sub_panel);
250:
251: mainFrame.addWindowListener(this );
252: // mainFrame.addComponentListener(this);
253:
254: panel.addMouseMotionListener(this );
255:
256: mainFrame.setVisible(true);
257:
258: graphics = panel.getGraphics();
259: current_color = SelectColor();
260: if (current_color == null)
261: current_color = Color.red;
262: graphics.setColor(current_color);
263: }
264:
265: /* --------------- Callbacks --------------- */
266:
267: public void mouseMoved(MouseEvent e) {
268: }
269:
270: public void mouseDragged(MouseEvent e) {
271: x = e.getX();
272: y = e.getY();
273: graphics.fillOval(x, y, 10, 10);
274: }
275:
276: public void clearPanel() {
277:
278: System.out.println("CLEAR");
279:
280: Rectangle bounds = panel.getBounds();
281: graphics.clearRect(0, 0, bounds.width, bounds.height);
282: }
283:
284: public void windowActivated(WindowEvent e) {
285: }
286:
287: public void windowClosed(WindowEvent e) {
288: }
289:
290: public void windowClosing(WindowEvent e) {
291: System.exit(0);
292: }
293:
294: public void windowDeactivated(WindowEvent e) {
295: }
296:
297: public void windowDeiconified(WindowEvent e) {
298: }
299:
300: public void windowIconified(WindowEvent e) {
301: }
302:
303: public void windowOpened(WindowEvent e) {
304: }
305:
306: // public void componentResized(ComponentEvent e) {
307: // System.out.println("RESIZED, size is " + mainFrame.getBounds());
308: // }
309:
310: // public void componentMoved(ComponentEvent e) {
311: // System.out.println("MOVED, location is: " + mainFrame.getLocation());
312: // }
313:
314: // public void componentShown(ComponentEvent e) {}
315:
316: // public void componentHidden(ComponentEvent e) {}
317:
318: public void actionPerformed(ActionEvent e) {
319: String command = e.getActionCommand();
320: if ("Clear".equals(command))
321: clearPanel();
322: else if ("Exit".equals(command)) {
323: mainFrame.setVisible(false);
324: System.exit(0);
325: } else
326: System.out.println("Unknown action");
327: }
328:
329: }
|