]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/handler/PreferenceHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / PreferenceHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.event.view.handler;\r
13 \r
14 import java.util.List;\r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.core.commands.AbstractHandler;\r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.jface.viewers.ISelection;\r
21 import org.eclipse.ui.commands.IElementUpdater;\r
22 import org.eclipse.ui.handlers.HandlerUtil;\r
23 import org.eclipse.ui.menus.UIElement;\r
24 import org.simantics.Simantics;\r
25 import org.simantics.db.ReadGraph;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.Session;\r
28 import org.simantics.db.WriteGraph;\r
29 import org.simantics.db.common.utils.TagUtil;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.layer0.SelectionHints;\r
32 import org.simantics.db.request.Read;\r
33 import org.simantics.utils.ui.ErrorLogger;\r
34 import org.simantics.utils.ui.ISelectionUtils;\r
35 \r
36 /**\r
37  * @author Tuukka Lehtonen\r
38  */\r
39 class PreferenceHandler extends AbstractHandler implements IElementUpdater {\r
40 \r
41     protected final String virtualGraphId;\r
42     private final String   tagURI;\r
43     private final boolean  tag;\r
44 \r
45     public PreferenceHandler() {\r
46         this("preferences", null, false);\r
47     }\r
48 \r
49     public PreferenceHandler(String tagURI, boolean tag) {\r
50         this("preferences", tagURI, tag);\r
51     }\r
52 \r
53     public PreferenceHandler(String virtualGraphId) {\r
54         this(virtualGraphId, null, false);\r
55     }\r
56 \r
57     public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {\r
58         this.virtualGraphId = virtualGraphId;\r
59         this.tagURI = tagURI;\r
60         this.tag = tag;\r
61     }\r
62 \r
63     @Override\r
64     public Object execute(ExecutionEvent event) throws ExecutionException {\r
65         Session session = Simantics.peekSession();\r
66         if (session == null)\r
67             return null;\r
68         ISelection selection = HandlerUtil.getCurrentSelection(event);\r
69         final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);\r
70         new TagUtil(virtualGraphId, tagURI, tag) {\r
71             @Override\r
72             protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {\r
73                 if (!process(graph))\r
74                     return;\r
75                 super.processSelection(graph, resources);\r
76             }\r
77         }.execute(session, resources);\r
78         return null;\r
79     }\r
80 \r
81     /**\r
82      * @return <code>false</code> to perform no further actions after this\r
83      *         method completes.\r
84      */\r
85     protected boolean process(WriteGraph graph) throws DatabaseException {\r
86         return true;\r
87     }\r
88 \r
89     @SuppressWarnings("rawtypes")\r
90     @Override\r
91     public void updateElement(UIElement element, Map parameters) {\r
92         Session session = Simantics.peekSession();\r
93         if (session == null)\r
94             return;\r
95         try {\r
96             boolean checked = session.syncRequest(new Read<Boolean>() {\r
97                 @Override\r
98                 public Boolean perform(ReadGraph graph) throws DatabaseException {\r
99                     return isChecked(graph);\r
100                 }\r
101             });\r
102             element.setChecked(checked);\r
103         } catch (DatabaseException e) {\r
104             ErrorLogger.defaultLogError(e);\r
105         }\r
106     }\r
107 \r
108     protected boolean isChecked(ReadGraph graph) throws DatabaseException {\r
109         return false;\r
110     }\r
111 \r
112 }\r