]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ConfigureIssueSources.java
Improvements to constraint-based issues
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / ConfigureIssueSources.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - Reorganization
12  *******************************************************************************/
13 package org.simantics.issues.ui.handler;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18
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;
53
54 /**
55  * @author Antti Villberg
56  */
57 public class ConfigureIssueSources extends AbstractHandler {
58
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;
64                 }
65                 public boolean isChecked() {
66                         return checked;
67                 }
68                 public void setChecked(boolean value) {
69                         checked = value;
70                 }
71         }
72
73         @Override
74         public Object execute(ExecutionEvent event) throws ExecutionException {
75                 try {
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);
80                 }
81                 return null;
82         }
83
84         public static void executeDefault(Resource indexRoot) throws ExecutionException {
85
86                 if (indexRoot == null)
87                         return;
88
89                 try {
90                         final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {
91
92                                 @Override
93                                 public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {
94
95                                         if(indexRoot == null) return Collections.emptyList();
96
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));
107                                         }
108                                         return result;
109                                 }
110
111                         });
112
113                         Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
114                         ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(
115                                         shell, sources,
116                                         Messages.ConfigureIssueSources_SelectAvailableIssueSources,
117                                         Messages.ConfigureIssueSources_SelectedSourcesAddRemoveMsg) {
118
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() {
123                                                 @Override
124                                                 public boolean isGrayed(Object o) {
125                                                         return false;
126                                                 }
127                                                 @Override
128                                                 public boolean isChecked(Object o) {
129                                                         IssueSourceEntry entry = (IssueSourceEntry)o;
130                                                         return entry.isChecked();
131                                                 }
132                                         });
133                                         viewer.addCheckStateListener(e -> {
134                                                 IssueSourceEntry entry = (IssueSourceEntry)e.getElement();
135                                                 entry.setChecked(e.getChecked());
136                                         });
137                                         viewer.getTable().addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
138                                                 viewer.getTable().deselectAll();
139                                         }));
140                                         return viewer;
141                                 }
142
143                         };
144                         int result = dialog.open();
145                         if (result != Dialog.OK)
146                                 return;
147
148                         Simantics.getSession().syncRequest((Write) graph -> {
149
150                                 Layer0 L0 = Layer0.getInstance(graph);
151
152                                 for(IssueSourceEntry entry : sources) {
153
154                                         Resource existing = graph.syncRequest(new PossibleObjectWithType(indexRoot, L0.ConsistsOf, entry.getResource()));
155
156                                         if(existing == null && entry.isChecked()) {
157                                                 String name = NameUtils.getSafeLabel(graph, entry.getResource());
158                                                 IssueUtils.addIssueSource(graph, indexRoot, entry.getResource(), name);
159                                         }
160
161                                         if(existing != null && !entry.isChecked()) {
162                                                 RemoverUtil.remove(graph, existing);
163                                         }
164
165                                 }
166
167                         });
168                 } catch (DatabaseException e) {
169                         ErrorLogger.defaultLogError(e);
170                 }
171
172         }
173
174 }