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.Collections;
16 import java.util.List;
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.CheckboxTableViewer;
24 import org.eclipse.jface.viewers.ICheckStateListener;
25 import org.eclipse.jface.viewers.ICheckStateProvider;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.ui.PlatformUI;
33 import org.simantics.Simantics;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.common.NamedResource;
38 import org.simantics.db.common.request.PossibleObjectWithType;
39 import org.simantics.db.common.request.UniqueRead;
40 import org.simantics.db.common.request.WriteRequest;
41 import org.simantics.db.common.utils.NameUtils;
42 import org.simantics.db.exception.DatabaseException;
43 import org.simantics.db.layer0.request.PossibleActiveModel;
44 import org.simantics.db.layer0.util.RemoverUtil;
45 import org.simantics.issues.common.IssueUtils;
46 import org.simantics.issues.ontology.IssueResource;
47 import org.simantics.layer0.Layer0;
48 import org.simantics.modeling.ModelingUtils;
49 import org.simantics.utils.ui.ErrorLogger;
50 import org.simantics.utils.ui.dialogs.ListDialog;
53 * @author Antti Villberg
55 public class ConfigureIssueSources extends AbstractHandler {
57 private static class IssueSourceEntry extends NamedResource {
58 private boolean checked;
59 public IssueSourceEntry(String name, Resource resource, boolean checked) {
60 super(name, resource);
61 this.checked = checked;
63 public boolean isChecked() {
66 public void setChecked(boolean value) {
72 public Object execute(ExecutionEvent event) throws ExecutionException {
76 final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {
79 public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {
81 Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
82 if(activeModel == null) return Collections.emptyList();
84 List<IssueSourceEntry> result = new ArrayList<IssueSourceEntry>();
85 Layer0 L0 = Layer0.getInstance(graph);
86 IssueResource ISSUE = IssueResource.getInstance(graph);
87 for(Resource type : ModelingUtils.searchByType(graph, activeModel, ISSUE.IssueSourceType)) {
88 String name = NameUtils.getSafeLabel(graph, type);
89 boolean exists = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, type)) != null;
90 boolean deprecated = graph.hasStatement(type, L0.Deprecated);
91 if(!exists && deprecated) continue;
92 result.add(new IssueSourceEntry(name, type, exists));
99 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
100 ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(
102 "Select available issue sources",
103 "Selected sources will be used and existing deselected sources will be removed.") {
105 protected CheckboxTableViewer createViewer(Composite composite) {
106 CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(
107 composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
108 final Table table = (Table)viewer.getControl();
109 viewer.setCheckStateProvider(new ICheckStateProvider() {
112 public boolean isGrayed(Object arg0) {
117 public boolean isChecked(Object arg0) {
118 IssueSourceEntry entry = (IssueSourceEntry)arg0;
119 return entry.isChecked();
123 viewer.addCheckStateListener(new ICheckStateListener() {
126 public void checkStateChanged(CheckStateChangedEvent arg0) {
127 IssueSourceEntry entry = (IssueSourceEntry)arg0.getElement();
128 entry.setChecked(arg0.getChecked());
131 table.addSelectionListener(new SelectionListener () {
133 public void widgetSelected(SelectionEvent e) {
137 public void widgetDefaultSelected(SelectionEvent e) {}
143 int result = dialog.open();
144 if (result != Dialog.OK)
147 Simantics.getSession().syncRequest(new WriteRequest() {
150 public void perform(WriteGraph graph) throws DatabaseException {
152 Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
153 if(activeModel == null) return;
155 Layer0 L0 = Layer0.getInstance(graph);
157 for(IssueSourceEntry entry : sources) {
159 Resource existing = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, entry.getResource()));
161 if(existing == null && entry.isChecked()) {
162 String name = NameUtils.getSafeLabel(graph, entry.getResource());
163 IssueUtils.addIssueSource(graph, activeModel, entry.getResource(), name);
166 if(existing != null && !entry.isChecked()) {
167 RemoverUtil.remove(graph, existing);
177 // PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
179 // public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
181 // purgeResolvedIssues(monitor);
182 // } catch (DatabaseException e) {
183 // throw new InvocationTargetException(e);
189 // } catch (InvocationTargetException e) {
190 // ErrorLogger.defaultLogError(e);
191 } catch (DatabaseException e) {
192 ErrorLogger.defaultLogError(e);
197 // private void purgeResolvedIssues(IProgressMonitor monitor) throws DatabaseException {
198 // Session session = Simantics.getSession();
199 // final Resource project = Simantics.getProjectResource();
200 // if (project == null)
203 // final SubMonitor mon = SubMonitor.convert(monitor, "Purging resolved issues...", 100);
205 // session.syncRequest(new DelayedWriteRequest() {
207 // public void perform(WriteGraph graph) throws DatabaseException {
208 // graph.markUndoPoint();
209 // IssueResource ISSUE = IssueResource.getInstance(graph);
210 // Set<Resource> toBeRemoved = new HashSet<Resource>();
211 // Map<Resource, Boolean> sourceIsContinuous = new THashMap<Resource, Boolean>();
212 // for (Resource activeIssue : graph.syncRequest(new AllActiveIssues(project))) {
213 // if (graph.hasStatement(activeIssue, ISSUE.Resolved)) {
214 // Resource managedBy = graph.getPossibleObject(activeIssue, ISSUE.IssueSource_Manages_Inverse);
215 // if (managedBy != null) {
216 // Boolean isContinuous = sourceIsContinuous.get(managedBy);
217 // if (isContinuous == null) {
218 // isContinuous = graph.isInstanceOf(managedBy, ISSUE.ContinuousIssueSource);
219 // sourceIsContinuous.put(managedBy, isContinuous);
224 // toBeRemoved.add(activeIssue);
228 // mon.setTaskName("Purging " + toBeRemoved.size() + " resolved batch issues...");
229 // mon.setWorkRemaining(toBeRemoved.size());
230 // StringBuilder sb = new StringBuilder();
231 // sb.append("Purged " + toBeRemoved.size() + " resolved batch issue(s)");
232 // for (Resource remove : toBeRemoved) {
233 // //sb.append(NameUtils.getSafeLabel(graph, remove) + " ");
234 // RemoverUtil.remove(graph, remove);
237 // Layer0Utils.addCommentMetadata(graph, sb.toString());