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.dialogs.MessageDialog;
10 import org.eclipse.jface.viewers.ColumnLabelProvider;
11 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
12 import org.eclipse.jface.viewers.DoubleClickEvent;
13 import org.eclipse.jface.viewers.IDoubleClickListener;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16 import org.eclipse.jface.viewers.StructuredSelection;
17 import org.eclipse.jface.viewers.TreeSelection;
18 import org.eclipse.jface.viewers.TreeViewer;
19 import org.eclipse.jface.viewers.TreeViewerColumn;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.FillLayout;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
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.common.request.ResourceRead;
33 import org.simantics.db.exception.DatabaseException;
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.layer0.variable.Variable;
38 import org.simantics.db.request.Read;
39 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
40 import org.simantics.district.selection.ElementSelectionResource;
41 import org.simantics.district.selection.ElementSelector;
42 import org.simantics.district.selection.ElementSelector.DiagramGenerator;
43 import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
44 import org.simantics.district.selection.ElementSelector.PropertySelector;
45 import org.simantics.district.selection.ElementSelector.SelectionResult;
46 import org.simantics.layer0.Layer0;
47 import org.simantics.ui.selection.AnyResource;
48 import org.simantics.ui.selection.AnyVariable;
49 import org.simantics.ui.selection.WorkbenchSelectionContentType;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class ElementSelectorTableUI extends Composite {
55 private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
57 private TreeViewer viewer;
58 private TreeViewerColumn column1;
59 private TreeViewerColumn column2;
61 public ElementSelectorTableUI(ESelectionService selectionService, Composite parent, int style) {
63 parent.setLayout(new FillLayout());
64 // GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
65 // GridLayoutFactory.fillDefaults().numColumns(1).applyTo(this);
66 this.setLayout(new FillLayout());
68 viewer = new TreeViewer(this, SWT.FULL_SELECTION);
69 viewer.addDoubleClickListener(new DoubleClickListener(selectionService));
71 viewer.setContentProvider(new ITreeContentProvider() {
73 public boolean hasChildren(Object element) {
78 public Object getParent(Object element) {
83 public Object[] getElements(Object inputElement) {
84 if (inputElement == null || !(inputElement instanceof Collection))
87 return ((Collection<?>)inputElement).toArray();
91 public Object[] getChildren(Object parentElement) {
96 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
97 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
101 Simantics.getSession().asyncRequest(new SelectionsRequest(), new SyncListenerAdapter<Collection<ElementSelector>>() {
102 public void execute(ReadGraph graph, Collection<ElementSelector> result) {
103 parent.getDisplay().asyncExec(() -> {
104 viewer.setInput(result);
109 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
110 LOGGER.error("Error getting element selector list", t);
114 public boolean isDisposed() {
115 return ElementSelectorTableUI.this.isDisposed();
119 ColumnViewerToolTipSupport.enableFor(viewer);
121 Tree table = viewer.getTree();
122 table.setHeaderVisible(true);
123 table.setLinesVisible(true);
125 column1 = new TreeViewerColumn(viewer, SWT.NONE);
126 column1.getColumn().setText("Name");
127 column1.getColumn().setWidth(200);
128 column1.getColumn().setResizable(true);
129 column1.setLabelProvider(new ColumnLabelProvider() {
131 public String getText(Object element) {
132 ElementSelector selector = (ElementSelector) element;
133 return selector.getName();
137 public Image getImage(Object element) {
142 column2 = new TreeViewerColumn(viewer, SWT.NONE);
143 column2.getColumn().setText("Query");
144 column2.getColumn().setWidth(600);
145 column2.getColumn().setResizable(true);
146 column2.setLabelProvider(new ColumnLabelProvider() {
148 public String getText(Object element) {
149 ElementSelector selector = (ElementSelector) element;
150 return selector.getExpression();
154 public Image getImage(Object element) {
160 public Tree getTree() {
161 return viewer.getTree();
164 public ElementSelector getSelectedItem() {
165 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
166 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
169 private static final class SelectionElement extends AdaptableHintContext {
170 private SelectionElement(Key[] keys) {
174 @SuppressWarnings("unchecked")
175 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
176 Resource element = getHint(SelectionHints.KEY_MAIN);
177 if (contentType instanceof AnyResource) {
180 else if (contentType instanceof AnyVariable) {
182 return (T) Simantics.getSession().syncRequest(new ResourceRead<Variable>(element) {
183 public Variable perform(ReadGraph graph) throws DatabaseException {
184 return ElementSelector.getVariableForElement(graph, resource);
187 } catch (DatabaseException e) {
196 public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
198 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
199 Layer0 L0 = Layer0.getInstance(graph);
200 ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
202 Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
204 return Collections.emptyList();
207 List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
209 return Collections.emptyList();
211 Resource lib = libs.get(0);
213 List<ElementSelector> result = new ArrayList<>();
214 for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
215 if (!graph.isInstanceOf(selection, ES.Selection))
218 result.add(ElementSelector.getSelector(graph, selection));
225 private static final class DoubleClickListener implements IDoubleClickListener {
226 private final ESelectionService selectionService;
227 private Resource model;
229 private DoubleClickListener(ESelectionService selectionService) {
230 this.selectionService = selectionService;
234 public void doubleClick(DoubleClickEvent event) {
235 TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
236 ElementSelector query = (ElementSelector) selection.getFirstElement();
238 SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
240 public SelectionResult perform(ReadGraph graph) throws DatabaseException {
241 model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
243 LOGGER.warn("No active model");
244 return new SelectionResult(Collections.emptyList(), 0, 0);
247 return query.selectElementsFrom(graph, model);
251 if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
252 DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), new ArrayList<>(result.elements));
255 selectionService.setPostSelection(new StructuredSelection(result.elements.stream()
257 AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS);
258 selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
259 selectionElement.setHint(SelectionHints.KEY_MODEL, model);
260 return selectionElement;
265 if (result.tailCount != result.tailSize) {
266 String name = query.getSelector() != null && query.getSelector() instanceof PropertySelector ? ((PropertySelector)query.getSelector()).propertyName : null;
267 String msg = "Last " + result.tailCount + " of the " + result.elements.size() + " selected elements are an arbitraty subset of " + result.tailSize + " elements with equal values" +
268 (name != null ? " for " + name : "");
269 MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Note", msg);
271 } catch (DatabaseException e) {
272 LOGGER.error("Element selection query failed", e);