--- /dev/null
+/*******************************************************************************\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