1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.property;
\r
14 import java.util.List;
\r
16 import org.eclipse.core.commands.AbstractHandler;
\r
17 import org.eclipse.core.commands.ExecutionEvent;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.jface.viewers.ISelection;
\r
20 import org.eclipse.ui.IWorkbenchPart;
\r
21 import org.eclipse.ui.handlers.HandlerUtil;
\r
22 import org.eclipse.ui.part.IPage;
\r
23 import org.eclipse.ui.part.PageBookView;
\r
24 import org.simantics.browsing.ui.common.ErrorLogger;
\r
25 import org.simantics.browsing.ui.common.property.IProperty;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.db.WriteGraph;
\r
28 import org.simantics.db.common.CommentMetadata;
\r
29 import org.simantics.db.common.request.WriteRequest;
\r
30 import org.simantics.db.common.utils.NameUtils;
\r
31 import org.simantics.db.exception.DatabaseException;
\r
32 import org.simantics.db.layer0.variable.Variable;
\r
33 import org.simantics.db.management.ISessionContext;
\r
34 import org.simantics.ui.SimanticsUI;
\r
35 import org.simantics.ui.workbench.IPropertyPage;
\r
36 import org.simantics.utils.datastructures.Callback;
\r
37 import org.simantics.utils.ui.ISelectionUtils;
\r
41 * @author Tuukka Lehtonen
\r
43 public class RestoreDefaultValueHandler extends AbstractHandler {
\r
45 IPage getCurrentPage(IWorkbenchPart part) {
\r
46 if (part instanceof PageBookView)
\r
47 return ((PageBookView) part).getCurrentPage();
\r
48 if (part instanceof org.simantics.browsing.ui.platform.PageBookView)
\r
49 return ((org.simantics.browsing.ui.platform.PageBookView) part).getCurrentPage();
\r
54 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
55 // Cannot use this, since the property view does not contribute a selection back to the workbench.
\r
56 //HandlerUtil.getCurrentSelection(event);
\r
58 ISessionContext sc = SimanticsUI.getSessionContext();
\r
62 IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
\r
63 IPage page = getCurrentPage(part);
\r
64 if (page == null || !(page instanceof IPropertyPage))
\r
66 IPropertyPage propPage = (IPropertyPage) page;
\r
67 ISelection sel = propPage.getSelection();
\r
71 final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);
\r
72 if (!vars.isEmpty()) {
\r
73 resetVariableProperties(sc, vars);
\r
75 final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);
\r
76 if (!props.isEmpty())
\r
77 resetLegacyProperties(sc, props);
\r
84 private void resetVariableProperties(ISessionContext sc, final List<Variable> vars) {
\r
85 // TODO: how to do this generically using only variables? How to remove a property value to let it be asserted again?
\r
86 sc.getSession().asyncRequest(new WriteRequest() {
\r
88 public void perform(WriteGraph graph) throws DatabaseException {
\r
89 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
90 for (Variable v : vars) {
\r
91 // Variable predicate = v.getPossiblePropertyValue(graph, Variables.PREDICATE);
\r
92 // if (predicate == null)
\r
94 // Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
\r
95 // if (predicateResource == null)
\r
97 Resource predicateResource = v.getPossiblePredicateResource(graph);
\r
98 if (predicateResource == null)
\r
100 Variable parent = v.getParent(graph);
\r
101 if (parent == null)
\r
103 Resource parentResource = parent.getPossibleRepresents(graph);
\r
104 if (parentResource == null)
\r
107 // System.out.println(parent.getURI(graph));
\r
108 // System.out.println(predicate.getURI(graph));
\r
109 // System.out.println(v.getURI(graph));
\r
111 graph.denyValue(parentResource, predicateResource);
\r
112 cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));
\r
114 graph.addMetadata(cm);
\r
116 }, new Callback<DatabaseException>() {
\r
118 public void run(DatabaseException parameter) {
\r
119 if (parameter != null)
\r
120 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);
\r
125 private boolean resetLegacyProperties(ISessionContext sc, final List<IProperty> props) {
\r
126 // Dry run to see if there's anything to remove before writing.
\r
127 boolean changes = false;
\r
128 for (IProperty cp : props) {
\r
129 if (IProperty.DIRECT.contains(cp.getType())) {
\r
135 sc.getSession().asyncRequest(new WriteRequest() {
\r
137 public void perform(WriteGraph graph) throws DatabaseException {
\r
138 for (IProperty cp : props) {
\r
139 if (IProperty.DIRECT.contains(cp.getType())) {
\r
140 Resource[] data = cp.getData(Resource[].class);
\r
141 graph.denyStatement(data[0], data[1], data[2]);
\r
145 }, new Callback<DatabaseException>() {
\r
147 public void run(DatabaseException parameter) {
\r
148 if (parameter != null)
\r
149 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);
\r