]> 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/ProcessBuilderView.java
add some comments to help understanding the approach
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / views / ProcessBuilderView.java
1 package org.argeo.slc.client.ui.views;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Properties;
8 import java.util.UUID;
9
10 import org.apache.commons.io.IOUtils;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.client.oxm.OxmInterface;
13 import org.argeo.slc.client.ui.ClientUiPlugin;
14 import org.argeo.slc.client.ui.controllers.ProcessController;
15 import org.argeo.slc.process.RealizedFlow;
16 import org.argeo.slc.process.SlcExecution;
17 import org.argeo.slc.runtime.SlcAgent;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.jface.viewers.ViewerDropAdapter;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.dnd.DND;
29 import org.eclipse.swt.dnd.TextTransfer;
30 import org.eclipse.swt.dnd.Transfer;
31 import org.eclipse.swt.dnd.TransferData;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableColumn;
37 import org.eclipse.ui.PartInitException;
38 import org.eclipse.ui.part.ViewPart;
39
40 /**
41 * Display a list of processes that are to be launched as batch. For the moment
42 * being, only one agent by batch is enabled. The batch is contructed by
43 * dropping process from the ExecutionModuleView. Wrong type of data dropped in
44 * this view might raise errors.
45 *
46 * @author bsinou
47 *
48 */
49 public class ProcessBuilderView extends ViewPart {
50 // private final static Log log =
51 // LogFactory.getLog(ProcessBuilderView.class);
52
53 public static final String ID = "org.argeo.slc.client.ui.processBuilderView";
54
55 // private final static Log log =
56 // LogFactory.getLog(ProcessBuilderView.class);
57
58 private TableViewer viewer;
59 private List<RealizedFlow> realizedFlows = new ArrayList<RealizedFlow>();
60 private String currentAgentUuid = null;
61 private String host = null;
62
63 // TODO find a better way to get index of the current selected row
64 // used in removeSelected
65 private int curSelectedRow = -1;
66
67 // IoC
68 private OxmInterface oxmBean;
69 private ProcessController processController;
70 private List<SlcAgent> slcAgents;
71
72 public void createPartControl(Composite parent) {
73 Table table = createTable(parent);
74 viewer = new TableViewer(table);
75 viewer.setLabelProvider(new ViewLabelProvider());
76 viewer.setContentProvider(new ViewContentProvider());
77 viewer.addSelectionChangedListener(new SelectionChangedListener());
78
79 int operations = DND.DROP_COPY | DND.DROP_MOVE;
80 Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
81 viewer.addDropSupport(operations, tt, new ViewDropListener(viewer));
82
83 viewer.setInput(getViewSite());
84 }
85
86 protected Table createTable(Composite parent) {
87 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
88 | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
89
90 Table table = new Table(parent, style);
91
92 // GridData gridData = new GridData(GridData.FILL_BOTH);
93 // gridData.grabExcessVerticalSpace = true;
94 // gridData.grabExcessHorizontalSpace = true;
95 // gridData.horizontalSpan = 3;
96 // table.setLayoutData(gridData);
97
98 table.setLinesVisible(true);
99 table.setHeaderVisible(true);
100
101 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
102 column.setText("Module");
103 column.setWidth(200);
104
105 column = new TableColumn(table, SWT.LEFT, 1);
106 column.setText("Flow");
107 column.setWidth(200);
108
109 return table;
110 }
111
112 protected void execute() {
113 // TODO: use agent proxy to retrieve it
114 SlcAgent agent = null;
115 SlcExecution slcExecution = new SlcExecution();
116 slcExecution.setUuid(UUID.randomUUID().toString());
117 slcExecution.setRealizedFlows(realizedFlows);
118 processController.execute(agent, slcExecution);
119 }
120
121 public void setFocus() {
122 viewer.getControl().setFocus();
123 }
124
125 // update one of the parameter of a given RealizedFlow
126 public void updateParameter(int realizedFlowIndex, String paramName,
127 Object value) {
128 RealizedFlow curRealizedFlow = realizedFlows.get(realizedFlowIndex);
129 curRealizedFlow.getFlowDescriptor().getValues().put(paramName, value);
130 }
131
132 // clear the realizedFlow<List>
133 public void clearBatch() {
134 // we clear the list
135 realizedFlows = new ArrayList<RealizedFlow>();
136 curSelectedRow = -1;
137 refreshParameterview();
138 viewer.refresh();
139 }
140
141 // Remove the selected process from the batch
142 public void removeSelected() {
143 if (curSelectedRow == -1)
144 return;
145 else
146 realizedFlows.remove(curSelectedRow);
147 curSelectedRow = -1;
148 refreshParameterview();
149 viewer.refresh();
150 }
151
152 // calling this method with index =-1 will cause the reset of the view.
153 private void refreshParameterview() {
154 // We choose to directly access the view rather than going through
155 // commands.
156 ProcessParametersView ppView;
157 try {
158 ppView = (ProcessParametersView) ClientUiPlugin.getDefault()
159 .getWorkbench().getActiveWorkbenchWindow().getActivePage()
160 .showView(ProcessParametersView.ID);
161
162 if (curSelectedRow == -1)
163 ppView.setRealizedFlow(-1, null);
164 else
165 ppView.setRealizedFlow(curSelectedRow,
166 realizedFlows.get(curSelectedRow));
167 } catch (PartInitException e) {
168 throw new SlcException(
169 "Cannot Retrieve ProcessParameterView to edit parameters of selected process",
170 e);
171 }
172 }
173
174 // Return the list of the processes to execute.
175 public void launchBatch() {
176 SlcExecution slcExecution = new SlcExecution();
177 slcExecution.setUuid(UUID.randomUUID().toString());
178
179 slcExecution.setRealizedFlows(realizedFlows);
180 slcExecution.setHost(host);
181
182 // TODO : insure that the concept has been well understood & the
183 // specification respected
184 SlcAgent curAgent;
185 for (int i = 0; i < slcAgents.size(); i++) {
186 if (currentAgentUuid == null)
187 throw new SlcException(
188 "Cannot launch a batch if no agent is specified");
189 if (currentAgentUuid.equals(slcAgents.get(i).getAgentUuid())) {
190 curAgent = slcAgents.get(i);
191 processController.execute(curAgent, slcExecution);
192 break;
193 }
194 }
195 }
196
197 // Specific Providers for the current view.
198 protected class ViewContentProvider implements IStructuredContentProvider {
199 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
200 }
201
202 public void dispose() {
203 }
204
205 public Object[] getElements(Object obj) {
206 return realizedFlows.toArray();
207 }
208 }
209
210 protected class ViewLabelProvider extends LabelProvider implements
211 ITableLabelProvider {
212 public String getColumnText(Object obj, int index) {
213 RealizedFlow rf = (RealizedFlow) obj;
214 switch (index) {
215 case 0:
216 return rf.getModuleName();
217 case 1:
218 return rf.getFlowDescriptor().getName();
219 }
220 return getText(obj);
221 }
222
223 public Image getColumnImage(Object obj, int index) {
224 return null;
225 }
226
227 }
228
229 // Handle Events
230 class SelectionChangedListener implements ISelectionChangedListener {
231 public void selectionChanged(SelectionChangedEvent evt) {
232
233 IStructuredSelection curSelection = (IStructuredSelection) evt
234 .getSelection();
235 Object obj = curSelection.getFirstElement();
236
237 if (obj instanceof RealizedFlow) {
238 RealizedFlow rf = (RealizedFlow) obj;
239 curSelectedRow = realizedFlows.indexOf(rf);
240 refreshParameterview();
241 setFocus();
242 }
243 }
244 }
245
246 // Implementation of the Drop Listener
247 protected class ViewDropListener extends ViewerDropAdapter {
248
249 public ViewDropListener(Viewer viewer) {
250 super(viewer);
251 }
252
253 @Override
254 public boolean performDrop(Object data) {
255
256 Properties props = new Properties();
257
258 // TODO : Handle wrong type of dropped data
259 ByteArrayInputStream in = new ByteArrayInputStream(data.toString()
260 .getBytes());
261 try {
262 props.load(in);
263 } catch (IOException e) {
264 throw new SlcException("Cannot create read flow node", e);
265 } finally {
266 IOUtils.closeQuietly(in);
267 }
268
269 String agentId = props.getProperty("agentId");
270 if (currentAgentUuid == null) {
271 currentAgentUuid = agentId;
272 host = props.getProperty("host");
273 } else if (!currentAgentUuid.equals(agentId)) {
274 // TODO: as for now, we can only construct batch on a single
275 // Agent, must be upgraded to enable batch on various agent.
276 throw new SlcException(
277 "Cannot create batch on two (or more) distinct agents",
278 null);
279 // return false;
280 }
281
282 String fdXml = props.getProperty("RealizedFlowAsXml");
283 if (fdXml == null)
284 return false;
285 RealizedFlow rf = (RealizedFlow) oxmBean.unmarshal(fdXml);
286 realizedFlows.add(rf);
287 curSelectedRow = realizedFlows.indexOf(rf);
288 refreshParameterview();
289 getViewer().refresh();
290 return true;
291 }
292
293 @Override
294 public boolean validateDrop(Object target, int operation,
295 TransferData transferType) {
296 return true;
297 }
298 }
299
300 // IoC
301 public void setSlcAgents(List<SlcAgent> slcAgents) {
302 this.slcAgents = slcAgents;
303 }
304
305 public void setOxmBean(OxmInterface oxmBean) {
306 this.oxmBean = oxmBean;
307 }
308
309 public void setProcessController(ProcessController processController) {
310 this.processController = processController;
311 }
312
313 }