]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ConfigureIssueSources.java
Playground for Antti.
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / ConfigureIssueSources.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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  *******************************************************************************/
12 package org.simantics.issues.ui.handler;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17
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;
51
52 /**
53  * @author Antti Villberg
54  */
55 public class ConfigureIssueSources extends AbstractHandler {
56
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;
62                 }
63                 public boolean isChecked() {
64                         return checked;
65                 }
66                 public void setChecked(boolean value) {
67                         checked = value;
68                 }
69         }
70         
71     @Override
72     public Object execute(ExecutionEvent event) throws ExecutionException {
73         try {
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);
78         }
79         return null;
80     }
81
82     public static void executeDefault(Resource indexRoot) throws ExecutionException {
83
84       try {
85
86         final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {
87
88                         @Override
89                         public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {
90                                 
91                                 if(indexRoot == null) return Collections.emptyList();
92                                 
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));
103                                 }
104                                 return result;
105                         }
106                 
107         });
108         
109          Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
110          ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(
111                  shell, sources,
112                  Messages.ConfigureIssueSources_SelectAvailableIssueSources,
113                  Messages.ConfigureIssueSources_SelectedSourcesAddRemoveMsg) {
114                  
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() {
120                                                 
121                                                 @Override
122                                                 public boolean isGrayed(Object arg0) {
123                                                         return false;
124                                                 }
125                                                 
126                                                 @Override
127                                                 public boolean isChecked(Object arg0) {
128                                                         IssueSourceEntry entry = (IssueSourceEntry)arg0;
129                                                         return entry.isChecked();
130                                                 }
131                                                 
132                                         });
133                         viewer.addCheckStateListener(new ICheckStateListener() {
134                                                 
135                                                 @Override
136                                                 public void checkStateChanged(CheckStateChangedEvent arg0) {
137                                                         IssueSourceEntry entry = (IssueSourceEntry)arg0.getElement();
138                                                         entry.setChecked(arg0.getChecked());
139                                                 }
140                                         });
141                         table.addSelectionListener(new SelectionListener () {
142                                 @Override
143                                 public void widgetSelected(SelectionEvent e) {
144                                         table.deselectAll();
145                                 }
146                                 @Override
147                                 public void widgetDefaultSelected(SelectionEvent e) {}
148                         });
149                         return viewer;
150                     }
151
152          };
153          int result = dialog.open();
154          if (result != Dialog.OK)
155              return;
156         
157         Simantics.getSession().syncRequest(new WriteRequest() {
158
159                         @Override
160                         public void perform(WriteGraph graph) throws DatabaseException {
161                                 
162                                 if(indexRoot == null) return;
163                                 
164                                 Layer0 L0 = Layer0.getInstance(graph);
165
166                                 for(IssueSourceEntry entry : sources) {
167
168                                         Resource existing = graph.syncRequest(new PossibleObjectWithType(indexRoot, L0.ConsistsOf, entry.getResource())); 
169                                         
170                                         if(existing == null && entry.isChecked()) {
171                                                 String name = NameUtils.getSafeLabel(graph, entry.getResource());
172                                                 IssueUtils.addIssueSource(graph, indexRoot, entry.getResource(), name);
173                                         }
174                                         
175                                         if(existing != null && !entry.isChecked()) {
176                                                 RemoverUtil.remove(graph, existing);
177                                         }
178                                         
179                                 }
180                                 
181                         }
182                 
183         });
184          
185         } catch (DatabaseException e) {
186             ErrorLogger.defaultLogError(e);
187         }
188         return;
189     }
190
191 }