Simple implementation for Issues property tab
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / property / IssuesTab.java
1 package org.simantics.issues.ui.property;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.jface.layout.GridLayoutFactory;
11 import org.eclipse.jface.viewers.CellLabelProvider;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredContentProvider;
14 import org.eclipse.jface.viewers.TableViewer;
15 import org.eclipse.jface.viewers.TableViewerColumn;
16 import org.eclipse.jface.viewers.ViewerCell;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
25 import org.simantics.db.common.request.ResourceRead;
26 import org.simantics.db.common.utils.NameUtils;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.management.ISessionContext;
29 import org.simantics.db.request.Read;
30 import org.simantics.issues.Severity;
31 import org.simantics.issues.common.IssueResourceContexts;
32 import org.simantics.issues.common.ListModelIssuesBySeverity;
33 import org.simantics.issues.common.SimpleIssue;
34 import org.simantics.layer0.Layer0;
35 import org.simantics.modeling.ModelingResources;
36 import org.simantics.selectionview.PropertyTabContributorImpl;
37
38 /**
39  * A very simple property tab for listing issues of a selected object.
40  * 
41  * @author luukkainen
42  *
43  */
44 public class IssuesTab extends PropertyTabContributorImpl{
45         
46         
47         private TableViewer viewer;
48         private Resource model;
49         private Resource element;
50         
51         private List<SimpleIssue> result;
52
53         public IssuesTab(ReadGraph graph, Resource model, Resource element) throws DatabaseException {
54                 this.model = model;
55                 this.element = element;
56
57                 Layer0 L0 = Layer0.getInstance(graph);
58                 List<Resource> contexts = getContexts(graph, element);
59                 Map<Severity, List<Resource>> issuesBySeverity = graph.syncRequest(
60                                 new ListModelIssuesBySeverity(model, true, true, Severity.NOTE),
61                                 TransientCacheListener.<Map<Severity, List<Resource>>>instance());
62
63                 result = new ArrayList<>();
64                 for (Severity severity : Severity.values()) {
65                         List<Resource> issues = issuesBySeverity.get(severity);
66                         if (issues != null) {
67                                 for (Resource issue : issues) {
68                                         Set<Resource> issueContexts = graph.syncRequest(new IssueResourceContexts(issue));
69                                         if (!Collections.disjoint(issueContexts, contexts)) {
70                                                 SimpleIssue si = new SimpleIssue((String) graph.getRelatedValue(issue, L0.HasLabel, Bindings.STRING),
71                                                                 severity, issue);
72                                                 result.add(si);
73                                                 System.out.println(si.label + " " + si.severity);
74                                         }
75                                 }
76                         }
77                 } 
78         }
79         
80         
81         /**
82          * @see org.simantics.modeling.ui.diagram.style.IssueDecorationStyle
83          * 
84          * @param graph
85          * @param element
86          * @return
87          * @throws DatabaseException
88          */
89         protected List<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
90
91         ModelingResources MOD = ModelingResources.getInstance(graph);
92         List<Resource> result = new ArrayList<Resource>(3);
93         result.add(element);
94         Resource config = graph.getPossibleObject(element, MOD.ElementToComponent);
95         if (config != null && result.indexOf(config) == -1) result.add(config);
96         config = graph.getPossibleObject(element, MOD.DiagramConnectionToConnection);
97         if (config != null && result.indexOf(config) == -1) result.add(config);
98         // For diagram reference element support
99         config = graph.getPossibleObject(element, MOD.HasParentComponent);
100         if (config != null && result.indexOf(config) == -1) result.add(config);
101         return result;
102
103     }
104         
105         
106         @Override
107         public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
108                 GridLayoutFactory.fillDefaults().numColumns(2).spacing(0, 0).applyTo(body);
109         
110                 viewer = new TableViewer(body);
111                 
112                 TableViewerColumn issueColumn = new TableViewerColumn(viewer, SWT.LEFT);
113                 TableViewerColumn severityColumn = new TableViewerColumn(viewer, SWT.LEFT);
114                 
115                 issueColumn.setLabelProvider(new CellLabelProvider() {
116                         
117                         @Override
118                         public void update(ViewerCell cell) {
119                                 SimpleIssue issue = (SimpleIssue)cell.getElement();
120                                 cell.setText(issue.label);
121                                 
122                         }
123                 });
124                 severityColumn.setLabelProvider(new CellLabelProvider() {
125                         
126                         @Override
127                         public void update(ViewerCell cell) {
128                                 SimpleIssue issue = (SimpleIssue)cell.getElement();
129                                 cell.setText(issue.severity.name());
130                         }
131                 });
132                 
133                 issueColumn.getColumn().setWidth(300);
134                 issueColumn.getColumn().setText("Issue");
135                 
136                 severityColumn.getColumn().setWidth(100);
137                 severityColumn.getColumn().setText("Severity");
138                 
139                 viewer.getTable().setHeaderVisible(true);
140                 viewer.getTable().setLinesVisible(true);
141                 
142                 viewer.setContentProvider(new IStructuredContentProvider() {
143                         
144                         @Override
145                         public Object[] getElements(Object inputElement) {
146                                 List<SimpleIssue> list = (List<SimpleIssue>)inputElement;
147                                 return list.toArray();
148                         }
149                 });
150                         
151                 viewer.setInput(result);
152                 GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl());
153                 
154         }
155         
156         @Override
157         public Read<String> getPartNameReadRequest(ISelection forSelection) {
158                 return new ObjectTitleRead(element);
159         }
160         
161         public static class ObjectTitleRead extends ResourceRead<String> {
162                 public ObjectTitleRead(Resource resource) {
163                         super(resource);
164                 }
165
166                 @Override
167                 public String perform(ReadGraph graph) throws DatabaseException {
168                         return NameUtils.getSafeName(graph, resource);
169                 }
170         }
171
172 }