1 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 { 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 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 ioe) { 82 DialogBox.error(plugin.tr("err.unableToBroadcastOperation") + ioe); 83 ioe.printStackTrace(); 84 } 85 } 86 } | Popular Tags |