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