]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/providers/ProcessParametersEditingSupport.java
Add support for combo box in editing parameters.
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / providers / ProcessParametersEditingSupport.java
1 package org.argeo.slc.client.ui.providers;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.client.ui.views.ProcessBuilderView;
12 import org.argeo.slc.client.ui.views.ProcessParametersView;
13 import org.argeo.slc.core.execution.PrimitiveAccessor;
14 import org.argeo.slc.core.execution.PrimitiveUtils;
15 import org.argeo.slc.core.execution.RefSpecAttribute;
16 import org.argeo.slc.core.execution.RefValue;
17 import org.argeo.slc.core.execution.RefValueChoice;
18 import org.argeo.slc.execution.ExecutionSpec;
19 import org.argeo.slc.execution.ExecutionSpecAttribute;
20 import org.eclipse.jface.viewers.CellEditor;
21 import org.eclipse.jface.viewers.ColumnViewer;
22 import org.eclipse.jface.viewers.ComboBoxCellEditor;
23 import org.eclipse.jface.viewers.EditingSupport;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.TextCellEditor;
26
27 /**
28 *
29 *
30 * Implements the ability to edit and save various type of parameter of a given
31 * process. Parameter values are directly saved as soon as the focus on a given
32 * field is lost.
33 *
34 *
35 * Note that EditingSupport is tightly coupled with both ProcessParametersView
36 * and ProcessBuilderView; it cannot serve as a generic EditingSupport as is.
37 * Note also that it assumes that processes in the ProcessBuilderView are stored
38 * as an ordered list.
39 *
40 * @author bsinou
41 *
42 */
43
44 public class ProcessParametersEditingSupport extends EditingSupport {
45
46 private final static Log log = LogFactory
47 .getLog(ProcessParametersEditingSupport.class);
48
49 private CellEditor strEditor;
50 // private ComboBoxCellEditor comboEditor;
51 // private int column;
52
53 // So that we can update corresponding process
54 private int curProcessIndex;
55 private ProcessBuilderView pbView;
56
57 // To populate drop down lists
58 private ExecutionSpec executionSpec;
59
60 // To persist combo box elements indexes
61 Map<String, List<String>> comboBoxes = new HashMap<String, List<String>>();
62
63 public ProcessParametersEditingSupport(ColumnViewer viewer, int column) {
64 super(viewer);
65 strEditor = new TextCellEditor(((TableViewer) viewer).getTable());
66 // TODO : add cell validators.
67 }
68
69 @Override
70 protected CellEditor getCellEditor(Object element) {
71
72 // TODO : check if all parameter always have a linked attribute.
73 if (element instanceof ProcessParametersView.ObjectWithName) {
74 ProcessParametersView.ObjectWithName own = (ProcessParametersView.ObjectWithName) element;
75 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
76 own.name);
77 if (esa != null) {
78 // Paramater is a primitive with no list of choices linked by
79 // the specs
80 if (own.obj instanceof PrimitiveAccessor
81 && !(esa instanceof RefSpecAttribute))
82 return strEditor;
83 else if (esa instanceof RefSpecAttribute) {
84 RefSpecAttribute rsa = (RefSpecAttribute) esa;
85
86 Iterator<RefValueChoice> choices = rsa.getChoices()
87 .iterator();
88 List<String> items = new ArrayList<String>();
89 while (choices.hasNext()) {
90 items.add(choices.next().getName());
91 }
92
93 // persists order of the elements in the current combo box
94 comboBoxes.put(own.name, items);
95
96 String[] itemsStr = new String[items.size()];
97 ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
98 ((TableViewer) getViewer()).getTable(),
99 items.toArray(itemsStr));
100 return comboEditor;
101 }
102 }
103 }
104 if (log.isTraceEnabled()) {
105 log.warn(" No cell Editor fits for element : " + element.toString()
106 + " of class : " + element.getClass().getName());
107 }
108 return null;
109 }
110
111 @Override
112 protected boolean canEdit(Object element) {
113 if (element instanceof ProcessParametersView.ObjectWithName) {
114 ProcessParametersView.ObjectWithName own = (ProcessParametersView.ObjectWithName) element;
115 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
116 own.name);
117 if (esa != null && !esa.getIsFrozen())
118 return true;
119 }
120 return false;
121 }
122
123 @Override
124 protected Object getValue(Object element) {
125 ProcessParametersView.ObjectWithName own = (ProcessParametersView.ObjectWithName) element;
126
127 if (own.obj instanceof PrimitiveAccessor) {
128 PrimitiveAccessor pv = (PrimitiveAccessor) own.obj;
129 return pv.getValue().toString();
130 } else if (own.obj instanceof RefValue) {
131 RefValue rv = (RefValue) own.obj;
132 List<String> values = comboBoxes.get(own.name);
133 log.debug("Get Value : " + rv.getRef() + " & index : "
134 + values.indexOf(rv.getRef()));
135 return values.indexOf(rv.getRef());
136 } else
137 return "unsupported param type";
138 }
139
140 @Override
141 protected void setValue(Object element, Object value) {
142 ProcessParametersView.ObjectWithName own = (ProcessParametersView.ObjectWithName) element;
143 if (own.obj instanceof PrimitiveAccessor) {
144 PrimitiveAccessor pv = (PrimitiveAccessor) own.obj;
145 if (PrimitiveUtils.typeAsClass(pv.getType()) != null)
146 pv.setValue(value);
147 pbView.updateParameter(curProcessIndex, own.name, own.obj);
148 getViewer().update(element, null);
149 } else if (own.obj instanceof RefValue) {
150 RefValue rv = (RefValue) own.obj;
151 List<String> values = comboBoxes.get(own.name);
152 rv.setRef(values.get(((Integer) value).intValue()));
153 getViewer().update(element, null);
154 }
155
156 }
157
158 // Store the index of the process which parameters are being edited
159 public void setCurrentProcessIndex(int index) {
160 this.curProcessIndex = index;
161 }
162
163 public void setCurrentExecutionSpec(ExecutionSpec executionSpec) {
164 this.executionSpec = executionSpec;
165 }
166
167 public void setCurrentProcessBuilderView(
168 ProcessBuilderView processbuilderView) {
169 this.pbView = processbuilderView;
170 }
171
172 }