]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/MenuActions.java
Merge "Improvements to constraint-based issues"
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / MenuActions.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.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jface.action.ActionContributionItem;
19 import org.eclipse.jface.action.ContributionItem;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.action.IContributionItem;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.widgets.Menu;
28 import org.eclipse.swt.widgets.MenuItem;
29 import org.simantics.Simantics;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.common.request.WriteRequest;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.SelectionHints;
36 import org.simantics.issues.Severity;
37 import org.simantics.issues.common.IssueConstants;
38 import org.simantics.issues.common.IssueUtils;
39 import org.simantics.issues.ontology.IssueResource;
40 import org.simantics.issues.ui.internal.Activator;
41 import org.simantics.ui.contribution.DynamicMenuContribution;
42 import org.simantics.utils.ui.ErrorLogger;
43 import org.simantics.utils.ui.ISelectionUtils;
44
45 /**
46  * @author Tuukka Lehtonen
47  */
48 public class MenuActions extends DynamicMenuContribution {
49
50     @Override
51     protected Object[] getSelectedObjects() {
52         ISelection sel = getSelection();
53         List<Resource> resources = ISelectionUtils.getPossibleKeys(sel, SelectionHints.KEY_MAIN, Resource.class);
54         return resources.toArray();
55     }
56
57     List<Resource> toResources(Object[] array) {
58         Resource[] a = new Resource[array.length];
59         for (int i = 0; i < array.length; ++i)
60             a[i] = (Resource) array[i];
61         return Arrays.asList(a);
62     }
63
64     @Override
65     protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
66         if (selection.length == 0)
67             return new IContributionItem[0];
68
69         List<Resource> input = toResources(selection);
70
71         @SuppressWarnings("unused")
72         boolean issues = false;
73         @SuppressWarnings("unused")
74         boolean userIssues = false;
75
76         IssueResource ISSUE = IssueResource.getInstance(graph);
77         int issueCount = 0;
78         int hiddenCount = 0; 
79         int resolvedCount = 0;
80         int userIssueCount = 0;
81         for (Resource r : input) {
82             boolean isIssue = graph.isInstanceOf(r, ISSUE.Issue);
83             issues |= isIssue;
84             if (isIssue) {
85                 ++issueCount;
86                 if (graph.hasStatement(r, ISSUE.Hidden))
87                     ++hiddenCount;
88                 if (graph.hasStatement(r, ISSUE.Resolved))
89                     ++resolvedCount;
90             }
91
92             boolean userIssue = graph.hasStatement(r, ISSUE.UserIssue);
93             userIssues |= userIssue;
94             if (userIssue) {
95                 ++userIssueCount;
96             }
97         }
98         boolean allHidden = hiddenCount == selection.length;
99         boolean allResolved = resolvedCount == selection.length;
100         boolean allUserIssues = userIssueCount == selection.length;
101         boolean allIssueSelection = issueCount == selection.length;
102
103         List<IContributionItem> actions = new ArrayList<IContributionItem>(2); 
104         if (!allHidden && allIssueSelection)
105             actions.add(new ActionContributionItem(hideAction(input)));
106         if (hiddenCount > 0 || allHidden && allIssueSelection)
107             actions.add(new ActionContributionItem(unhideAction(input)));
108         if (!allResolved && allIssueSelection)
109             actions.add(new ActionContributionItem(resolveAction(input)));
110         if (resolvedCount > 0 || allResolved && allIssueSelection)
111             actions.add(new ActionContributionItem(unresolveAction(input)));
112         if (allUserIssues)
113             actions.add(setSeverityAction(input));
114
115         return actions.toArray(new IContributionItem[actions.size()]);
116     }
117
118     private IAction unhideAction(List<Resource> input) {
119         return tagAction(Messages.MenuActions_Unhide, Activator.UNHIDE_ICON, IssueResource.URIs.Hidden, false, input);
120     }
121
122     private IAction hideAction(List<Resource> input) {
123         return tagAction(Messages.MenuActions_Hide, Activator.HIDE_ICON, IssueResource.URIs.Hidden, true, input);
124     }
125
126     private IAction resolveAction(List<Resource> input) {
127         return tagAction(Messages.MenuActions_MarkResolved, Activator.RESOLVE_ICON, IssueResource.URIs.Resolved, true, input);
128     }
129
130     private IAction unresolveAction(List<Resource> input) {
131         return tagAction(Messages.MenuActions_MarkUnresolved, Activator.UNRESOLVE_ICON, IssueResource.URIs.Resolved, false, input);
132     }
133
134     private IAction tagAction(String label, ImageDescriptor image, String tagURI, boolean tag, List<Resource> input) {
135         return new TagAction(label, image, IssueConstants.ISSUE_VG, tagURI, tag, input);
136         //return new TagAction(label, image, null, tagURI, tag, input);
137     }
138
139     private IContributionItem setSeverityAction(final List<Resource> userIssues) {
140         return new ContributionItem() {
141             @Override
142             public void fill(Menu menu, int index) {
143                 MenuItem setSeverityItem = new MenuItem(menu, SWT.CASCADE);
144                 setSeverityItem.setText(Messages.MenuActions_SetSeverity);
145                 Menu setSeverity = new Menu(menu);
146                 setSeverityItem.setMenu(setSeverity);
147                 for (final Severity sev : Severity.values()) {
148                     if (sev.equals(Severity.UNKNOWN))
149                         continue;
150
151                     MenuItem item = new MenuItem(setSeverity, SWT.PUSH);
152                     item.setText(sev.toString().toLowerCase());
153                     item.setImage(Activator.getDefault().getImageRegistry().get(sev.toString() + "-full")); //$NON-NLS-1$
154                     item.addSelectionListener(new SelectionAdapter() {
155                         @Override
156                         public void widgetSelected(SelectionEvent e) {
157                             setSeverity(userIssues, sev);
158                         }
159                     });
160                 }
161             }
162         };
163     }
164
165     private void setSeverity(final List<Resource> userIssues, final Severity severity) {
166         Simantics.getSession().asyncRequest(new WriteRequest() {
167             @Override
168             public void perform(WriteGraph graph) throws DatabaseException {
169                 IssueResource ISSUE = IssueResource.getInstance(graph);
170                 Resource sev = IssueUtils.toSeverityResource(ISSUE, severity);
171                 if (sev != null) {
172                     for (Resource issue : userIssues) {
173                         if (!graph.hasStatement(issue, ISSUE.Issue_HasSeverity, sev)) {
174                             graph.deny(issue, ISSUE.Issue_HasSeverity);
175                             graph.claim(issue, ISSUE.Issue_HasSeverity, null, sev);
176                         }
177                     }
178                 } else {
179                     for (Resource issue : userIssues)
180                         graph.deny(issue, ISSUE.Issue_HasSeverity);
181                 }
182             }
183         }, e -> {
184             if (e != null)
185                 ErrorLogger.defaultLogError(e);
186         });
187     }
188
189 }