1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.client.ui;
22
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27 import java.util.ArrayList;
28 import java.util.Observable;
29 import java.util.Observer;
30
31 import javax.swing.DefaultComboBoxModel;
32 import javax.swing.JFrame;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JScrollPane;
36 import javax.swing.JTextField;
37 import javax.swing.SwingUtilities;
38 import javax.swing.event.DocumentEvent;
39 import javax.swing.event.DocumentListener;
40 import javax.swing.text.Document;
41
42 import coras.client.SwingWorker;
43 import coras.common.CorasElement;
44 import coras.reuse.CategoryEnum;
45 import coras.reuse.Experience;
46 import coras.structure.ConcernEnum;
47 import coras.structure.SubProcessEnum;
48 import coras.structure.ViewpointEnum;
49 import coras.types.ElementTypeEnum;
50 import coras.types.SubtypeEnum;
51 /***
52 * @author fvr
53 *
54 * TODO To change the template for this generated type comment go to
55 * Window - Preferences - Java - Code Style - Code Templates
56 */
57 public class ElementDetailsPanel extends JPanel {
58
59 public static final String ELEMENT_NAME = "elementName";
60 public static final String ELEMENT_SHORT_DESCRIPTION = "elementShortDescription";
61 public static final String ELEMENT_FULL_DESCRIPTION = "elementFullDescription";
62 public static final String ELEMENT_DOMAIN = "elementDomain";
63 public static final String ELEMENT_CONCERN = "elementConcern";
64 public static final String ELEMENT_VIEWPOINTS = "elementViewpoints";
65
66 private CorasElement element = null;
67 private boolean editable = false;
68
69 private JLabel nameLabel = null;
70 private JLabel shortDescriptionLabel = null;
71 private JLabel concernLabel = null;
72 private JTextField nameField = null;
73 private JTextField shortDescriptionField = null;
74 private JLabel viewpointLabel = null;
75 private JLabel fullDescriptionLabel = null;
76 private CorasTextArea fullDescriptionArea = null;
77 private JScrollPane descriptionScrollPane = null;
78
79 private CorasComboBox concernBox = null;
80 private JLabel domainLabel = null;
81 private CorasComboBox domainBox = null;
82 private ViewpointPanel viewpointPanel = null;
83
84
85 private JLabel typeLabel = null;
86 private CorasComboBox typeBox = null;
87 private JLabel categoryLabel = null;
88 private JTextField categoryField = null;
89 /***
90 * This method initializes subtypeBox
91 *
92 * @return coras.client.ui.CorasComboBox
93 */
94 private CorasComboBox getTypeBox() {
95 if (typeBox == null) {
96 typeBox = new CorasComboBox();
97 typeBox.setEditable(false);
98 typeBox.setComboBoxEditable(false);
99 typeBox.addPropertyChangeListener(new PropertyChangeListener() {
100 public void propertyChange(PropertyChangeEvent e) {
101 Object o = e.getNewValue();
102 if (o instanceof SubtypeEnum) {
103 SubtypeEnum subtype = (SubtypeEnum) o;
104 if (subtype != SubtypeEnum.OTHER_DIAGRAM) {
105 getConcernBox().setSelectedItem(subtype.getConcern());
106 }
107 getConcernBox().setEditable(editable &&
108 (subtype == null || subtype == SubtypeEnum.OTHER_DIAGRAM));
109 }
110 }
111
112 });
113 }
114 return typeBox;
115 }
116
117 /***
118 * This method initializes jTextField
119 *
120 * @return javax.swing.JTextField
121 */
122 private JTextField getCategoryField() {
123 if (categoryField == null) {
124 categoryField = new JTextField();
125 categoryField.setEditable(false);
126
127 }
128 return categoryField;
129 }
130 public static void main(String[] args) {
131 JFrame frame = new JFrame();
132 frame.getContentPane().add(new ElementDetailsPanel());
133 frame.setVisible(true);
134 }
135
136 /***
137 * This is the default constructor
138 */
139 public ElementDetailsPanel() {
140 super();
141 initialize();
142 }
143
144 public synchronized void setElement(CorasElement newElement) {
145 if (element == newElement) {
146 return;
147 }
148 if (element != null) {
149 element.deleteObserver(observer);
150 }
151 element = newElement;
152 if (element != null) {
153 element.addObserver(observer);
154 }
155 fireUpdateEvent();
156 }
157
158 public boolean isEditable() {
159 return editable;
160 }
161
162 public boolean isDirty() {
163 boolean clean = element != null;
164 clean &= equals(getElementName(), element.getName());
165 clean &= equals(getShortDescription(), element.getShortDescription());
166 clean &= equals(getFullDescription(), element.getFullDescription());
167 clean &= equals(getConcern(), element.getConcern());
168 clean &= equals(getViewpoints(), element.getViewpoints());
169 if (element instanceof Experience) {
170 Experience e = (Experience) element;
171
172 clean &= equals(getCategory(), e.getCategory());
173 }
174 return !clean;
175 }
176
177 /***
178 * @param b
179 */
180 public synchronized void setEditable(boolean editable) {
181 if (this.editable == editable) {
182 return;
183 }
184 this.editable = editable;
185
186 SubtypeEnum subtype = getSubtype();
187 getNameField().setEditable(editable);
188 getShortDescriptionField().setEditable(editable);
189 getDomainBox().setEditable(editable);
190 getConcernBox().setEditable(editable &&
191 (subtype == null || subtype == SubtypeEnum.OTHER_DIAGRAM));
192 getViewpointPanel().setEditable(editable);
193 getFullDescriptionArea().setEditable(editable);
194 }
195
196 public boolean isAllowMultipleViewpoints() {
197 return getViewpointPanel().isAllowMultipleViewpoints();
198 }
199
200 public void setAllowMultipleViewpoints(boolean allowMultipleViewpoints) {
201 getViewpointPanel().setAllowMultipleViewpoints(allowMultipleViewpoints);
202 }
203
204 public boolean isCategoryEnabled() {
205 return getCategoryField().isVisible();
206 }
207
208 public void setCategoryEnabled(boolean enabled) {
209 categoryLabel.setVisible(enabled);
210 getCategoryField().setVisible(enabled);
211 }
212
213 public boolean isDomainEnabled() {
214 return getDomainBox().isVisible();
215 }
216
217 public synchronized void setDomainEnabled(boolean enabled) {
218 domainLabel.setVisible(enabled);
219 getDomainBox().setVisible(enabled);
220 }
221
222 public boolean isDomainEditable() {
223 return getDomainBox().isComboBoxEditable();
224 }
225
226 public void setDomainEditable(boolean editable) {
227 getDomainBox().setComboBoxEditable(editable);
228 }
229
230 public boolean isEnableTypeSelection() {
231 return getTypeBox().isEditable();
232 }
233
234 public void setEnableTypeSelection(boolean editable) {
235 getTypeBox().setEditable(editable);
236 }
237
238 public void setSubtypeChoices(ArrayList subtypes) {
239 Object[] choices = subtypes != null ? subtypes.toArray() : new Object[0];
240 getTypeBox().setModel(new DefaultComboBoxModel(choices));
241 if (choices.length > 0) {
242 getTypeBox().setSelectedItem(choices[0]);
243 }
244 SubtypeEnum subtype = getSubtype();
245 if (subtype != null && subtype != SubtypeEnum.OTHER_DIAGRAM) {
246 getConcernBox().setSelectedItem(subtype.getConcern());
247 getConcernBox().setEditable(false);
248 } else {
249 getConcernBox().setEditable(editable);
250 }
251 }
252
253 public SubtypeEnum getSubtype() {
254 Object o = getTypeBox().getSelectedItem();
255 if (o instanceof SubtypeEnum) {
256 return (SubtypeEnum) o;
257 } else {
258 return null;
259 }
260 }
261 /***
262 * @return
263 */
264 public String getElementName() {
265 return getNameField().getText();
266 }
267
268 /***
269 * @return
270 */
271 public String getShortDescription() {
272 return getShortDescriptionField().getText();
273 }
274
275 /***
276 * @return
277 */
278 public String getFullDescription() {
279 return getFullDescriptionArea().getText();
280 }
281
282 public ConcernEnum getConcern() {
283 Object item = getConcernBox().getSelectedItem();
284 if (item == null || item instanceof ConcernEnum) {
285 return (ConcernEnum) item;
286 } else {
287
288 return null;
289 }
290 }
291
292 /***
293 * @param concern
294 */
295 public void setConcern(ConcernEnum concern) {
296 getConcernBox().setSelectedItem(concern);
297 }
298
299 public void setSubprocess(SubProcessEnum subprocess) {
300 getConcernBox().setModel(new DefaultComboBoxModel(ConcernEnum.forSubProcess(subprocess).toArray()));
301 }
302
303 public ViewpointEnum[] getViewpoints() {
304 return getViewpointPanel().getViewpoints();
305 }
306
307 public ViewpointEnum getViewpoint() {
308 return getViewpointPanel().getViewpoint();
309 }
310
311 public String getDomain() {
312 return (String) getDomainBox().getSelectedItem();
313 }
314
315 public CategoryEnum getCategory() {
316 return CategoryEnum.forName(getCategoryField().getText());
317 }
318
319 private Observer observer = new Observer() {
320 public void update(Observable o, Object arg) {
321 if (o != element) {
322 return;
323 }
324 fireUpdateEvent();
325 }
326 };
327
328 private void update() {
329 ElementTypeEnum type = element != null ? element.getType() : null;
330 SubtypeEnum subtype = element != null ? element.getSubtype() : null;
331 if (subtype != null) {
332 getTypeBox().setModel(new DefaultComboBoxModel(new Object[] { subtype }));
333 getTypeBox().setSelectedItem(subtype);
334 } else {
335 getTypeBox().setModel(new DefaultComboBoxModel(new Object[] { type }));
336 getTypeBox().setSelectedItem(type);
337 }
338 getNameField().setText(element != null ? element.getName() : "");
339 getShortDescriptionField().setText(element != null ? element.getShortDescription() : "");
340 if (element instanceof Experience) {
341 getCategoryField().setText(((Experience)element).getCategory().toString());
342 String domain = ((Experience)element).getDomain();
343 getDomainBox().setSelectedItem(domain);
344 }
345 getConcernBox().setSelectedItem(element != null ? element.getConcern() : null);
346 getViewpointPanel().setViewpoints(element != null ? element.getViewpoints() : null);
347 getFullDescriptionArea().setText(element != null ? element.getFullDescription() : "");
348 }
349
350
351 private void fireUpdateEvent() {
352 SwingUtilities.invokeLater(new Runnable() {
353 public void run() {
354 update();
355 }
356 });
357 }
358
359 private PropertyChangeListener propListener = new PropertyChangeListener() {
360 public void propertyChange(final PropertyChangeEvent e) {
361 new SwingWorker() {
362 public Object construct() {
363 String property = e.getPropertyName();
364 Object source = e.getSource();
365 if (source == getViewpointPanel() && property == ViewpointPanel.SELECTED_VIEWPOINTS) {
366 firePropertyChange(ELEMENT_VIEWPOINTS, e.getOldValue(), e.getNewValue());
367 } else if (source == getConcernBox() && property == CorasComboBox.SELECTED_ITEM) {
368 firePropertyChange(ELEMENT_CONCERN, null, getConcern());
369 } else if (source == getDomainBox() && property == CorasComboBox.SELECTED_ITEM) {
370 firePropertyChange(ELEMENT_DOMAIN, null, getConcern());
371 } else {
372 System.out.println("Unknown property or source: " + property + " / " + source);
373 }
374 return null;
375 }
376 }.start();
377 }
378 };
379
380 private DocumentListener docListener = new DocumentListener() {
381 public void changedUpdate(DocumentEvent e) {
382
383 System.out.println("This should never happen with non-styled documents");
384 }
385 public void insertUpdate(DocumentEvent e) {
386 firePropertyChange(e);
387 }
388 public void removeUpdate(DocumentEvent e) {
389 firePropertyChange(e);
390 }
391 };
392
393 private void firePropertyChange(final DocumentEvent e) {
394 new SwingWorker() {
395 public Object construct() {
396 Document doc = e.getDocument();
397 if (doc == getNameField().getDocument()) {
398 firePropertyChange(ELEMENT_NAME, null, getElementName());
399 } else if (doc == getShortDescriptionField().getDocument()) {
400 firePropertyChange(ELEMENT_SHORT_DESCRIPTION, null, getShortDescription());
401 } else if (doc == getFullDescriptionArea().getDocument()) {
402 firePropertyChange(ELEMENT_FULL_DESCRIPTION, null, getFullDescription());
403 } else {
404 System.out.println("Unknown document: " + doc);
405 }
406 return null;
407 }
408 }.start();
409 }
410
411 private boolean equals(Object o1, Object o2) {
412 if (o1 == o2) {
413 return true;
414 }
415 if (o1 == null || o2 == null) {
416 return false;
417 }
418 return o1.equals(o2);
419 }
420
421 /***
422 * @param a1
423 * @param a2
424 * @return
425 */
426 private boolean equals(ViewpointEnum[] a1, ViewpointEnum[] a2) {
427 if (a1 == a2)
428 return true;
429 if (a1 == null || a2 == null)
430 return false;
431 if (a1.length != a2.length)
432 return false;
433 for (int i = 0; i < a1.length; i++) {
434 ViewpointEnum v1 = a1[i];
435 ViewpointEnum v2 = a2[i];
436 if (v1 == v2 || (v1 != null && v1.equals(v2)))
437 continue;
438 return false;
439 }
440 return true;
441 }
442
443 /***
444 * This method initializes this
445 *
446 * @return void
447 */
448 private void initialize() {
449 categoryLabel = new JLabel();
450 typeLabel = new JLabel();
451 GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
452 domainLabel = new JLabel();
453 fullDescriptionLabel = new JLabel();
454 viewpointLabel = new JLabel();
455 concernLabel = new JLabel();
456 shortDescriptionLabel = new JLabel();
457 nameLabel = new JLabel();
458 GridBagConstraints viewpointPanelConstraints = new GridBagConstraints();
459 GridBagConstraints concernBoxConstraints = new GridBagConstraints();
460 GridBagConstraints nameLabelConstraints = new GridBagConstraints();
461 GridBagConstraints shortDescriptionLabelConstraints = new GridBagConstraints();
462 GridBagConstraints concernLabelConstraints = new GridBagConstraints();
463 GridBagConstraints nameFieldConstraints = new GridBagConstraints();
464 GridBagConstraints shortDescriptionFieldConstraints = new GridBagConstraints();
465 GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
466 GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
467 GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
468 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
469 GridBagConstraints domainLabelConstraints = new GridBagConstraints();
470 GridBagConstraints domainBoxConstraints = new GridBagConstraints();
471 GridBagConstraints viewpointLabelConstraints = new GridBagConstraints();
472 GridBagConstraints fullDescriptionLabelConstraints = new GridBagConstraints();
473 GridBagConstraints descriptionScrollPaneConstraints = new GridBagConstraints();
474 this.setLayout(new GridBagLayout());
475 this.setBounds(0, 0, 400, 300);
476 nameLabelConstraints.gridx = 0;
477 nameLabelConstraints.gridy = 1;
478 nameLabelConstraints.anchor = java.awt.GridBagConstraints.WEST;
479 nameLabel.setText("Name:");
480 shortDescriptionLabelConstraints.gridx = 0;
481 shortDescriptionLabelConstraints.gridy = 2;
482 shortDescriptionLabelConstraints.anchor = java.awt.GridBagConstraints.WEST;
483 shortDescriptionLabel.setText("Description:");
484 concernLabelConstraints.gridx = 0;
485 concernLabelConstraints.gridy = 5;
486 concernLabelConstraints.anchor = java.awt.GridBagConstraints.WEST;
487 concernLabel.setText("Concern:");
488 nameFieldConstraints.gridx = 1;
489 nameFieldConstraints.gridy = 1;
490 nameFieldConstraints.weightx = 1.0;
491 nameFieldConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
492 shortDescriptionFieldConstraints.gridx = 1;
493 shortDescriptionFieldConstraints.gridy = 2;
494 shortDescriptionFieldConstraints.weightx = 1.0;
495 shortDescriptionFieldConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
496 gridBagConstraints13.gridx = 1;
497 gridBagConstraints13.gridy = 2;
498 gridBagConstraints13.weightx = 1.0;
499 gridBagConstraints13.fill = java.awt.GridBagConstraints.HORIZONTAL;
500 viewpointLabelConstraints.gridx = 0;
501 viewpointLabelConstraints.gridy = 6;
502 viewpointLabelConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
503 viewpointLabel.setText("Viewpoint:");
504 fullDescriptionLabelConstraints.gridx = 0;
505 fullDescriptionLabelConstraints.gridy = 7;
506 fullDescriptionLabelConstraints.anchor = java.awt.GridBagConstraints.WEST;
507 fullDescriptionLabel.setText("Full description:");
508 descriptionScrollPaneConstraints.gridx = 0;
509 descriptionScrollPaneConstraints.gridy = 8;
510 descriptionScrollPaneConstraints.weightx = 1.0;
511 descriptionScrollPaneConstraints.weighty = 1.0;
512 descriptionScrollPaneConstraints.fill = java.awt.GridBagConstraints.BOTH;
513 descriptionScrollPaneConstraints.gridwidth = 2;
514 concernBoxConstraints.gridx = 1;
515 concernBoxConstraints.gridy = 5;
516 concernBoxConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
517 domainLabelConstraints.gridx = 0;
518 domainLabelConstraints.gridy = 4;
519 domainLabelConstraints.anchor = java.awt.GridBagConstraints.WEST;
520 domainLabel.setText("Domain:");
521 domainBoxConstraints.gridx = 1;
522 domainBoxConstraints.gridy = 4;
523 domainBoxConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
524 viewpointPanelConstraints.gridx = 1;
525 viewpointPanelConstraints.fill = java.awt.GridBagConstraints.BOTH;
526 viewpointPanelConstraints.gridy = 6;
527 viewpointPanelConstraints.anchor = java.awt.GridBagConstraints.WEST;
528 gridBagConstraints1.gridx = 0;
529 gridBagConstraints1.gridy = 0;
530 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
531 typeLabel.setText("Type:");
532 gridBagConstraints11.gridx = 1;
533 gridBagConstraints11.gridy = 0;
534 gridBagConstraints11.fill = java.awt.GridBagConstraints.HORIZONTAL;
535 gridBagConstraints6.gridx = 0;
536 gridBagConstraints6.gridy = 3;
537 gridBagConstraints6.anchor = java.awt.GridBagConstraints.WEST;
538 categoryLabel.setText("Category:");
539 gridBagConstraints7.gridx = 1;
540 gridBagConstraints7.gridy = 3;
541 gridBagConstraints7.weightx = 1.0;
542 gridBagConstraints7.fill = java.awt.GridBagConstraints.HORIZONTAL;
543 this.add(nameLabel, nameLabelConstraints);
544 this.add(getNameField(), nameFieldConstraints);
545 this.add(shortDescriptionLabel, shortDescriptionLabelConstraints);
546 this.add(getShortDescriptionField(), shortDescriptionFieldConstraints);
547 this.add(domainLabel, domainLabelConstraints);
548 this.add(getDomainBox(), domainBoxConstraints);
549 this.add(concernLabel, concernLabelConstraints);
550 this.add(getConcernBox(), concernBoxConstraints);
551 this.add(viewpointLabel, viewpointLabelConstraints);
552 this.add(getViewpointPanel(), viewpointPanelConstraints);
553 this.add(fullDescriptionLabel, fullDescriptionLabelConstraints);
554 this.add(getDescriptionScrollPane(), descriptionScrollPaneConstraints);
555 this.add(typeLabel, gridBagConstraints1);
556 this.add(getTypeBox(), gridBagConstraints11);
557 this.add(categoryLabel, gridBagConstraints6);
558 this.add(getCategoryField(), gridBagConstraints7);
559 }
560 /***
561 * This method initializes nameField
562 *
563 * @return javax.swing.JTextField
564 */
565 private JTextField getNameField() {
566 if (nameField == null) {
567 nameField = new JTextField();
568 nameField.setEditable(editable);
569 nameField.getDocument().addDocumentListener(docListener);
570
571 }
572 return nameField;
573 }
574
575 /***
576 * This method initializes shortDescriptionField
577 *
578 * @return javax.swing.JTextField
579 */
580 private JTextField getShortDescriptionField() {
581 if (shortDescriptionField == null) {
582 shortDescriptionField = new JTextField();
583 shortDescriptionField.setEditable(editable);
584 shortDescriptionField.getDocument().addDocumentListener(docListener);
585
586 }
587 return shortDescriptionField;
588 }
589 /***
590 * This method initializes fullDescriptionArea
591 *
592 * @return javax.swing.JTextArea
593 */
594 private CorasTextArea getFullDescriptionArea() {
595 if (fullDescriptionArea == null) {
596 fullDescriptionArea = new CorasTextArea();
597 fullDescriptionArea.setEditable(editable);
598 fullDescriptionArea.setWrapStyleWord(true);
599 fullDescriptionArea.setLineWrap(true);
600 fullDescriptionArea.getDocument().addDocumentListener(docListener);
601
602 }
603 return fullDescriptionArea;
604 }
605 /***
606 * This method initializes descriptionScrollPane
607 *
608 * @return javax.swing.JScrollPane
609 */
610 private JScrollPane getDescriptionScrollPane() {
611 if (descriptionScrollPane == null) {
612 descriptionScrollPane = new JScrollPane();
613 descriptionScrollPane.setViewportView(getFullDescriptionArea());
614 descriptionScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
615 }
616 return descriptionScrollPane;
617 }
618 /***
619 * This method initializes corasChoiceBox
620 *
621 * @return coras.client.ui.CorasChoiceBox
622 */
623 private CorasComboBox getConcernBox() {
624 if (concernBox == null) {
625 concernBox = new CorasComboBox();
626 concernBox.setEditable(editable);
627 concernBox.setModel(new DefaultComboBoxModel(ConcernEnum.VALUES.toArray()));
628 concernBox.addPropertyChangeListener(CorasComboBox.SELECTED_ITEM, propListener);
629 }
630 return concernBox;
631 }
632 /***
633 * This method initializes corasComboBox
634 *
635 * @return coras.client.ui.CorasComboBox
636 */
637 private CorasComboBox getDomainBox() {
638 if (domainBox == null) {
639 domainBox = new CorasComboBox();
640 domainBox.setEditable(editable);
641 domainBox.addPropertyChangeListener(CorasComboBox.SELECTED_ITEM, propListener);
642 }
643 return domainBox;
644 }
645 /***
646 * This method initializes viewpointPanel
647 *
648 * @return coras.client.ui.ViewpointPanel
649 */
650 private ViewpointPanel getViewpointPanel() {
651 if (viewpointPanel == null) {
652 viewpointPanel = new ViewpointPanel();
653 viewpointPanel.setEditable(editable);
654 viewpointPanel.addPropertyChangeListener(ViewpointPanel.SELECTED_VIEWPOINTS, propListener);
655 }
656 return viewpointPanel;
657 }
658 }