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