]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessBuilderPage.java
1444ff185c5750f99bb4d297966ff97260b228c1
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / editors / ProcessBuilderPage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.editors;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.SortedSet;
22 import java.util.TreeSet;
23
24 import javax.jcr.Node;
25 import javax.jcr.NodeIterator;
26 import javax.jcr.Property;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.nodetype.NodeType;
29 import javax.jcr.observation.Event;
30 import javax.jcr.observation.EventListener;
31 import javax.jcr.observation.ObservationManager;
32 import javax.jcr.query.Query;
33 import javax.jcr.query.QueryManager;
34
35 import org.argeo.ArgeoException;
36 import org.argeo.eclipse.ui.jcr.AsyncUiEventListener;
37 import org.argeo.jcr.JcrUtils;
38 import org.argeo.slc.SlcException;
39 import org.argeo.slc.client.ui.SlcImages;
40 import org.argeo.slc.core.execution.PrimitiveAccessor;
41 import org.argeo.slc.core.execution.PrimitiveUtils;
42 import org.argeo.slc.execution.ExecutionProcess;
43 import org.argeo.slc.jcr.SlcJcrUtils;
44 import org.argeo.slc.jcr.SlcNames;
45 import org.argeo.slc.jcr.SlcTypes;
46 import org.eclipse.jface.viewers.CellEditor;
47 import org.eclipse.jface.viewers.ColumnLabelProvider;
48 import org.eclipse.jface.viewers.ColumnViewer;
49 import org.eclipse.jface.viewers.ComboBoxCellEditor;
50 import org.eclipse.jface.viewers.EditingSupport;
51 import org.eclipse.jface.viewers.ISelectionChangedListener;
52 import org.eclipse.jface.viewers.IStructuredContentProvider;
53 import org.eclipse.jface.viewers.IStructuredSelection;
54 import org.eclipse.jface.viewers.ITreeContentProvider;
55 import org.eclipse.jface.viewers.SelectionChangedEvent;
56 import org.eclipse.jface.viewers.StructuredSelection;
57 import org.eclipse.jface.viewers.TableViewer;
58 import org.eclipse.jface.viewers.TableViewerColumn;
59 import org.eclipse.jface.viewers.TextCellEditor;
60 import org.eclipse.jface.viewers.TreeViewer;
61 import org.eclipse.jface.viewers.Viewer;
62 import org.eclipse.jface.viewers.ViewerDropAdapter;
63 import org.eclipse.swt.SWT;
64 import org.eclipse.swt.custom.SashForm;
65 import org.eclipse.swt.dnd.DND;
66 import org.eclipse.swt.dnd.TextTransfer;
67 import org.eclipse.swt.dnd.Transfer;
68 import org.eclipse.swt.dnd.TransferData;
69 import org.eclipse.swt.events.SelectionEvent;
70 import org.eclipse.swt.events.SelectionListener;
71 import org.eclipse.swt.graphics.Image;
72 import org.eclipse.swt.layout.FillLayout;
73 import org.eclipse.swt.layout.GridData;
74 import org.eclipse.swt.layout.GridLayout;
75 import org.eclipse.swt.layout.RowData;
76 import org.eclipse.swt.layout.RowLayout;
77 import org.eclipse.swt.widgets.Button;
78 import org.eclipse.swt.widgets.Composite;
79 import org.eclipse.swt.widgets.Label;
80 import org.eclipse.swt.widgets.Menu;
81 import org.eclipse.swt.widgets.MenuItem;
82 import org.eclipse.swt.widgets.Table;
83 import org.eclipse.ui.forms.AbstractFormPart;
84 import org.eclipse.ui.forms.IManagedForm;
85 import org.eclipse.ui.forms.editor.FormPage;
86 import org.eclipse.ui.forms.widgets.FormToolkit;
87 import org.eclipse.ui.forms.widgets.ScrolledForm;
88
89 /** Definition of the process. */
90 public class ProcessBuilderPage extends FormPage implements SlcNames {
91 // private final static Log log =
92 // LogFactory.getLog(ProcessBuilderPage.class);
93
94 public final static String ID = "processBuilderPage";
95
96 /** To be displayed in empty lists */
97 final static String NONE = "<none>";
98
99 private Node processNode;
100
101 private TreeViewer flowsViewer;
102 private TableViewer valuesViewer;
103 private Label statusLabel;
104 private Button run;
105 private Button remove;
106 private Button clear;
107
108 private AbstractFormPart formPart;
109 private EventListener statusObserver;
110
111 public ProcessBuilderPage(ProcessEditor editor, Node processNode) {
112 super(editor, ID, "Definition");
113 this.processNode = processNode;
114 }
115
116 @Override
117 protected void createFormContent(IManagedForm mf) {
118 try {
119 ScrolledForm form = mf.getForm();
120 form.setExpandHorizontal(true);
121 form.setExpandVertical(true);
122 form.setText("Process " + processNode.getName());
123 GridLayout mainLayout = new GridLayout(1, true);
124 form.getBody().setLayout(mainLayout);
125
126 createControls(form.getBody());
127 createBuilder(form.getBody());
128
129 // form
130 formPart = new AbstractFormPart() {
131
132 };
133 getManagedForm().addPart(formPart);
134
135 // observation
136 statusObserver = new AsyncUiEventListener(form.getDisplay()) {
137 protected void onEventInUiThread(List<Event> events) {
138 statusChanged();
139 }
140 };
141 ObservationManager observationManager = processNode.getSession()
142 .getWorkspace().getObservationManager();
143 observationManager.addEventListener(statusObserver,
144 Event.PROPERTY_CHANGED, processNode.getPath(), true, null,
145 null, false);
146
147 // make sure all controls are in line with status
148 statusChanged();
149
150 // add initial flows
151 addInitialFlows();
152
153 } catch (RepositoryException e) {
154 throw new ArgeoException("Cannot create form content", e);
155 }
156 }
157
158 protected void createControls(Composite parent) {
159 FormToolkit tk = getManagedForm().getToolkit();
160
161 Composite controls = tk.createComposite(parent);
162 controls.setLayout(new RowLayout());
163 controls.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
164
165 run = tk.createButton(controls, null, SWT.PUSH);
166 run.setToolTipText("Run");
167 run.setImage(SlcImages.LAUNCH);
168 run.addSelectionListener(new SelectionListener() {
169 public void widgetSelected(SelectionEvent e) {
170 if (isFinished(getProcessStatus())) {
171 ((ProcessEditor) getEditor()).relaunch();
172 } else if (isRunning(getProcessStatus())) {
173 ((ProcessEditor) getEditor()).kill();
174 } else {
175 ((ProcessEditor) getEditor()).process();
176 }
177 }
178
179 public void widgetDefaultSelected(SelectionEvent e) {
180 widgetSelected(e);
181 }
182 });
183
184 remove = tk.createButton(controls, null, SWT.PUSH);
185 remove.setImage(SlcImages.REMOVE_ONE);
186 remove.setToolTipText("Remove selected flows");
187 remove.addSelectionListener(new SelectionListener() {
188 public void widgetSelected(SelectionEvent e) {
189 removeSelectedFlows();
190 }
191
192 public void widgetDefaultSelected(SelectionEvent e) {
193 widgetSelected(e);
194 }
195 });
196
197 clear = tk.createButton(controls, null, SWT.PUSH);
198 clear.setImage(SlcImages.REMOVE_ALL);
199 clear.setToolTipText("Clear all flows");
200 clear.addSelectionListener(new SelectionListener() {
201 public void widgetSelected(SelectionEvent e) {
202 removeAllFlows();
203 }
204
205 public void widgetDefaultSelected(SelectionEvent e) {
206 widgetSelected(e);
207 }
208 });
209
210 Composite statusComposite = tk.createComposite(controls);
211 RowData rowData = new RowData();
212 rowData.width = 100;
213 rowData.height = 16;
214 statusComposite.setLayoutData(rowData);
215 statusComposite.setLayout(new FillLayout());
216 statusLabel = tk.createLabel(statusComposite, getProcessStatus());
217
218 }
219
220 protected void createBuilder(Composite parent) {
221 FormToolkit tk = getManagedForm().getToolkit();
222 SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
223 sashForm.setSashWidth(4);
224 GridData sahFormGd = new GridData(SWT.FILL, SWT.FILL, true, true);
225 sahFormGd.widthHint = 400;
226 sashForm.setLayoutData(sahFormGd);
227
228 Composite flowsComposite = tk.createComposite(sashForm);
229 flowsComposite.setLayout(new GridLayout(1, false));
230
231 flowsViewer = new TreeViewer(flowsComposite);
232 flowsViewer.getTree().setLayoutData(
233 new GridData(SWT.FILL, SWT.FILL, true, true));
234 flowsViewer.setLabelProvider(new FlowsLabelProvider());
235 flowsViewer.setContentProvider(new FlowsContentProvider());
236 flowsViewer.addSelectionChangedListener(new FlowsSelectionListener());
237
238 int operations = DND.DROP_COPY | DND.DROP_MOVE;
239 Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
240 flowsViewer.addDropSupport(operations, tt, new FlowsDropListener(
241 flowsViewer));
242
243 // Context menu
244 addContextMenu();
245
246 flowsViewer.setInput(getEditorSite());
247 flowsViewer.setInput(processNode);
248
249 Composite valuesComposite = tk.createComposite(sashForm);
250 valuesComposite.setLayout(new GridLayout(1, false));
251
252 valuesViewer = new TableViewer(valuesComposite);
253 GridData valuedGd = new GridData(SWT.FILL, SWT.FILL, true, true);
254 // valuedGd.widthHint = 200;
255 valuesViewer.getTable().setLayoutData(valuedGd);
256 valuesViewer.getTable().setHeaderVisible(true);
257
258 valuesViewer.setContentProvider(new ValuesContentProvider());
259 initializeValuesViewer(valuesViewer);
260 sashForm.setWeights(getWeights());
261 valuesViewer.setInput(getEditorSite());
262 }
263
264 /** Creates the columns of the values viewer */
265 protected void initializeValuesViewer(TableViewer viewer) {
266 String[] titles = { "Name", "Value" };
267 int[] bounds = { 200, 100 };
268
269 for (int i = 0; i < titles.length; i++) {
270 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
271 column.getColumn().setText(titles[i]);
272 column.getColumn().setWidth(bounds[i]);
273 column.getColumn().setResizable(true);
274 column.getColumn().setMoveable(true);
275 if (i == 0) {
276 column.setLabelProvider(new ColumnLabelProvider() {
277 public String getText(Object element) {
278 try {
279 Node specAttrNode = (Node) element;
280 return specAttrNode.getName();
281 } catch (RepositoryException e) {
282 throw new SlcException("Cannot get value", e);
283 }
284 }
285 });
286 } else if (i == 1) {
287 column.setLabelProvider(new ColumnLabelProvider() {
288 public String getText(Object element) {
289 return getAttributeSpecText((Node) element);
290 }
291 });
292 column.setEditingSupport(new ValuesEditingSupport(viewer));
293 }
294
295 }
296 Table table = viewer.getTable();
297 table.setHeaderVisible(false);
298 table.setLinesVisible(true);
299 }
300
301 protected int[] getWeights() {
302 return new int[] { 50, 50 };
303 }
304
305 /*
306 * CONTROLLERS
307 */
308 /** Reflects a status change */
309 protected void statusChanged() {
310 String status = getProcessStatus();
311 statusLabel.setText(status);
312 Boolean isEditable = isEditable(status);
313 run.setEnabled(status.equals(ExecutionProcess.RUNNING) || isEditable);
314 remove.setEnabled(isEditable);
315 clear.setEnabled(isEditable);
316 // flowsViewer.getTree().setEnabled(isEditable);
317 if (status.equals(ExecutionProcess.RUNNING)) {
318 run.setEnabled(true);
319 run.setImage(SlcImages.KILL);
320 run.setToolTipText("Kill");
321 } else if (isFinished(status)) {
322 run.setEnabled(true);
323 run.setImage(SlcImages.RELAUNCH);
324 run.setToolTipText("Relaunch");
325 }
326
327 if (flowsViewer != null)
328 flowsViewer.refresh();
329 }
330
331 /** Adds initial flows from the editor input if any */
332 protected void addInitialFlows() {
333 for (String path : ((ProcessEditorInput) getEditorInput())
334 .getInitialFlowPaths()) {
335 addFlow(path);
336 }
337 }
338
339 /**
340 * Adds a new flow.
341 *
342 * @param path
343 * the path of the flow
344 */
345 protected void addFlow(String path) {
346 try {
347 Node flowNode = processNode.getSession().getNode(path);
348 Node realizedFlowNode = processNode.getNode(SLC_FLOW).addNode(
349 SLC_FLOW);
350 realizedFlowNode.addMixin(SlcTypes.SLC_REALIZED_FLOW);
351 Node address = realizedFlowNode.addNode(SLC_ADDRESS,
352 NodeType.NT_ADDRESS);
353 address.setProperty(Property.JCR_PATH, path);
354
355 // copy spec attributes
356 Node specAttrsBase;
357 if (flowNode.hasProperty(SLC_SPEC)) {
358 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
359 .getNode();
360 specAttrsBase = executionSpecNode;
361 String executionSpecName = executionSpecNode.getProperty(
362 SLC_NAME).getString();
363 realizedFlowNode.setProperty(SLC_SPEC, executionSpecName);
364 } else
365 specAttrsBase = flowNode;
366
367 specAttrs: for (NodeIterator nit = specAttrsBase.getNodes(); nit
368 .hasNext();) {
369 Node specAttrNode = nit.nextNode();
370 String attrName = specAttrNode.getName();
371 if (!specAttrNode
372 .isNodeType(SlcTypes.SLC_EXECUTION_SPEC_ATTRIBUTE))
373 continue specAttrs;
374 Node realizedAttrNode = realizedFlowNode.addNode(specAttrNode
375 .getName());
376 JcrUtils.copy(specAttrNode, realizedAttrNode);
377
378 // override with flow value
379 if (flowNode.hasNode(attrName)) {
380 // assuming this is a primitive
381 Node attrNode = flowNode.getNode(attrName);
382 if (attrNode.hasProperty(SLC_VALUE))
383 realizedAttrNode.setProperty(SLC_VALUE, attrNode
384 .getProperty(SLC_VALUE).getValue());
385 }
386 }
387
388 flowsViewer.refresh();
389 formPart.markDirty();
390 } catch (RepositoryException e) {
391 throw new SlcException("Cannot drop " + path, e);
392 }
393 }
394
395 @SuppressWarnings("unchecked")
396 protected void removeSelectedFlows() {
397 if (!flowsViewer.getSelection().isEmpty()) {
398 Iterator<Object> it = ((StructuredSelection) flowsViewer
399 .getSelection()).iterator();
400 while (it.hasNext()) {
401 Node node = (Node) it.next();
402 try {
403 node.remove();
404 } catch (RepositoryException e) {
405 throw new ArgeoException("Cannot remove " + node, e);
406 }
407 }
408 flowsViewer.refresh();
409 formPart.markDirty();
410 }
411 }
412
413 protected void removeAllFlows() {
414 try {
415 for (NodeIterator nit = processNode.getNode(SLC_FLOW).getNodes(); nit
416 .hasNext();) {
417 nit.nextNode().remove();
418 }
419 flowsViewer.refresh();
420 formPart.markDirty();
421 } catch (RepositoryException e) {
422 throw new ArgeoException("Cannot remove flows from " + processNode,
423 e);
424 }
425 }
426
427 public void commit(Boolean onSave) {
428 if (onSave)
429 statusLabel.setText(getProcessStatus());
430 formPart.commit(onSave);
431 }
432
433 /*
434 * STATE
435 */
436 protected String getProcessStatus() {
437 try {
438 return processNode.getProperty(SLC_STATUS).getString();
439 } catch (RepositoryException e) {
440 throw new SlcException("Cannot retrieve status for " + processNode,
441 e);
442 }
443 }
444
445 /** Optimization so that we don't call the node each time */
446 protected static Boolean isEditable(String status) {
447 return status.equals(ExecutionProcess.NEW)
448 || status.equals(ExecutionProcess.INITIALIZED);
449 }
450
451 protected static Boolean isFinished(String status) {
452 return status.equals(ExecutionProcess.COMPLETED)
453 || status.equals(ExecutionProcess.ERROR)
454 || status.equals(ExecutionProcess.KILLED);
455 }
456
457 protected static Boolean isRunning(String status) {
458 return status.equals(ExecutionProcess.RUNNING);
459 }
460
461 /*
462 * LIFECYCLE
463 */
464 @Override
465 public void dispose() {
466 JcrUtils.unregisterQuietly(processNode, statusObserver);
467 super.dispose();
468 }
469
470 /*
471 * UTILITIES
472 */
473 protected static String getAttributeSpecText(Node specAttrNode) {
474 try {
475 if (specAttrNode.isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
476 if (!specAttrNode.hasProperty(SLC_VALUE))
477 return "";
478 String type = specAttrNode.getProperty(SLC_TYPE).getString();
479 if (PrimitiveAccessor.TYPE_PASSWORD.equals(type))
480 return "****************";
481 Object value = PrimitiveUtils.convert(type, specAttrNode
482 .getProperty(SLC_VALUE).getString());
483 return value.toString();
484 } else if (specAttrNode.isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
485 if (specAttrNode.hasProperty(SLC_VALUE)) {
486 int value = (int) specAttrNode.getProperty(SLC_VALUE)
487 .getLong();
488 NodeIterator children = specAttrNode.getNodes();
489 int index = 0;
490 while (children.hasNext()) {
491 Node child = children.nextNode();
492 if (index == value)
493 return child.getProperty(Property.JCR_TITLE)
494 .getString();
495 index++;
496 }
497 throw new SlcException("No child node with index " + value
498 + " for spec attribute " + specAttrNode);
499 } else
500 return "";
501 }
502 throw new SlcException("Unsupported type for spec attribute "
503 + specAttrNode);
504 } catch (RepositoryException e) {
505 throw new SlcException("Cannot get value", e);
506 }
507 }
508
509 /*
510 * FLOWS SUBCLASSES
511 */
512 class FlowsContentProvider implements ITreeContentProvider {
513 public Object[] getElements(Object obj) {
514 if (!(obj instanceof Node))
515 return new Object[0];
516
517 try {
518 Node node = (Node) obj;
519 List<Node> children = new ArrayList<Node>();
520 for (NodeIterator nit = node.getNode(SLC_FLOW).getNodes(); nit
521 .hasNext();) {
522 Node flowNode = nit.nextNode();
523 children.add(flowNode);
524 }
525 return children.toArray();
526 } catch (RepositoryException e) {
527 throw new SlcException("Cannot list flows of " + obj, e);
528 }
529 }
530
531 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
532 }
533
534 public void dispose() {
535 }
536
537 public Object[] getChildren(Object parentElement) {
538 // no children for the time being
539 return null;
540 }
541
542 public Object getParent(Object element) {
543 return null;
544 }
545
546 public boolean hasChildren(Object element) {
547 return false;
548 }
549
550 }
551
552 static class FlowsLabelProvider extends ColumnLabelProvider {
553 @Override
554 public String getText(Object element) {
555 Node node = (Node) element;
556 try {
557 if (node.isNodeType(SlcTypes.SLC_REALIZED_FLOW)) {
558 if (node.hasNode(SLC_ADDRESS)) {
559 String path = node.getNode(SLC_ADDRESS)
560 .getProperty(Property.JCR_PATH).getString();
561 String executionModuleName = SlcJcrUtils
562 .moduleName(path);
563 // Node executionModuleNode = node.getSession().getNode(
564 // SlcJcrUtils.modulePath(path));
565 // String executionModuleName = executionModuleNode
566 // .getProperty(SLC_NAME).getString();
567 return executionModuleName + ":"
568 + SlcJcrUtils.flowRelativePath(path);
569 }
570 }
571 } catch (RepositoryException e) {
572 throw new SlcException("Cannot display " + element, e);
573 }
574 return super.getText(element);
575 }
576
577 @Override
578 public Image getImage(Object element) {
579 Node node = (Node) element;
580 try {
581 if (node.isNodeType(SlcTypes.SLC_REALIZED_FLOW)) {
582 if (node.hasProperty(SLC_STATUS)) {
583 String status = node.getProperty(SLC_STATUS)
584 .getString();
585 // TODO: factorize with process view ?
586 if (status.equals(ExecutionProcess.RUNNING))
587 return SlcImages.PROCESS_RUNNING;
588 else if (status.equals(ExecutionProcess.ERROR)
589 || status.equals(ExecutionProcess.KILLED))
590 return SlcImages.PROCESS_ERROR;
591 else if (status.equals(ExecutionProcess.COMPLETED))
592 return SlcImages.PROCESS_COMPLETED;
593 }
594 return SlcImages.FLOW;
595 }
596 } catch (RepositoryException e) {
597 throw new SlcException("Cannot display " + element, e);
598 }
599 return super.getImage(element);
600 }
601
602 }
603
604 /** Parameter view is updated each time a new line is selected */
605 class FlowsSelectionListener implements ISelectionChangedListener {
606 public void selectionChanged(SelectionChangedEvent evt) {
607 if (evt.getSelection().isEmpty()) {
608 valuesViewer.setInput(getEditorSite());
609 return;
610 }
611 Node realizedFlowNode = (Node) ((IStructuredSelection) evt
612 .getSelection()).getFirstElement();
613 valuesViewer.setInput(realizedFlowNode);
614 }
615 }
616
617 /**
618 * Add a context menu that call private methods. It only relies on selected
619 * item(s) not on parameter that are passed in the menuAboutToShow method
620 **/
621 private void addContextMenu() {
622 Menu menu = new Menu(flowsViewer.getControl());
623
624 MenuItem removeItems = new MenuItem(menu, SWT.PUSH);
625 removeItems.addSelectionListener(new SelectionListener() {
626
627 public void widgetSelected(SelectionEvent e) {
628 removeSelectedFlows();
629 }
630
631 public void widgetDefaultSelected(SelectionEvent e) {
632 }
633 });
634 removeItems.setText("Remove selected flow(s)");
635
636 MenuItem removeAllItems = new MenuItem(menu, SWT.PUSH);
637 removeAllItems.addSelectionListener(new SelectionListener() {
638
639 public void widgetSelected(SelectionEvent e) {
640 removeAllFlows();
641 }
642
643 public void widgetDefaultSelected(SelectionEvent e) {
644 }
645 });
646 removeAllItems.setText("Remove all flows");
647 flowsViewer.getTree().setMenu(menu);
648 }
649
650 /** Manages drop event. */
651 class FlowsDropListener extends ViewerDropAdapter {
652
653 public FlowsDropListener(Viewer viewer) {
654 super(viewer);
655 }
656
657 @Override
658 public boolean performDrop(Object data) {
659
660 // Parse the received String, paths are separated with a carriage
661 // return
662 String[] paths = data.toString().split(new String("\n"));
663 SortedSet<String> resultPaths = new TreeSet<String>();
664 for (String path : paths) {
665 try {
666 // either a node or a whole directory can have been dragged
667 QueryManager qm = processNode.getSession().getWorkspace()
668 .getQueryManager();
669 String statement = "SELECT * FROM ["
670 + SlcTypes.SLC_EXECUTION_FLOW
671 + "] WHERE ISDESCENDANTNODE(['" + path
672 + "']) OR ISSAMENODE(['" + path + "'])";
673 Query query = qm.createQuery(statement, Query.JCR_SQL2);
674
675 // order paths
676 for (NodeIterator nit = query.execute().getNodes(); nit
677 .hasNext();) {
678 String currPath = nit.nextNode().getPath();
679 // do not add twice a same flow
680 if (!resultPaths.contains(currPath))
681 resultPaths.add(currPath);
682 }
683 } catch (RepositoryException e) {
684 throw new SlcException("Cannot query flows under " + path,
685 e);
686 }
687 }
688 for (String p : resultPaths) {
689 addFlow(p);
690 }
691 return true;
692
693 }
694
695 @Override
696 public boolean validateDrop(Object target, int operation,
697 TransferData transferType) {
698 return isEditable(getProcessStatus());
699 }
700 }
701
702 /*
703 * VALUES SUBCLASSES
704 */
705 static class ValuesContentProvider implements IStructuredContentProvider {
706
707 public Object[] getElements(Object inputElement) {
708 if (!(inputElement instanceof Node))
709 return new Object[0];
710
711 try {
712 Node realizedFlowNode = (Node) inputElement;
713 List<Node> specAttributes = new ArrayList<Node>();
714 specAttrs: for (NodeIterator nit = realizedFlowNode.getNodes(); nit
715 .hasNext();) {
716 Node specAttrNode = nit.nextNode();
717 if (!specAttrNode
718 .isNodeType(SlcTypes.SLC_EXECUTION_SPEC_ATTRIBUTE))
719 continue specAttrs;
720 // workaround to enable hiding of necessary but unusable
721 // flow parameters
722 else if (specAttrNode.hasProperty(SlcNames.SLC_IS_HIDDEN)
723 && specAttrNode.getProperty(SlcNames.SLC_IS_HIDDEN)
724 .getBoolean())
725 continue specAttrs;
726 specAttributes.add(specAttrNode);
727 }
728 return specAttributes.toArray();
729 } catch (RepositoryException e) {
730 throw new SlcException("Cannot get elements", e);
731 }
732 }
733
734 public void dispose() {
735 }
736
737 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
738 }
739 }
740
741 class ValuesEditingSupport extends EditingSupport {
742 private final TableViewer tableViewer;
743
744 public ValuesEditingSupport(ColumnViewer viewer) {
745 super(viewer);
746 tableViewer = (TableViewer) viewer;
747 }
748
749 @Override
750 protected CellEditor getCellEditor(Object element) {
751 try {
752 Node specAttrNode = (Node) element;
753 if (specAttrNode
754 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
755 String type = specAttrNode.getProperty(SLC_TYPE)
756 .getString();
757 if (PrimitiveAccessor.TYPE_PASSWORD.equals(type)) {
758 return new TextCellEditor(tableViewer.getTable(),
759 SWT.PASSWORD);
760 } else {
761 return new TextCellEditor(tableViewer.getTable());
762 }
763 } else if (specAttrNode
764 .isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
765 NodeIterator children = specAttrNode.getNodes();
766 ArrayList<String> items = new ArrayList<String>();
767 while (children.hasNext()) {
768 Node child = children.nextNode();
769 if (child.isNodeType(NodeType.MIX_TITLE))
770 items.add(child.getProperty(Property.JCR_TITLE)
771 .getString());
772 }
773 return new ComboBoxCellEditor(tableViewer.getTable(),
774 items.toArray(new String[items.size()]));
775 }
776 return null;
777 } catch (RepositoryException e) {
778 throw new SlcException("Cannot get cell editor", e);
779 }
780 }
781
782 @Override
783 protected boolean canEdit(Object element) {
784 try {
785 Node specAttrNode = (Node) element;
786 Boolean cannotEdit = specAttrNode.getProperty(SLC_IS_IMMUTABLE)
787 .getBoolean()
788 || specAttrNode.getProperty(SLC_IS_CONSTANT)
789 .getBoolean();
790 return !cannotEdit && isSupportedAttributeType(specAttrNode);
791 } catch (RepositoryException e) {
792 throw new SlcException("Cannot check whether " + element
793 + " is editable", e);
794 }
795 }
796
797 /**
798 * Supports {@link SlcTypes#SLC_PRIMITIVE_SPEC_ATTRIBUTE} and
799 * {@link SlcTypes#SLC_REF_SPEC_ATTRIBUTE}
800 */
801 protected boolean isSupportedAttributeType(Node specAttrNode)
802 throws RepositoryException {
803 return specAttrNode
804 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)
805 || specAttrNode.isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE);
806 }
807
808 @Override
809 protected Object getValue(Object element) {
810 Node specAttrNode = (Node) element;
811 try {
812 if (specAttrNode
813 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
814 if (!specAttrNode.hasProperty(SLC_VALUE))
815 return NONE;
816 String type = specAttrNode.getProperty(SLC_TYPE)
817 .getString();
818 // TODO optimize based on data type?
819 Object value = PrimitiveUtils.convert(type, specAttrNode
820 .getProperty(SLC_VALUE).getString());
821 return value.toString();
822 } else if (specAttrNode
823 .isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
824 if (!specAttrNode.hasProperty(SLC_VALUE))
825 return 0;
826 // return the index of the sub node as set by setValue()
827 // in the future we may manage references as well
828 return (int) specAttrNode.getProperty(SLC_VALUE).getLong();
829 }
830 throw new SlcException("Unsupported type for spec attribute "
831 + specAttrNode);
832 } catch (RepositoryException e) {
833 throw new SlcException("Cannot get value for " + element, e);
834 }
835 }
836
837 @Override
838 protected void setValue(Object element, Object value) {
839 try {
840 Node specAttrNode = (Node) element;
841 if (specAttrNode
842 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
843 String type = specAttrNode.getProperty(SLC_TYPE)
844 .getString();
845 SlcJcrUtils.setPrimitiveAsProperty(specAttrNode, SLC_VALUE,
846 type, value);
847 valuesViewer.refresh();
848 formPart.markDirty();
849 } else if (specAttrNode
850 .isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
851 specAttrNode.setProperty(SLC_VALUE,
852 ((Integer) value).longValue());
853 valuesViewer.refresh();
854 formPart.markDirty();
855 }
856 } catch (RepositoryException e) {
857 throw new SlcException("Cannot get celle editor", e);
858 }
859 }
860
861 }
862 }