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