1 19 package org.lucane.applications.whiteboard; 20 21 import java.io.IOException ; 22 23 import org.jgraph.graph.DefaultGraphModel; 24 import org.jgraph.graph.GraphModel; 25 import org.lucane.applications.whiteboard.graph.MyGraph; 26 import org.lucane.applications.whiteboard.net.BoardClient; 27 import org.lucane.applications.whiteboard.net.BoardServer; 28 import org.lucane.client.Client; 29 import org.lucane.client.Communicator; 30 import org.lucane.client.Plugin; 31 import org.lucane.client.StandalonePlugin; 32 import org.lucane.client.widgets.DialogBox; 33 import org.lucane.common.*; 34 import org.lucane.common.net.ObjectConnection; 35 36 public class WhiteBoard extends StandalonePlugin 37 { 38 private ConnectInfo[] friends; 39 private ConnectInfo host; 40 private BoardClient client; 41 private GraphGui gui; 42 private int boardPort; 43 private boolean server; 44 45 46 public WhiteBoard() 47 { 48 } 50 51 public Plugin newInstance(ConnectInfo[] friends) 52 { 53 return new WhiteBoard(friends); 54 } 55 56 public WhiteBoard(ConnectInfo[] friends) 57 { 58 this.friends = friends; 59 } 60 61 public void load(ObjectConnection friend, ConnectInfo who, String data) 62 { 63 friend.close(); 64 this.host = who; 65 this.client = new BoardClient(who.getHostName(), Integer.parseInt(data)); 66 } 67 68 public void start() 69 { 70 this.server = true; 71 72 int boardPort = 0; 73 try { 74 boardPort = startServer(); 75 } catch(IOException ioe) { 76 DialogBox.error(tr("err.unableToStartServer") + ioe); 77 return; 78 } 79 this.boardPort = boardPort; 80 81 ConnectInfo my = Client.getInstance().getMyInfos(true); 82 this.client = new BoardClient(my.getHostName(), boardPort); 83 startClient(); 84 85 sendInvitations(friends); 86 } 87 88 public void follow() 89 { 90 this.server = false; 91 92 String msg = tr("msg.joinBoardSession").replaceAll("%1", host.getName()); 93 if(DialogBox.question(getTitle(), msg)) 94 startClient(); 95 } 96 97 public boolean isServer() 98 { 99 return this.server; 100 } 101 102 private int startServer() 103 throws IOException 104 { 105 BoardServer server = new BoardServer(this); 106 server.listen(); 107 108 Logging.getLogger().fine("board server started"); 109 return server.getPort(); 110 } 111 112 private void startClient() 113 { 114 gui = new GraphGui(this, client); 115 this.client.setGui(gui); 116 117 try { 118 this.client.start(); 119 this.client.joinBoard(Client.getInstance().getMyInfos().getName()); 120 Logging.getLogger().fine("board client started"); 121 122 gui.showWindow(this, getTitle()); 123 } catch (Exception e) { 124 DialogBox.error(tr("err.unableToStartClient") + e); 125 } 126 } 127 128 public void sendInvitations(ConnectInfo[] friends) 129 { 130 for(int i=0;i<friends.length;i++) 131 Communicator.getInstance().sendMessageTo(friends[i], getName(), String.valueOf(boardPort)); 132 } 133 134 public BoardClient getClient() 135 { 136 return this.client; 137 } 138 139 public MyGraph getGraph() 140 { 141 return this.gui.getGraph(); 142 } 143 144 public DefaultGraphModel getGraphModel() 145 { 146 return this.gui.getModel(); 147 } 148 149 public void setGraphModel(GraphModel model) 150 { 151 152 try { 153 this.gui.setModel(model); 154 this.client.broadcastModel(Client.getInstance().getMyInfos().getName(), this.gui.getModel()); 155 } catch(IOException ioe) { 156 DialogBox.error(tr("err.unableToBroadcastOperation") + ioe); 157 } 158 } 159 } 160 | Popular Tags |