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.IStructuredSelection;
14 import org.eclipse.jface.viewers.ITreeContentProvider;
15 import org.eclipse.jface.viewers.StructuredSelection;
16 import org.eclipse.jface.viewers.TreeSelection;
17 import org.eclipse.jface.viewers.TreeViewer;
18 import org.eclipse.jface.viewers.TreeViewerColumn;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Tree;
25 import org.simantics.Simantics;
26 import org.simantics.browsing.ui.common.AdaptableHintContext;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
30 import org.simantics.db.common.request.ResourceRead;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.QueryIndexUtils;
33 import org.simantics.db.layer0.SelectionHints;
34 import org.simantics.db.layer0.request.ActiveModels;
35 import org.simantics.db.layer0.variable.Variable;
36 import org.simantics.db.request.Read;
37 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
38 import org.simantics.district.selection.ElementSelectionResource;
39 import org.simantics.district.selection.ElementSelector;
40 import org.simantics.district.selection.ElementSelector.DiagramGenerator;
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 DoubleClickListener(selectionService));
68 viewer.setContentProvider(new ITreeContentProvider() {
70 public boolean hasChildren(Object element) {
75 public Object getParent(Object element) {
80 public Object[] getElements(Object inputElement) {
81 if (inputElement == null || !(inputElement instanceof Collection))
84 return ((Collection<?>)inputElement).toArray();
88 public Object[] getChildren(Object parentElement) {
93 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
94 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
98 Simantics.getSession().asyncRequest(new SelectionsRequest(), new SyncListenerAdapter<Collection<ElementSelector>>() {
99 public void execute(ReadGraph graph, Collection<ElementSelector> result) {
100 parent.getDisplay().asyncExec(() -> {
101 viewer.setInput(result);
106 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
107 LOGGER.error("Error getting element selector list", t);
111 public boolean isDisposed() {
112 return ElementSelectorTableUI.this.isDisposed();
116 ColumnViewerToolTipSupport.enableFor(viewer);
118 Tree table = viewer.getTree();
119 table.setHeaderVisible(true);
120 table.setLinesVisible(true);
122 column1 = new TreeViewerColumn(viewer, SWT.NONE);
123 column1.getColumn().setText("Name");
124 column1.getColumn().setWidth(200);
125 column1.getColumn().setResizable(true);
126 column1.setLabelProvider(new ColumnLabelProvider() {
128 public String getText(Object element) {
129 ElementSelector selector = (ElementSelector) element;
130 return selector.getName();
134 public Image getImage(Object element) {
139 column2 = new TreeViewerColumn(viewer, SWT.NONE);
140 column2.getColumn().setText("Query");
141 column2.getColumn().setWidth(600);
142 column2.getColumn().setResizable(true);
143 column2.setLabelProvider(new ColumnLabelProvider() {
145 public String getText(Object element) {
146 ElementSelector selector = (ElementSelector) element;
147 return selector.getExpression();
151 public Image getImage(Object element) {
157 public Tree getTree() {
158 return viewer.getTree();
161 public ElementSelector getSelectedItem() {
162 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
163 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
166 private static final class SelectionElement extends AdaptableHintContext {
167 private SelectionElement(Key[] keys) {
171 @SuppressWarnings("unchecked")
172 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
173 Resource element = getHint(SelectionHints.KEY_MAIN);
174 if (contentType instanceof AnyResource) {
177 else if (contentType instanceof AnyVariable) {
179 return (T) Simantics.getSession().syncRequest(new ResourceRead<Variable>(element) {
180 public Variable perform(ReadGraph graph) throws DatabaseException {
181 return ElementSelector.getVariableForElement(graph, resource);
184 } catch (DatabaseException e) {
193 public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
195 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
196 Layer0 L0 = Layer0.getInstance(graph);
197 ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
199 Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
201 return Collections.emptyList();
204 List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
206 return Collections.emptyList();
208 Resource lib = libs.get(0);
210 List<ElementSelector> result = new ArrayList<>();
211 for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
212 if (!graph.isInstanceOf(selection, ES.Selection))
215 result.add(ElementSelector.getSelector(graph, selection));
222 private static final class DoubleClickListener implements IDoubleClickListener {
223 private final ESelectionService selectionService;
224 private Resource model;
226 private DoubleClickListener(ESelectionService selectionService) {
227 this.selectionService = selectionService;
231 public void doubleClick(DoubleClickEvent event) {
232 TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
233 ElementSelector query = (ElementSelector) selection.getFirstElement();
235 List<Resource> result = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
237 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
238 model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
240 LOGGER.warn("No active model");
241 return Collections.emptyList();
244 return query.selectElementsFrom(graph, model);
248 if (query.getGenerator() instanceof DiagramGenerator) {
249 DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), result);
252 selectionService.setPostSelection(new StructuredSelection(Lists.map(new FunctionImpl1<Resource, AdaptableHintContext>() {
253 public AdaptableHintContext apply(Resource p0) {
254 AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS);
255 selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
256 selectionElement.setHint(SelectionHints.KEY_MODEL, model);
257 return selectionElement;
261 } catch (DatabaseException e) {
262 LOGGER.error("Element selection query failed", e);