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