]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/RestoreDefaultValueHandler.java
d19e6dbfbc1415c8afe9142e6384938937dee65a
[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.ui.ISelectionUtils;
37
38
39 /**
40  * @author Tuukka Lehtonen
41  */
42 public class RestoreDefaultValueHandler extends AbstractHandler {
43
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();
49         return null;
50     }
51
52     @Override
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);
56
57         ISessionContext sc = SimanticsUI.getSessionContext();
58         if (sc == null)
59             return null;
60
61         IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
62         IPage page = getCurrentPage(part);
63         if (page == null || !(page instanceof IPropertyPage))
64             return null;
65         IPropertyPage propPage = (IPropertyPage) page;
66         ISelection sel = propPage.getSelection();
67         if (sel == null)
68             return null;
69
70         final List<Variable> vars = ISelectionUtils.filterSelection(sel, Variable.class);
71         if (!vars.isEmpty()) {
72             resetVariableProperties(sc, vars);
73         } else {
74             final List<IProperty> props = ISelectionUtils.filterSelection(sel, IProperty.class);
75             if (!props.isEmpty())
76                 resetLegacyProperties(sc, props);
77         }
78
79         return null;
80     }
81
82
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() {
86             @Override
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)
92 //                        continue;
93 //                    Resource predicateResource = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
94 //                    if (predicateResource == null)
95 //                        continue;
96                         Resource predicateResource = v.getPossiblePredicateResource(graph);
97                         if (predicateResource == null)
98                                 continue;
99                     Variable parent = v.getParent(graph);
100                     if (parent == null)
101                         continue;
102                     Resource parentResource = parent.getPossibleRepresents(graph);
103                     if (parentResource == null)
104                         continue;
105
106 //                    System.out.println(parent.getURI(graph));
107 //                    System.out.println(predicate.getURI(graph));
108 //                    System.out.println(v.getURI(graph));
109
110                     graph.denyValue(parentResource, predicateResource);
111                     cm.add("Restored default value for property " + NameUtils.getSafeName(graph, predicateResource));
112                 }
113                 graph.addMetadata(cm);
114             }
115         }, e -> {
116             if (e != null)
117                 ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", e);
118         });
119     }
120
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())) {
126                 changes = true;
127                 break;
128             }
129         }
130         if (changes) {
131             sc.getSession().asyncRequest(new WriteRequest() {
132                 @Override
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]);
138                         }
139                     }
140                 }
141             }, e -> {
142                 if (e != null)
143                     ErrorLogger.defaultLogError("Failed to restore default property values, see exception for details.", e);
144             });
145         }
146         return changes;
147     }
148
149 }