-/*******************************************************************************\r
- * Copyright (c) 2013 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- * Semantum Oy - index based searching and graph manipulation (#4255)\r
- *******************************************************************************/\r
-package org.simantics.debug.ui.internal;\r
-\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.debug.ui.GraphDebugger;\r
-import org.simantics.debug.ui.ResourceSearch;\r
-import org.simantics.debug.ui.SearchResourceDialog;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.utils.Container;\r
-import org.simantics.utils.DataContainer;\r
-\r
-/**\r
- * @author Tuukka Lehtonen \r
- */\r
-public class DebugUtils {\r
-\r
- public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- String name = NameUtils.getSafeName(graph, r);\r
- Statement stm = graph.getPossibleStatement(r, l0.HasLabel);\r
- if (stm != null) {\r
- String label = NameUtils.getSafeLabel(graph, r);\r
- if (!label.isEmpty() && !stm.isAsserted(r))\r
- name += " (" + label + ")";\r
- }\r
- return name;\r
- }\r
-\r
- public static String getSafeURI(ReadGraph graph, Resource r) throws DatabaseException {\r
- String uri = graph.getPossibleURI(r);\r
- if (uri != null)\r
- return uri;\r
- String name = NameUtils.getSafeName(graph, r, true);\r
- return name;\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public static void addResource(Session s, GraphDebugger debugger) throws DatabaseException {\r
- Shell shell = debugger.getShell();\r
-\r
- SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Create New Resource");\r
- rld.setBlockOnOpen(true);\r
- rld.setResourceFilter(ResourceSearch.FILTER_TYPES);\r
-\r
- Resource subject_ = debugger.getDebuggerLocation();\r
- if (subject_ == null) {\r
- rld.setBlockOnOpen(true);\r
- rld.setMessage("Select Subject");\r
- rld.setInitialSelections(new Object[] {});\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- subject_ = ((Container<Resource>)rld.getResult()[0]).get();\r
- }\r
- final Resource subject = subject_;\r
-\r
- rld.setBlockOnOpen(true);\r
- rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);\r
- rld.setMessage("Select Predicate");\r
- rld.setInitialSelections(new Object[] {});\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- final Resource predicate = ((Container<Resource>)rld.getResult()[0]).get();\r
-\r
- rld.setMessage("Select Type of New Object Instance");\r
- rld.setResourceFilter(ResourceSearch.FILTER_TYPES);\r
- rld.setInitialSelections(new Object[] {});\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- final Resource type = ((Container<Resource>)rld.getResult()[0]).get();\r
- final DataContainer<Resource> result = new DataContainer<Resource>();\r
- s.syncRequest(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph g) throws DatabaseException {\r
- Resource r = g.newResource();\r
- g.claim(r, Layer0.getInstance(g).InstanceOf, type);\r
- g.claim(subject, predicate, r);\r
- result.set(r);\r
- }\r
- });\r
-\r
- if (result.get()!=null)\r
- debugger.changeLocation(result.get());\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public static void addStatement(Session s, GraphDebugger debugger) throws DatabaseException {\r
- Shell shell = debugger.getShell();\r
- SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Create New Statement");\r
-\r
- Resource subject_ = debugger.getDebuggerLocation();\r
- if (subject_ == null) {\r
- rld.setBlockOnOpen(true);\r
- rld.setMessage("Select Subject");\r
- rld.setInitialSelections(new Object[] {});\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- subject_ = ((Container<Resource>)rld.getResult()[0]).get();\r
- }\r
- final Resource subject = subject_;\r
-\r
- rld.setBlockOnOpen(true);\r
- rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);\r
- rld.setMessage("Select Predicate");\r
- rld.setInitialSelections(new Object[] {});\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- final Resource predicate = ((Container<Resource>)rld.getResult()[0]).get();\r
-\r
- rld.setResourceFilter(ResourceSearch.FILTER_ALL);\r
- rld.setMessage("Select Object");\r
- rld.setInitialSelections(new Object[] {});\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- final Resource object = ((Container<Resource>)rld.getResult()[0]).get();\r
-\r
- s.syncRequest(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph g) throws DatabaseException {\r
- g.claim(subject, predicate, object);\r
- }\r
- });\r
- }\r
-\r
- public static void find(Session s, GraphDebugger debugger) {\r
- Shell shell = debugger.getShell();\r
- SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Select Resource to View");\r
- rld.setBlockOnOpen(true);\r
- if (rld.open()!=org.eclipse.jface.window.Window.OK) return;\r
- if (rld.getResult()==null) return;\r
- for (Object o : rld.getResult()) {\r
- @SuppressWarnings("unchecked")\r
- Container<Resource> rc = (Container<Resource>) o;\r
- debugger.changeLocation(rc.get());\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2013 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - index based searching and graph manipulation (#4255)
+ *******************************************************************************/
+package org.simantics.debug.ui.internal;
+
+import java.util.Set;
+
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.databoard.util.URIStringUtils;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.Statement;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.debug.ui.GraphDebugger;
+import org.simantics.debug.ui.ResourceSearch;
+import org.simantics.debug.ui.SearchResourceDialog;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.Container;
+import org.simantics.utils.DataContainer;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class DebugUtils {
+
+ public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {
+ Layer0 l0 = Layer0.getInstance(graph);
+ String name = NameUtils.getSafeName(graph, r);
+ Statement stm = graph.getPossibleStatement(r, l0.HasLabel);
+ if (stm != null) {
+ String label = NameUtils.getSafeLabel(graph, r);
+ if (!label.isEmpty() && !stm.isAsserted(r))
+ name += " (" + label + ")"; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ return name;
+ }
+
+ public static String getSafeURI(ReadGraph graph, Resource r) throws DatabaseException {
+ String uri = graph.getPossibleURI(r);
+ if (uri != null)
+ return uri;
+ String name = NameUtils.getSafeName(graph, r, true);
+ return name;
+ }
+
+ public static String getPossibleRootRelativePath(ReadGraph graph, Resource r) throws DatabaseException {
+ Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));
+ String uri = graph.getPossibleURI(r);
+ if (indexRoot != null && uri != null) {
+ Layer0 L0 = Layer0.getInstance(graph);
+ Set<Resource> types = graph.getTypes(indexRoot);
+ if (!types.contains(L0.Ontology)) {
+ Resource indexRootParent = graph.getPossibleObject(indexRoot, L0.PartOf);
+ if (indexRootParent != null) {
+ String rootParentUri = graph.getPossibleURI(indexRootParent);
+ if (rootParentUri != null) {
+ return URIStringUtils.unescape( uri.substring(rootParentUri.length()+1) );
+ }
+ }
+ }
+ }
+ return uri;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void addResource(Session s, GraphDebugger debugger) throws DatabaseException {
+ Shell shell = debugger.getShell();
+
+ SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, Messages.DebugUtils_CreateNewResource);
+ rld.setBlockOnOpen(true);
+ rld.setResourceFilter(ResourceSearch.FILTER_TYPES);
+
+ Resource subject_ = debugger.getDebuggerLocation();
+ if (subject_ == null) {
+ rld.setBlockOnOpen(true);
+ rld.setMessage(Messages.DebugUtils_SelectSubject);
+ rld.setInitialSelections(new Object[] {});
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ subject_ = ((Container<Resource>)rld.getResult()[0]).get();
+ }
+ final Resource subject = subject_;
+
+ rld.setBlockOnOpen(true);
+ rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);
+ rld.setMessage(Messages.DebugUtils_SelectPredicate);
+ rld.setInitialSelections(new Object[] {});
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ final Resource predicate = ((Container<Resource>)rld.getResult()[0]).get();
+
+ rld.setMessage(Messages.DebugUtils_SelectTypeOfNewObjectInstance);
+ rld.setResourceFilter(ResourceSearch.FILTER_TYPES);
+ rld.setInitialSelections(new Object[] {});
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ final Resource type = ((Container<Resource>)rld.getResult()[0]).get();
+ final DataContainer<Resource> result = new DataContainer<Resource>();
+ s.syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Resource r = g.newResource();
+ g.claim(r, Layer0.getInstance(g).InstanceOf, type);
+ g.claim(subject, predicate, r);
+ result.set(r);
+ }
+ });
+
+ if (result.get()!=null)
+ debugger.changeLocation(result.get());
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void addStatement(Session s, GraphDebugger debugger) throws DatabaseException {
+ Shell shell = debugger.getShell();
+ SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, Messages.DebugUtils_CreateNewStatement);
+
+ Resource subject_ = debugger.getDebuggerLocation();
+ if (subject_ == null) {
+ rld.setBlockOnOpen(true);
+ rld.setMessage(Messages.DebugUtils_SelectSubject);
+ rld.setInitialSelections(new Object[] {});
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ subject_ = ((Container<Resource>)rld.getResult()[0]).get();
+ }
+ final Resource subject = subject_;
+
+ rld.setBlockOnOpen(true);
+ rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);
+ rld.setMessage(Messages.DebugUtils_SelectPredicate);
+ rld.setInitialSelections(new Object[] {});
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ final Resource predicate = ((Container<Resource>)rld.getResult()[0]).get();
+
+ rld.setResourceFilter(ResourceSearch.FILTER_ALL);
+ rld.setMessage(Messages.DebugUtils_SelectObject);
+ rld.setInitialSelections(new Object[] {});
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ final Resource object = ((Container<Resource>)rld.getResult()[0]).get();
+
+ s.syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ g.claim(subject, predicate, object);
+ }
+ });
+ }
+
+ public static void find(Session s, GraphDebugger debugger) {
+ Shell shell = debugger.getShell();
+ SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, Messages.DebugUtils_SelectResourceToView);
+ rld.setBlockOnOpen(true);
+ if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
+ if (rld.getResult()==null) return;
+ for (Object o : rld.getResult()) {
+ @SuppressWarnings("unchecked")
+ Container<Resource> rc = (Container<Resource>) o;
+ debugger.changeLocation(rc.get());
+ }
+ }
+
+}