]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/RestoreDefaultValueHandler.java
a439527ca4b73bc364c10304946236d7d6b1e5d7
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / RestoreDefaultValueHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.property;
13
14 import java.util.List;
15
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;
38
39
40 /**
41  * @author Tuukka Lehtonen
42  */
43 public class RestoreDefaultValueHandler extends AbstractHandler {
44
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();
50         return null;
51     }
52
53     @Override
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);
57
58         ISessionContext sc = SimanticsUI.getSessionContext();
59         if (sc == null)
60             return null;
61
62         IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
63         IPage page = getCurrentPage(part);
64         if (page == null || !(page instanceof IPropertyPage))
65             return null;
66         IPropertyPage propPage = (IPropertyPage) page;
67         ISelection sel = propPage.getSelection();
68         if (sel == null)
69             return null;
70
71         final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);
72         if (!vars.isEmpty()) {
73             resetVariableProperties(sc, vars);
74         } else {
75             final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);
76             if (!props.isEmpty())
77                 resetLegacyProperties(sc, props);
78         }
79
80         return null;
81     }
82
83
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() {
87             @Override
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)
93 //                        continue;
94 //                    Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
95 //                    if (predicateResource == null)
96 //                        continue;
97                         Resource predicateResource = v.getPossiblePredicateResource(graph);
98                         if (predicateResource == null)
99                                 continue;
100                     Variable parent = v.getParent(graph);
101                     if (parent == null)
102                         continue;
103                     Resource parentResource = parent.getPossibleRepresents(graph);
104                     if (parentResource == null)
105                         continue;
106
107 //                    System.out.println(parent.getURI(graph));
108 //                    System.out.println(predicate.getURI(graph));
109 //                    System.out.println(v.getURI(graph));
110
111                     graph.denyValue(parentResource, predicateResource);
112                     cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));
113                 }
114                 graph.addMetadata(cm);
115             }
116         }, new Callback<DatabaseException>() {
117             @Override
118             public void run(DatabaseException parameter) {
119                 if (parameter != null)
120                     ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);
121             }
122         });
123     }
124
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())) {
130                 changes = true;
131                 break;
132             }
133         }
134         if (changes) {
135             sc.getSession().asyncRequest(new WriteRequest() {
136                 @Override
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]);
142                         }
143                     }
144                 }
145             }, new Callback<DatabaseException>() {
146                 @Override
147                 public void run(DatabaseException parameter) {
148                     if (parameter != null)
149                         ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", parameter);
150                 }
151             });
152         }
153         return changes;
154     }
155
156 }