X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=inline;f=org.simantics.district.selection.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fselection%2Fui%2FElementSelectorTableUI.java;h=001c8730970895461b67ffba3e766c9a4be5dae8;hb=ae7a997a6776a0b3b6f94cc82b8cc93a6e82cc79;hp=0786ce5381b2ea994e8d4a42d1e2bdb1f4d3ca55;hpb=09b78f5c26bb8b521beb6eeeb5c3f44ac1aa4d07;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java index 0786ce53..001c8730 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/ElementSelectorTableUI.java @@ -6,14 +6,13 @@ import java.util.Collections; import java.util.List; import org.eclipse.e4.ui.workbench.modeling.ESelectionService; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ColumnLabelProvider; 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; @@ -23,24 +22,28 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Tree; import org.simantics.Simantics; import org.simantics.browsing.ui.common.AdaptableHintContext; 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.district.selection.ElementSelector.PropertySelector; +import org.simantics.district.selection.ElementSelector.SelectionResult; import org.simantics.layer0.Layer0; -import org.simantics.scl.runtime.Lists; -import org.simantics.scl.runtime.function.FunctionImpl1; import org.simantics.ui.selection.AnyResource; import org.simantics.ui.selection.AnyVariable; import org.simantics.ui.selection.WorkbenchSelectionContentType; @@ -63,40 +66,7 @@ public class ElementSelectorTableUI extends Composite { 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 elements = Simantics.getSession().syncRequest(new Read>() { - @SuppressWarnings("unchecked") - @Override - public List perform(ReadGraph graph) throws DatabaseException { - Resource model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource()); - if (model == null) { - LOGGER.warn("No active model"); - return Collections.emptyList(); - } - - List result = query.selectElementsFrom(graph, model); - return Lists.map(new FunctionImpl1() { - 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 @@ -196,12 +166,9 @@ public class ElementSelectorTableUI extends Composite { 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") @@ -212,7 +179,11 @@ public class ElementSelectorTableUI extends Composite { } else if (contentType instanceof AnyVariable) { try { - return (T)ElementSelector.getVariableForElement(graph, element); + return (T) Simantics.getSession().syncRequest(new ResourceRead(element) { + public Variable perform(ReadGraph graph) throws DatabaseException { + return ElementSelector.getVariableForElement(graph, resource); + } + }); } catch (DatabaseException e) { return null; } @@ -222,7 +193,7 @@ public class ElementSelectorTableUI extends Composite { } } - public static class SelectionsRequest implements Read> { + public static final class SelectionsRequest implements Read> { @Override public Collection perform(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); @@ -250,4 +221,56 @@ public class ElementSelectorTableUI extends Composite { 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 { + SelectionResult result = Simantics.getSession().syncRequest(new Read() { + @Override + public SelectionResult perform(ReadGraph graph) throws DatabaseException { + model = ActiveModels.getPossibleActiveModel(graph, Simantics.getProjectResource()); + if (model == null) { + LOGGER.warn("No active model"); + return new SelectionResult(Collections.emptyList(), 0, 0); + } + + return query.selectElementsFrom(graph, model); + } + }); + + if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) { + DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), new ArrayList<>(result.elements)); + } + else { + selectionService.setPostSelection(new StructuredSelection(result.elements.stream() + .map(p0 -> { + AdaptableHintContext selectionElement = new SelectionElement(SelectionHints.STD_KEYS); + selectionElement.setHint(SelectionHints.KEY_MAIN, p0); + selectionElement.setHint(SelectionHints.KEY_MODEL, model); + return selectionElement; + }) + .toArray())); + } + + if (result.tailCount != result.tailSize) { + String name = query.getSelector() != null && query.getSelector() instanceof PropertySelector ? ((PropertySelector)query.getSelector()).propertyName : null; + String msg = "Last " + result.tailCount + " of the " + result.elements.size() + " selected elements are an arbitraty subset of " + result.tailSize + " elements with equal values" + + (name != null ? " for " + name : ""); + MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Note", msg); + } + } catch (DatabaseException e) { + LOGGER.error("Element selection query failed", e); + } + } + } }