comparison java/src/uk/ac/qmul/eecs/ccmi/gui/DiagramPanel.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78b7fc5391a2
1 /*
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
3
4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 package uk.ac.qmul.eecs.ccmi.gui;
22
23 import java.awt.BorderLayout;
24 import java.io.IOException;
25
26 import javax.swing.JPanel;
27 import javax.swing.JScrollPane;
28 import javax.swing.JSplitPane;
29 import javax.swing.event.ChangeEvent;
30 import javax.swing.event.ChangeListener;
31
32 import uk.ac.qmul.eecs.ccmi.gui.awareness.AwarenessPanel;
33 import uk.ac.qmul.eecs.ccmi.gui.awareness.DisplayFilter;
34 import uk.ac.qmul.eecs.ccmi.network.NetDiagram;
35
36 /**
37 * It's the panel which displays a diagram. It contains a {@link GraphPanel}, a {@link DiagramTree}
38 * a {@link GraphToolbar} and the {@code AwarenessPanel}.
39 * It's backed up by an instance of {@code Diagram}.
40 */
41 @SuppressWarnings("serial")
42 public class DiagramPanel extends JPanel{
43
44 /**
45 * Creates a new instance of {@code DiagramPanel} holding the diagram passed as argument.
46 *
47 * @param diagram the diagram this panel is backed up by
48 * @param tabbedPane the tabbed pane this DiagramPanel will be added to. This reference
49 * is used to updated the tab label when the diagram is modified or save (in the former
50 * case a star is added to the label, in the latter case the star is removed)
51 */
52 public DiagramPanel(Diagram diagram, EditorTabbedPane tabbedPane){
53 this.diagram = diagram;
54 this.tabbedPane = tabbedPane;
55
56 setName(diagram.getLabel());
57 setLayout(new BorderLayout());
58
59 modelChangeListener = new ChangeListener(){
60 @Override
61 public void stateChanged(ChangeEvent e) {
62 setModified(true);
63 }
64 };
65
66 toolbar = new GraphToolbar(diagram);
67 graphPanel = new GraphPanel(diagram, toolbar);
68 /* the focus must be hold by the tree and the tab panel only */
69 toolbar.setFocusable(false);
70 graphPanel.setFocusable(false);
71
72 tree = new DiagramTree(diagram);
73
74 /* the panel containing the graph and the toolbar and the awareness panel */
75 visualPanel = new JPanel(new BorderLayout());
76 visualPanel.add(toolbar, BorderLayout.NORTH);
77 visualPanel.add(new JScrollPane(graphPanel),BorderLayout.CENTER);
78 awarenessSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
79 awarenessSplitPane.setTopComponent(visualPanel);
80
81 /* divides the tree from the visual diagram */
82 JSplitPane treeSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
83 new JScrollPane(tree),
84 awarenessSplitPane);
85 treeSplitPane.setDividerLocation((int)tree.getPreferredSize().width*2);
86 add(treeSplitPane, BorderLayout.CENTER);
87 diagram.getCollectionModel().addChangeListener(modelChangeListener);
88 }
89
90 /**
91 * When a diagram is saved on the file system the its path is associated to the diagram panel
92 * and it's shown when the user hover on the its tab title.
93 *
94 * @return the path of the file where this diagram has been saved last time or {@code null}
95 */
96 public String getFilePath(){
97 return filePath;
98 }
99
100 /**
101 * Sets the file path to a new path. This method should be called after the backing diagram has
102 * been saved to a file.
103 *
104 * @param newValue the path of the file where the backing diagram has been saved last time
105 */
106 public void setFilePath(String newValue){
107 filePath = newValue;
108 }
109
110 /**
111 * Returns a reference to the backing diagram of this diagram panel.
112 *
113 * @return a reference to the backing diagram of this diagram panel
114 */
115 public Diagram getDiagram(){
116 return diagram;
117 }
118
119 /**
120 * Enables or disables the awareness panel of this diagram panel. As default the awareness panel
121 * is disabled but if the diagram is shared (either on a local or on a remote server) the awareness
122 * panel gets enabled. In fact, from now on, awareness messages will be received from the server and,
123 * even if the awareness panel is not visible, some messages (username messages)
124 * will still have to be taken into account.
125 *
126 * @param enabled {@code true} if the panel is to be enabled, {@code false} otherwise.
127 */
128 public void setAwarenessPanelEnabled(boolean enabled){
129 if(!(diagram instanceof NetDiagram))
130 return;
131 /* if the display filter has not been created yet, do create it */
132 DisplayFilter filter = DisplayFilter.getInstance();
133 if(filter == null)
134 try{
135 filter = DisplayFilter.createInstance();
136 }catch(IOException ioe){
137 SpeechOptionPane.showMessageDialog(this, ioe.getLocalizedMessage());
138 return;
139 }
140
141 NetDiagram netDiagram = (NetDiagram)diagram;
142 if(enabled){
143 awarenessPanel = new AwarenessPanel(diagram.getName());
144 awarenessPanelScrollPane = new JScrollPane(awarenessPanel);
145 netDiagram.enableAwareness(awarenessPanel);
146 if(awarenessPanelListener != null)
147 awarenessPanelListener.awarenessPanelEnabled(true);
148 }else{ //disabled
149 netDiagram.disableAwareness(awarenessPanel);
150 if(awarenessSplitPane.getRightComponent() != null){
151 // hide the panel
152 awarenessSplitPane.remove(awarenessPanelScrollPane);
153 }
154 awarenessPanelScrollPane = null;
155 awarenessPanel = null;
156 awarenessSplitPane.validate();
157 if(awarenessPanelListener != null)
158 awarenessPanelListener.awarenessPanelEnabled(false);
159 }
160 }
161
162 /**
163 * Makes the awareness panel visible or invisible, assuming that it has been enabled beforehand. If the
164 * awareness panel hasn't been enables this call has no effect.
165 *
166 * @param visible {@code true} if the panel is to be made visible, {@code false} otherwise.
167 */
168 public void setAwarenessPanelVisible(boolean visible){
169 if(awarenessPanelScrollPane == null)
170 return;
171 if(visible){
172 awarenessSplitPane.setRightComponent(awarenessPanelScrollPane);
173 awarenessSplitPane.setDividerLocation(0.8);
174 awarenessSplitPane.setResizeWeight(1.0);
175 awarenessSplitPane.validate();
176 if(awarenessPanelListener != null)
177 awarenessPanelListener.awarenessPanelVisible(true);
178 }else{
179 awarenessSplitPane.remove(awarenessPanelScrollPane);
180 awarenessSplitPane.validate();
181 if(awarenessPanelListener != null)
182 awarenessPanelListener.awarenessPanelVisible(false);
183 }
184 }
185
186 /**
187 * Queries the diagram panel on whether the awareness panel is currently visible.
188 *
189 * @return {@code true} if the awareness panel is currently visible, {@code false} otherwise.
190 */
191 public boolean isAwarenessPanelVisible(){
192 return (awarenessSplitPane.getRightComponent() != null);
193 }
194
195 /**
196 * Returns a reference to the inner awareness panel.
197 *
198 * @return the inner awareness panel if it has been enabled of {@code null} otherwise
199 */
200 public AwarenessPanel getAwarenessPanel(){
201 return awarenessPanel;
202 }
203
204 /**
205 * Sets the backing up delegate diagram for this panel. This method is used when a diagram is shared
206 * (or reverted). A shared diagram has a different way of updating the
207 * The modified status is changed according to
208 * the modified status of the {@code DiagramModel} internal to the new {@code Diagram}
209 *
210 * @param diagram the backing up delegate diagram
211 */
212 public void setDiagram(Diagram diagram){
213 /* remove the listener from the old model */
214 this.diagram.getCollectionModel().removeChangeListener(modelChangeListener);
215 diagram.getCollectionModel().addChangeListener(modelChangeListener);
216
217 this.diagram = diagram;
218 tree.setDiagram(diagram);
219 graphPanel.setModelUpdater(diagram.getModelUpdater());
220 setName(diagram.getLabel());
221 /* set the * according to the new diagram's model modification status */
222 setModified(isModified());
223 }
224
225 /**
226 * Returns a reference to the graph panel in this diagram panel. The tree's model is the
227 * model returned by a calling {@code getTreeModel()} on the backing diagram.
228 *
229 * @return the graph panel contained by this diagram panel
230 */
231 public GraphPanel getGraphPanel(){
232 return graphPanel;
233 }
234
235 /**
236 * Returns a reference to the tree in this diagram panel. The graph model is the
237 * model returned by a calling {@code getCollectionModel()} on the backing diagram.
238 *
239 * @return the tree contained by this diagram panel
240 */
241 public DiagramTree getTree(){
242 return tree;
243 }
244
245 /**
246 * Changes the {@code modified} status of the backing diagram of this panel. If set to {@code true}
247 * then a star will appear after the name of the diagram, returned by {@code getName()}.
248 *
249 * When called passing false as argument (which should be done after the diagram is saved on a file)
250 * listeners are notified that the diagram has been saved.
251 *
252 * @param modified {@code true} when the diagram has been modified, {@code false} when it has been saved
253 */
254 public void setModified(boolean modified){
255 if(!modified)
256 diagram.getCollectionModel().setUnmodified();
257 /* add an asterisk to notify that the diagram has changed */
258 if(modified)
259 setName(getName()+"*");
260 else
261 setName(diagram.getLabel());
262 tabbedPane.refreshComponentTabTitle(this);
263 }
264
265 /**
266 * Whether the backing diagram has been modified. The diagram is modified as a result of changes
267 * to the {@code TreeModel} or {@code CollectionModel} it contains. To change the {@code modified}
268 * status of the diagram (and of its models) {@code setModified()} must be used.
269 *
270 * @return {@code true} if the diagram is modified, {@code false} otherwise
271 */
272 public boolean isModified(){
273 return diagram.getCollectionModel().isModified();
274 }
275
276 void setAwarenessPanelListener(AwarenessPanelEnablingListener listener){
277 awarenessPanelListener = listener;
278 }
279
280 private Diagram diagram;
281 private GraphPanel graphPanel;
282 private JSplitPane awarenessSplitPane;
283 private DiagramTree tree;
284 private JPanel visualPanel;
285 private GraphToolbar toolbar;
286 private AwarenessPanel awarenessPanel;
287 private JScrollPane awarenessPanelScrollPane;
288 private String filePath;
289 private ChangeListener modelChangeListener;
290 private EditorTabbedPane tabbedPane;
291 private AwarenessPanelEnablingListener awarenessPanelListener;
292 }
293
294 interface AwarenessPanelEnablingListener {
295 public void awarenessPanelEnabled(boolean enabled);
296 public void awarenessPanelVisible(boolean visible);
297 }
298