KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.opensymphony.workflow.designer.editor;
2
3 import java.awt.event.ActionEvent JavaDoc;
4 import java.awt.event.ActionListener JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import javax.swing.*;
7
8 import com.jgoodies.forms.builder.PanelBuilder;
9 import com.jgoodies.forms.layout.CellConstraints;
10 import com.jgoodies.forms.layout.FormLayout;
11 import com.opensymphony.workflow.designer.StepCell;
12 import com.opensymphony.workflow.designer.UIFactory;
13 import com.opensymphony.workflow.designer.ResourceManager;
14 import com.opensymphony.workflow.designer.beanutils.BeanConnector;
15 import com.opensymphony.workflow.designer.model.ConditionsTableModel;
16 import com.opensymphony.workflow.designer.model.FunctionsTableModel;
17 import com.opensymphony.workflow.loader.*;
18
19 public class StepEditor 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 name = new JTextField(12);
25   private JTextField view = new JTextField(12);
26   private JCheckBox auto = new JCheckBox();
27   private JComboBox restrict = new JComboBox(new String JavaDoc[]{"AND", "OR"});
28
29   private ConditionsTableModel conditionsModel = new ConditionsTableModel();
30   private JTable conditionsTable;
31
32   private FunctionsTableModel preModel = new FunctionsTableModel();
33   private JTable pre;
34
35   private FunctionsTableModel postModel = new FunctionsTableModel();
36   private JTable post;
37
38   private BeanConnector connector = new BeanConnector();
39
40   public StepEditor()
41   {
42   }
43
44   protected void initComponents()
45   {
46     FormLayout layout = new FormLayout("2dlu, max(30dlu;pref), 2dlu, pref:grow, 4dlu", "pref, 2dlu, " + // 1 Info
47
"pref, 2dlu, " + // 3 ID
48
"pref, 2dlu, " + // 5 Name
49
"pref, 2dlu, " + // 7 View
50
"pref, 2dlu, " + // 9 auto
51
"pref, 2dlu, " + // 11 permission
52
"pref, 2dlu, " + // 13 type
53
"40dlu:grow, " + // 15 table
54
"pref, 2dlu, " + // 16 buttons
55
"pref, 2dlu, " + // 18 pre-function
56
"40dlu:grow, " + // 20 table
57
"pref, 2dlu, " + // 21 buttons
58
"pref, 2dlu, " + // 23 post-function
59
"40dlu:grow, " + // 25 table
60
"pref, 2dlu"); // 26 buttons
61

62     PanelBuilder builder = new PanelBuilder(this, layout);
63     CellConstraints cc = new CellConstraints();
64
65     builder.addSeparator(ResourceManager.getString("info"), cc.xywh(2, 1, 3, 1));
66
67     builder.addLabel(ResourceManager.getString("id"), cc.xy(2, 3));
68     connector.connect(id, "id");
69     builder.add(id, cc.xy(4, 3));
70
71     builder.addLabel(ResourceManager.getString("name"), cc.xy(2, 5));
72     connector.connect(name, "name");
73     builder.add(name, cc.xy(4, 5));
74
75     builder.addLabel(ResourceManager.getString("view"), cc.xy(2, 7));
76     connector.connect(view, "actions[0].view");
77     builder.add(view, cc.xy(4, 7));
78
79     builder.addLabel(ResourceManager.getString("auto"), cc.xy(2, 9));
80     connector.connect(auto, "actions[0].autoExecute");
81     builder.add(auto, cc.xy(4, 9));
82
83     builder.addSeparator(ResourceManager.getString("permissions"), cc.xywh(2, 11, 3, 1));
84
85     builder.addLabel(ResourceManager.getString("type"), cc.xy(2, 13));
86     connector.connect(restrict, "restriction/conditionType");
87     builder.add(restrict, cc.xy(4, 13));
88
89     conditionsTable = new JTable(conditionsModel);
90     conditionsModel.setType(ConditionsTableModel.PERMISSION);
91     conditionsModel.setGraphModel(getModel());
92     builder.add(UIFactory.createTablePanel(conditionsTable), cc.xywh(2, 15, 3, 1));
93     builder.add(UIFactory.getAddRemovePropertiesBar(this, "permission", BUTTONS), cc.xywh(2, 16, 3, 1));
94
95     builder.addSeparator(ResourceManager.getString("prefunctions"), cc.xywh(2, 18, 3, 1));
96
97     pre = new JTable(preModel);
98     preModel.setGraphModel(getModel());
99     builder.add(UIFactory.createTablePanel(pre), cc.xywh(2, 20, 3, 1));
100     builder.add(UIFactory.getAddRemovePropertiesBar(this, "pre", BUTTONS), cc.xywh(2, 21, 3, 1));
101
102     builder.addSeparator(ResourceManager.getString("postfunctions"), cc.xywh(2, 23, 3, 1));
103
104     post = new JTable(postModel);
105     postModel.setGraphModel(getModel());
106     builder.add(UIFactory.createTablePanel(post), cc.xywh(2, 25, 3, 1));
107     builder.add(UIFactory.getAddRemovePropertiesBar(this, "post", BUTTONS), cc.xywh(2, 26, 3, 1));
108   }
109
110   public void actionPerformed(ActionEvent JavaDoc e)
111   {
112     String JavaDoc command = e.getActionCommand().toLowerCase();
113     if(command.equals("permissionadd"))
114     {
115       add();
116     }
117     else if(command.equals("permissionremove"))
118     {
119       remove();
120     }
121     else if(command.equals("permissionedit"))
122     {
123       modify();
124     }
125     else if(command.equals("preadd"))
126     {
127       preadd();
128     }
129     else if(command.equals("preremove"))
130     {
131       preremove();
132     }
133     else if(command.equals("preedit"))
134     {
135       premodify();
136     }
137     else if(command.equals("postadd"))
138     {
139       postadd();
140     }
141     else if(command.equals("postremove"))
142     {
143       postremove();
144     }
145     else if(command.equals("postedit"))
146     {
147       postmodify();
148     }
149   }
150
151   public String JavaDoc getTitle()
152   {
153     return ResourceManager.getString("title.step", new Object JavaDoc[]{id.getText()});
154   }
155
156   protected void viewClosed()
157   {
158     if(conditionsTable.isEditing())
159     {
160       conditionsTable.getCellEditor().stopCellEditing();
161     }
162   }
163
164   protected void updateView()
165   {
166     StepCell cell = (StepCell)getCell();
167     StepDescriptor stepDescriptor = cell.getDescriptor();
168
169     ActionDescriptor firstAction;
170
171     //todo need to do this in some way that handles multiple actions
172
if(stepDescriptor.getActions().size()>0)
173     {
174       firstAction = (ActionDescriptor)stepDescriptor.getActions().get(0);
175     }
176     else
177     {
178       firstAction = null;
179     }
180
181     if(firstAction!=null)
182     {
183       RestrictionDescriptor rd = firstAction.getRestriction();
184       if(rd==null)
185       {
186         rd = new RestrictionDescriptor();
187         rd.setParent(firstAction);
188         ConditionsDescriptor conditions = new ConditionsDescriptor();
189         rd.getConditions().add(conditions);
190         conditions.setParent(rd);
191         conditions.setType((String JavaDoc)restrict.getSelectedItem());
192         firstAction.setRestriction(rd);
193       }
194       //todo no nested conditions allowed
195
conditionsModel.setList(((ConditionsDescriptor)rd.getConditions().get(0)).getConditions());
196     }
197     else
198     {
199       conditionsModel.setList(new ArrayList JavaDoc());
200     }
201     conditionsTable.getSelectionModel().clearSelection();
202
203     preModel.setList(firstAction==null ? new ArrayList JavaDoc() : firstAction.getPreFunctions());
204     pre.getSelectionModel().clearSelection();
205
206     postModel.setList(firstAction==null ? new ArrayList JavaDoc() : firstAction.getPostFunctions());
207     post.getSelectionModel().clearSelection();
208
209     connector.setSource(stepDescriptor);
210   }
211
212   private void add()
213   {
214     StepPermissionEditor editor = new StepPermissionEditor((StepCell)getCell());
215     editor.setModel(getModel());
216     ConditionDescriptor cond = editor.add();
217     if(cond != null)
218     {
219       conditionsModel.add(cond);
220     }
221   }
222
223   private void remove()
224   {
225     int[] rows = conditionsTable.getSelectedRows();
226     for(int i = 0; i < rows.length; i++)
227     {
228       conditionsModel.remove(rows[i]);
229     }
230   }
231
232   private void modify()
233   {
234     int[] rows = conditionsTable.getSelectedRows();
235     for(int i = 0; i < rows.length; i++)
236     {
237       modify(rows[i]);
238     }
239   }
240
241   private void modify(int selected)
242   {
243     ConditionDescriptor cond = (ConditionDescriptor)conditionsModel.get(selected);
244
245     StepPermissionEditor editor = new StepPermissionEditor((StepCell)getCell());
246     editor.setModel(getModel());
247     editor.modify(cond);
248
249     conditionsModel.fireTableDataChanged();
250   }
251
252   private void preadd()
253   {
254     StepFunctionEditor editor = new StepFunctionEditor((StepCell)getCell());
255     editor.setModel(getModel());
256     FunctionDescriptor func = editor.add();
257     if(func != null)
258     {
259       preModel.add(func);
260     }
261   }
262
263   private void preremove()
264   {
265     int[] rows = pre.getSelectedRows();
266     for(int i = 0; i < rows.length; i++)
267     {
268       preModel.remove(rows[i]);
269     }
270   }
271
272   private void premodify()
273   {
274     int[] rows = pre.getSelectedRows();
275     for(int i = 0; i < rows.length; i++)
276     {
277       premodify(rows[i]);
278     }
279   }
280
281   private void premodify(int selected)
282   {
283     FunctionDescriptor func = (FunctionDescriptor)preModel.get(selected);
284
285     StepFunctionEditor editor = new StepFunctionEditor((StepCell)getCell());
286     editor.setModel(getModel());
287     editor.modify(func);
288
289     preModel.fireTableDataChanged();
290   }
291
292   private void postadd()
293   {
294     StepFunctionEditor editor = new StepFunctionEditor((StepCell)getCell());
295     editor.setModel(getModel());
296     FunctionDescriptor func = editor.add();
297     if(func != null)
298     {
299       postModel.add(func);
300     }
301   }
302
303   private void postremove()
304   {
305     int[] rows = post.getSelectedRows();
306     for(int i = 0; i < rows.length; i++)
307     {
308       postModel.remove(rows[i]);
309     }
310   }
311
312   private void postmodify()
313   {
314     int[] rows = post.getSelectedRows();
315     for(int i = 0; i < rows.length; i++)
316     {
317       postmodify(rows[i]);
318     }
319   }
320
321   private void postmodify(int selected)
322   {
323     FunctionDescriptor func = (FunctionDescriptor)postModel.get(selected);
324
325     StepFunctionEditor editor = new StepFunctionEditor((StepCell)getCell());
326     editor.setModel(getModel());
327     editor.modify(func);
328
329     postModel.fireTableDataChanged();
330   }
331 }
Popular Tags