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.CardLayout;
24  import java.awt.Frame;
25  import java.beans.PropertyChangeEvent;
26  import java.beans.PropertyChangeListener;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.Map;
31  
32  import javax.swing.Icon;
33  import javax.swing.ImageIcon;
34  import javax.swing.JButton;
35  import javax.swing.JDialog;
36  import javax.swing.JLabel;
37  import javax.swing.JOptionPane;
38  import javax.swing.JPanel;
39  import javax.swing.SwingUtilities;
40  import javax.swing.border.TitledBorder;
41  
42  import coras.common.CorasRepresentation;
43  import coras.representations.DiagramRepresentation;
44  import coras.representations.DocumentRepresentation;
45  import coras.representations.FaultTreeModel;
46  import coras.representations.RepresentationTypeEnum;
47  import coras.representations.UMLModelRepresentation;
48  import coras.structure.ConcernEnum;
49  import coras.structure.SubProcessEnum;
50  import coras.structure.ViewpointEnum;
51  import coras.types.ElementTypeEnum;
52  import coras.types.SubtypeEnum;
53  /***
54   * @author fvr
55   *
56   * TODO To change the template for this generated type comment go to
57   * Window - Preferences - Java - Code Style - Code Templates
58   */
59  public class NewElementDialog extends JDialog {
60  
61  	private static final ClassLoader cl = NewElementDialog.class.getClassLoader();
62  	private static final Icon EMPTY_ICON = new ImageIcon(cl.getResource(Messages.getString("NewElementDialog.emptyIcon"))); //$NON-NLS-1$
63  	private static final Icon WARNING_ICON = new ImageIcon(cl.getResource(Messages.getString("NewElementDialog.warningIcon"))); //$NON-NLS-1$
64  	private static final Icon ERROR_ICON = new ImageIcon(cl.getResource(Messages.getString("NewElementDialog.errorIcon"))); //$NON-NLS-1$
65  
66  	private static final String WHITESPACE_PATTERN = "//s*"; //$NON-NLS-1$
67  	
68      private static final int STATE_DETAILS = 0;
69      private static final int STATE_UML_MODEL = STATE_DETAILS + 1;
70      private static final int STATE_UML_DIAGRAM = STATE_UML_MODEL + 1;
71      private static final int STATE_FAULT_TREE_MODEL = STATE_UML_DIAGRAM + 1;
72      private static final int STATE_FAULT_TREE_DIAGRAM = STATE_FAULT_TREE_MODEL + 1;
73      private static final int STATE_DOCUMENT_CONTENT = STATE_FAULT_TREE_DIAGRAM + 1;
74      
75      private static final RepresentationTypeEnum[] stateRepresentations = new RepresentationTypeEnum[] {
76              null, // DETAILS
77              null, // use umlType
78              RepresentationTypeEnum.IMAGE, // UML_DIAGRAM
79              RepresentationTypeEnum.FAULT_TREE_MODEL,
80              RepresentationTypeEnum.IMAGE, // FAULT_TREE_DIAGRAM
81              RepresentationTypeEnum.DOCUMENT
82      };
83      
84      private static final String[] stateTitles = new String[] {
85              null, // DETAILS
86              Messages.getString("NewElementDialog.umlModelTitle"), //$NON-NLS-1$
87              Messages.getString("NewElementDialog.umlDiagramTitle"), //$NON-NLS-1$
88              Messages.getString("NewElementDialog.faultTreeModelTitle"), //$NON-NLS-1$
89              Messages.getString("NewElementDialog.faultTreeDiagramTitle"), //$NON-NLS-1$
90              Messages.getString("NewElementDialog.documentTitle") //$NON-NLS-1$
91      };
92      
93      private Map representations = new HashMap();
94  
95      private int result = JOptionPane.CANCEL_OPTION;
96      private int state = STATE_DETAILS;
97      
98      private ElementTypeEnum type = ElementTypeEnum.UML_MODEL;
99      private RepresentationTypeEnum umlType = RepresentationTypeEnum.XMI_MODEL;
100     
101     private javax.swing.JPanel jContentPane = null;
102 
103 	private JPanel buttonsPanel = null;
104 	private JButton backButton = null;
105 	private JButton nextButton = null;
106 	private JButton okButton = null;
107 	private JButton cancelButton = null;
108 
109 	private JPanel cardPanel = null;
110 	private ElementDetailsPanel elementDetailsPanel = null;
111 	private RepresentationPanel representationPanel = null;
112 	private JLabel statusLabel = null;
113 
114 	/***
115 	 * This is the default constructor
116 	 */
117 	public NewElementDialog() {
118 	    this(null);
119 	}
120 	
121 	public NewElementDialog(Frame owner) {
122 		super(owner);
123 		initialize();
124 		update();
125 	}
126 
127     public boolean isCategoryEnabled() {
128         return getElementDetailsPanel().isCategoryEnabled();
129     }
130     
131     public void setCategoryEnabled(boolean enabled) {
132         getElementDetailsPanel().setCategoryEnabled(enabled);
133     }
134     
135     public boolean isDomainEnabled() {
136         return elementDetailsPanel.isDomainEnabled();
137     }
138     
139     public void setDomainEnabled(boolean enabled) {
140         elementDetailsPanel.setDomainEnabled(enabled);
141     }
142     
143     public boolean isAllowMultipleViewpoints() {
144         return elementDetailsPanel.isAllowMultipleViewpoints();
145     }
146     
147     public void setAllowMultipleViewpoints(boolean allowMultipleViewpoints) {
148         elementDetailsPanel.setAllowMultipleViewpoints(allowMultipleViewpoints);
149     }
150     
151     public String getElementName() {
152         return getElementDetailsPanel().getElementName();
153     }
154     
155     public String getShortDescription() {
156         return getElementDetailsPanel().getShortDescription();
157     }
158     
159     public ConcernEnum getConcern() {
160         return getElementDetailsPanel().getConcern();
161     }
162 
163     public ViewpointEnum[] getViewpoints() {
164         return getElementDetailsPanel().getViewpoints();
165     }
166 
167     public ViewpointEnum getViewpoint() {
168         return getElementDetailsPanel().getViewpoint();
169     }
170 
171     public String getFullDescription() {
172         return getElementDetailsPanel().getFullDescription();
173     }
174 
175     /***
176      * @return
177      */
178     public ElementTypeEnum getType() {
179         return type;
180     }
181 
182     /***
183      * @param type
184      */
185     public synchronized void setType(ElementTypeEnum type, SubProcessEnum subprocessFilter, RepresentationTypeEnum umlType) {
186         /*
187         if (this.type == type) {
188             return;
189         }
190         */
191         this.type = type;
192         this.umlType = umlType != null ? umlType : RepresentationTypeEnum.XMI_LIGHT;
193 //        setTitle("New " + (type != null ? type.toString() : "Result"));
194         
195         boolean enableTypeSelection = true;
196         ArrayList subtypes = new ArrayList();
197         if (type == ElementTypeEnum.UML_MODEL) {
198         	if (umlType == RepresentationTypeEnum.DGM_MODEL) {
199         		subtypes.add(umlType);
200         		enableTypeSelection = false;
201         	} else {
202         		for (Iterator i = SubtypeEnum.DIAGRAM_VALUES.iterator(); i.hasNext(); ) {
203         			SubtypeEnum subtype = (SubtypeEnum) i.next();
204         			if (subprocessFilter == null || subtype == SubtypeEnum.OTHER_DIAGRAM || subprocessFilter == subtype.getConcern().getSubProcess()) {
205         				subtypes.add(subtype);
206         			}
207         		}
208         	}
209         } else if (type == ElementTypeEnum.TABLE) {
210             for (Iterator i = SubtypeEnum.TABLE_VALUES.iterator(); i.hasNext(); ) {
211                 SubtypeEnum subtype = (SubtypeEnum) i.next();
212                 if (subprocessFilter == null || subprocessFilter == subtype.getConcern().getSubProcess()) {
213                     subtypes.add(subtype);
214                 }
215             }
216         } else {
217             subtypes.add(type);
218             enableTypeSelection = false;
219         }
220         getElementDetailsPanel().setSubtypeChoices(subtypes);
221         getElementDetailsPanel().setEnableTypeSelection(enableTypeSelection);
222         getElementDetailsPanel().setSubprocess(subprocessFilter);
223 		update();
224     }
225     
226     /***
227      * @return
228      */
229     public SubtypeEnum getSubtype() {
230         return getElementDetailsPanel().getSubtype();
231     }
232 
233     public int getResult() {
234         return result;
235     }
236 	
237     /***
238      * @return
239      */
240     public UMLModelRepresentation getUMLModel() {
241     	Object representation = representations.get(umlType);
242         if (representation == null || representation instanceof UMLModelRepresentation) {
243             return (UMLModelRepresentation) representation;
244         } else {
245             return null; // FIXME error
246         }
247     }
248 
249     /***
250      * @return
251      */
252     public FaultTreeModel getFaultTreeModel() {
253         Object representation = representations.get(RepresentationTypeEnum.FAULT_TREE_MODEL);
254         if (representation == null || representation instanceof FaultTreeModel) {
255             return (FaultTreeModel) representation;
256         } else {
257             return null; // FIXME error
258         }
259     }
260 
261     /***
262      * @return
263      */
264     public DocumentRepresentation getDocument() {
265         Object representation = representations.get(RepresentationTypeEnum.DOCUMENT);
266         if (representation == null || representation instanceof DocumentRepresentation) {
267             return (DocumentRepresentation) representation;
268         } else {
269             return null; // FIXME error
270         }
271     }
272 
273     /***
274      * @return
275      */
276     public DiagramRepresentation getDiagram() {
277         Object representation = representations.get(RepresentationTypeEnum.IMAGE);
278         if (representation == null || representation instanceof DiagramRepresentation) {
279             return (DiagramRepresentation) representation;
280         } else {
281             return null; // FIXME error
282         }
283     }
284    
285     /***
286      * @param b
287      */
288     private void setResult(int result) {
289         this.result = result;
290     }
291 
292     /***
293      * 
294      */
295     private synchronized void previous() {
296         int newState;
297         switch (state) {
298         	case STATE_UML_MODEL :
299         	case STATE_FAULT_TREE_MODEL :
300         	case STATE_DOCUMENT_CONTENT :
301         	    newState = STATE_DETAILS;
302         	    break;
303         	case STATE_UML_DIAGRAM :
304         	    newState = STATE_UML_MODEL;
305         	    break;
306         	case STATE_FAULT_TREE_DIAGRAM : 
307         	    newState = STATE_FAULT_TREE_MODEL;
308         	    break;
309         	default :
310         	    return; // ERROR
311         }
312         setState(newState);
313     }
314     
315     /***
316      * 
317      */
318     private synchronized void next() {
319         int newState;
320         switch (state) {
321         	case STATE_DETAILS :
322         	    ElementTypeEnum type = getType();
323         	    if (type == ElementTypeEnum.DOCUMENT) {
324         	        newState = STATE_DOCUMENT_CONTENT;
325         	    } else if (type == ElementTypeEnum.UML_MODEL 
326         	    		&& umlType != RepresentationTypeEnum.DGM_MODEL) {
327         	        newState = STATE_UML_MODEL;
328         	    } else if (type == ElementTypeEnum.FAULT_TREE) {
329         	        newState = STATE_FAULT_TREE_MODEL;
330         	    } else {
331         	        return; // ERROR
332         	    }
333         	    break;
334         	case STATE_UML_MODEL :
335         	    newState = STATE_UML_DIAGRAM;
336         	    break;
337         	case STATE_FAULT_TREE_MODEL :
338         	    newState = STATE_FAULT_TREE_DIAGRAM;
339         	    break;
340         	default:
341         	    return; // ERROR    
342         }
343         setState(newState);
344     }
345 
346     /***
347      * @param newState
348      */
349     private void setState(int newState) {
350         if (state == newState) {
351             return;
352         }
353         
354         // FIXME save old representation data
355         RepresentationTypeEnum representationType = getRepresentationType();
356         if (representationType != null) {
357             representations.put(representationType, getRepresentationPanel().getRepresentation());
358         }
359         
360         state = newState;
361         fireUpdateEvent();
362     }
363 
364 	private RepresentationTypeEnum getRepresentationType() {
365 		if (state == STATE_UML_MODEL) {
366 			return umlType;
367 		} else {
368 			return stateRepresentations[state];
369 		}
370 	}
371     
372     private void update() {
373     	if (state == STATE_DETAILS && getElementName().matches(WHITESPACE_PATTERN)) { 
374     		statusLabel.setText(Messages.getString("NewElementDialog.emptyNameErrorMessage")); //$NON-NLS-1$
375     		statusLabel.setIcon(ERROR_ICON);
376     		// statusLabel.invalidate();
377     	} else if (state == STATE_DETAILS && getViewpoint() == null) {
378     		statusLabel.setText(Messages.getString("NewElementDialog.noViewpointWarningMessage")); //$NON-NLS-1$
379     		statusLabel.setIcon(WARNING_ICON);
380     	} else {
381     		statusLabel.setText(null);
382     		statusLabel.setIcon(EMPTY_ICON);
383     	}
384         RepresentationTypeEnum representationType = getRepresentationType();
385         CorasRepresentation representation = (CorasRepresentation) representations.get(representationType);
386         boolean canFinish = 
387         	!getElementName().matches(WHITESPACE_PATTERN); 
388         /*
389             (getType() == ElementTypeEnum.TABLE && state == STATE_DETAILS)
390             || (getType() == ElementTypeEnum.UML_MODEL && state == STATE_UML_DIAGRAM)
391             || (getType() == ElementTypeEnum.FAULT_TREE && state == STATE_FAULT_TREE_DIAGRAM)
392             || (getType() == ElementTypeEnum.DOCUMENT && state == STATE_DOCUMENT_CONTENT);
393         */
394         boolean nextEnabled = 
395         	!getElementName().matches(WHITESPACE_PATTERN)  
396 			&& ((getType() == ElementTypeEnum.UML_MODEL && umlType != RepresentationTypeEnum.DGM_MODEL && state != STATE_UML_DIAGRAM)
397 					|| (getType() == ElementTypeEnum.FAULT_TREE && state != STATE_FAULT_TREE_DIAGRAM)
398 					|| (getType() == ElementTypeEnum.DOCUMENT && state != STATE_DOCUMENT_CONTENT));
399         
400         getBackButton().setEnabled(state != STATE_DETAILS);
401         getNextButton().setEnabled(nextEnabled);
402         getOkButton().setEnabled(canFinish);
403         
404 		CardLayout cardLayout = (CardLayout) getCardPanel().getLayout();
405         switch (state) {
406         	case STATE_DETAILS :
407         	    cardLayout.show(getCardPanel(), getElementDetailsPanel().getName());
408         	    break;
409         	default:
410         	    if (representation != null) {
411         	        getRepresentationPanel().setRepresentation(representation);
412         	    } else {
413         	    	getRepresentationPanel().setType(representationType);
414         	    }
415         		TitledBorder border = (TitledBorder) getRepresentationPanel().getBorder();
416         		border.setTitle(stateTitles[state]);
417         		getRepresentationPanel().repaint();
418         	    cardLayout.show(getCardPanel(), getRepresentationPanel().getName());
419         	    break;
420         }
421         //validate();
422         //repaint();
423     }
424     
425     private void fireUpdateEvent() {
426         SwingUtilities.invokeLater(new Runnable() {
427             public void run() {
428                 update();
429             }
430         });
431     }
432     
433 	/***
434 	 * This method initializes this
435 	 * 
436 	 * @return void
437 	 */
438 	private void initialize() {
439 		this.setModal(true);
440 		this.setBounds(0, 0, 400, 400);
441 		this.setContentPane(getJContentPane());
442 	}
443 
444     /***
445      * This method initializes representationPanel	
446      * 	
447      * @return coras.client.ui.RepresentationPanel	
448      */    
449     private RepresentationPanel getRepresentationPanel() {
450     	if (representationPanel == null) {
451     		representationPanel = new RepresentationPanel();
452     		representationPanel.setName("representation"); //$NON-NLS-1$
453     		representationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Messages.getString("NewElementDialog.defaultRepresentationPanelTitle"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); //$NON-NLS-1$
454     		representationPanel.setEditable(true);
455     	}
456     	return representationPanel;
457     }
458 	/***
459 	 * This method initializes jContentPane
460 	 * 
461 	 * @return javax.swing.JPanel
462 	 */
463 	private javax.swing.JPanel getJContentPane() {
464 		if(jContentPane == null) {
465 			statusLabel = new JLabel();
466 			statusLabel.setIcon(EMPTY_ICON);
467 			jContentPane = new javax.swing.JPanel();
468 			jContentPane.setLayout(new java.awt.BorderLayout());
469 			jContentPane.add(statusLabel, java.awt.BorderLayout.NORTH);
470 			jContentPane.add(getCardPanel(), java.awt.BorderLayout.CENTER);
471 			jContentPane.add(getButtonsPanel(), java.awt.BorderLayout.SOUTH);
472 		}
473 		return jContentPane;
474 	}
475 	/***
476 	 * This method initializes jPanel	
477 	 * 	
478 	 * @return javax.swing.JPanel	
479 	 */    
480 	private JPanel getButtonsPanel() {
481 		if (buttonsPanel == null) {
482 			buttonsPanel = new JPanel();
483 			buttonsPanel.add(getBackButton(), null);
484 			buttonsPanel.add(getNextButton(), null);
485 			buttonsPanel.add(getOkButton(), null);
486 			buttonsPanel.add(getCancelButton(), null);
487 		}
488 		return buttonsPanel;
489 	}
490 	/***
491 	 * This method initializes jButton	
492 	 * 	
493 	 * @return javax.swing.JButton	
494 	 */    
495 	private JButton getBackButton() {
496 		if (backButton == null) {
497 			backButton = new JButton();
498 			backButton.setText(Messages.getString("NewElementDialog.backButton")); //$NON-NLS-1$
499 			backButton.setMnemonic(java.awt.event.KeyEvent.VK_B);
500 			backButton.addActionListener(new java.awt.event.ActionListener() { 
501 				public void actionPerformed(java.awt.event.ActionEvent e) {    
502 				    previous();
503 				}
504 			});
505 		}
506 		return backButton;
507 	}
508 	/***
509 	 * This method initializes jButton	
510 	 * 	
511 	 * @return javax.swing.JButton	
512 	 */    
513 	private JButton getNextButton() {
514 		if (nextButton == null) {
515 			nextButton = new JButton();
516 			nextButton.setText(Messages.getString("NewElementDialog.nextButton")); //$NON-NLS-1$
517 			nextButton.setMnemonic(java.awt.event.KeyEvent.VK_N);
518 			nextButton.addActionListener(new java.awt.event.ActionListener() { 
519 				public void actionPerformed(java.awt.event.ActionEvent e) {    
520 				    next();
521 				}
522 			});
523 		}
524 		return nextButton;
525 	}
526 	/***
527 	 * This method initializes jButton1	
528 	 * 	
529 	 * @return javax.swing.JButton	
530 	 */    
531 	private JButton getOkButton() {
532 		if (okButton == null) {
533 			okButton = new JButton();
534 			okButton.setText(Messages.getString("NewElementDialog.okButton")); //$NON-NLS-1$
535 			okButton.setMnemonic(java.awt.event.KeyEvent.VK_O);
536 			okButton.setEnabled(false);
537 			okButton.addActionListener(new java.awt.event.ActionListener() { 
538 				public void actionPerformed(java.awt.event.ActionEvent e) {    
539 			        // FIXME save representation data
540 			        RepresentationTypeEnum type = getRepresentationType();
541 			        if (type != null) {
542 			            representations.put(type, getRepresentationPanel().getRepresentation());
543 			        }
544 			        
545 				    setResult(JOptionPane.OK_OPTION);
546 				    dispose();
547 				}
548 			});
549 		}
550 		return okButton;
551 	}
552 	/***
553 	 * This method initializes jButton2	
554 	 * 	
555 	 * @return javax.swing.JButton	
556 	 */    
557 	private JButton getCancelButton() {
558 		if (cancelButton == null) {
559 			cancelButton = new JButton();
560 			cancelButton.setText(Messages.getString("NewElementDialog.cancelButton")); //$NON-NLS-1$
561 			cancelButton.addActionListener(new java.awt.event.ActionListener() { 
562 				public void actionPerformed(java.awt.event.ActionEvent e) {    
563 				    setResult(JOptionPane.CANCEL_OPTION);
564 				    dispose();
565 				}
566 			});
567 		}
568 		return cancelButton;
569 	}
570 
571 	/***
572 	 * This method initializes jPanel	
573 	 * 	
574 	 * @return javax.swing.JPanel	
575 	 */    
576 	private JPanel getCardPanel() {
577 		if (cardPanel == null) {
578 			cardPanel = new JPanel();
579 			cardPanel.setLayout(new CardLayout());
580 			cardPanel.setName("type"); //$NON-NLS-1$
581 			cardPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
582 			cardPanel.add(getElementDetailsPanel(), getElementDetailsPanel().getName());
583 			cardPanel.add(getRepresentationPanel(), getRepresentationPanel().getName());
584 		}
585 		return cardPanel;
586 	}
587 
588 	/***
589 	 * This method initializes elementDetailsPanel	
590 	 * 	
591 	 * @return coras.client.ui.ElementDetailsPanel	
592 	 */    
593 	private ElementDetailsPanel getElementDetailsPanel() {
594 		if (elementDetailsPanel == null) {
595 			elementDetailsPanel = new ElementDetailsPanel();
596 			elementDetailsPanel.setName("details"); //$NON-NLS-1$
597 			elementDetailsPanel.setEditable(true);
598 			elementDetailsPanel.setCategoryEnabled(false);
599 			elementDetailsPanel.setDomainEnabled(false);
600 			PropertyChangeListener listener = new PropertyChangeListener() {
601 				public void propertyChange(PropertyChangeEvent evt) {
602 					fireUpdateEvent();
603 				}
604 			};
605 			elementDetailsPanel.addPropertyChangeListener(ElementDetailsPanel.ELEMENT_NAME, listener);
606 			elementDetailsPanel.addPropertyChangeListener(ElementDetailsPanel.ELEMENT_VIEWPOINTS, listener);
607 		}
608 		return elementDetailsPanel;
609 	}
610 
611 }