1 package org
.argeo
.slc
.client
.ui
.views
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
7 import java
.util
.Properties
;
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
.core
.commands
.Command
;
21 import org
.eclipse
.core
.commands
.IParameter
;
22 import org
.eclipse
.core
.commands
.Parameterization
;
23 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
24 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
25 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
28 import org
.eclipse
.jface
.viewers
.LabelProvider
;
29 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
30 import org
.eclipse
.jface
.viewers
.TableViewer
;
31 import org
.eclipse
.jface
.viewers
.Viewer
;
32 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.dnd
.DND
;
35 import org
.eclipse
.swt
.dnd
.TextTransfer
;
36 import org
.eclipse
.swt
.dnd
.Transfer
;
37 import org
.eclipse
.swt
.dnd
.TransferData
;
38 import org
.eclipse
.swt
.graphics
.Image
;
39 import org
.eclipse
.swt
.layout
.GridData
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Table
;
42 import org
.eclipse
.swt
.widgets
.TableColumn
;
43 import org
.eclipse
.ui
.IWorkbench
;
44 import org
.eclipse
.ui
.IWorkbenchWindow
;
45 import org
.eclipse
.ui
.commands
.ICommandService
;
46 import org
.eclipse
.ui
.handlers
.IHandlerService
;
47 import org
.eclipse
.ui
.part
.ViewPart
;
50 * Display a list of processes that are to be launched as batch. For the moment
51 * being, only one agent by batch is enabled. The batch is contructed by
52 * dropping process from the ExecutionModuleView. Wrong type of data dropped in
53 * this view might raise errors.
58 public class ProcessBuilderView
extends ViewPart
{
59 private final static Log log
= LogFactory
.getLog(ProcessBuilderView
.class);
61 public static final String ID
= "org.argeo.slc.client.ui.processBuilderView";
62 private static final String EDIT_CMD
= "org.argeo.slc.client.ui.editRealizedFlowDetails";
63 private static final String FLOWASXML_PARAM
= "org.argeo.slc.client.commands.realizedFlowAsXml";
64 private static final String INDEX_PARAM
= "org.argeo.slc.client.commands.realizedFlowIndex";
66 // private final static Log log =
67 // LogFactory.getLog(ProcessBuilderView.class);
69 private TableViewer viewer
;
70 private List
<RealizedFlow
> realizedFlows
= new ArrayList
<RealizedFlow
>();
71 private String currentAgentUuid
= null;
72 private String host
= null;
74 // TODO find a better way to get index of the current selected row
75 // used in removeSelected
76 private int curSelectedRow
= -1;
79 private OxmInterface oxmBean
;
80 private ProcessController processController
;
81 private List
<SlcAgent
> slcAgents
;
83 public void createPartControl(Composite parent
) {
84 Table table
= createTable(parent
);
85 viewer
= new TableViewer(table
);
86 viewer
.setLabelProvider(new ViewLabelProvider());
87 viewer
.setContentProvider(new ViewContentProvider());
88 viewer
.addSelectionChangedListener(new SelectionChangedListener());
90 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
91 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
92 viewer
.addDropSupport(operations
, tt
, new ViewDropListener(viewer
));
94 viewer
.setInput(getViewSite());
97 protected Table
createTable(Composite parent
) {
98 int style
= SWT
.SINGLE
| SWT
.BORDER
| SWT
.H_SCROLL
| SWT
.V_SCROLL
99 | SWT
.FULL_SELECTION
| SWT
.HIDE_SELECTION
;
101 Table table
= new Table(parent
, style
);
103 GridData gridData
= new GridData(GridData
.FILL_BOTH
);
104 gridData
.grabExcessVerticalSpace
= true;
105 gridData
.grabExcessHorizontalSpace
= true;
106 gridData
.horizontalSpan
= 3;
107 table
.setLayoutData(gridData
);
109 table
.setLinesVisible(true);
110 table
.setHeaderVisible(true);
112 TableColumn column
= new TableColumn(table
, SWT
.LEFT
, 0);
113 column
.setText("Module");
114 column
.setWidth(200);
116 column
= new TableColumn(table
, SWT
.LEFT
, 1);
117 column
.setText("Flow");
118 column
.setWidth(200);
123 protected void execute() {
124 // TODO: use agent proxy to retrieve it
125 SlcAgent agent
= null;
126 SlcExecution slcExecution
= new SlcExecution();
127 slcExecution
.setUuid(UUID
.randomUUID().toString());
128 slcExecution
.setRealizedFlows(realizedFlows
);
129 processController
.execute(agent
, slcExecution
);
132 public void setFocus() {
133 viewer
.getControl().setFocus();
136 // update one of the parameter of a given RealizedFlow
137 public void updateParameter(int realizedFlowIndex
, String paramName
,
139 RealizedFlow curRealizedFlow
= realizedFlows
.get(realizedFlowIndex
);
140 curRealizedFlow
.getFlowDescriptor().getValues().put(paramName
, value
);
143 // clear the realizedFlow<List>
144 public void clearBatch() {
146 realizedFlows
= new ArrayList
<RealizedFlow
>();
148 refreshParameterview(null);
152 // Remove the selected process from the batch
153 public void removeSelected() {
154 if (curSelectedRow
== -1)
157 realizedFlows
.remove(curSelectedRow
);
159 refreshParameterview(null);
163 // calling this method with index =-1 will cause the reset of the view.
164 private void refreshParameterview(RealizedFlow rf
) {
165 IWorkbench iw
= ClientUiPlugin
.getDefault().getWorkbench();
166 IHandlerService handlerService
= (IHandlerService
) iw
167 .getService(IHandlerService
.class);
169 // get the command from plugin.xml
170 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
171 ICommandService cmdService
= (ICommandService
) window
172 .getService(ICommandService
.class);
173 Command cmd
= cmdService
.getCommand(EDIT_CMD
);
175 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
178 Parameterization params
;
180 // The current index to be able to records changes on
182 iparam
= cmd
.getParameter(INDEX_PARAM
);
183 params
= new Parameterization(iparam
,
184 (new Integer(curSelectedRow
)).toString());
185 parameters
.add(params
);
187 if (curSelectedRow
!= -1) {
188 // The current Realized flow marshalled as XML
189 String result
= oxmBean
.marshal(rf
);
190 iparam
= cmd
.getParameter(FLOWASXML_PARAM
);
191 params
= new Parameterization(iparam
, result
);
192 parameters
.add(params
);
194 // build the parameterized command
195 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
196 parameters
.toArray(new Parameterization
[parameters
.size()]));
198 // execute the command
199 handlerService
= (IHandlerService
) window
200 .getService(IHandlerService
.class);
201 handlerService
.executeCommand(pc
, null);
203 } catch (Exception e
) {
205 throw new SlcException("Problem while rendering result. "
211 // Return the list of the processes to execute.
212 public void launchBatch() {
213 SlcExecution slcExecution
= new SlcExecution();
214 slcExecution
.setUuid(UUID
.randomUUID().toString());
216 slcExecution
.setRealizedFlows(realizedFlows
);
217 slcExecution
.setHost(host
);
219 // TODO : insure that the concept has been well understood & the
220 // specification respected
222 for (int i
= 0; i
< slcAgents
.size(); i
++) {
223 if (currentAgentUuid
== null)
224 throw new SlcException(
225 "Cannot launch a batch if no agent is specified");
226 if (currentAgentUuid
.equals(slcAgents
.get(i
).getAgentUuid())) {
227 curAgent
= slcAgents
.get(i
);
228 processController
.execute(curAgent
, slcExecution
);
234 // Specific Providers for the current view.
235 protected class ViewContentProvider
implements IStructuredContentProvider
{
236 public void inputChanged(Viewer arg0
, Object arg1
, Object arg2
) {
239 public void dispose() {
242 public Object
[] getElements(Object obj
) {
243 return realizedFlows
.toArray();
247 protected class ViewLabelProvider
extends LabelProvider
implements
248 ITableLabelProvider
{
249 public String
getColumnText(Object obj
, int index
) {
250 RealizedFlow rf
= (RealizedFlow
) obj
;
253 return rf
.getModuleName();
255 return rf
.getFlowDescriptor().getName();
260 public Image
getColumnImage(Object obj
, int index
) {
267 class SelectionChangedListener
implements ISelectionChangedListener
{
268 public void selectionChanged(SelectionChangedEvent evt
) {
270 IStructuredSelection curSelection
= (IStructuredSelection
) evt
272 Object obj
= curSelection
.getFirstElement();
274 if (obj
instanceof RealizedFlow
) {
275 RealizedFlow rf
= (RealizedFlow
) obj
;
276 curSelectedRow
= realizedFlows
.indexOf(rf
);
277 refreshParameterview(rf
);
282 // Implementation of the Drop Listener
283 protected class ViewDropListener
extends ViewerDropAdapter
{
285 public ViewDropListener(Viewer viewer
) {
290 public boolean performDrop(Object data
) {
292 Properties props
= new Properties();
294 // TODO : Handle wrong type of dropped data
295 ByteArrayInputStream in
= new ByteArrayInputStream(data
.toString()
299 } catch (IOException e
) {
300 throw new SlcException("Cannot create read flow node", e
);
302 IOUtils
.closeQuietly(in
);
305 String agentId
= props
.getProperty("agentId");
306 if (currentAgentUuid
== null) {
307 currentAgentUuid
= agentId
;
308 host
= props
.getProperty("host");
309 } else if (!currentAgentUuid
.equals(agentId
)) {
310 // TODO: as for now, we can only construct batch on a single
311 // Agent, must be upgraded to enable batch on various agent.
312 throw new SlcException(
313 "Cannot create batch on two (or more) distinct agents",
318 String fdXml
= props
.getProperty("RealizedFlowAsXml");
321 RealizedFlow rf
= (RealizedFlow
) oxmBean
.unmarshal(fdXml
);
322 realizedFlows
.add(rf
);
324 getViewer().refresh();
329 public boolean validateDrop(Object target
, int operation
,
330 TransferData transferType
) {
336 public void setSlcAgents(List
<SlcAgent
> slcAgents
) {
337 this.slcAgents
= slcAgents
;
340 public void setOxmBean(OxmInterface oxmBean
) {
341 this.oxmBean
= oxmBean
;
344 public void setProcessController(ProcessController processController
) {
345 this.processController
= processController
;