]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/PreferenceHandler.java
7a82395982868b3332197603b6b16ce5fb7adc8c
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / 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.issues.ui.handler;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.ui.IWorkbenchSite;
26 import org.eclipse.ui.commands.IElementUpdater;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.eclipse.ui.menus.UIElement;
29 import org.simantics.Simantics;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.Session;
33 import org.simantics.db.WriteGraph;
34 import org.simantics.db.common.utils.TagUtil;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.layer0.SelectionHints;
37 import org.simantics.db.layer0.adapter.ActionFactory;
38 import org.simantics.db.layer0.adapter.ActionFactory2;
39 import org.simantics.db.layer0.variable.Variable;
40 import org.simantics.db.request.Read;
41 import org.simantics.issues.ontology.IssueResource;
42 import org.simantics.utils.datastructures.SelectionUtils;
43 import org.simantics.utils.ui.ErrorLogger;
44 import org.simantics.utils.ui.ISelectionUtils;
45
46 /**
47  * @author Tuukka Lehtonen
48  */
49 class PreferenceHandler extends AbstractHandler implements IElementUpdater, ActionFactory, ActionFactory2 {
50
51     protected final String virtualGraphId;
52     private final String   tagURI;
53     private final boolean  tag;
54
55     public PreferenceHandler() {
56         this("preferences", null, false);
57     }
58
59     public PreferenceHandler(String tagURI, boolean tag) {
60         this("preferences", tagURI, tag);
61     }
62
63     public PreferenceHandler(String virtualGraphId) {
64         this(virtualGraphId, null, false);
65     }
66
67     public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {
68         this.virtualGraphId = virtualGraphId;
69         this.tagURI = tagURI;
70         this.tag = tag;
71     }
72
73     public ArrayList<Resource> filteredSelection(ReadGraph graph, List<Resource> resources, List<Variable> vars) throws DatabaseException {
74         IssueResource ISSUE = IssueResource.getInstance(graph);
75         ArrayList<Resource> sel = new ArrayList<Resource>();
76         for (Variable v : vars) {
77             //System.out.println("var: " + v.getURI(graph));
78             Resource r = v.getPossibleRepresents(graph);
79             if (r != null && graph.isInstanceOf(r, ISSUE.Issue))
80                 sel.add(r);
81         }
82         for (Resource r : resources)
83             if (graph.isInstanceOf(r, ISSUE.Issue))
84                 sel.add(r);
85                 return sel;
86     }
87
88     @Override
89     public Object execute(ExecutionEvent event) throws ExecutionException {
90         IWorkbenchSite site = HandlerUtil.getActiveSite(event);
91         ISelectionProvider sp = site.getSelectionProvider();
92         ISelection selection = sp.getSelection();
93         final List<Variable> vars = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Variable.class);
94         final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
95         execute(vars, resources);
96         return null;
97     }
98
99     public void execute(final List<Variable> vars, List<Resource> resources) {
100         Session session = Simantics.peekSession();
101         if (session == null)
102             return;
103         new TagUtil(virtualGraphId, tagURI, tag) {
104             @Override
105             protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
106                 graph.markUndoPoint();
107                 ArrayList<Resource> sel = filteredSelection(graph, resources, vars);
108                 if (!process(graph))
109                     return;
110                 super.processSelection(graph, sel);
111             }
112         }.execute(session, resources);
113     }
114
115     /**
116      * @return <code>false</code> to perform no further actions after this
117      *         method completes.
118      */
119     protected boolean process(WriteGraph graph) throws DatabaseException {
120         return true;
121     }
122
123     @SuppressWarnings("rawtypes")
124     @Override
125     public void updateElement(UIElement element, Map parameters) {
126         Session session = Simantics.peekSession();
127         if (session == null)
128             return;
129         try {
130             boolean checked = session.syncRequest(new Read<Boolean>() {
131                 @Override
132                 public Boolean perform(ReadGraph graph) throws DatabaseException {
133                     return isChecked(graph);
134                 }
135             });
136             element.setChecked(checked);
137         } catch (DatabaseException e) {
138             ErrorLogger.defaultLogError(e);
139         }
140     }
141
142     protected boolean isChecked(ReadGraph graph) throws DatabaseException {
143         return false;
144     }
145
146         @Override
147         public Runnable create(Object target) {
148
149                 if(!(target instanceof Variable))
150                         return null;
151
152                 final Variable issue = (Variable)target;
153
154                 return new Runnable() {
155
156                         @Override
157                         public void run() {
158                                 execute(Collections.singletonList(issue), Collections.<Resource>emptyList());
159                         }
160
161                 };
162
163         }
164
165         @Override
166         public Runnable create(Collection<?> targets) {
167                 final List<Variable> issues = new ArrayList<Variable>(ISelectionUtils.filterSetSelection(targets, Variable.class));
168                 if (issues.isEmpty())
169                         return null;
170                 return new Runnable() {
171                         @Override
172                         public void run() {
173                                 execute(issues, Collections.<Resource>emptyList());
174                         }
175                 };
176         }
177
178 }