KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > GraphGui


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;
20
21 import java.awt.*;
22 import java.util.ArrayList JavaDoc;
23
24 import javax.swing.*;
25 import javax.swing.border.Border JavaDoc;
26 import org.jgraph.graph.*;
27 import org.lucane.applications.whiteboard.graph.ModelUtils;
28 import org.lucane.applications.whiteboard.graph.MyGraph;
29 import org.lucane.applications.whiteboard.graph.MyMarqueeHandler;
30 import org.lucane.applications.whiteboard.graph.shapes.ShapeManager;
31 import org.lucane.applications.whiteboard.gui.ActionToolBar;
32 import org.lucane.applications.whiteboard.gui.InviteAction;
33 import org.lucane.applications.whiteboard.gui.PenButton;
34 import org.lucane.applications.whiteboard.gui.ShapeToolBar;
35 import org.lucane.applications.whiteboard.listeners.*;
36 import org.lucane.applications.whiteboard.net.BoardClient;
37 import org.lucane.applications.whiteboard.operations.GraphOperation;
38 import org.lucane.client.Client;
39 import org.lucane.client.widgets.DialogBox;
40 import org.lucane.client.widgets.ManagedWindow;
41
42 public class GraphGui
43 {
44     private static final Border JavaDoc drawBorder = BorderFactory.createLineBorder(Color.RED, 2);
45     private static final Border JavaDoc viewBorder = BorderFactory.createLineBorder(Color.BLACK, 2);
46     
47     private WhiteBoard plugin;
48     private BoardClient client;
49     
50     private DefaultGraphModel model;
51     private MyGraph graph;
52     private NetworkGraphListener graphListener;
53     private NetworkSelectionListener selectionListener;
54     
55     private ShapeManager shapeManager;
56     private ActionToolBar actionToolBar;
57     private ShapeToolBar shapeToolBar;
58     private JList userList;
59     private PenButton penButton;
60
61     public GraphGui(WhiteBoard plugin, BoardClient client)
62     {
63         this.plugin = plugin;
64         this.client = client;
65         this.model = new DefaultGraphModel();
66         this.graph = new MyGraph(model);
67         this.shapeManager = new ShapeManager(plugin);
68         this.actionToolBar = new ActionToolBar(plugin);
69         this.shapeToolBar = new ShapeToolBar(plugin, shapeManager);
70
71         this.graph.setMarqueeHandler(new MyMarqueeHandler(graph, shapeToolBar, shapeManager));
72
73         
74         this.userList = new JList();
75         this.penButton = new PenButton(plugin);
76         this.penButton.addActionListener(new PenActionListener(plugin, client));
77         
78         this.graphListener = new NetworkGraphListener(plugin, client);
79         this.selectionListener = new NetworkSelectionListener(plugin, this, client);
80         this.setGraphAsDest();
81     }
82     
83     public void showWindow(WhiteBoard plugin, String JavaDoc title)
84     {
85         ManagedWindow win = new ManagedWindow(plugin, title);
86         win.getContentPane().add(new JScrollPane(graph), BorderLayout.CENTER);
87
88         JPanel toolbars = new JPanel(new GridLayout(2, 1));
89         toolbars.add(actionToolBar);
90         toolbars.add(shapeToolBar);
91         win.getContentPane().add(toolbars, BorderLayout.NORTH);
92         
93         JPanel panel = new JPanel(new BorderLayout());
94         if(plugin.isServer())
95             panel.add(new JButton(new InviteAction(plugin)), BorderLayout.NORTH);
96         panel.add(new JScrollPane(userList), BorderLayout.CENTER);
97         panel.add(penButton, BorderLayout.SOUTH);
98         panel.setPreferredSize(new Dimension(110, 600));
99         
100         win.getContentPane().add(panel, BorderLayout.EAST);
101         win.setPreferredSize(new Dimension(600, 600));
102         
103         win.addWindowListener(new GraphWindowListener(plugin, this, client));
104         win.setExitPluginOnClose(true);
105         win.show();
106     }
107
108     public MyGraph getGraph()
109     {
110         return this.graph;
111     }
112     
113     public DefaultGraphModel getModel()
114     {
115         return this.model;
116     }
117
118     public void setModel(GraphModel model)
119     {
120         this.model = (DefaultGraphModel)model;
121         ModelUtils.removeListeners(model,
122                 this.model.getGraphModelListeners(), this.model.getUndoableEditListeners());
123         
124         this.graph.setModel(this.model);
125         if(graph.isEnabled())
126             model.addGraphModelListener(graphListener);
127         this.graph.repaint();
128     }
129     
130     //-- used by client
131

132     public void changeUserList(ArrayList JavaDoc users)
133     {
134         this.userList.setListData(users.toArray());
135     }
136     
137     public void changePenOwner(String JavaDoc user, int queueSize)
138     {
139         penButton.setQueueSize(0);
140         if(user != null && user.equals(Client.getInstance().getMyInfos().getName()))
141         {
142             penButton.setState(PenButton.STATE_ACTIVE);
143             penButton.setUser(null);
144             penButton.setQueueSize(queueSize);
145             setGraphAsSource();
146         }
147         else
148         {
149             penButton.setUser(user);
150             setGraphAsDest();
151         }
152     }
153     
154     public void serverExit()
155     {
156         DialogBox.info(plugin.tr("msg.serverExited"));
157         penButton.setState(PenButton.STATE_INACTIVE);
158         penButton.setUser(null);
159         penButton.setEnabled(false);
160         userList.setListData(new Object JavaDoc[0]);
161         setGraphAsSource();
162         graph.removeGraphSelectionListener(selectionListener);
163         model.removeGraphModelListener(graphListener);
164     }
165     
166     public void applyGraphOperation(GraphOperation operation)
167     {
168         operation.apply(graph);
169     }
170     
171     //-- end used by client
172

173     public void setGraphAsSource()
174     {
175       graph.resetUndoManager();
176       model.addGraphModelListener(graphListener);
177       graph.addGraphSelectionListener(selectionListener);
178       graph.setBorder(GraphGui.drawBorder);
179       graph.setEnabled(true);
180       shapeToolBar.setEnabled(true);
181       actionToolBar.setEnabled(true);
182     }
183     
184     public void setGraphAsDest()
185     {
186       model.removeGraphModelListener(graphListener);
187       graph.removeGraphSelectionListener(selectionListener);
188       graph.setSelectionCells(new Object JavaDoc[0]);
189       graph.setBorder(GraphGui.viewBorder);
190       graph.setEnabled(false);
191       shapeToolBar.setEnabled(false);
192       actionToolBar.setEnabled(false);
193     }
194 }
195
Popular Tags