]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/RestoreDefaultValueHandler.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / RestoreDefaultValueHandler.java
index 36fd177fcf4688f8bf0e63a31467417cec1dceaa..179173ede933e5b35b89090fc1e999437c42831c 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 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
- *******************************************************************************/\r
-package org.simantics.modeling.ui.property;\r
-\r
-import java.util.List;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.eclipse.ui.part.IPage;\r
-import org.eclipse.ui.part.PageBookView;\r
-import org.simantics.browsing.ui.common.ErrorLogger;\r
-import org.simantics.browsing.ui.common.property.IProperty;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.CommentMetadata;\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.db.layer0.variable.Variable;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.workbench.IPropertyPage;\r
-import org.simantics.utils.datastructures.Callback;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class RestoreDefaultValueHandler extends AbstractHandler {\r
-\r
-    IPage getCurrentPage(IWorkbenchPart part) {\r
-        if (part instanceof PageBookView)\r
-            return ((PageBookView) part).getCurrentPage();\r
-        if (part instanceof org.simantics.browsing.ui.platform.PageBookView)\r
-            return ((org.simantics.browsing.ui.platform.PageBookView) part).getCurrentPage();\r
-        return null;\r
-    }\r
-\r
-    @Override\r
-    public Object execute(ExecutionEvent event) throws ExecutionException {\r
-        // Cannot use this, since the property view does not contribute a selection back to the workbench.\r
-        //HandlerUtil.getCurrentSelection(event);\r
-\r
-        ISessionContext sc = SimanticsUI.getSessionContext();\r
-        if (sc == null)\r
-            return null;\r
-\r
-        IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);\r
-        IPage page = getCurrentPage(part);\r
-        if (page == null || !(page instanceof IPropertyPage))\r
-            return null;\r
-        IPropertyPage propPage = (IPropertyPage) page;\r
-        ISelection sel = propPage.getSelection();\r
-        if (sel == null)\r
-            return null;\r
-\r
-        final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);\r
-        if (!vars.isEmpty()) {\r
-            resetVariableProperties(sc, vars);\r
-        } else {\r
-            final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);\r
-            if (!props.isEmpty())\r
-                resetLegacyProperties(sc, props);\r
-        }\r
-\r
-        return null;\r
-    }\r
-\r
-\r
-    private void resetVariableProperties(ISessionContext sc, final List<Variable> vars) {\r
-        // TODO: how to do this generically using only variables? How to remove a property value to let it be asserted again?\r
-        sc.getSession().asyncRequest(new WriteRequest() {\r
-            @Override\r
-            public void perform(WriteGraph graph) throws DatabaseException {\r
-                CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
-                for (Variable v : vars) {\r
-//                    Variable predicate = v.getPossiblePropertyValue(graph, Variables.PREDICATE);\r
-//                    if (predicate == null)\r
-//                        continue;\r
-//                    Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);\r
-//                    if (predicateResource == null)\r
-//                        continue;\r
-                       Resource predicateResource = v.getPossiblePredicateResource(graph);\r
-                       if (predicateResource == null)\r
-                               continue;\r
-                    Variable parent = v.getParent(graph);\r
-                    if (parent == null)\r
-                        continue;\r
-                    Resource parentResource = parent.getPossibleRepresents(graph);\r
-                    if (parentResource == null)\r
-                        continue;\r
-\r
-//                    System.out.println(parent.getURI(graph));\r
-//                    System.out.println(predicate.getURI(graph));\r
-//                    System.out.println(v.getURI(graph));\r
-\r
-                    graph.denyValue(parentResource, predicateResource);\r
-                    cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));\r
-                }\r
-                graph.addMetadata(cm);\r
-            }\r
-        }, new Callback<DatabaseException>() {\r
-            @Override\r
-            public void run(DatabaseException parameter) {\r
-                if (parameter != null)\r
-                    ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);\r
-            }\r
-        });\r
-    }\r
-\r
-    private boolean resetLegacyProperties(ISessionContext sc, final List<IProperty> props) {\r
-        // Dry run to see if there's anything to remove before writing.\r
-        boolean changes = false;\r
-        for (IProperty cp : props) {\r
-            if (IProperty.DIRECT.contains(cp.getType())) {\r
-                changes = true;\r
-                break;\r
-            }\r
-        }\r
-        if (changes) {\r
-            sc.getSession().asyncRequest(new WriteRequest() {\r
-                @Override\r
-                public void perform(WriteGraph graph) throws DatabaseException {\r
-                    for (IProperty cp : props) {\r
-                        if (IProperty.DIRECT.contains(cp.getType())) {\r
-                            Resource[] data = cp.getData(Resource[].class);\r
-                            graph.denyStatement(data[0], data[1], data[2]);\r
-                        }\r
-                    }\r
-                }\r
-            }, new Callback<DatabaseException>() {\r
-                @Override\r
-                public void run(DatabaseException parameter) {\r
-                    if (parameter != null)\r
-                        ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);\r
-                }\r
-            });\r
-        }\r
-        return changes;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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
+ *******************************************************************************/
+package org.simantics.modeling.ui.property;
+
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.ui.part.IPage;
+import org.eclipse.ui.part.PageBookView;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.common.ErrorLogger;
+import org.simantics.browsing.ui.common.property.IProperty;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.ui.workbench.IPropertyPage;
+import org.simantics.utils.ui.ISelectionUtils;
+
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class RestoreDefaultValueHandler extends AbstractHandler {
+
+    IPage getCurrentPage(IWorkbenchPart part) {
+        if (part instanceof PageBookView)
+            return ((PageBookView) part).getCurrentPage();
+        if (part instanceof org.simantics.browsing.ui.platform.PageBookView)
+            return ((org.simantics.browsing.ui.platform.PageBookView) part).getCurrentPage();
+        return null;
+    }
+
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        // Cannot use this, since the property view does not contribute a selection back to the workbench.
+        //HandlerUtil.getCurrentSelection(event);
+
+        ISessionContext sc = Simantics.getSessionContext();
+        if (sc == null)
+            return null;
+
+        IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
+        IPage page = getCurrentPage(part);
+        if (page == null || !(page instanceof IPropertyPage))
+            return null;
+        IPropertyPage propPage = (IPropertyPage) page;
+        ISelection sel = propPage.getSelection();
+        if (sel == null)
+            return null;
+
+        final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);
+        if (!vars.isEmpty()) {
+            resetVariableProperties(sc, vars);
+        } else {
+            final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);
+            if (!props.isEmpty())
+                resetLegacyProperties(sc, props);
+        }
+
+        return null;
+    }
+
+
+    private void resetVariableProperties(ISessionContext sc, final List<Variable> vars) {
+        // TODO: how to do this generically using only variables? How to remove a property value to let it be asserted again?
+        sc.getSession().asyncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+                for (Variable v : vars) {
+//                    Variable predicate = v.getPossiblePropertyValue(graph, Variables.PREDICATE);
+//                    if (predicate == null)
+//                        continue;
+//                    Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
+//                    if (predicateResource == null)
+//                        continue;
+                       Resource predicateResource = v.getPossiblePredicateResource(graph);
+                       if (predicateResource == null)
+                               continue;
+                    Variable parent = v.getParent(graph);
+                    if (parent == null)
+                        continue;
+                    Resource parentResource = parent.getPossibleRepresents(graph);
+                    if (parentResource == null)
+                        continue;
+
+//                    System.out.println(parent.getURI(graph));
+//                    System.out.println(predicate.getURI(graph));
+//                    System.out.println(v.getURI(graph));
+
+                    graph.denyValue(parentResource, predicateResource);
+                    cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));
+                }
+                graph.addMetadata(cm);
+            }
+        }, e -> {
+            if (e != null)
+                ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", e);
+        });
+    }
+
+    private boolean resetLegacyProperties(ISessionContext sc, final List<IProperty> props) {
+        // Dry run to see if there's anything to remove before writing.
+        boolean changes = false;
+        for (IProperty cp : props) {
+            if (IProperty.DIRECT.contains(cp.getType())) {
+                changes = true;
+                break;
+            }
+        }
+        if (changes) {
+            sc.getSession().asyncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    for (IProperty cp : props) {
+                        if (IProperty.DIRECT.contains(cp.getType())) {
+                            Resource[] data = cp.getData(Resource[].class);
+                            graph.denyStatement(data[0], data[1], data[2]);
+                        }
+                    }
+                }
+            }, e -> {
+                if (e != null)
+                    ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", e);
+            });
+        }
+        return changes;
+    }
+
+}