From: Reino Ruusu Date: Fri, 22 Feb 2019 15:11:00 +0000 (+0200) Subject: Selection queries from explicit sets of elements. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2801a636fc48e9636283a85276130594c27979a7;p=simantics%2Fdistrict.git Selection queries from explicit sets of elements. gitlab #28 Change-Id: Iac2f3a51b66b37b4fbe04dc1f351327231ea00c7 --- diff --git a/org.simantics.district.selection.ui/META-INF/MANIFEST.MF b/org.simantics.district.selection.ui/META-INF/MANIFEST.MF index 91f61246..31f83e38 100644 --- a/org.simantics.district.selection.ui/META-INF/MANIFEST.MF +++ b/org.simantics.district.selection.ui/META-INF/MANIFEST.MF @@ -26,7 +26,9 @@ Require-Bundle: javax.inject, org.simantics.district.route.ontology, org.simantics.modeling.ontology, org.simantics.silk.ontology;bundle-version="1.1.0", - org.simantics.district.network.ui + org.simantics.district.network.ui, + org.simantics.structural.ontology, + org.simantics.diagram.ontology Bundle-ManifestVersion: 2 Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Bundle-Vendor: Semantum oy 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 8ed8d3e1..09f34609 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 @@ -38,6 +38,7 @@ import org.simantics.district.network.ui.DistrictNetworkUIUtil; import org.simantics.district.selection.ElementSelectionResource; 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; @@ -245,7 +246,7 @@ public class ElementSelectorTableUI extends Composite { } }); - if (query.getGenerator() instanceof DiagramGenerator) { + if (query.getGenerator() instanceof DiagramGenerator || query.getGenerator() instanceof ExplicitGenerator) { DistrictNetworkUIUtil.openDNDiagramWithSelection(event.getViewer().getControl().getDisplay(), result); } else { diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/CreateNewElementSelector.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/CreateNewElementSelector.java index b9df8b84..10ea9e81 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/CreateNewElementSelector.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/CreateNewElementSelector.java @@ -3,12 +3,15 @@ package org.simantics.district.selection.ui.handlers; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.e4.ui.workbench.IWorkbench; import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Shell; +import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.district.selection.ui.parts.EditSelectorDialog; +import org.simantics.utils.ui.AdaptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,8 +26,10 @@ public class CreateNewElementSelector { @Execute public void createNewElementSelector(IEclipseContext context, IWorkbench workbench) { + Object currentSelection = context.get(IServiceConstants.ACTIVE_SELECTION); + Shell shell = context.getActive(Shell.class); - EditSelectorDialog dialog = new EditSelectorDialog(shell, null); + EditSelectorDialog dialog = new EditSelectorDialog(shell, null, AdaptionUtils.adaptToCollection(currentSelection, Resource.class)); LOGGER.debug("Opening dialog"); int result = dialog.open(); LOGGER.debug("Dialog closed with result code " + result); diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/EditElementSelector.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/EditElementSelector.java index f914d161..69fcbccd 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/EditElementSelector.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/EditElementSelector.java @@ -1,20 +1,26 @@ package org.simantics.district.selection.ui.handlers; +import java.util.Collection; + import javax.inject.Inject; -import org.eclipse.e4.core.contexts.ContextInjectionFactory; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.e4.ui.workbench.IWorkbench; import org.eclipse.e4.ui.workbench.modeling.EPartService; import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Shell; +import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; +import org.simantics.district.selection.ElementSelector; +import org.simantics.district.selection.ElementSelector.ExplicitGenerator; import org.simantics.district.selection.ui.parts.EditSelectorDialog; import org.simantics.district.selection.ui.parts.ElementSelectionView; +import org.simantics.utils.ui.AdaptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,6 +46,8 @@ public class EditElementSelector { @Execute public void editElementSelector(IEclipseContext context, IWorkbench workbench) { + Object currentSelection = context.get(IServiceConstants.ACTIVE_SELECTION); + MPart part = partService.getActivePart(); if (part == null) return; @@ -49,8 +57,17 @@ public class EditElementSelector { ElementSelectionView view = (ElementSelectionView)object; + Collection selection; + ElementSelector selectedItem = view.getSelectedItem(); + if (selectedItem.getGenerator() instanceof ExplicitGenerator) { + selection = ((ExplicitGenerator)selectedItem.getGenerator()).elements; + } + else { + selection = AdaptionUtils.adaptToCollection(currentSelection, Resource.class); + } + Shell shell = context.getActive(Shell.class); - EditSelectorDialog dialog = new EditSelectorDialog(shell, view.getSelectedItem()); + EditSelectorDialog dialog = new EditSelectorDialog(shell, selectedItem, selection); LOGGER.debug("Opening dialog"); int result = dialog.open(); diff --git a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java index 42270da9..b68c3fe1 100644 --- a/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java +++ b/org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java @@ -1,6 +1,7 @@ package org.simantics.district.selection.ui.parts; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,8 +39,10 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.RuntimeDatabaseException; import org.simantics.db.layer0.QueryIndexUtils; import org.simantics.db.layer0.request.ActiveModels; +import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.request.Read; import org.simantics.db.request.WriteResult; +import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.region.ontology.DiagramRegionsResource; import org.simantics.district.route.ontology.RouteResource; import org.simantics.district.selection.ElementSelectionResource; @@ -69,6 +72,9 @@ public class EditSelectorDialog extends Dialog { private static Logger LOGGER = LoggerFactory.getLogger(EditSelectorDialog.class); private ElementSelector elementSelector; + + // Currently selected elements + Collection currentSelection; // Data for comboboxes Map diagrams; @@ -106,7 +112,7 @@ public class EditSelectorDialog extends Dialog { private Composite content; @Inject - public EditSelectorDialog(Shell shell, ElementSelector elementSelector) { + public EditSelectorDialog(Shell shell, ElementSelector elementSelector, Collection currentSelection) { super(shell); this.elementSelector = elementSelector; @@ -124,6 +130,8 @@ public class EditSelectorDialog extends Dialog { } } + this.currentSelection = currentSelection; + final Map regions = new HashMap<>(); final Map routes = new HashMap<>(); @@ -229,8 +237,11 @@ public class EditSelectorDialog extends Dialog { public void perform(WriteGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); ElementSelectionResource ES = ElementSelectionResource.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); graph.markUndoPoint(); + Layer0Utils.addCommentMetadata(graph, "Created new element selection"); Resource lib = ElementSelectionUtils.ensureSelectionLibrary(graph); @@ -258,12 +269,26 @@ public class EditSelectorDialog extends Dialog { break; case 1: generatorType = ES.Generator_Diagram; - Resource composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite); + Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite); graph.claim(generator, ES.Generator_HasDiagram, composite != null ? composite : diagram); break; case 2: generatorType = ES.Generator_Explicit; - // TODO: Claim relations to current selection elements + for (Resource r : currentSelection) { + // No connections + if (graph.isInstanceOf(r, DIA.Connection)) + continue; + if (!graph.isInstanceOf(r, DIA.Element)) { + if (!graph.hasStatement(r, MOD.ComponentToElement)) + continue; + + r = graph.getPossibleObject(r, MOD.ComponentToElement); + if (r == null) + continue; + } + + graph.claim(generator, ES.Generator_HasSelectedElement, r); + } break; default: throw new IllegalStateException("Invalid source index " + generatorIndex); }