View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui.gui;
7   
8   import javax.swing.*;
9   import javax.swing.border.TitledBorder;
10  import java.awt.BorderLayout;
11  import java.util.List;
12  
13  import net.kwfgrid.gworkflowdl.protocol.structure.IStructureObject;
14  import net.kwfgrid.gworkflowdl.protocol.structure.ProtocolWorkflow;
15  
16  public abstract class AbstractJLabelView extends AbstractWorkflowElementView {
17      public JPanel _view;
18      public JLabel _label;
19  
20      /***
21         Constructor.
22         Adds this view as a <code>IStructureListener</code> to the specified workflow.
23         @param target The document containing the displayed element.
24         @param element The displayed element.
25       */
26      protected AbstractJLabelView(ProtocolWorkflow target, IStructureObject element) {
27  	super(target, element);
28  	_view = null;
29  	_label = null;
30      }
31  
32      /***
33         Get the label to be displayed by this view.
34       */
35      protected abstract String getLabel();
36  
37      /***
38         Get the title for this view.
39       */
40      protected abstract String getTitle();
41  
42      /***
43         Check if a "objectsAdded" or "objectsRemoved" modification of the structure affects the label displayed by this view.
44       */
45      protected abstract boolean affectsLabel(IStructureObject parent, String namespace, String name, List objects);
46  
47      /***
48         Check if a "propertyChanged" modification of the structure affects the label displayed by this view.
49       */
50      protected abstract boolean affectsLabel(IStructureObject parent, String namespace, String name, Object object);
51  
52      public void objectsAdded(IStructureObject parent, String namespace, String name, List objects) {
53  	if (affectsLabel(parent, namespace, name, objects)) updateView();
54      }
55      
56      public void objectsRemoved(IStructureObject parent, String namespace, String name, List objects) {
57  	if (affectsLabel(parent, namespace, name, objects)) updateView();
58      }
59      
60      public void propertyChanged(IStructureObject parent, String namespace, String name, Object newvalue) {
61  	if (affectsLabel(parent, namespace, name, newvalue)) updateView();
62      }    
63  
64      protected void updateState() {
65  	_view.setEnabled(isEnabled());
66  	_label.setEnabled(isEnabled());
67      }
68  
69      protected void updateView() {
70  	updateState();
71  	_label.setText(getLabel());
72  	_view.invalidate();
73  	_view.setSize(_view.getLayout().preferredLayoutSize(_view));
74  	_view.doLayout();
75  	_view.validate();		
76      }
77  
78      public JComponent getView() {
79  	if (_view == null) {
80  	    _label = new JLabel(getLabel());
81  	    _view = new JPanel();
82  	    _view.setBorder(createBorder(getTitle()));
83  	    _view.setLayout(new BorderLayout());
84  	    _view.add(_label, BorderLayout.CENTER);
85  	}
86  	return _view;
87      }
88  }