]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ConfigureIssueSources.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / ConfigureIssueSources.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.issues.ui.handler;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collections;\r
16 import java.util.List;\r
17 \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
51 \r
52 /**\r
53  * @author Antti Villberg\r
54  */\r
55 public class ConfigureIssueSources extends AbstractHandler {\r
56 \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
62                 }\r
63                 public boolean isChecked() {\r
64                         return checked;\r
65                 }\r
66                 public void setChecked(boolean value) {\r
67                         checked = value;\r
68                 }\r
69         }\r
70         \r
71     @Override\r
72     public Object execute(ExecutionEvent event) throws ExecutionException {\r
73 \r
74       try {\r
75 \r
76         final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {\r
77 \r
78                         @Override\r
79                         public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {\r
80                                 \r
81                                 Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));\r
82                                 if(activeModel == null) return Collections.emptyList();\r
83                                 \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
93                                 }\r
94                                 return result;\r
95                         }\r
96                 \r
97         });\r
98         \r
99          Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
100          ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(\r
101                  shell, sources,\r
102                  "Select available issue sources",\r
103                  "Selected sources will be used and existing deselected sources will be removed.") {\r
104                  \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
110                                                 \r
111                                                 @Override\r
112                                                 public boolean isGrayed(Object arg0) {\r
113                                                         return false;\r
114                                                 }\r
115                                                 \r
116                                                 @Override\r
117                                                 public boolean isChecked(Object arg0) {\r
118                                                         IssueSourceEntry entry = (IssueSourceEntry)arg0;\r
119                                                         return entry.isChecked();\r
120                                                 }\r
121                                                 \r
122                                         });\r
123                         viewer.addCheckStateListener(new ICheckStateListener() {\r
124                                                 \r
125                                                 @Override\r
126                                                 public void checkStateChanged(CheckStateChangedEvent arg0) {\r
127                                                         IssueSourceEntry entry = (IssueSourceEntry)arg0.getElement();\r
128                                                         entry.setChecked(arg0.getChecked());\r
129                                                 }\r
130                                         });\r
131                         table.addSelectionListener(new SelectionListener () {\r
132                                 @Override\r
133                                 public void widgetSelected(SelectionEvent e) {\r
134                                         table.deselectAll();\r
135                                 }\r
136                                 @Override\r
137                                 public void widgetDefaultSelected(SelectionEvent e) {}\r
138                         });\r
139                         return viewer;\r
140                     }\r
141 \r
142          };\r
143          int result = dialog.open();\r
144          if (result != Dialog.OK)\r
145              return null;\r
146         \r
147         Simantics.getSession().syncRequest(new WriteRequest() {\r
148 \r
149                         @Override\r
150                         public void perform(WriteGraph graph) throws DatabaseException {\r
151                                 \r
152                                 Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));\r
153                                 if(activeModel == null) return;\r
154                                 \r
155                                 Layer0 L0 = Layer0.getInstance(graph);\r
156 \r
157                                 for(IssueSourceEntry entry : sources) {\r
158 \r
159                                         Resource existing = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, entry.getResource())); \r
160                                         \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
164                                         }\r
165                                         \r
166                                         if(existing != null && !entry.isChecked()) {\r
167                                                 RemoverUtil.remove(graph, existing);\r
168                                         }\r
169                                         \r
170                                 }\r
171                                 \r
172                         }\r
173                 \r
174         });\r
175          \r
176 //        try {\r
177 //            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {\r
178 //                @Override\r
179 //                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
180 //                    try {\r
181 //                        purgeResolvedIssues(monitor);\r
182 //                    } catch (DatabaseException e) {\r
183 //                        throw new InvocationTargetException(e);\r
184 //                    } finally {\r
185 //                        monitor.done();\r
186 //                    }\r
187 //                }\r
188 //            });\r
189 //        } catch (InvocationTargetException e) {\r
190 //            ErrorLogger.defaultLogError(e);\r
191         } catch (DatabaseException e) {\r
192             ErrorLogger.defaultLogError(e);\r
193         }\r
194         return null;\r
195     }\r
196 \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
201 //            return;\r
202 //\r
203 //        final SubMonitor mon = SubMonitor.convert(monitor, "Purging resolved issues...", 100);\r
204 //\r
205 //        session.syncRequest(new DelayedWriteRequest() {\r
206 //            @Override\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
220 //                            }\r
221 //                            if (isContinuous)\r
222 //                                continue;\r
223 //                        }\r
224 //                        toBeRemoved.add(activeIssue);\r
225 //                    }\r
226 //                }\r
227 //\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
235 //                    mon.worked(1);\r
236 //                }\r
237 //                Layer0Utils.addCommentMetadata(graph, sb.toString());\r
238 //            }\r
239 //        });\r
240 //    }\r
241 \r
242 }\r