import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
+import org.simantics.db.common.request.ResourceRead;
import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.exception.RuntimeDatabaseException;
import org.simantics.db.layer0.QueryIndexUtils;
import org.simantics.db.layer0.SelectionHints;
import org.simantics.db.layer0.request.ActiveModels;
+import org.simantics.db.layer0.variable.Variable;
import org.simantics.db.request.Read;
+import org.simantics.district.network.ui.DistrictNetworkUIUtil;
import org.simantics.district.selection.ElementSelectionResource;
-import org.simantics.district.selection.ElementSelectionUtils;
import org.simantics.district.selection.ElementSelector;
+import org.simantics.district.selection.ElementSelector.DiagramGenerator;
+import org.simantics.district.selection.ElementSelector.ExplicitGenerator;
import org.simantics.layer0.Layer0;
import org.simantics.scl.runtime.Lists;
import org.simantics.scl.runtime.function.FunctionImpl1;
this.setLayout(new FillLayout());
viewer = new TreeViewer(this, SWT.FULL_SELECTION);
- viewer.addDoubleClickListener(new IDoubleClickListener() {
- @Override
- public void doubleClick(DoubleClickEvent event) {
- TreeSelection selection = (TreeSelection) viewer.getSelection();
- ElementSelector query = (ElementSelector) selection.getFirstElement();
- try {
- List<AdaptableHintContext> elements = Simantics.getSession().syncRequest(new Read<List<AdaptableHintContext>>() {
- @SuppressWarnings("unchecked")
- @Override
- public List<AdaptableHintContext> perform(ReadGraph graph) throws DatabaseException {
- Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
- if (model == null) {
- LOGGER.warn("No active model");
- return Collections.emptyList();
- }
-
- List<Resource> result = query.selectElementsFrom(graph, model);
- return Lists.map(new FunctionImpl1<Resource, AdaptableHintContext>() {
- public AdaptableHintContext apply(Resource p0) {
- AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS, graph);
- selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
- selectionElement.setHint(SelectionHints.KEY_MODEL, model);
- return selectionElement;
- }
- }, result);
- }
- });
-
- selectionService.setPostSelection(new StructuredSelection(elements));
- } catch (DatabaseException e) {
- LOGGER.error("Element selection query failed", e);
- }
- }
- });
+ viewer.addDoubleClickListener(new DoubleClickListener(selectionService));
viewer.setContentProvider(new ITreeContentProvider() {
@Override
return selection != null ? (ElementSelector) selection.getFirstElement() : null;
}
- private final class SelectionElement extends AdaptableHintContext {
- private final ReadGraph graph;
-
- private SelectionElement(Key[] keys, ReadGraph graph) {
+ private static final class SelectionElement extends AdaptableHintContext {
+ private SelectionElement(Key[] keys) {
super(keys);
- this.graph = graph;
}
@SuppressWarnings("unchecked")
}
else if (contentType instanceof AnyVariable) {
try {
- return (T)ElementSelector.getVariableForElement(graph, element);
+ return (T) Simantics.getSession().syncRequest(new ResourceRead<Variable>(element) {
+ public Variable perform(ReadGraph graph) throws DatabaseException {
+ return ElementSelector.getVariableForElement(graph, resource);
+ }
+ });
} catch (DatabaseException e) {
return null;
}
}
}
- public static class SelectionsRequest implements Read<Collection<ElementSelector>> {
+ public static final class SelectionsRequest implements Read<Collection<ElementSelector>> {
@Override
public Collection<ElementSelector> perform(ReadGraph graph) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(graph);
return result;
}
}
+
+ private static final class DoubleClickListener implements IDoubleClickListener {
+ private final ESelectionService selectionService;
+ private Resource model;
+
+ private DoubleClickListener(ESelectionService selectionService) {
+ this.selectionService = selectionService;
+ }
+
+ @Override
+ public void doubleClick(DoubleClickEvent event) {
+ TreeSelection selection = (TreeSelection) event.getViewer().getSelection();
+ ElementSelector query = (ElementSelector) selection.getFirstElement();
+ try {
+ List<Resource> result = Simantics.getSession().syncRequest(new Read<List<Resource>>() {
+ @Override
+ public List<Resource> perform(ReadGraph graph) throws DatabaseException {
+ model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource());
+ if (model == null) {
+ LOGGER.warn("No active model");
+ return Collections.emptyList();
+ }
+
+ return query.selectElementsFrom(graph, model);
+ }
+ });
+
+ if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) {
+ DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), result);
+ }
+ else {
+ selectionService.setPostSelection(new StructuredSelection(Lists.map(new FunctionImpl1<Resource, AdaptableHintContext>() {
+ public AdaptableHintContext apply(Resource p0) {
+ AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS);
+ selectionElement.setHint(SelectionHints.KEY_MAIN, p0);
+ selectionElement.setHint(SelectionHints.KEY_MODEL, model);
+ return selectionElement;
+ }
+ }, result)));
+ }
+ } catch (DatabaseException e) {
+ LOGGER.error("Element selection query failed", e);
+ }
+ }
+ }
}