Simple implementation for Issues property tab 92/4292/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 30 Jun 2020 08:38:34 +0000 (11:38 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 30 Jun 2020 08:51:21 +0000 (08:51 +0000)
gitlab #567

Change-Id: If675e438e23229347c9fe5e47748ffcb7bb8fc05
(cherry picked from commit f64c5fbbb51c58e122a67299c67494af573c1104)

bundles/org.simantics.issues.ui/META-INF/MANIFEST.MF
bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/property/IssuesTab.java [new file with mode: 0644]

index ba76bbb0e128a2d9977395f09d2a8d852863ee16..abb1bf685ee7b3757c996d8bbd43d02e32e93531 100644 (file)
@@ -24,6 +24,7 @@ Bundle-ActivationPolicy: lazy
 Bundle-Activator: org.simantics.issues.ui.internal.Activator
 Export-Package: org.simantics.issues.ui,
  org.simantics.issues.ui.contribution,
- org.simantics.issues.ui.handler
+ org.simantics.issues.ui.handler,
+ org.simantics.issues.ui.property
 Bundle-Vendor: VTT Technical Research Centre of Finland
 Automatic-Module-Name: org.simantics.issues.ui
diff --git a/bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/property/IssuesTab.java b/bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/property/IssuesTab.java
new file mode 100644 (file)
index 0000000..fb1e0a9
--- /dev/null
@@ -0,0 +1,172 @@
+package org.simantics.issues.ui.property;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.viewers.CellLabelProvider;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.TableViewerColumn;
+import org.eclipse.jface.viewers.ViewerCell;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.ui.IWorkbenchSite;
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.request.Read;
+import org.simantics.issues.Severity;
+import org.simantics.issues.common.IssueResourceContexts;
+import org.simantics.issues.common.ListModelIssuesBySeverity;
+import org.simantics.issues.common.SimpleIssue;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.selectionview.PropertyTabContributorImpl;
+
+/**
+ * A very simple property tab for listing issues of a selected object.
+ * 
+ * @author luukkainen
+ *
+ */
+public class IssuesTab extends PropertyTabContributorImpl{
+       
+       
+       private TableViewer viewer;
+       private Resource model;
+       private Resource element;
+       
+       private List<SimpleIssue> result;
+
+       public IssuesTab(ReadGraph graph, Resource model, Resource element) throws DatabaseException {
+               this.model = model;
+               this.element = element;
+
+               Layer0 L0 = Layer0.getInstance(graph);
+               List<Resource> contexts = getContexts(graph, element);
+               Map<Severity, List<Resource>> issuesBySeverity = graph.syncRequest(
+                               new ListModelIssuesBySeverity(model, true, true, Severity.NOTE),
+                               TransientCacheListener.<Map<Severity, List<Resource>>>instance());
+
+               result = new ArrayList<>();
+               for (Severity severity : Severity.values()) {
+                       List<Resource> issues = issuesBySeverity.get(severity);
+                       if (issues != null) {
+                               for (Resource issue : issues) {
+                                       Set<Resource> issueContexts = graph.syncRequest(new IssueResourceContexts(issue));
+                                       if (!Collections.disjoint(issueContexts, contexts)) {
+                                               SimpleIssue si = new SimpleIssue((String) graph.getRelatedValue(issue, L0.HasLabel, Bindings.STRING),
+                                                               severity, issue);
+                                               result.add(si);
+                                               System.out.println(si.label + " " + si.severity);
+                                       }
+                               }
+                       }
+               } 
+       }
+       
+       
+       /**
+        * @see org.simantics.modeling.ui.diagram.style.IssueDecorationStyle
+        * 
+        * @param graph
+        * @param element
+        * @return
+        * @throws DatabaseException
+        */
+       protected List<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
+
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        List<Resource> result = new ArrayList<Resource>(3);
+        result.add(element);
+        Resource config = graph.getPossibleObject(element, MOD.ElementToComponent);
+        if (config != null && result.indexOf(config) == -1) result.add(config);
+        config = graph.getPossibleObject(element, MOD.DiagramConnectionToConnection);
+        if (config != null && result.indexOf(config) == -1) result.add(config);
+        // For diagram reference element support
+        config = graph.getPossibleObject(element, MOD.HasParentComponent);
+        if (config != null && result.indexOf(config) == -1) result.add(config);
+        return result;
+
+    }
+       
+       
+       @Override
+       public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
+               GridLayoutFactory.fillDefaults().numColumns(2).spacing(0, 0).applyTo(body);
+       
+               viewer = new TableViewer(body);
+               
+               TableViewerColumn issueColumn = new TableViewerColumn(viewer, SWT.LEFT);
+               TableViewerColumn severityColumn = new TableViewerColumn(viewer, SWT.LEFT);
+               
+               issueColumn.setLabelProvider(new CellLabelProvider() {
+                       
+                       @Override
+                       public void update(ViewerCell cell) {
+                               SimpleIssue issue = (SimpleIssue)cell.getElement();
+                               cell.setText(issue.label);
+                               
+                       }
+               });
+               severityColumn.setLabelProvider(new CellLabelProvider() {
+                       
+                       @Override
+                       public void update(ViewerCell cell) {
+                               SimpleIssue issue = (SimpleIssue)cell.getElement();
+                               cell.setText(issue.severity.name());
+                       }
+               });
+               
+               issueColumn.getColumn().setWidth(300);
+               issueColumn.getColumn().setText("Issue");
+               
+               severityColumn.getColumn().setWidth(100);
+               severityColumn.getColumn().setText("Severity");
+               
+               viewer.getTable().setHeaderVisible(true);
+               viewer.getTable().setLinesVisible(true);
+               
+               viewer.setContentProvider(new IStructuredContentProvider() {
+                       
+                       @Override
+                       public Object[] getElements(Object inputElement) {
+                               List<SimpleIssue> list = (List<SimpleIssue>)inputElement;
+                               return list.toArray();
+                       }
+               });
+                       
+               viewer.setInput(result);
+               GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl());
+               
+       }
+       
+       @Override
+       public Read<String> getPartNameReadRequest(ISelection forSelection) {
+               return new ObjectTitleRead(element);
+       }
+       
+       public static class ObjectTitleRead extends ResourceRead<String> {
+               public ObjectTitleRead(Resource resource) {
+                       super(resource);
+               }
+
+               @Override
+               public String perform(ReadGraph graph) throws DatabaseException {
+                       return NameUtils.getSafeName(graph, resource);
+               }
+       }
+
+}