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