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.QueryIndexUtils;
44 import org.simantics.db.layer0.request.PossibleActiveModel;
45 import org.simantics.db.layer0.util.RemoverUtil;
46 import org.simantics.issues.common.IssueUtils;
47 import org.simantics.issues.ontology.IssueResource;
48 import org.simantics.layer0.Layer0;
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 {
74 Resource indexRoot = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
75 executeDefault(indexRoot);
76 } catch (DatabaseException e) {
77 throw new ExecutionException("Exception while showing validation configuration dialog", e);
82 public static void executeDefault(Resource indexRoot) throws ExecutionException {
86 final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {
89 public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {
91 if(indexRoot == null) return Collections.emptyList();
93 List<IssueSourceEntry> result = new ArrayList<IssueSourceEntry>();
94 Layer0 L0 = Layer0.getInstance(graph);
95 IssueResource ISSUE = IssueResource.getInstance(graph);
96 for(Resource type : QueryIndexUtils.searchByType(graph, indexRoot, ISSUE.IssueSourceType)) {
97 String name = NameUtils.getSafeLabel(graph, type);
98 boolean exists = graph.syncRequest(new PossibleObjectWithType(indexRoot, L0.ConsistsOf, type)) != null;
99 boolean deprecated = graph.hasStatement(type, L0.Deprecated);
100 boolean abstract_ = graph.hasStatement(type, L0.Abstract);
101 if(!exists && (deprecated || abstract_)) continue;
102 result.add(new IssueSourceEntry(name, type, exists));
109 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
110 ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(
112 Messages.ConfigureIssueSources_SelectAvailableIssueSources,
113 Messages.ConfigureIssueSources_SelectedSourcesAddRemoveMsg) {
115 protected CheckboxTableViewer createViewer(Composite composite) {
116 CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(
117 composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
118 final Table table = (Table)viewer.getControl();
119 viewer.setCheckStateProvider(new ICheckStateProvider() {
122 public boolean isGrayed(Object arg0) {
127 public boolean isChecked(Object arg0) {
128 IssueSourceEntry entry = (IssueSourceEntry)arg0;
129 return entry.isChecked();
133 viewer.addCheckStateListener(new ICheckStateListener() {
136 public void checkStateChanged(CheckStateChangedEvent arg0) {
137 IssueSourceEntry entry = (IssueSourceEntry)arg0.getElement();
138 entry.setChecked(arg0.getChecked());
141 table.addSelectionListener(new SelectionListener () {
143 public void widgetSelected(SelectionEvent e) {
147 public void widgetDefaultSelected(SelectionEvent e) {}
153 int result = dialog.open();
154 if (result != Dialog.OK)
157 Simantics.getSession().syncRequest(new WriteRequest() {
160 public void perform(WriteGraph graph) throws DatabaseException {
162 if(indexRoot == null) return;
164 Layer0 L0 = Layer0.getInstance(graph);
166 for(IssueSourceEntry entry : sources) {
168 Resource existing = graph.syncRequest(new PossibleObjectWithType(indexRoot, L0.ConsistsOf, entry.getResource()));
170 if(existing == null && entry.isChecked()) {
171 String name = NameUtils.getSafeLabel(graph, entry.getResource());
172 IssueUtils.addIssueSource(graph, indexRoot, entry.getResource(), name);
175 if(existing != null && !entry.isChecked()) {
176 RemoverUtil.remove(graph, existing);
185 } catch (DatabaseException e) {
186 ErrorLogger.defaultLogError(e);