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 org.glassbox.Theme;
9   import org.glassbox.gui.Visible;
10  
11  import net.kwfgrid.gwui.SwingFactory2;
12  import net.kwfgrid.gwui.GWUI;
13  import net.kwfgrid.gwui.workflow.XMLUtilities;
14  import net.kwfgrid.gworkflowdl.structure.Transition;
15  import net.kwfgrid.gworkflowdl.protocol.structure.*;
16  
17  import java.util.List;
18  import java.awt.GridBagLayout;
19  import java.awt.GridBagConstraints;
20  import java.awt.Insets;
21  import java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  import java.awt.event.MouseEvent;
24  import java.awt.event.MouseListener;
25  import java.awt.event.MouseAdapter;
26  import java.awt.event.ItemEvent;
27  import java.awt.event.ItemListener;
28  import java.io.*;
29  import javax.swing.*;
30  import javax.swing.border.TitledBorder;
31  import javax.swing.event.DocumentListener;
32  import javax.swing.event.DocumentEvent;
33  
34  import org.apache.log4j.Logger;
35  
36  /***
37     A view for status information of a transition.
38   */
39  public class TransitionInfoView extends AbstractWorkflowElementView {
40      private static final Logger logger = Logger.getLogger(TransitionInfoView.class);
41  
42      private static final String WARNING_ICON_KEY = "kwfgrid.TransitionWarningIcon.icon";
43      private static final Icon WARNING_ICON = Theme.getIcon(WARNING_ICON_KEY);
44  
45      private boolean _transitionenabled;
46  
47      private JComponent _view;
48      private JLabel _label_type;    
49      private JLabel _label_operationname;
50      private JLabel _label_warning;
51  
52      
53      public TransitionInfoView(ProtocolWorkflow workflow, ProtocolTransition element) {
54  	super(workflow, element);
55  	_view = null;
56  	_transitionenabled = element.isEnabled();
57      }
58  
59      public void objectsAdded(IStructureObject parent, String namespace, String name, List objects) {
60  	if (affectsInfo(parent)) updateView();
61      }
62      
63      public void objectsRemoved(IStructureObject parent, String namespace, String name, List objects) {
64  	if (affectsInfo(parent)) updateView();
65      }
66      
67      public void propertyChanged(IStructureObject parent, String namespace, String name, Object newvalue) {
68  	if (affectsInfo(parent)) updateView();
69      }
70      
71      private boolean affectsInfo(IStructureObject parent) {
72  	if (_transitionenabled != ((Transition)getElement()).isEnabled()) 
73  	    return true;
74  	while (parent instanceof IChildObject) {
75  	    if (parent == getElement()) return true;
76  	    parent = ((IChildObject)parent).getParent();
77  	}
78  	return false;
79      }
80      
81      protected void updateState() {
82  	//_label_operationname.setEnabled(isEnabled());
83  	_label_type.setEnabled(isEnabled());
84  	_label_warning.setEnabled(isEnabled());
85      }
86      
87      /* TEMPORARY! FIXME */
88      protected boolean needsDecision(Transition t) {
89  	if (t.isEnabled()) {
90  	    String[] keys = t.getProperties().keys(); 
91  	    for (int i=0; i<keys.length; i++) {
92  		String prop = t.getProperties().get(keys[i]);
93  		if (keys[i].indexOf("decision")==0) {
94  		    return true;
95  		}
96  	    }
97  	}
98  	return false;
99      }
100 
101     /* TEMPORARY! FIXME */
102     protected boolean wctRefinementFailed(Transition t) {
103 	if (t.isEnabled()) {
104 	    String[] keys = t.getProperties().keys(); 
105 	    for (int i=0; i<keys.length; i++) {
106 		String prop = t.getProperties().get(keys[i]);
107 		if (keys[i].indexOf("wct.refinement.failed")==0) {
108 		    return true;
109 		}
110 	    }
111 	}
112 	return false;
113     }
114 
115     /* TEMPORARY! FIXME */
116     protected boolean aabRefinementFailed(Transition t) {
117 	if (t.isEnabled()) {
118 	    String[] keys = t.getProperties().keys(); 
119 	    for (int i=0; i<keys.length; i++) {
120 		String prop = t.getProperties().get(keys[i]);
121 		if (keys[i].indexOf("aab.refinement.failed")==0) {
122 		    return true;
123 		}
124 	    }
125 	}
126 	return false;
127     }
128 
129     /* TEMPORARY! FIXME (copied from TransitionNode) */
130 
131     protected void updateView() {
132 	updateState();
133 	// _view.invalidate();
134 
135 	Transition transition = (Transition)getElement();
136 	_transitionenabled = transition.isEnabled();
137 	_label_type.setText(XMLUtilities.getAbstractionLevelDescription(transition));
138 
139 	StringBuffer t = new StringBuffer();
140 	boolean f = true;
141 	if (needsDecision(transition)) {
142 	    f = false;
143 	    t.append("<html>");
144 	    t.append("Transition is involved in a Conflict.<br>It can be solved by manipulating the conditions.");
145 	}
146 	if (wctRefinementFailed(transition)) {
147 	    if (!f) t.append("<br>");
148 	    else t.append("<html>");
149 	    f = false;
150 	    t.append("Transition could not be refined by WCT.<br>Please provide output Token(s) and remove input Token(s) manually.");
151 	}
152 	if (aabRefinementFailed(transition)) {
153 	    if (!f) t.append("<br>");
154 	    else t.append("<html>");
155 	    f = false;
156 	    t.append("Transition could not be refined by AAB.<br>Please provide output Token(s) and remove input Token(s) manually.");
157 	}
158 	if (t.length()>0) {
159 	    _label_warning.setText(t.toString());
160  	    _label_warning.setVisible(true);
161 	} else {
162  	    _label_warning.setVisible(false);
163 	}
164 
165 	//_view.setSize(_view.getLayout().preferredLayoutSize(_view));
166 	//_view.doLayout();
167 	//_view.validate();
168     }
169 
170     public JComponent getView() {
171 	if (_view==null) {
172 	    Transition transition = (Transition)getElement();
173 
174 	    _label_type = new JLabel(XMLUtilities.getAbstractionLevelDescription(transition));
175 
176 	    _label_warning = new JLabel(WARNING_ICON);
177 
178 	    GridBagLayout gridbag = new GridBagLayout();
179 	    GridBagConstraints c = new GridBagConstraints();
180 
181 	    _view = new JPanel();
182 	    _view.setBorder(createBorder("Info"));
183 	    _view.setLayout(gridbag);
184 
185 	    c.fill = GridBagConstraints.BOTH;
186 	    c.insets = new Insets(5, 5, 0, 5);
187 	    c.anchor = GridBagConstraints.WEST;
188 	    c.weightx = 1.0;
189 	    c.weighty = 0.0;
190 	    c.gridx = 0;
191 	    c.gridy = 0;
192 	    c.gridwidth = GridBagConstraints.REMAINDER;
193 
194 	    _view.add(_label_type, c);
195 
196 	    c.weightx = 1.0;
197 	    c.gridx = 0;
198 	    c.gridy = 1;
199 	    c.gridwidth = GridBagConstraints.REMAINDER;
200 
201 	    //_view.add(_label_operationname, c);
202 	    
203 	    c.gridx = 0;
204 	    //c.gridy = 2;
205 	    c.fill = GridBagConstraints.NONE;
206 	    c.gridwidth = GridBagConstraints.REMAINDER;
207 
208 	    _view.add(_label_warning, c);
209 
210 	    updateView();
211 	}
212 	return _view;
213     }    
214 }
215