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.datastructures.Callback;
37 import org.simantics.utils.ui.ISelectionUtils;
41 * @author Tuukka Lehtonen
43 public class RestoreDefaultValueHandler extends AbstractHandler {
45 IPage getCurrentPage(IWorkbenchPart part) {
46 if (part instanceof PageBookView)
47 return ((PageBookView) part).getCurrentPage();
48 if (part instanceof org.simantics.browsing.ui.platform.PageBookView)
49 return ((org.simantics.browsing.ui.platform.PageBookView) part).getCurrentPage();
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55 // Cannot use this, since the property view does not contribute a selection back to the workbench.
56 //HandlerUtil.getCurrentSelection(event);
58 ISessionContext sc = SimanticsUI.getSessionContext();
62 IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
63 IPage page = getCurrentPage(part);
64 if (page == null || !(page instanceof IPropertyPage))
66 IPropertyPage propPage = (IPropertyPage) page;
67 ISelection sel = propPage.getSelection();
71 final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);
72 if (!vars.isEmpty()) {
73 resetVariableProperties(sc, vars);
75 final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);
77 resetLegacyProperties(sc, props);
84 private void resetVariableProperties(ISessionContext sc, final List<Variable> vars) {
85 // TODO: how to do this generically using only variables? How to remove a property value to let it be asserted again?
86 sc.getSession().asyncRequest(new WriteRequest() {
88 public void perform(WriteGraph graph) throws DatabaseException {
89 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
90 for (Variable v : vars) {
91 // Variable predicate = v.getPossiblePropertyValue(graph, Variables.PREDICATE);
92 // if (predicate == null)
94 // Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
95 // if (predicateResource == null)
97 Resource predicateResource = v.getPossiblePredicateResource(graph);
98 if (predicateResource == null)
100 Variable parent = v.getParent(graph);
103 Resource parentResource = parent.getPossibleRepresents(graph);
104 if (parentResource == null)
107 // System.out.println(parent.getURI(graph));
108 // System.out.println(predicate.getURI(graph));
109 // System.out.println(v.getURI(graph));
111 graph.denyValue(parentResource, predicateResource);
112 cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));
114 graph.addMetadata(cm);
116 }, new Callback<DatabaseException>() {
118 public void run(DatabaseException parameter) {
119 if (parameter != null)
120 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);
125 private boolean resetLegacyProperties(ISessionContext sc, final List<IProperty> props) {
126 // Dry run to see if there's anything to remove before writing.
127 boolean changes = false;
128 for (IProperty cp : props) {
129 if (IProperty.DIRECT.contains(cp.getType())) {
135 sc.getSession().asyncRequest(new WriteRequest() {
137 public void perform(WriteGraph graph) throws DatabaseException {
138 for (IProperty cp : props) {
139 if (IProperty.DIRECT.contains(cp.getType())) {
140 Resource[] data = cp.getData(Resource[].class);
141 graph.denyStatement(data[0], data[1], data[2]);
145 }, new Callback<DatabaseException>() {
147 public void run(DatabaseException parameter) {
148 if (parameter != null)
149 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);