]> gerrit.simantics Code Review - simantics/district.git/blob
001c8730970895461b67ffba3e766c9a4be5dae8
[simantics/district.git] /
1 package org.simantics.district.selection.ui;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7
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;
52
53 public class ElementSelectorTableUI extends Composite {
54
55         private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
56
57         private TreeViewer viewer;
58         private TreeViewerColumn column1;
59         private TreeViewerColumn column2;
60
61         public ElementSelectorTableUI(ESelectionService selectionService, Composite parent, int style) {
62                 super(parent, 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());
67
68                 viewer = new TreeViewer(this, SWT.FULL_SELECTION);
69                 viewer.addDoubleClickListener(new DoubleClickListener(selectionService));
70                 
71                 viewer.setContentProvider(new ITreeContentProvider() {
72                         @Override
73                         public boolean hasChildren(Object element) {
74                                 return false;
75                         }
76
77                         @Override
78                         public Object getParent(Object element) {
79                                 return null;
80                         }
81
82                         @Override
83                         public Object[] getElements(Object inputElement) {
84                                 if (inputElement == null || !(inputElement instanceof Collection))
85                                         return new Object[0];
86                                 
87                                 return ((Collection<?>)inputElement).toArray();
88                         }
89
90                         @Override
91                         public Object[] getChildren(Object parentElement) {
92                                 return null;
93                         }
94
95                         @Override
96                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
97                                 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
98                         }
99                 });
100                 
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);
105                                 });
106                         }
107
108                         @Override
109                         public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
110                                 LOGGER.error("Error getting element selector list", t);
111                         }
112
113                         @Override
114                         public boolean isDisposed() {
115                                 return ElementSelectorTableUI.this.isDisposed();
116                         }
117                 });
118
119                 ColumnViewerToolTipSupport.enableFor(viewer);
120
121                 Tree table = viewer.getTree();
122                 table.setHeaderVisible(true);
123                 table.setLinesVisible(true);
124
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() {
130                         @Override
131                         public String getText(Object element) {
132                                 ElementSelector selector = (ElementSelector) element;
133                                 return selector.getName();
134                         }
135                         
136                         @Override
137                         public Image getImage(Object element) {
138                                 return null;
139                         }
140                 });
141
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() {
147                         @Override
148                         public String getText(Object element) {
149                                 ElementSelector selector = (ElementSelector) element;
150                                 return selector.getExpression();
151                         }
152                         
153                         @Override
154                         public Image getImage(Object element) {
155                                 return null;
156                         }
157                 });
158         }
159
160         public Tree getTree() {
161                 return viewer.getTree();
162         }
163
164         public ElementSelector getSelectedItem() {
165                 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
166                 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
167         }
168
169         private static final class SelectionElement extends AdaptableHintContext {
170                 private SelectionElement(Key[] keys) {
171                         super(keys);
172                 }
173
174                 @SuppressWarnings("unchecked")
175                 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
176                         Resource element = getHint(SelectionHints.KEY_MAIN);
177                         if (contentType instanceof AnyResource) {
178                                 return (T)element;
179                         }
180                         else if (contentType instanceof AnyVariable) {
181                                 try {
182                                         return (T) Simantics.getSession().syncRequest(new ResourceRead<Variable>(element) {
183                                                 public Variable perform(ReadGraph graph) throws DatabaseException {
184                                                         return ElementSelector.getVariableForElement(graph, resource);
185                                                 }
186                                         });
187                                 } catch (DatabaseException e) {
188                                         return null;
189                                 }
190                         }
191                         
192                         return null;
193                 }
194         }
195
196         public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
197                 @Override
198                 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
199                         Layer0 L0 = Layer0.getInstance(graph);
200                         ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
201                         
202                         Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
203                         if (model == null) {
204                                 return Collections.emptyList();
205                         }
206                         
207                         List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
208                         if (libs.isEmpty())
209                                 return Collections.emptyList();
210                         
211                         Resource lib = libs.get(0);
212                         
213                         List<ElementSelector> result = new ArrayList<>();
214                         for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
215                                 if (!graph.isInstanceOf(selection, ES.Selection))
216                                         continue;
217                                 
218                                 result.add(ElementSelector.getSelector(graph, selection));
219                         }
220                         
221                         return result;
222                 }
223         }
224
225         private static final class DoubleClickListener implements IDoubleClickListener {
226                 private final ESelectionService selectionService;
227                 private Resource model;
228         
229                 private DoubleClickListener(ESelectionService selectionService) {
230                         this.selectionService = selectionService;
231                 }
232         
233                 @Override
234                 public void doubleClick(DoubleClickEvent event) {
235                         TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
236                         ElementSelector query = (ElementSelector) selection.getFirstElement();
237                         try {
238                                 SelectionResult result = Simantics.getSession().syncRequest(new Read<SelectionResult>() {
239                                         @Override
240                                         public SelectionResult perform(ReadGraph graph) throws DatabaseException {
241                                                 model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
242                                                 if (model == null) {
243                                                         LOGGER.warn("No active model");
244                                                         return new SelectionResult(Collections.emptyList(), 0, 0);
245                                                 }
246                                                 
247                                                 return query.selectElementsFrom(graph, model);
248                                         }
249                                 });
250                                 
251                                 if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
252                                         DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), new ArrayList<>(result.elements));
253                                 }
254                                 else {
255                                         selectionService.setPostSelection(new StructuredSelection(result.elements.stream()
256                                                         .map(p0 -> {
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;
261                                                         })
262                                                         .toArray()));
263                                 }
264                                 
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);
270                                 }
271                         } catch (DatabaseException e) {
272                                 LOGGER.error("Element selection query failed", e);
273                         }
274                 }
275         }
276 }