]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ConfigureIssueSources.java b/bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ConfigureIssueSources.java
new file mode 100644 (file)
index 0000000..797a74e
--- /dev/null
@@ -0,0 +1,242 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.issues.ui.handler;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.viewers.CheckStateChangedEvent;\r
+import org.eclipse.jface.viewers.CheckboxTableViewer;\r
+import org.eclipse.jface.viewers.ICheckStateListener;\r
+import org.eclipse.jface.viewers.ICheckStateProvider;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Table;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.NamedResource;\r
+import org.simantics.db.common.request.PossibleObjectWithType;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PossibleActiveModel;\r
+import org.simantics.db.layer0.util.RemoverUtil;\r
+import org.simantics.issues.common.IssueUtils;\r
+import org.simantics.issues.ontology.IssueResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingUtils;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.dialogs.ListDialog;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class ConfigureIssueSources extends AbstractHandler {\r
+\r
+       private static class IssueSourceEntry extends NamedResource {\r
+               private boolean checked;\r
+               public IssueSourceEntry(String name, Resource resource, boolean checked) {\r
+                       super(name, resource);\r
+                       this.checked = checked;\r
+               }\r
+               public boolean isChecked() {\r
+                       return checked;\r
+               }\r
+               public void setChecked(boolean value) {\r
+                       checked = value;\r
+               }\r
+       }\r
+       \r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+      try {\r
+\r
+       final List<IssueSourceEntry> sources = Simantics.getSession().syncRequest(new UniqueRead<List<IssueSourceEntry>>() {\r
+\r
+                       @Override\r
+                       public List<IssueSourceEntry> perform(ReadGraph graph) throws DatabaseException {\r
+                               \r
+                               Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));\r
+                               if(activeModel == null) return Collections.emptyList();\r
+                               \r
+                               List<IssueSourceEntry> result = new ArrayList<IssueSourceEntry>();\r
+                               Layer0 L0 = Layer0.getInstance(graph);\r
+                               IssueResource ISSUE = IssueResource.getInstance(graph);\r
+                               for(Resource type : ModelingUtils.searchByType(graph, activeModel, ISSUE.IssueSourceType)) {\r
+                                       String name = NameUtils.getSafeLabel(graph, type);\r
+                                       boolean exists = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, type)) != null;\r
+                                       boolean deprecated = graph.hasStatement(type, L0.Deprecated);\r
+                                       if(!exists && deprecated) continue;\r
+                                       result.add(new IssueSourceEntry(name, type, exists));\r
+                               }\r
+                               return result;\r
+                       }\r
+               \r
+       });\r
+       \r
+        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
+         ListDialog<IssueSourceEntry> dialog = new ListDialog<IssueSourceEntry>(\r
+                 shell, sources,\r
+                 "Select available issue sources",\r
+                 "Selected sources will be used and existing deselected sources will be removed.") {\r
+                \r
+                   protected CheckboxTableViewer createViewer(Composite composite) {\r
+                       CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(\r
+                               composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);\r
+                       final Table table = (Table)viewer.getControl();\r
+                       viewer.setCheckStateProvider(new ICheckStateProvider() {\r
+                                               \r
+                                               @Override\r
+                                               public boolean isGrayed(Object arg0) {\r
+                                                       return false;\r
+                                               }\r
+                                               \r
+                                               @Override\r
+                                               public boolean isChecked(Object arg0) {\r
+                                                       IssueSourceEntry entry = (IssueSourceEntry)arg0;\r
+                                                       return entry.isChecked();\r
+                                               }\r
+                                               \r
+                                       });\r
+                       viewer.addCheckStateListener(new ICheckStateListener() {\r
+                                               \r
+                                               @Override\r
+                                               public void checkStateChanged(CheckStateChangedEvent arg0) {\r
+                                                       IssueSourceEntry entry = (IssueSourceEntry)arg0.getElement();\r
+                                                       entry.setChecked(arg0.getChecked());\r
+                                               }\r
+                                       });\r
+                       table.addSelectionListener(new SelectionListener () {\r
+                               @Override\r
+                               public void widgetSelected(SelectionEvent e) {\r
+                                       table.deselectAll();\r
+                               }\r
+                               @Override\r
+                               public void widgetDefaultSelected(SelectionEvent e) {}\r
+                       });\r
+                       return viewer;\r
+                   }\r
+\r
+         };\r
+         int result = dialog.open();\r
+         if (result != Dialog.OK)\r
+             return null;\r
+       \r
+       Simantics.getSession().syncRequest(new WriteRequest() {\r
+\r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               \r
+                               Resource activeModel = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));\r
+                               if(activeModel == null) return;\r
+                               \r
+                               Layer0 L0 = Layer0.getInstance(graph);\r
+\r
+                               for(IssueSourceEntry entry : sources) {\r
+\r
+                                       Resource existing = graph.syncRequest(new PossibleObjectWithType(activeModel, L0.ConsistsOf, entry.getResource())); \r
+                                       \r
+                                       if(existing == null && entry.isChecked()) {\r
+                                               String name = NameUtils.getSafeLabel(graph, entry.getResource());\r
+                                               IssueUtils.addIssueSource(graph, activeModel, entry.getResource(), name);\r
+                                       }\r
+                                       \r
+                                       if(existing != null && !entry.isChecked()) {\r
+                                               RemoverUtil.remove(graph, existing);\r
+                                       }\r
+                                       \r
+                               }\r
+                               \r
+                       }\r
+               \r
+       });\r
+         \r
+//        try {\r
+//            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {\r
+//                @Override\r
+//                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+//                    try {\r
+//                        purgeResolvedIssues(monitor);\r
+//                    } catch (DatabaseException e) {\r
+//                        throw new InvocationTargetException(e);\r
+//                    } finally {\r
+//                        monitor.done();\r
+//                    }\r
+//                }\r
+//            });\r
+//        } catch (InvocationTargetException e) {\r
+//            ErrorLogger.defaultLogError(e);\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+        return null;\r
+    }\r
+\r
+//    private void purgeResolvedIssues(IProgressMonitor monitor) throws DatabaseException {\r
+//        Session session = Simantics.getSession();\r
+//        final Resource project = Simantics.getProjectResource();\r
+//        if (project == null)\r
+//            return;\r
+//\r
+//        final SubMonitor mon = SubMonitor.convert(monitor, "Purging resolved issues...", 100);\r
+//\r
+//        session.syncRequest(new DelayedWriteRequest() {\r
+//            @Override\r
+//            public void perform(WriteGraph graph) throws DatabaseException {\r
+//                graph.markUndoPoint();\r
+//                IssueResource ISSUE = IssueResource.getInstance(graph);\r
+//                Set<Resource> toBeRemoved = new HashSet<Resource>();\r
+//                Map<Resource, Boolean> sourceIsContinuous = new THashMap<Resource, Boolean>(); \r
+//                for (Resource activeIssue : graph.syncRequest(new AllActiveIssues(project))) {\r
+//                    if (graph.hasStatement(activeIssue, ISSUE.Resolved)) {\r
+//                        Resource managedBy = graph.getPossibleObject(activeIssue, ISSUE.IssueSource_Manages_Inverse);\r
+//                        if (managedBy != null) {\r
+//                            Boolean isContinuous = sourceIsContinuous.get(managedBy);\r
+//                            if (isContinuous == null) {\r
+//                                isContinuous = graph.isInstanceOf(managedBy, ISSUE.ContinuousIssueSource);\r
+//                                sourceIsContinuous.put(managedBy, isContinuous);\r
+//                            }\r
+//                            if (isContinuous)\r
+//                                continue;\r
+//                        }\r
+//                        toBeRemoved.add(activeIssue);\r
+//                    }\r
+//                }\r
+//\r
+//                mon.setTaskName("Purging " + toBeRemoved.size() + " resolved batch issues...");\r
+//                mon.setWorkRemaining(toBeRemoved.size());\r
+//                StringBuilder sb = new StringBuilder();\r
+//                sb.append("Purged " + toBeRemoved.size() + " resolved batch issue(s)");\r
+//                for (Resource remove : toBeRemoved) {\r
+//                    //sb.append(NameUtils.getSafeLabel(graph, remove) + " ");\r
+//                    RemoverUtil.remove(graph, remove);\r
+//                    mon.worked(1);\r
+//                }\r
+//                Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+//            }\r
+//        });\r
+//    }\r
+\r
+}\r