]> gerrit.simantics Code Review - simantics/district.git/blob
0786ce5381b2ea994e8d4a42d1e2bdb1f4d3ca55
[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.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;
49
50 public class ElementSelectorTableUI extends Composite {
51
52         private static final Logger LOGGER = LoggerFactory.getLogger(ElementSelectorTableUI.class);
53
54         private TreeViewer viewer;
55         private TreeViewerColumn column1;
56         private TreeViewerColumn column2;
57
58         public ElementSelectorTableUI(ESelectionService selectionService, Composite parent, int style) {
59                 super(parent, 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());
64
65                 viewer = new TreeViewer(this, SWT.FULL_SELECTION);
66                 viewer.addDoubleClickListener(new IDoubleClickListener() {
67                         @Override
68                         public void doubleClick(DoubleClickEvent event) {
69                                 TreeSelection selection = (TreeSelection) viewer.getSelection();
70                                 ElementSelector query = (ElementSelector) selection.getFirstElement();
71                                 try {
72                                         List<AdaptableHintContext> elements = Simantics.getSession().syncRequest(new Read<List<AdaptableHintContext>>() {
73                                                 @SuppressWarnings("unchecked")
74                                                 @Override
75                                                 public List<AdaptableHintContext> perform(ReadGraph graph) throws DatabaseException {
76                                                         Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
77                                                         if (model == null) {
78                                                                 LOGGER.warn("No active model");
79                                                                 return Collections.emptyList();
80                                                         }
81                                                         
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;
89                                                                 }
90                                                         }, result);
91                                                 }
92                                         });
93                                         
94                                         selectionService.setPostSelection(new StructuredSelection(elements));
95                                 } catch (DatabaseException e) {
96                                         LOGGER.error("Element selection query failed", e);
97                                 }
98                         }
99                 });
100                 
101                 viewer.setContentProvider(new ITreeContentProvider() {
102                         @Override
103                         public boolean hasChildren(Object element) {
104                                 return false;
105                         }
106
107                         @Override
108                         public Object getParent(Object element) {
109                                 return null;
110                         }
111
112                         @Override
113                         public Object[] getElements(Object inputElement) {
114                                 if (inputElement == null || !(inputElement instanceof Collection))
115                                         return new Object[0];
116                                 
117                                 return ((Collection<?>)inputElement).toArray();
118                         }
119
120                         @Override
121                         public Object[] getChildren(Object parentElement) {
122                                 return null;
123                         }
124
125                         @Override
126                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
127                                 ITreeContentProvider.super.inputChanged(viewer, oldInput, newInput);
128                         }
129                 });
130                 
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);
135                                 });
136                         }
137
138                         @Override
139                         public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
140                                 LOGGER.error("Error getting element selector list", t);
141                         }
142
143                         @Override
144                         public boolean isDisposed() {
145                                 return ElementSelectorTableUI.this.isDisposed();
146                         }
147                 });
148
149                 ColumnViewerToolTipSupport.enableFor(viewer);
150
151                 Tree table = viewer.getTree();
152                 table.setHeaderVisible(true);
153                 table.setLinesVisible(true);
154
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() {
160                         @Override
161                         public String getText(Object element) {
162                                 ElementSelector selector = (ElementSelector) element;
163                                 return selector.getName();
164                         }
165                         
166                         @Override
167                         public Image getImage(Object element) {
168                                 return null;
169                         }
170                 });
171
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() {
177                         @Override
178                         public String getText(Object element) {
179                                 ElementSelector selector = (ElementSelector) element;
180                                 return selector.getExpression();
181                         }
182                         
183                         @Override
184                         public Image getImage(Object element) {
185                                 return null;
186                         }
187                 });
188         }
189
190         public Tree getTree() {
191                 return viewer.getTree();
192         }
193
194         public ElementSelector getSelectedItem() {
195                 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
196                 return selection != null ? (ElementSelector) selection.getFirstElement() : null;
197         }
198
199         private final class SelectionElement extends AdaptableHintContext {
200                 private final ReadGraph graph;
201
202                 private SelectionElement(Key[] keys, ReadGraph graph) {
203                         super(keys);
204                         this.graph = graph;
205                 }
206
207                 @SuppressWarnings("unchecked")
208                 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
209                         Resource element = getHint(SelectionHints.KEY_MAIN);
210                         if (contentType instanceof AnyResource) {
211                                 return (T)element;
212                         }
213                         else if (contentType instanceof AnyVariable) {
214                                 try {
215                                         return (T)ElementSelector.getVariableForElement(graph, element);
216                                 } catch (DatabaseException e) {
217                                         return null;
218                                 }
219                         }
220                         
221                         return null;
222                 }
223         }
224
225         public static class SelectionsRequest implements Read<Collection<ElementSelector>> {
226                 @Override
227                 public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
228                         Layer0 L0 = Layer0.getInstance(graph);
229                         ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
230                         
231                         Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
232                         if (model == null) {
233                                 return Collections.emptyList();
234                         }
235                         
236                         List<Resource> libs = QueryIndexUtils.searchByType(graph, model, ES.SelectionLibrary);
237                         if (libs.isEmpty())
238                                 return Collections.emptyList();
239                         
240                         Resource lib = libs.get(0);
241                         
242                         List<ElementSelector> result = new ArrayList<>();
243                         for (Resource selection : graph.getObjects(lib, L0.ConsistsOf)) {
244                                 if (!graph.isInstanceOf(selection, ES.Selection))
245                                         continue;
246                                 
247                                 result.add(ElementSelector.getSelector(graph, selection));
248                         }
249                         
250                         return result;
251                 }
252         }
253 }