001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.whiteboard;
020:
021: import java.io.IOException;
022:
023: import org.jgraph.graph.DefaultGraphModel;
024: import org.jgraph.graph.GraphModel;
025: import org.lucane.applications.whiteboard.graph.MyGraph;
026: import org.lucane.applications.whiteboard.net.BoardClient;
027: import org.lucane.applications.whiteboard.net.BoardServer;
028: import org.lucane.client.Client;
029: import org.lucane.client.Communicator;
030: import org.lucane.client.Plugin;
031: import org.lucane.client.StandalonePlugin;
032: import org.lucane.client.widgets.DialogBox;
033: import org.lucane.common.*;
034: import org.lucane.common.net.ObjectConnection;
035:
036: public class WhiteBoard extends StandalonePlugin {
037: private ConnectInfo[] friends;
038: private ConnectInfo host;
039: private BoardClient client;
040: private GraphGui gui;
041: private int boardPort;
042: private boolean server;
043:
044: public WhiteBoard() {
045: //nothing
046: }
047:
048: public Plugin newInstance(ConnectInfo[] friends) {
049: return new WhiteBoard(friends);
050: }
051:
052: public WhiteBoard(ConnectInfo[] friends) {
053: this .friends = friends;
054: }
055:
056: public void load(ObjectConnection friend, ConnectInfo who,
057: String data) {
058: friend.close();
059: this .host = who;
060: this .client = new BoardClient(who.getHostName(), Integer
061: .parseInt(data));
062: }
063:
064: public void start() {
065: this .server = true;
066:
067: int boardPort = 0;
068: try {
069: boardPort = startServer();
070: } catch (IOException ioe) {
071: DialogBox.error(tr("err.unableToStartServer") + ioe);
072: return;
073: }
074: this .boardPort = boardPort;
075:
076: ConnectInfo my = Client.getInstance().getMyInfos(true);
077: this .client = new BoardClient(my.getHostName(), boardPort);
078: startClient();
079:
080: sendInvitations(friends);
081: }
082:
083: public void follow() {
084: this .server = false;
085:
086: String msg = tr("msg.joinBoardSession").replaceAll("%1",
087: host.getName());
088: if (DialogBox.question(getTitle(), msg))
089: startClient();
090: }
091:
092: public boolean isServer() {
093: return this .server;
094: }
095:
096: private int startServer() throws IOException {
097: BoardServer server = new BoardServer(this );
098: server.listen();
099:
100: Logging.getLogger().fine("board server started");
101: return server.getPort();
102: }
103:
104: private void startClient() {
105: gui = new GraphGui(this , client);
106: this .client.setGui(gui);
107:
108: try {
109: this .client.start();
110: this .client.joinBoard(Client.getInstance().getMyInfos()
111: .getName());
112: Logging.getLogger().fine("board client started");
113:
114: gui.showWindow(this , getTitle());
115: } catch (Exception e) {
116: DialogBox.error(tr("err.unableToStartClient") + e);
117: }
118: }
119:
120: public void sendInvitations(ConnectInfo[] friends) {
121: for (int i = 0; i < friends.length; i++)
122: Communicator.getInstance().sendMessageTo(friends[i],
123: getName(), String.valueOf(boardPort));
124: }
125:
126: public BoardClient getClient() {
127: return this .client;
128: }
129:
130: public MyGraph getGraph() {
131: return this .gui.getGraph();
132: }
133:
134: public DefaultGraphModel getGraphModel() {
135: return this .gui.getModel();
136: }
137:
138: public void setGraphModel(GraphModel model) {
139:
140: try {
141: this .gui.setModel(model);
142: this .client.broadcastModel(Client.getInstance()
143: .getMyInfos().getName(), this .gui.getModel());
144: } catch (IOException ioe) {
145: DialogBox.error(tr("err.unableToBroadcastOperation") + ioe);
146: }
147: }
148: }
|