01: /*
02: * Lucane - a collaborative platform
03: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19: package org.lucane.applications.whiteboard.listeners;
20:
21: import java.io.IOException;
22:
23: import org.lucane.applications.whiteboard.WhiteBoard;
24: import org.lucane.applications.whiteboard.net.BoardClient;
25: import org.lucane.applications.whiteboard.operations.*;
26: import org.lucane.client.Client;
27: import org.lucane.client.widgets.DialogBox;
28:
29: import org.jgraph.event.*;
30: import org.jgraph.graph.GraphModel;
31:
32: public class NetworkGraphListener implements GraphModelListener {
33: private WhiteBoard plugin;
34: private BoardClient client;
35:
36: public NetworkGraphListener(WhiteBoard plugin, BoardClient client) {
37: this .plugin = plugin;
38: this .client = client;
39: }
40:
41: public void graphChanged(GraphModelEvent gme) {
42: //ensure we are the source
43: if (!plugin.getGraph().isEnabled())
44: return;
45:
46: GraphModel model = (GraphModel) gme.getSource();
47:
48: if (isInsert(gme.getChange()))
49: send(gme, new GraphInsert());
50:
51: else if (gme.getChange().getRemoved() != null
52: && gme.getChange().getRemoved().length > 0)
53: send(gme, new GraphRemove());
54:
55: else if (gme.getChange().getChanged() != null
56: && gme.getChange().getChanged().length > 0
57: && gme.getChange().getChanged()[0] != null)
58: send(gme, new GraphEdit());
59: }
60:
61: private boolean isInsert(GraphModelEvent.GraphModelChange change) {
62: if (change.getInserted() == null)
63: return false;
64: if (change.getInserted().length < 1)
65: return false;
66: if (change.getPreviousParentMap() == null)
67: return true;
68: if (change.getPreviousParentMap().size() > change.getInserted().length)
69: return false;
70:
71: return true;
72: }
73:
74: private void send(GraphModelEvent gme, GraphOperation op) {
75: op.init(gme);
76: try {
77: client.broadcastOperation(Client.getInstance().getMyInfos()
78: .getName(), op);
79: } catch (IOException ioe) {
80: DialogBox.error(plugin.tr("err.unableToBroadcastOperation")
81: + ioe);
82: ioe.printStackTrace();
83: }
84: }
85: }
|