1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.issues.ui.handler;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
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;
46 * @author Tuukka Lehtonen
48 public class MenuActions extends DynamicMenuContribution {
51 protected Object[] getSelectedObjects() {
52 ISelection sel = getSelection();
53 List<Resource> resources = ISelectionUtils.getPossibleKeys(sel, SelectionHints.KEY_MAIN, Resource.class);
54 return resources.toArray();
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);
65 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
66 if (selection.length == 0)
67 return new IContributionItem[0];
69 List<Resource> input = toResources(selection);
71 @SuppressWarnings("unused")
72 boolean issues = false;
73 @SuppressWarnings("unused")
74 boolean userIssues = false;
76 IssueResource ISSUE = IssueResource.getInstance(graph);
79 int resolvedCount = 0;
80 int userIssueCount = 0;
81 for (Resource r : input) {
82 boolean isIssue = graph.isInstanceOf(r, ISSUE.Issue);
86 if (graph.hasStatement(r, ISSUE.Hidden))
88 if (graph.hasStatement(r, ISSUE.Resolved))
92 boolean userIssue = graph.hasStatement(r, ISSUE.UserIssue);
93 userIssues |= userIssue;
98 boolean allHidden = hiddenCount == selection.length;
99 boolean allResolved = resolvedCount == selection.length;
100 boolean allUserIssues = userIssueCount == selection.length;
101 boolean allIssueSelection = issueCount == selection.length;
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)));
113 actions.add(setSeverityAction(input));
115 return actions.toArray(new IContributionItem[actions.size()]);
118 private IAction unhideAction(List<Resource> input) {
119 return tagAction(Messages.MenuActions_Unhide, Activator.UNHIDE_ICON, IssueResource.URIs.Hidden, false, input);
122 private IAction hideAction(List<Resource> input) {
123 return tagAction(Messages.MenuActions_Hide, Activator.HIDE_ICON, IssueResource.URIs.Hidden, true, input);
126 private IAction resolveAction(List<Resource> input) {
127 return tagAction(Messages.MenuActions_MarkResolved, Activator.RESOLVE_ICON, IssueResource.URIs.Resolved, true, input);
130 private IAction unresolveAction(List<Resource> input) {
131 return tagAction(Messages.MenuActions_MarkUnresolved, Activator.UNRESOLVE_ICON, IssueResource.URIs.Resolved, false, input);
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);
139 private IContributionItem setSeverityAction(final List<Resource> userIssues) {
140 return new ContributionItem() {
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))
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() {
156 public void widgetSelected(SelectionEvent e) {
157 setSeverity(userIssues, sev);
165 private void setSeverity(final List<Resource> userIssues, final Severity severity) {
166 Simantics.getSession().asyncRequest(new WriteRequest() {
168 public void perform(WriteGraph graph) throws DatabaseException {
169 IssueResource ISSUE = IssueResource.getInstance(graph);
170 Resource sev = IssueUtils.toSeverityResource(ISSUE, severity);
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);
179 for (Resource issue : userIssues)
180 graph.deny(issue, ISSUE.Issue_HasSeverity);
185 ErrorLogger.defaultLogError(e);