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