1 /*******************************************************************************
2 * Copyright (c) 2007, 2019 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 * Semantum Oy - Reorganization
12 *******************************************************************************/
13 package org.simantics.issues.ui.handler;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.viewers.CheckStateChangedEvent;
24 import org.eclipse.jface.viewers.CheckboxTableViewer;
25 import org.eclipse.jface.viewers.ICheckStateListener;
26 import org.eclipse.jface.viewers.ICheckStateProvider;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.ui.PlatformUI;
34 import org.simantics.Simantics;
35 import org.simantics.db.ReadGraph;
36 import org.simantics.db.Resource;
37 import org.simantics.db.WriteGraph;
38 import org.simantics.db.common.NamedResource;
39 import org.simantics.db.common.request.PossibleObjectWithType;
40 import org.simantics.db.common.request.UniqueRead;
41 import org.simantics.db.common.request.WriteRequest;
42 import org.simantics.db.common.utils.NameUtils;
43 import org.simantics.db.exception.DatabaseException;
44 import org.simantics.db.layer0.QueryIndexUtils;
45 import org.simantics.db.layer0.request.PossibleActiveModel;
46 import org.simantics.db.layer0.util.RemoverUtil;
47 import org.simantics.db.request.Write;
48 import org.simantics.issues.common.IssueUtils;
49 import org.simantics.issues.ontology.IssueResource;
50 import org.simantics.layer0.Layer0;
51 import org.simantics.utils.ui.ErrorLogger;
52 import org.simantics.utils.ui.dialogs.ListDialog;
55 * @author Antti Villberg
57 public class ConfigureIssueSources extends AbstractHandler {
59 private static class IssueSourceEntry extends NamedResource {
60 private boolean checked;
61 public IssueSourceEntry(String name, Resource resource, boolean checked) {
62 super(name, resource);
63 this.checked = checked;
65 public boolean isChecked() {
68 public void setChecked(boolean value) {
74 public Object execute(ExecutionEvent event) throws ExecutionException {
76 Resource indexRoot = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
77 executeDefault(indexRoot);
78 } catch (DatabaseException e) {
79 throw new ExecutionException("Exception while showing validation configuration dialog", e);
84 public static void executeDefault(Resource indexRoot) throws ExecutionException {
86 if (indexRoot == null)
90 final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {
93 public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {
95 if(indexRoot == null) return Collections.emptyList();
97 List<IssueSourceEntry> result = new ArrayList<IssueSourceEntry>();
98 Layer0 L0 = Layer0.getInstance(graph);
99 IssueResource ISSUE = IssueResource.getInstance(graph);
100 for(Resource type : QueryIndexUtils.searchByType(graph, indexRoot, ISSUE.IssueSourceType)) {
101 String name = NameUtils.getSafeLabel(graph, type);
102 boolean exists = graph.syncRequest(new PossibleObjectWithType(indexRoot, L0.ConsistsOf, type)) != null;
103 boolean deprecated = graph.hasStatement(type, L0.Deprecated);
104 boolean abstract_ = graph.hasStatement(type, L0.Abstract);
105 if(!exists && (deprecated || abstract_)) continue;
106 result.add(new IssueSourceEntry(name, type, exists));
113 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
114 ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(
116 Messages.ConfigureIssueSources_SelectAvailableIssueSources,
117 Messages.ConfigureIssueSources_SelectedSourcesAddRemoveMsg) {
119 protected CheckboxTableViewer createViewer(Composite composite) {
120 CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(
121 composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
122 viewer.setCheckStateProvider(new ICheckStateProvider() {
124 public boolean isGrayed(Object o) {
128 public boolean isChecked(Object o) {
129 IssueSourceEntry entry = (IssueSourceEntry)o;
130 return entry.isChecked();
133 viewer.addCheckStateListener(e -> {
134 IssueSourceEntry entry = (IssueSourceEntry)e.getElement();
135 entry.setChecked(e.getChecked());
137 viewer.getTable().addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
138 viewer.getTable().deselectAll();
144 int result = dialog.open();
145 if (result != Dialog.OK)
148 Simantics.getSession().syncRequest((Write) graph -> {
150 Layer0 L0 = Layer0.getInstance(graph);
152 for(IssueSourceEntry entry : sources) {
154 Resource existing = graph.syncRequest(new PossibleObjectWithType(indexRoot, L0.ConsistsOf, entry.getResource()));
156 if(existing == null && entry.isChecked()) {
157 String name = NameUtils.getSafeLabel(graph, entry.getResource());
158 IssueUtils.addIssueSource(graph, indexRoot, entry.getResource(), name);
161 if(existing != null && !entry.isChecked()) {
162 RemoverUtil.remove(graph, existing);
168 } catch (DatabaseException e) {
169 ErrorLogger.defaultLogError(e);