View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client.ui;
22  
23  import java.awt.event.ActionListener;
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.Observable;
30  import java.util.Observer;
31  
32  import javax.swing.JButton;
33  import javax.swing.JInternalFrame;
34  import javax.swing.JPanel;
35  import javax.swing.JTabbedPane;
36  
37  import org.vrjuggler.vrjconfig.PopupButton;
38  
39  import coras.common.CorasElement;
40  import coras.common.CorasRepresentation;
41  import coras.repository.interfaces.CorasServices;
42  import coras.representations.DiagramRepresentation;
43  import coras.representations.DocumentRepresentation;
44  import coras.representations.FaultTreeModel;
45  import coras.representations.RepresentationTypeEnum;
46  import coras.representations.UMLModelRepresentation;
47  import coras.structure.ConcernEnum;
48  import coras.structure.ViewpointEnum;
49  import coras.table.CorasTableModel;
50  import coras.table.columnchoices.jaxb.ColumnChoicesType;
51  import coras.types.IDocument;
52  import coras.types.IFaultTree;
53  import coras.types.ITable;
54  import coras.types.IUMLModel;
55  import javax.swing.JPopupMenu;
56  import javax.swing.JMenuItem;
57  /***
58   * @author fvr
59   *
60   * TODO To change the template for this generated type comment go to
61   * Window - Preferences - Java - Code Style - Code Templates
62   */
63  public class ElementFrame extends JInternalFrame {
64  
65  	private CorasElement theElement = null;
66      private boolean editing = false;
67      private Map representationPanels = new HashMap();
68  
69      private javax.swing.JPanel jContentPane = null;
70  
71  	private JTabbedPane jTabbedPane = null;
72  	private ElementDetailsPanel elementDetailsPanel = null;
73  	private JPanel buttonPanel = null;
74  	private JButton closeButton = null;
75  	private PopupButton saveButton = null;
76      private TableEditorPanel tablePanel = null;
77      
78  	/***
79  	 * This is the default constructor
80  	 */
81  	public ElementFrame() {
82  	    this(null, null);
83  	}
84  	
85  	public ElementFrame(CorasElement element, CorasServices services) {
86  		super();
87  		this.theElement = element;
88  		initialize();
89  	    getElementDetailsPanel().setElement(element);
90  	    getElementDetailsPanel().addPropertyChangeListener(propListener);
91  	    if (element instanceof IUMLModel) {
92  	        UMLModelRepresentation umlModel = ((IUMLModel)element).getUMLModel();
93  	        DiagramRepresentation diagram = ((IUMLModel)element).getUMLDiagram();
94  	        RepresentationPanel modelPanel =
95  	        	addRepresentationPanel(umlModel, RepresentationTypeEnum.XMI_LIGHT, "UML Model");
96  	        RepresentationPanel diagramPanel =
97  	        	addRepresentationPanel(diagram, RepresentationTypeEnum.IMAGE, "UML Diagram");
98  	        getJTabbedPane().setSelectedComponent(modelPanel);
99  	    } else if (element instanceof IFaultTree) {
100 	        FaultTreeModel faultTreeModel = ((IFaultTree)element).getFaultTreeModel();
101 	        DiagramRepresentation diagram = ((IFaultTree)element).getFaultTreeDiagram();
102 	        RepresentationPanel modelPanel =
103 	        	addRepresentationPanel(faultTreeModel, RepresentationTypeEnum.FAULT_TREE_MODEL, "Fault Tree Model");
104 	        RepresentationPanel diagramPanel =
105 	        	addRepresentationPanel(diagram, RepresentationTypeEnum.IMAGE, "Fault Tree Diagram");
106 	        getJTabbedPane().setSelectedComponent(modelPanel);
107 	    } else if (element instanceof ITable) {
108 	        /*
109 	        RepresentationPanel tablePanel = new RepresentationPanel(table, RepresentationTypeEnum.TABLE);
110 	        representationPanels.put(RepresentationTypeEnum.TABLE, tablePanel);
111 	        */
112 	        CorasTableModel tableModel = ((ITable)element).getTableModel();
113 	        ColumnChoicesType columnChoices = null;
114 	        try {
115 	            columnChoices = services.getColumnChoices(element.getId());
116             } catch (Exception e) {
117                 // TODO Auto-generated catch block
118                 e.printStackTrace();
119             }
120             
121 	        tableModel.setColumnChoices(columnChoices);
122 	        tableModel.addPropertyChangeListener(CorasTableModel.DIRTY, propListener);
123 	        tablePanel = new TableEditorPanel(tableModel);
124 	        tablePanel.setEditable(false);
125 			getJTabbedPane().addTab("Table Editor", tablePanel);
126 			getJTabbedPane().setSelectedComponent(tablePanel);
127 	    } else if (element instanceof IDocument) {
128 	        DocumentRepresentation document = ((IDocument)element).getDocumentContent();
129 	        RepresentationPanel panel =
130 	        	addRepresentationPanel(document, RepresentationTypeEnum.DOCUMENT, "Document");
131 	        getJTabbedPane().setSelectedComponent(panel);
132 	    }
133 	    theElement.addObserver(new Observer() {
134             public void update(Observable o, Object arg) {
135                 if (o == theElement) {
136                 	updateSaveButton();
137                 }
138             }
139 	    });
140 	    updateSaveButton();
141 	}
142 
143 	/***
144 	 * @param representation
145 	 * @param type TODO
146 	 * @param label TODO
147 	 */
148 	private RepresentationPanel addRepresentationPanel(CorasRepresentation representation, RepresentationTypeEnum type, String label) {
149 		RepresentationPanel panel = new RepresentationPanel(theElement, representation, type);
150 		representationPanels.put(type, panel);
151 		getJTabbedPane().add(label, panel);
152 		panel.addPropertyChangeListener(RepresentationPanel.DIRTY, propListener);
153 		return panel;
154 	}
155 	
156     public ConcernEnum getConcern() {
157         return getElementDetailsPanel().getConcern();
158     }
159     public String getElementName() {
160         return getElementDetailsPanel().getElementName();
161     }
162     public String getFullDescription() {
163         return getElementDetailsPanel().getFullDescription();
164     }
165     public String getShortDescription() {
166         return getElementDetailsPanel().getShortDescription();
167     }
168     public ViewpointEnum[] getViewpoints() {
169         return getElementDetailsPanel().getViewpoints();
170     }
171     
172     public boolean isAllowMultipleViewpoints() {
173         return getElementDetailsPanel().isAllowMultipleViewpoints();
174     }
175 
176     public void setAllowMultipleViewpoints(boolean allowMultipleViewpoints) {
177         getElementDetailsPanel().setAllowMultipleViewpoints(allowMultipleViewpoints);
178     }
179     
180     public boolean isCategoryEnabled() {
181         return getElementDetailsPanel().isCategoryEnabled();
182     }
183     
184     public void setCategoryEnabled(boolean enabled) {
185         getElementDetailsPanel().setCategoryEnabled(enabled);
186     }
187     
188     public boolean isDomainEnabled() {
189         return getElementDetailsPanel().isDomainEnabled();
190     }
191     
192     public void setDomainEnabled(boolean enabled) {
193         getElementDetailsPanel().setDomainEnabled(enabled);
194     }
195     
196 	public CorasRepresentation getRepresentation(RepresentationTypeEnum type) {
197 	    Object o = representationPanels.get(type);
198 	    if (o instanceof RepresentationPanel) {
199 	        return ((RepresentationPanel)o).getRepresentation();
200 	    } else {
201 	        return null;
202 	    }
203 	}
204 
205     public synchronized void setEditing(boolean editing) {
206         if (this.editing == editing) {
207             return;
208         }
209         this.editing = editing;
210         updateSaveButton();
211         getElementDetailsPanel().setEditable(editing);
212         if (tablePanel != null) {
213             tablePanel.setEditable(editing);
214         }
215         for (Iterator i = representationPanels.values().iterator(); i.hasNext();) {
216             RepresentationPanel panel = (RepresentationPanel) i.next();
217             panel.setEditable(editing);
218         }
219     }
220 
221     /***
222      * @return
223      */
224     public boolean isEditing() {
225         return editing;
226     }
227 
228     public boolean isOpenExternal() {
229     	boolean openExternal = false;
230     	for (Iterator i = representationPanels.values().iterator(); i.hasNext();) {
231     		RepresentationPanel rp = (RepresentationPanel) i.next();
232     		openExternal |= rp.isOpenExternal();
233     	}
234     	return openExternal;
235     }
236     
237     public boolean isDirty() {
238     	boolean dirty = elementDetailsPanel.isDirty();
239     	dirty |= theElement != null && theElement.isDirty();
240     	for (Iterator i = representationPanels.values().iterator(); i.hasNext();) {
241 			RepresentationPanel rp = (RepresentationPanel) i.next();
242 			dirty |= rp.isDirty();
243 		}
244     	return dirty;
245     }
246     
247     public void makeClean() {
248     	for (Iterator i = representationPanels.values().iterator(); i.hasNext();) {
249     		RepresentationPanel rp = (RepresentationPanel) i.next();
250     		rp.makeClean();
251     	}
252     }
253     
254     /***
255      * @param listener
256      */
257     public void addSaveActionListener(ActionListener listener) {
258         getSaveMenuItem().addActionListener(listener);
259     }
260     
261     /***
262      * @param listener
263      */
264     public void addSaveWithCommentActionListener(ActionListener listener) {
265     	getSaveWithCommentMenuItem().addActionListener(listener);
266     }
267     
268     private void updateSaveButton() {
269     	getSaveButton().setEnabled(editing && isDirty());
270 		saveButton.setVisible(editing);
271     }
272 
273     private PropertyChangeListener propListener = new PropertyChangeListener() {
274 		public void propertyChange(PropertyChangeEvent e) {
275 			updateSaveButton();
276 		}
277     };
278 	private JPopupMenu savePopupMenu = null;  //  @jve:decl-index=0:visual-constraint="562,313"
279 	private JMenuItem saveMenuItem = null;
280 	private JMenuItem saveWithCommentMenuItem = null;
281     
282     /***
283 	 * This method initializes this
284 	 * 
285 	 * @return void
286 	 */
287 	private void initialize() {
288 		this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
289 		this.setClosable(true);
290 		this.setBounds(0, 0, 500, 400);
291 		this.setIconifiable(true);
292 		this.setResizable(true);
293 		this.setMaximizable(true);
294 		this.setContentPane(getJContentPane());
295 	}
296 	/***
297 	 * This method initializes jContentPane
298 	 * 
299 	 * @return javax.swing.JPanel
300 	 */
301 	private javax.swing.JPanel getJContentPane() {
302 		if(jContentPane == null) {
303 			jContentPane = new javax.swing.JPanel();
304 			jContentPane.setLayout(new java.awt.BorderLayout());
305 			jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER);
306 			jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH);
307 		}
308 		return jContentPane;
309 	}
310 	/***
311 	 * This method initializes jTabbedPane	
312 	 * 	
313 	 * @return javax.swing.JTabbedPane	
314 	 */    
315 	private JTabbedPane getJTabbedPane() {
316 		if (jTabbedPane == null) {
317 			jTabbedPane = new JTabbedPane();
318 			jTabbedPane.addTab("Details", null, getElementDetailsPanel(), null);
319 		}
320 		return jTabbedPane;
321 	}
322 	/***
323 	 * This method initializes elementDetailsPanel	
324 	 * 	
325 	 * @return coras.client.ui.ElementDetailsPanel	
326 	 */    
327 	private ElementDetailsPanel getElementDetailsPanel() {
328 		if (elementDetailsPanel == null) {
329 			elementDetailsPanel = new ElementDetailsPanel();
330 			elementDetailsPanel.setEditable(editing);
331 		}
332 		return elementDetailsPanel;
333 	}
334 	/***
335 	 * This method initializes jPanel	
336 	 * 	
337 	 * @return javax.swing.JPanel	
338 	 */    
339 	private JPanel getButtonPanel() {
340 		if (buttonPanel == null) {
341 			buttonPanel = new JPanel();
342 			buttonPanel.add(getSaveButton(), null);
343 			buttonPanel.add(getCloseButton(), null);
344 		}
345 		return buttonPanel;
346 	}
347 	/***
348 	 * This method initializes jButton	
349 	 * 	
350 	 * @return javax.swing.JButton	
351 	 */    
352 	private JButton getCloseButton() {
353 		if (closeButton == null) {
354 			closeButton = new JButton();
355 			closeButton.setText("Close");
356 			closeButton.addActionListener(new java.awt.event.ActionListener() { 
357 				public void actionPerformed(java.awt.event.ActionEvent e) {    
358 				    doDefaultCloseAction();
359 				}
360 			});
361 		}
362 		return closeButton;
363 	}
364 	/***
365 	 * This method initializes jButton1	
366 	 * 	
367 	 * @return javax.swing.JButton	
368 	 */    
369 	private PopupButton getSaveButton() {
370 		if (saveButton == null) {
371 			saveButton = new PopupButton();
372 			saveButton.setPopupMenu(getSavePopupMenu());
373 //			saveButton.setText("Save");
374 			updateSaveButton();
375 		}
376 		return saveButton;
377 	}
378 
379 	/***
380 	 * This method initializes savePopupMenu	
381 	 * 	
382 	 * @return javax.swing.JPopupMenu	
383 	 */
384 	private JPopupMenu getSavePopupMenu() {
385 		if (savePopupMenu == null) {
386 			savePopupMenu = new JPopupMenu();
387 			savePopupMenu.add(getSaveMenuItem());
388 			savePopupMenu.add(getSaveWithCommentMenuItem());
389 		}
390 		return savePopupMenu;
391 	}
392 
393 	/***
394 	 * This method initializes saveMenuItem	
395 	 * 	
396 	 * @return javax.swing.JMenuItem	
397 	 */
398 	private JMenuItem getSaveMenuItem() {
399 		if (saveMenuItem == null) {
400 			saveMenuItem = new JMenuItem();
401 			saveMenuItem.setText("Save");
402 		}
403 		return saveMenuItem;
404 	}
405 
406 	/***
407 	 * This method initializes saveWithCommentMenuItem	
408 	 * 	
409 	 * @return javax.swing.JMenuItem	
410 	 */
411 	private JMenuItem getSaveWithCommentMenuItem() {
412 		if (saveWithCommentMenuItem == null) {
413 			saveWithCommentMenuItem = new JMenuItem();
414 			saveWithCommentMenuItem.setText("Save with comment");
415 		}
416 		return saveWithCommentMenuItem;
417 	}
418 }