1 package org.simantics.district.selection.ui;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
8 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
9 import org.eclipse.jface.viewers.ColumnLabelProvider;
10 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
11 import org.eclipse.jface.viewers.DoubleClickEvent;
12 import org.eclipse.jface.viewers.IDoubleClickListener;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16 import org.eclipse.jface.viewers.SelectionChangedEvent;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.viewers.TreeSelection;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.jface.viewers.TreeViewerColumn;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.FillLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Tree;
27 import org.simantics.Simantics;
28 import org.simantics.browsing.ui.common.AdaptableHintContext;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.exception.RuntimeDatabaseException;
34 import org.simantics.db.layer0.QueryIndexUtils;
35 import org.simantics.db.layer0.SelectionHints;
36 import org.simantics.db.layer0.request.ActiveModels;
37 import org.simantics.db.request.Read;
38 import org.simantics.district.selection.ElementSelectionResource;
39 import org.simantics.district.selection.ElementSelectionUtils;
40 import org.simantics.district.selection.ElementSelector;
41 import org.simantics.layer0.Layer0;
42 import org.simantics.scl.runtime.Lists;
43 import org.simantics.scl.runtime.function.FunctionImpl1;
44 import org.simantics.ui.selection.AnyResource;
45 import org.simantics.ui.selection.AnyVariable;
46 import org.simantics.ui.selection.WorkbenchSelectionContentType;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class ElementSelectorTableUI extends Composite {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
54 private TreeViewer viewer;
55 private TreeViewerColumn column1;
56 private TreeViewerColumn column2;
58 public ElementSelectorTableUI(ESelectionService selectionService, Composite parent, int style) {
60 parent.setLayout(new FillLayout());
61 // GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
62 // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
63 this.setLayout(new FillLayout());
65 viewer = new TreeViewer(this, SWT.FULL_SELECTION);
66 viewer.addDoubleClickListener(new IDoubleClickListener() {
68 public void doubleClick(DoubleClickEvent event) {
69 TreeSelection selection = (TreeSelection) viewer.getSelection();
70 ElementSelector query = (ElementSelector) selection.getFirstElement();
72 List<AdaptableHintContext> elements = Simantics.getSession().syncRequest(new Read<List<AdaptableHintContext>>() {
73 @SuppressWarnings("unchecked")
75 public List<AdaptableHintContext> perform(ReadGraph graph) throws DatabaseException {
76 Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
78 LOGGER.warn("No active model");
79 return Collections.emptyList();
82 List<Resource> result = query.selectElementsFrom(graph, model);
83 return Lists.map(new FunctionImpl1<Resource, AdaptableHintContext>() {
84 public AdaptableHintContext apply(Resource p0) {
85 AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS, graph);
86 selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
87 selectionElement.setHint(SelectionHints.KEY_MODEL, model);
88 return selectionElement;
94 selectionService.setPostSelection(new StructuredSelection(elements));
95 } catch (DatabaseException e) {
96 LOGGER.error("Element selection query failed", e);
101 viewer.setContentProvider(new ITreeContentProvider() {
103 public boolean hasChildren(Object element) {
108 public Object getParent(Object element) {
113 public Object[] getElements(Object inputElement) {
114 if (inputElement == null || !(inputElement instanceof Collection))
115 return new Object[0];
117 return ((Collection<?>)inputElement).toArray();
121 public Object[] getChildren(Object parentElement) {
126 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
127 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
131 Simantics.getSession().asyncRequest(new SelectionsRequest(), new SyncListenerAdapter<Collection<ElementSelector>>() {
132 public void execute(ReadGraph graph, Collection<ElementSelector> result) {
133 parent.getDisplay().asyncExec(() -> {
134 viewer.setInput(result);
139 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
140 LOGGER.error("Error getting element selector list", t);
144 public boolean isDisposed() {
145 return ElementSelectorTableUI.this.isDisposed();
149 ColumnViewerToolTipSupport.enableFor(viewer);
151 Tree table = viewer.getTree();
152 table.setHeaderVisible(true);
153 table.setLinesVisible(true);
155 column1 = new TreeViewerColumn(viewer, SWT.NONE);
156 column1.getColumn().setText("Name");
157 column1.getColumn().setWidth(200);
158 column1.getColumn().setResizable(true);
159 column1.setLabelProvider(new ColumnLabelProvider() {
161 public String getText(Object element) {
162 ElementSelector selector = (ElementSelector) element;
163 return selector.getName();
167 public Image getImage(Object element) {
172 column2 = new TreeViewerColumn(viewer, SWT.NONE);
173 column2.getColumn().setText("Query");
174 column2.getColumn().setWidth(600);
175 column2.getColumn().setResizable(true);
176 column2.setLabelProvider(new ColumnLabelProvider() {
178 public String getText(Object element) {
179 ElementSelector selector = (ElementSelector) element;
180 return selector.getExpression();
184 public Image getImage(Object element) {
190 public Tree getTree() {
191 return viewer.getTree();
194 public ElementSelector getSelectedItem() {
195 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
196 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
199 private final class SelectionElement extends AdaptableHintContext {
200 private final ReadGraph graph;
202 private SelectionElement(Key[] keys, ReadGraph graph) {
207 @SuppressWarnings("unchecked")
208 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
209 Resource element = getHint(SelectionHints.KEY_MAIN);
210 if (contentType instanceof AnyResource) {
213 else if (contentType instanceof AnyVariable) {
215 return (T)ElementSelector.getVariableForElement(graph, element);
216 } catch (DatabaseException e) {
225 public static class SelectionsRequest implements Read<Collection<ElementSelector>> {
227 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
228 Layer0 L0 = Layer0.getInstance(graph);
229 ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
231 Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
233 return Collections.emptyList();
236 List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
238 return Collections.emptyList();
240 Resource lib = libs.get(0);
242 List<ElementSelector> result = new ArrayList<>();
243 for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
244 if (!graph.isInstanceOf(selection, ES.Selection))
247 result.add(ElementSelector.getSelector(graph, selection));