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