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.district.selection.ElementSelector.ExplicitGenerator;
42 import org.simantics.layer0.Layer0;
43 import org.simantics.scl.runtime.Lists;
44 import org.simantics.scl.runtime.function.FunctionImpl1;
45 import org.simantics.ui.selection.AnyResource;
46 import org.simantics.ui.selection.AnyVariable;
47 import org.simantics.ui.selection.WorkbenchSelectionContentType;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class ElementSelectorTableUI extends Composite {
53 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
55 private TreeViewer viewer;
56 private TreeViewerColumn column1;
57 private TreeViewerColumn column2;
59 public ElementSelectorTableUI(ESelectionService selectionService, Composite parent, int style) {
61 parent.setLayout(new FillLayout());
62 // GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
63 // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
64 this.setLayout(new FillLayout());
66 viewer = new TreeViewer(this, SWT.FULL_SELECTION);
67 viewer.addDoubleClickListener(new DoubleClickListener(selectionService));
69 viewer.setContentProvider(new ITreeContentProvider() {
71 public boolean hasChildren(Object element) {
76 public Object getParent(Object element) {
81 public Object[] getElements(Object inputElement) {
82 if (inputElement == null || !(inputElement instanceof Collection))
85 return ((Collection<?>)inputElement).toArray();
89 public Object[] getChildren(Object parentElement) {
94 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
95 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
99 Simantics.getSession().asyncRequest(new SelectionsRequest(), new SyncListenerAdapter<Collection<ElementSelector>>() {
100 public void execute(ReadGraph graph, Collection<ElementSelector> result) {
101 parent.getDisplay().asyncExec(() -> {
102 viewer.setInput(result);
107 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
108 LOGGER.error("Error getting element selector list", t);
112 public boolean isDisposed() {
113 return ElementSelectorTableUI.this.isDisposed();
117 ColumnViewerToolTipSupport.enableFor(viewer);
119 Tree table = viewer.getTree();
120 table.setHeaderVisible(true);
121 table.setLinesVisible(true);
123 column1 = new TreeViewerColumn(viewer, SWT.NONE);
124 column1.getColumn().setText("Name");
125 column1.getColumn().setWidth(200);
126 column1.getColumn().setResizable(true);
127 column1.setLabelProvider(new ColumnLabelProvider() {
129 public String getText(Object element) {
130 ElementSelector selector = (ElementSelector) element;
131 return selector.getName();
135 public Image getImage(Object element) {
140 column2 = new TreeViewerColumn(viewer, SWT.NONE);
141 column2.getColumn().setText("Query");
142 column2.getColumn().setWidth(600);
143 column2.getColumn().setResizable(true);
144 column2.setLabelProvider(new ColumnLabelProvider() {
146 public String getText(Object element) {
147 ElementSelector selector = (ElementSelector) element;
148 return selector.getExpression();
152 public Image getImage(Object element) {
158 public Tree getTree() {
159 return viewer.getTree();
162 public ElementSelector getSelectedItem() {
163 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
164 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
167 private static final class SelectionElement extends AdaptableHintContext {
168 private SelectionElement(Key[] keys) {
172 @SuppressWarnings("unchecked")
173 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
174 Resource element = getHint(SelectionHints.KEY_MAIN);
175 if (contentType instanceof AnyResource) {
178 else if (contentType instanceof AnyVariable) {
180 return (T) Simantics.getSession().syncRequest(new ResourceRead<Variable>(element) {
181 public Variable perform(ReadGraph graph) throws DatabaseException {
182 return ElementSelector.getVariableForElement(graph, resource);
185 } catch (DatabaseException e) {
194 public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
196 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
197 Layer0 L0 = Layer0.getInstance(graph);
198 ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
200 Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
202 return Collections.emptyList();
205 List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
207 return Collections.emptyList();
209 Resource lib = libs.get(0);
211 List<ElementSelector> result = new ArrayList<>();
212 for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
213 if (!graph.isInstanceOf(selection, ES.Selection))
216 result.add(ElementSelector.getSelector(graph, selection));
223 private static final class DoubleClickListener implements IDoubleClickListener {
224 private final ESelectionService selectionService;
225 private Resource model;
227 private DoubleClickListener(ESelectionService selectionService) {
228 this.selectionService = selectionService;
232 public void doubleClick(DoubleClickEvent event) {
233 TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
234 ElementSelector query = (ElementSelector) selection.getFirstElement();
236 List<Resource> result = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
238 public List<Resource> perform(ReadGraph graph) throws DatabaseException {
239 model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
241 LOGGER.warn("No active model");
242 return Collections.emptyList();
245 return query.selectElementsFrom(graph, model);
249 if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
250 DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), result);
253 selectionService.setPostSelection(new StructuredSelection(Lists.map(new FunctionImpl1<Resource, AdaptableHintContext>() {
254 public AdaptableHintContext apply(Resource p0) {
255 AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS);
256 selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
257 selectionElement.setHint(SelectionHints.KEY_MODEL, model);
258 return selectionElement;
262 } catch (DatabaseException e) {
263 LOGGER.error("Element selection query failed", e);