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