1 19 package org.lucane.applications.whiteboard; 20 21 import java.awt.*; 22 import java.util.ArrayList ; 23 24 import javax.swing.*; 25 import javax.swing.border.Border ; 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 drawBorder = BorderFactory.createLineBorder(Color.RED, 2); 45 private static final Border 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 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 132 public void changeUserList(ArrayList users) 133 { 134 this.userList.setListData(users.toArray()); 135 } 136 137 public void changePenOwner(String 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 [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 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 [0]); 189 graph.setBorder(GraphGui.viewBorder); 190 graph.setEnabled(false); 191 shapeToolBar.setEnabled(false); 192 actionToolBar.setEnabled(false); 193 } 194 } 195 | Popular Tags |