KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > listeners > NetworkGraphListener


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 JavaDoc;
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 {
34     private WhiteBoard plugin;
35     private BoardClient client;
36
37     public NetworkGraphListener(WhiteBoard plugin, BoardClient client)
38     {
39         this.plugin = plugin;
40         this.client = client;
41     }
42     
43     public void graphChanged(GraphModelEvent gme)
44     {
45         //ensure we are the source
46
if(!plugin.getGraph().isEnabled())
47             return;
48         
49         GraphModel model = (GraphModel)gme.getSource();
50                 
51         if(isInsert(gme.getChange()))
52             send(gme, new GraphInsert());
53
54         else if(gme.getChange().getRemoved() != null && gme.getChange().getRemoved().length > 0)
55             send(gme, new GraphRemove());
56
57         else if(gme.getChange().getChanged() != null && gme.getChange().getChanged().length > 0
58                 && gme.getChange().getChanged()[0] != null)
59             send(gme, new GraphEdit());
60     }
61
62     private boolean isInsert(GraphModelEvent.GraphModelChange change)
63     {
64         if(change.getInserted() == null)
65             return false;
66         if(change.getInserted().length < 1)
67             return false;
68         if(change.getPreviousParentMap() == null)
69             return true;
70         if(change.getPreviousParentMap().size() > change.getInserted().length)
71             return false;
72             
73         return true;
74     }
75     
76     private void send(GraphModelEvent gme, GraphOperation op)
77     {
78         op.init(gme);
79         try {
80             client.broadcastOperation(Client.getInstance().getMyInfos().getName(), op);
81         } catch(IOException JavaDoc ioe) {
82             DialogBox.error(plugin.tr("err.unableToBroadcastOperation") + ioe);
83             ioe.printStackTrace();
84         }
85     }
86 }
Popular Tags