import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.request.ReadRequest;
-import org.simantics.db.common.utils.ListUtils;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
import org.simantics.diagram.content.ConnectionUtil;
import org.simantics.diagram.flag.FlagUtil;
import org.simantics.diagram.stubs.DiagramResource;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.ModelingResources;
import org.simantics.modeling.ui.Activator;
-import org.simantics.ui.SimanticsUI;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.ui.selection.AnyResource;
+import org.simantics.ui.selection.AnyVariable;
+import org.simantics.ui.selection.WorkbenchSelectionContentType;
+import org.simantics.ui.selection.WorkbenchSelectionElement;
import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
+import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.ui.ISelectionUtils;
/**
* @author Tuukka Lehtonen
private static final String EDITOR_ID = "org.simantics.modeling.ui.plainDiagramEditor";
public OpenDiagramFromIssue() {
- super("Open Diagram Containing Referenced Element", Activator.COMPOSITE_ICON);
+ super("Open Diagram Containing Referenced Component", Activator.COMPOSITE_ICON);
}
protected String getEditorId() {
}
@Override
- public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
- return findElement(g, r) != null;
+ public boolean canHandle(ReadGraph g, Object input) throws DatabaseException {
+ return extractContext(g, input) != null;
}
@Override
- public void openEditor(final Resource r) throws Exception {
- SimanticsUI.getSession().asyncRequest(new ReadRequest() {
+ public void openEditor(Object input) throws Exception {
+ Simantics.getSession().asyncRequest(new ReadRequest() {
@Override
public void run(ReadGraph g) throws DatabaseException {
-
- Layer0 L0 = Layer0.getInstance(g);
- ModelingResources MOD = ModelingResources.getInstance(g);
-
- Resource element = findElement(g, r);
- if(element == null) return;
-
- System.err.println("element " + g.getURI(element));
-
- Resource diagram = g.getSingleObject(element, L0.PartOf);
- Resource composite = g.getSingleObject(diagram, MOD.DiagramToComposite);
-
- OpenDiagramFromConfigurationAdapter.openEditor(g, composite, getEditorId(), Collections.<Object>singleton(element));
-
+ Pair<Resource, Collection<Object>> data = extractContext(g, input);
+ if (data != null)
+ OpenDiagramFromConfigurationAdapter.openEditor(g, data.first, getEditorId(), data.second);
}
});
}
- private static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
+ private static Pair<Resource, Collection<Object>> extractContext(ReadGraph graph, Object input) throws DatabaseException {
+ Pair<Variable, Resource> p = extractInput(graph, input);
+ Pair<Resource, Collection<Object>> result = p.first != null ? findConfigurationAndObjects(graph, p.first) : null;
+ if (result == null && p.second != null)
+ result = findConfigurationAndObjects(graph, p.second);
+ return result;
+ }
- Layer0 L0 = Layer0.getInstance(graph);
- ModelingResources MOD = ModelingResources.getInstance(graph);
+ protected static <T> T extractContent(ReadGraph graph, Object input, WorkbenchSelectionContentType<T> contentType, Class<T> contentClass) throws DatabaseException {
+ if (contentClass.isInstance(input))
+ return contentClass.cast(input);
+ if (input instanceof WorkbenchSelectionElement) {
+ WorkbenchSelectionElement single = (WorkbenchSelectionElement) input;
+ if (single != null)
+ return single.getContent(contentType);
+ }
+ return ISelectionUtils.filterSingleSelection(input, contentClass);
+ }
- Resource diagram = graph.getPossibleObject(element, L0.PartOf);
- if (diagram == null) return null;
+ protected static Pair<Variable, Resource> extractInput(ReadGraph graph, Object input) throws DatabaseException {
+ return Pair.make(
+ extractContent(graph, input, new AnyVariable(graph), Variable.class),
+ extractContent(graph, input, new AnyResource(graph), Resource.class));
+ }
- return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
-
+ protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, Resource issue) throws DatabaseException {
+ IssueResource ISSUE = IssueResource.getInstance(graph);
+ if (!graph.isInstanceOf(issue, ISSUE.Issue))
+ return null;
+ List<Resource> contexts = graph.getRelatedValue2(issue, ISSUE.Issue_contexts);
+ return contexts != null ? findConfigurationAndObjects(graph, contexts) : null;
}
-
- private static Resource findElement(ReadGraph graph, Resource issue) throws DatabaseException {
-
- IssueResource ISSUE = IssueResource.getInstance(graph);
- DiagramResource DIA = DiagramResource.getInstance(graph);
-
- if(!graph.isInstanceOf(issue, ISSUE.Issue)) return null;
-
- Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);
- for(Resource context : ListUtils.toList(graph, list)) {
- if(graph.isInstanceOf(context, DIA.Element) && !graph.isInstanceOf(context, DIA.Diagram)) {
- return context;
- }
-
- }
-
- return null;
-
+
+ protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, Variable v) throws DatabaseException {
+ List<Resource> contexts = v.getPossiblePropertyValue(graph, IssueResource.getInstance(graph).Issue_contexts);
+ return contexts != null ? findConfigurationAndObjects(graph, contexts) : null;
+ }
+
+ protected static Pair<Resource, Collection<Object>> findConfigurationAndObjects(ReadGraph graph, List<Resource> contexts) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+
+ for(Resource context : contexts) {
+ Set<Resource> types = graph.getTypes(context);
+ if (types.contains(DIA.Element)) {
+ Resource config = findConfigurationForElement(graph, context);
+ if (config != null) {
+ Collection<Object> elements = Collections.<Object>singleton(context);
+ return Pair.make(config, elements);
+ }
+ } else if (types.contains(STR.Component) && !types.contains(STR.Composite)) {
+ Resource config = graph.getPossibleObject(context, L0.PartOf);
+ if (config != null) {
+ return Pair.make(config, findElementObjects(graph, context));
+ }
+ } else if (types.contains(STR.Connection)) {
+ Resource element = graph.getPossibleObject(context, MOD.ConnectionToDiagramConnection);
+ if (element != null) {
+ Resource config = findConfigurationForElement(graph, element);
+ if (config != null) {
+ return Pair.make(config, Collections.<Object>singleton(element));
+ }
+ }
+ }
+ }
+
+ return null;
}
- public static Collection<Object> findElementObjects(ReadGraph g, Resource module) throws DatabaseException {
+ protected static Resource findConfigurationForElement(ReadGraph graph, Resource element) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+ Resource diagram = graph.getPossibleObject(element, L0.PartOf);
+ if (diagram == null) return null;
+ return graph.getPossibleObject(diagram, MOD.DiagramToComposite);
+ }
+
+ protected static Collection<Object> findElementObjects(ReadGraph g, Resource component) throws DatabaseException {
+ Collection<Object> result = findElementObjects(g, component, "");
+ ModelingResources MOD = ModelingResources.getInstance(g);
+ for (Resource element : g.getObjects(component, MOD.HasParentComponent_Inverse))
+ result.add(element);
+ return result;
+ }
+
+ public static Collection<Object> findElementObjects(ReadGraph g, Resource component, String rviFromComponent) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(g);
DiagramResource DIA = DiagramResource.getInstance(g);
ModelingResources MOD = ModelingResources.getInstance(g);
- final Collection<Object> selectedObjects = new ArrayList<Object>();
- for (Resource element : g.getObjects(module, MOD.ComponentToElement)) {
- if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {
- // Use external flag primarily if one exists in the correspondences
- selectedObjects.clear();
- selectedObjects.add(element);
- break;
- } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {
- selectedObjects.add(element);
- } else if (g.isInstanceOf(element, DIA.Connection)) {
- // Okay, we need to find a part of the connection
- ConnectionUtil cu = new ConnectionUtil(g);
- cu.gatherConnectionParts(element, selectedObjects);
- } else {
- selectedObjects.add(element);
+ final Collection<Object> selectedObjects = new ArrayList<>(4);
+ if (rviFromComponent.isEmpty()) {
+ // The selected objects are configuration objects
+ for (Resource element : g.getObjects(component, MOD.ComponentToElement)) {
+ if (g.isInstanceOf(element, DIA.Flag) && FlagUtil.isExternal(g, element)) {
+ // Use external flag primarily if one exists in the correspondences
+ selectedObjects.clear();
+ selectedObjects.add(element);
+ break;
+ } else if (g.isInstanceOf(element, DIA.RouteGraphConnection)) {
+ selectedObjects.add(element);
+ } else if (g.isInstanceOf(element, DIA.Connection)) {
+ // Okay, we need to find a part of the connection
+ ConnectionUtil cu = new ConnectionUtil(g);
+ cu.gatherConnectionParts(element, selectedObjects);
+ } else {
+ selectedObjects.add(element);
+ }
+ }
+ } else {
+ // The selected objects are generated components
+ for (Resource refElement : g.getObjects(component, MOD.HasParentComponent_Inverse)) {
+ Resource relation = g.getPossibleObject(refElement, MOD.HasReferenceRelation);
+ if (relation != null) {
+ String suffix = g.getPossibleRelatedValue(relation, L0.HasName, Bindings.STRING);
+ if (suffix != null) {
+ if (rviFromComponent.equals(suffix)) {
+ selectedObjects.add(refElement);
+ }
+ }
+ }
}
- }
- for(Resource element : g.getObjects(module, MOD.HasParentComponent_Inverse)) {
- selectedObjects.add(element);
}
return selectedObjects;
- }
+ }
}