KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > editor > ResultEditor


1 package com.opensymphony.workflow.designer.editor;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.event.ActionListener JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import javax.swing.*;
7
8 import com.jgoodies.forms.layout.FormLayout;
9 import com.jgoodies.forms.layout.CellConstraints;
10 import com.jgoodies.forms.builder.PanelBuilder;
11 import com.opensymphony.workflow.designer.model.ConditionsTableModel;
12 import com.opensymphony.workflow.designer.model.FunctionsTableModel;
13 import com.opensymphony.workflow.designer.ResultEdge;
14 import com.opensymphony.workflow.designer.UIFactory;
15 import com.opensymphony.workflow.designer.ResourceManager;
16 import com.opensymphony.workflow.designer.beanutils.BeanConnector;
17 import com.opensymphony.workflow.loader.*;
18
19 public class ResultEditor extends DetailPanel implements ActionListener JavaDoc
20 {
21   private static final String JavaDoc[] BUTTONS = new String JavaDoc[]{"add", "remove", "edit"};
22
23   private JTextField id = UIFactory.createReadOnlyTextField(12);
24   private JTextField owner = new JTextField(12);
25   private JComboBox status = new JComboBox();
26   private JComboBox oldStatus = new JComboBox();
27   private FunctionsTableModel preFunctionsModel = new FunctionsTableModel();
28   private JTable preFunctionsTable;
29   private FunctionsTableModel postFunctionsModel = new FunctionsTableModel();
30   private JTable postFunctionsTable;
31   private JComboBox type = new JComboBox(new String JavaDoc[]{"AND", "OR"});
32   private ConditionsTableModel conditionsModel = new ConditionsTableModel();
33   private JTable conditionsTable;
34   private JPanel panel;
35   private BeanConnector connector = new BeanConnector();
36
37   public ResultEditor()
38   {
39   }
40
41
42   protected void initComponents()
43   {
44     String JavaDoc colLayout = "2dlu, max(30dlu;pref), 2dlu, pref:grow, 4dlu";
45
46     String JavaDoc rowLayout = "pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 4dlu,pref, 2dlu, 40dlu:grow, pref, 2dlu, pref, 2dlu, 40dlu:grow, pref, 2dlu";
47
48     // ResultEdge edge = (ResultEdge)getEdge();
49
// ResultDescriptor descriptor = edge.getDescriptor();
50
// if(isConditional()){
51
rowLayout += ", pref, 2dlu, pref, 2dlu, 40dlu:grow, pref, 2dlu";
52     // }
53

54     FormLayout layout = new FormLayout(colLayout, rowLayout);
55
56     PanelBuilder builder = new PanelBuilder(this, layout);
57     CellConstraints cc = new CellConstraints();
58
59     builder.addSeparator(ResourceManager.getString("info"), cc.xywh(2, 1, 3, 1));
60
61     builder.addLabel(ResourceManager.getString("id"), cc.xy(2, 3));
62     builder.add(id, cc.xy(4, 3));
63     connector.connect(id, "id");
64
65     builder.addLabel(ResourceManager.getString("owner"), cc.xy(2, 5));
66     connector.connect(owner, "owner");
67     builder.add(owner, cc.xy(4, 5));
68
69     builder.addLabel(ResourceManager.getString("status"), cc.xy(2, 7));
70     builder.add(status, cc.xy(4, 7));
71     connector.connect(status, "status");
72
73     builder.addLabel(ResourceManager.getString("status.old"), cc.xy(2, 9));
74     builder.add(oldStatus, cc.xy(4, 9));
75     connector.connect(oldStatus, "oldStatus");
76
77     builder.addSeparator(ResourceManager.getString("prefunctions"), cc.xywh(2, 11, 3, 1));
78     preFunctionsTable = new JTable(preFunctionsModel);
79     preFunctionsModel.setGraphModel(getModel());
80     builder.add(UIFactory.createTablePanel(preFunctionsTable), cc.xywh(2, 13, 3, 1));
81     builder.add(UIFactory.getAddRemovePropertiesBar(this, "pre", BUTTONS), cc.xywh(2, 14, 3, 1));
82
83     builder.addSeparator(ResourceManager.getString("postfunctions"), cc.xywh(2, 16, 3, 1));
84     postFunctionsTable = new JTable(postFunctionsModel);
85     postFunctionsModel.setGraphModel(getModel());
86     builder.add(UIFactory.createTablePanel(postFunctionsTable), cc.xywh(2, 18, 3, 1));
87     builder.add(UIFactory.getAddRemovePropertiesBar(this, "post", BUTTONS), cc.xywh(2, 19, 3, 1));
88
89     // if(isConditional()){
90
builder.addSeparator(ResourceManager.getString("conditions"), cc.xywh(2, 21, 3, 1));
91
92     builder.addLabel(ResourceManager.getString("type"), cc.xy(2, 23));
93     connector.connect(type, "conditionType");
94     builder.add(type, cc.xy(4, 23));
95
96     conditionsTable = new JTable(conditionsModel);
97     conditionsModel.setGraphModel(getModel());
98     conditionsModel.setType(ConditionsTableModel.RESULT);
99     builder.add(UIFactory.createTablePanel(conditionsTable), cc.xywh(2, 25, 3, 1));
100     panel = UIFactory.getAddRemovePropertiesBar(this, "condition", BUTTONS);
101     builder.add(panel, cc.xywh(2, 26, 3, 1));
102     // }
103
}
104
105   public String JavaDoc getTitle()
106   {
107     return ResourceManager.getString("title.result", new Object JavaDoc[]{id.getText()});
108   }
109
110   protected void updateView()
111   {
112     PaletteDescriptor palette = getModel().getPalette();
113     status.setModel(new DefaultComboBoxModel(palette.getStatusNames()));
114     oldStatus.setModel(new DefaultComboBoxModel(palette.getStatusNames()));
115     ResultEdge result = (ResultEdge)getEdge();
116     ResultDescriptor descriptor = result.getDescriptor();
117
118     preFunctionsModel.setList(descriptor.getPreFunctions());
119     preFunctionsTable.getSelectionModel().clearSelection();
120
121     postFunctionsModel.setList(descriptor.getPostFunctions());
122     postFunctionsTable.getSelectionModel().clearSelection();
123
124     if(isConditional())
125     {
126       setConditional(true);
127       ConditionalResultDescriptor cond = (ConditionalResultDescriptor)descriptor;
128       if(cond.getConditions().size() == 0)
129       {
130         ConditionsDescriptor conditions = new ConditionsDescriptor();
131         conditions.setParent(cond);
132         cond.getConditions().add(conditions);
133       }
134       conditionsModel.setList(((ConditionsDescriptor)cond.getConditions().get(0)).getConditions());
135       conditionsTable.getSelectionModel().clearSelection();
136     }
137     else
138     {
139       setConditional(false);
140     }
141     connector.setSource(descriptor);
142   }
143
144   public void actionPerformed(ActionEvent JavaDoc e)
145   {
146     String JavaDoc command = e.getActionCommand().toLowerCase();
147     if("preadd".equals(command))
148     {
149       preadd();
150     }
151     else if("preremove".equals(command))
152     {
153       preremove();
154     }
155     else if("preedit".equals(command))
156     {
157       premodify();
158     }
159     else if("postadd".equals(command))
160     {
161       postadd();
162     }
163     else if("postremove".equals(command))
164     {
165       postremove();
166     }
167     else if("postedit".equals(command))
168     {
169       postmodify();
170     }
171     else if("conditionadd".equals(command))
172     {
173       add();
174     }
175     else if("conditionremove".equals(command))
176     {
177       remove();
178     }
179     else if("conditionedit".equals(command))
180     {
181       modify();
182     }
183   }
184
185   private void preadd()
186   {
187     ResultFunctionEditor editor = new ResultFunctionEditor((ResultEdge)getEdge());
188     editor.setModel(getModel());
189     FunctionDescriptor func = editor.add();
190     if(func != null)
191     {
192       this.preFunctionsModel.add(func);
193     }
194   }
195
196   private void preremove()
197   {
198     int[] rows = preFunctionsTable.getSelectedRows();
199     for(int i = 0; i < rows.length; i++)
200     {
201       preFunctionsModel.remove(rows[i]);
202     }
203   }
204
205   private void premodify()
206   {
207     int[] rows = preFunctionsTable.getSelectedRows();
208     for(int i = 0; i < rows.length; i++)
209     {
210       premodify(rows[i]);
211     }
212   }
213
214   private void premodify(int selected)
215   {
216     FunctionDescriptor func = (FunctionDescriptor)preFunctionsModel.get(selected);
217
218     ResultFunctionEditor editor = new ResultFunctionEditor((ResultEdge)getEdge());
219     editor.setModel(getModel());
220     editor.modify(func);
221
222     preFunctionsModel.fireTableDataChanged();
223   }
224
225   private void postadd()
226   {
227     ResultFunctionEditor editor = new ResultFunctionEditor((ResultEdge)getEdge());
228     editor.setModel(getModel());
229     FunctionDescriptor func = editor.add();
230     if(func != null)
231     {
232       postFunctionsModel.add(func);
233     }
234   }
235
236   private void postremove()
237   {
238     int[] rows = postFunctionsTable.getSelectedRows();
239     for(int i = 0; i < rows.length; i++)
240     {
241       postFunctionsModel.remove(rows[i]);
242     }
243   }
244
245   private void postmodify()
246   {
247     int[] rows = postFunctionsTable.getSelectedRows();
248     for(int i = 0; i < rows.length; i++)
249     {
250       postmodify(rows[i]);
251     }
252   }
253
254   private void postmodify(int selected)
255   {
256     FunctionDescriptor func = (FunctionDescriptor)postFunctionsModel.get(selected);
257
258     ResultFunctionEditor editor = new ResultFunctionEditor((ResultEdge)getEdge());
259     editor.setModel(getModel());
260     editor.modify(func);
261
262     postFunctionsModel.fireTableDataChanged();
263   }
264
265   private boolean isConditional()
266   {
267     ResultEdge edge = (ResultEdge)getEdge();
268     ResultDescriptor result = edge.getDescriptor();
269
270     return (result instanceof ConditionalResultDescriptor);
271   }
272
273   private void add()
274   {
275     ResultConditionEditor editor = new ResultConditionEditor((ResultEdge)getEdge());
276     editor.setModel(getModel());
277     ConditionDescriptor cond = editor.add();
278     if(cond != null)
279     {
280       conditionsModel.add(cond);
281     }
282   }
283
284   private void remove()
285   {
286     int[] rows = conditionsTable.getSelectedRows();
287     for(int i = 0; i < rows.length; i++)
288     {
289       conditionsModel.remove(rows[i]);
290     }
291   }
292
293   private void modify()
294   {
295     int[] rows = conditionsTable.getSelectedRows();
296     for(int i = 0; i < rows.length; i++)
297     {
298       modify(rows[i]);
299     }
300   }
301
302   private void modify(int selected)
303   {
304     ConditionDescriptor cond = (ConditionDescriptor)conditionsModel.get(selected);
305
306     ResultConditionEditor editor = new ResultConditionEditor((ResultEdge)getEdge());
307     editor.setModel(getModel());
308     editor.modify(cond);
309
310     conditionsModel.fireTableDataChanged();
311   }
312
313   private void setConditional(boolean enabled)
314   {
315     type.setEnabled(enabled);
316     conditionsTable.setEnabled(enabled);
317     Component JavaDoc[] comps = panel.getComponents();
318     for(int i = 0; i < comps.length; i++)
319     {
320       comps[i].setEnabled(enabled);
321     }
322   }
323 }
Popular Tags