1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.property;
14 import java.util.List;
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22 import org.eclipse.ui.part.IPage;
23 import org.eclipse.ui.part.PageBookView;
24 import org.simantics.browsing.ui.common.ErrorLogger;
25 import org.simantics.browsing.ui.common.property.IProperty;
26 import org.simantics.db.Resource;
27 import org.simantics.db.WriteGraph;
28 import org.simantics.db.common.CommentMetadata;
29 import org.simantics.db.common.request.WriteRequest;
30 import org.simantics.db.common.utils.NameUtils;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.variable.Variable;
33 import org.simantics.db.management.ISessionContext;
34 import org.simantics.ui.SimanticsUI;
35 import org.simantics.ui.workbench.IPropertyPage;
36 import org.simantics.utils.ui.ISelectionUtils;
40 * @author Tuukka Lehtonen
42 public class RestoreDefaultValueHandler extends AbstractHandler {
44 IPage getCurrentPage(IWorkbenchPart part) {
45 if (part instanceof PageBookView)
46 return ((PageBookView) part).getCurrentPage();
47 if (part instanceof org.simantics.browsing.ui.platform.PageBookView)
48 return ((org.simantics.browsing.ui.platform.PageBookView) part).getCurrentPage();
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54 // Cannot use this, since the property view does not contribute a selection back to the workbench.
55 //HandlerUtil.getCurrentSelection(event);
57 ISessionContext sc = SimanticsUI.getSessionContext();
61 IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
62 IPage page = getCurrentPage(part);
63 if (page == null || !(page instanceof IPropertyPage))
65 IPropertyPage propPage = (IPropertyPage) page;
66 ISelection sel = propPage.getSelection();
70 final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);
71 if (!vars.isEmpty()) {
72 resetVariableProperties(sc, vars);
74 final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);
76 resetLegacyProperties(sc, props);
83 private void resetVariableProperties(ISessionContext sc, final List<Variable> vars) {
84 // TODO: how to do this generically using only variables? How to remove a property value to let it be asserted again?
85 sc.getSession().asyncRequest(new WriteRequest() {
87 public void perform(WriteGraph graph) throws DatabaseException {
88 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
89 for (Variable v : vars) {
90 // Variable predicate = v.getPossiblePropertyValue(graph, Variables.PREDICATE);
91 // if (predicate == null)
93 // Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
94 // if (predicateResource == null)
96 Resource predicateResource = v.getPossiblePredicateResource(graph);
97 if (predicateResource == null)
99 Variable parent = v.getParent(graph);
102 Resource parentResource = parent.getPossibleRepresents(graph);
103 if (parentResource == null)
106 // System.out.println(parent.getURI(graph));
107 // System.out.println(predicate.getURI(graph));
108 // System.out.println(v.getURI(graph));
110 graph.denyValue(parentResource, predicateResource);
111 cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));
113 graph.addMetadata(cm);
117 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", e);
121 private boolean resetLegacyProperties(ISessionContext sc, final List<IProperty> props) {
122 // Dry run to see if there's anything to remove before writing.
123 boolean changes = false;
124 for (IProperty cp : props) {
125 if (IProperty.DIRECT.contains(cp.getType())) {
131 sc.getSession().asyncRequest(new WriteRequest() {
133 public void perform(WriteGraph graph) throws DatabaseException {
134 for (IProperty cp : props) {
135 if (IProperty.DIRECT.contains(cp.getType())) {
136 Resource[] data = cp.getData(Resource[].class);
137 graph.denyStatement(data[0], data[1], data[2]);
143 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", e);