]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ProcessParametersView.java
git-svn-id: https://svn.argeo.org/slc/trunk@4431 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / views / ProcessParametersView.java
1 package org.argeo.slc.client.ui.views;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.slc.client.ui.ClientUiPlugin;
10 import org.argeo.slc.client.ui.providers.ProcessParametersEditingSupport;
11 import org.argeo.slc.core.execution.PrimitiveAccessor;
12 import org.argeo.slc.core.execution.RefValue;
13 import org.argeo.slc.process.RealizedFlow;
14 import org.eclipse.jface.viewers.IStructuredContentProvider;
15 import org.eclipse.jface.viewers.ITableLabelProvider;
16 import org.eclipse.jface.viewers.LabelProvider;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.TableViewerColumn;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.ui.part.ViewPart;
25
26 /**
27 *
28 * @author bsinou
29 *
30 * This view, directly linked with the <code> ProcessBuilderView </code>
31 * enables the display and editing the parameters of a given process.
32 *
33 *
34 * Note that for a given RealizedFlow :
35 *
36 * + paramaters value are set using
37 * <code>RealizedFlow.ExecutionFlowDescriptor.values</code>, that might
38 * have default values
39 *
40 * + possible "values" for a given parameters are stored in
41 * <code>RealizedFlow.ExecutionSpec.</code>
42 *
43 */
44 public class ProcessParametersView extends ViewPart {
45 private static final Log log = LogFactory
46 .getLog(ProcessParametersView.class);
47
48 public static final String ID = "org.argeo.slc.client.ui.processParametersView";
49
50 // This map stores actual values :
51 // * default values (if defined) at instantiation time
52 // * values filled-in or modified by the end-user
53 private Map<String, Object> values;
54
55 // This map stores the spec of the attributes used to offer the end user
56 // some choices.
57 // private Map<String, ExecutionSpecAttribute> specAttributes;
58
59 // We must keep a reference to the current EditingSupport so that we can
60 // update the index of the process being updated
61 ProcessParametersEditingSupport ppEditingSupport;
62
63 // view attributes
64 private TableViewer viewer;
65
66 public void createPartControl(Composite parent) {
67 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
68 | SWT.V_SCROLL | SWT.FULL_SELECTION);
69 createColumns(viewer);
70
71 // WARNING
72 // for the moment being, we support only one process builder at a time
73 // we set the corresponding view in the editor here.
74 ProcessBuilderView pbView = (ProcessBuilderView) ClientUiPlugin
75 .getDefault().getWorkbench().getActiveWorkbenchWindow()
76 .getActivePage().findView(ProcessBuilderView.ID);
77 ppEditingSupport.setCurrentProcessBuilderView(pbView);
78
79 viewer.setLabelProvider(new ViewLabelProvider());
80 viewer.setContentProvider(new ViewContentProvider());
81 viewer.setInput(getViewSite());
82
83 }
84
85 // This will create the columns for the table
86 private void createColumns(TableViewer viewer) {
87
88 String[] titles = { "Attribute name", "value" };
89 int[] bounds = { 200, 200 };
90
91 for (int i = 0; i < titles.length; i++) {
92 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
93 column.getColumn().setText(titles[i]);
94 column.getColumn().setWidth(bounds[i]);
95 column.getColumn().setResizable(true);
96 column.getColumn().setMoveable(true);
97 if (i == 1) {
98 // we create the used EditingSupport and enable editing support
99 // for value Column
100 ppEditingSupport = new ProcessParametersEditingSupport(viewer,
101 i);
102 column.setEditingSupport(ppEditingSupport);
103 }
104 }
105 Table table = viewer.getTable();
106 table.setHeaderVisible(true);
107 table.setLinesVisible(true);
108
109 }
110
111 public void setFocus() {
112 viewer.getControl().setFocus();
113 }
114
115 // set class attributes, refresh the lists of process paramaters to edit.
116 public void setRealizedFlow(int index, RealizedFlow rf) {
117 // force the cleaning of the view
118 if (index == -1) {
119 viewer.setInput(null);
120 return;
121 }
122 // we store the index of the edited Process in the editor so that it can
123 // save entries modified by the end user.
124 ppEditingSupport.setCurrentProcessIndex(index);
125
126 // We also store corresponding ExecutionSpec to be able to retrieve
127 // possible values for dropdown lists
128 ppEditingSupport.setCurrentExecutionSpec(rf.getExecutionSpec());
129 // ExecutionSpec es = rf.getExecutionSpec();
130 // if (es != null && es.getAttributes() != null)
131 // parameters = es.getAttributes();
132 // if (parameters != null)
133 // viewer.setInput(parameters);
134
135 values = rf.getFlowDescriptor().getValues();
136 // specAttributes = rf.getFlowDescriptor().getExecutionSpec()
137 // .getAttributes();
138
139 if (values != null)
140 viewer.setInput(values);
141 else
142 // No parameters to edit, we reset the view.
143 viewer.setInput(null);
144
145 }
146
147 // Inner Classes we should use ExecutionSpecAttribute instead of values
148 // see below
149 protected class ViewContentProvider implements IStructuredContentProvider {
150 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
151 }
152
153 public void dispose() {
154 }
155
156 @SuppressWarnings({ "unchecked", "rawtypes" })
157 // we cast the Map<String, Object> to List<Object>
158 public Object[] getElements(Object obj) {
159
160 if (obj instanceof Map && ((Map) obj).size() != 0) {
161 List<ObjectWithName> list = new ArrayList<ObjectWithName>();
162 Map<String, Object> map = (Map<String, Object>) obj;
163 for (String key : map.keySet()) {
164 list.add(new ObjectWithName(key, map.get(key)));
165 }
166 return list.toArray();
167 } else {
168 return new Object[0];
169 }
170 }
171 }
172
173 protected class ViewLabelProvider extends LabelProvider implements
174 ITableLabelProvider {
175
176 public String getColumnText(Object obj, int index) {
177 // NOTE : the passed object is a line of the table !!!
178
179 if (obj instanceof ObjectWithName) {
180 ObjectWithName own = (ObjectWithName) obj;
181 switch (index) {
182 case 0:
183 return own.name;
184 case 1:
185 if (own.obj instanceof PrimitiveAccessor) {
186 PrimitiveAccessor pa = (PrimitiveAccessor) own.obj;
187 return pa.getValue().toString();
188 } else if (own.obj instanceof RefValue) {
189 RefValue refValue = (RefValue) own.obj;
190 return refValue.getRef();
191 } else {
192 if (log.isTraceEnabled()) {
193 log.warn("Not a Primitive accessor neither a ref Value : "
194 + own.obj.toString()
195 + " and class : "
196 + own.obj.getClass().toString());
197 }
198 return own.obj.toString();
199 }
200 default:
201 return getText(obj);
202 }
203 } else {
204 return getText(obj);
205 }
206 }
207
208 public Image getColumnImage(Object obj, int index) {
209 return null;
210 }
211
212 }
213
214 // We add an inner class to enrich the ExecutionSpecAttribute with a name
215 // so that we can display it.
216 public class ObjectWithName {
217 public Object obj;
218 public String name;
219
220 public ObjectWithName(String name, Object obj) {
221 this.name = name;
222 this.obj = obj;
223 }
224 }
225 }