b4fd1edd8a1c8fdad594ff86e6a6b610f8b5abcb
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / issues / SCLIssuesView.java
1 package org.simantics.scl.ui.issues;
2
3 import org.eclipse.jface.action.Action;
4 import org.eclipse.jface.action.IAction;
5 import org.eclipse.jface.resource.ImageRegistry;
6 import org.eclipse.jface.viewers.ColumnLabelProvider;
7 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
8 import org.eclipse.jface.viewers.DoubleClickEvent;
9 import org.eclipse.jface.viewers.IDoubleClickListener;
10 import org.eclipse.jface.viewers.IStructuredSelection;
11 import org.eclipse.jface.viewers.TableViewer;
12 import org.eclipse.jface.viewers.TableViewerColumn;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ControlAdapter;
15 import org.eclipse.swt.events.ControlEvent;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.ScrollBar;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.IViewSite;
25 import org.eclipse.ui.PartInitException;
26 import org.eclipse.ui.part.ViewPart;
27 import org.simantics.scl.compiler.errors.ErrorSeverity;
28 import org.simantics.scl.osgi.SCLOsgi;
29 import org.simantics.scl.ui.Activator;
30 import org.simantics.scl.ui.editor2.OpenSCLDefinition;
31
32 public class SCLIssuesView extends ViewPart {
33
34     private static final int COLUMN1_DEFAULT_WIDTH = 300;
35     private static final int COLUMN2_MIN_WIDTH = 100;
36     
37     Composite parent;
38     TableViewer tableViewer;
39     TableViewerColumn column1;
40     TableViewerColumn column2;
41
42     ImageRegistry imageRegistry;
43     
44     SCLIssuesContentProvider issuesContentProvider = new SCLIssuesContentProvider();
45     
46     public SCLIssuesView() {
47         super();
48         imageRegistry = Activator.getInstance().getImageRegistry();
49     }
50     
51     @Override
52     public void init(IViewSite site, IMemento memento)
53             throws PartInitException {
54         super.init(site, memento);
55         IAction action = new Action("Refresh") {
56             @Override
57             public void run() {
58                 SCLOsgi.MODULE_REPOSITORY.getSourceRepository().checkUpdates();
59             }
60         };
61         action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh"));
62         site.getActionBars().getToolBarManager().add(action);
63     }
64     
65     @Override
66     public void createPartControl(Composite parent) {
67         this.parent = parent;
68         parent.setLayout(new FillLayout());
69         tableViewer = new TableViewer(parent,
70                 SWT.FULL_SELECTION | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL);
71         ColumnViewerToolTipSupport.enableFor(tableViewer);
72         tableViewer.setContentProvider(issuesContentProvider);
73
74         Table table = tableViewer.getTable();
75         table.setHeaderVisible(true);
76         table.setLinesVisible(true);
77
78         column1 = new TableViewerColumn(tableViewer, SWT.NONE);
79         column1.getColumn().setText("Module");
80         column1.getColumn().setWidth(COLUMN1_DEFAULT_WIDTH);
81         column1.getColumn().setResizable(true);
82         column1.setLabelProvider(new ColumnLabelProvider() {
83             @Override
84             public String getText(Object element) {
85                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
86                 return entry.moduleName;
87             }
88             @Override
89             public Image getImage(Object element) {
90                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
91                 return entry.error.severity == ErrorSeverity.ERROR
92                         ? imageRegistry.get("error")
93                         : imageRegistry.get("warning");
94             }
95         });
96         
97         column2 = new TableViewerColumn(tableViewer, SWT.NONE);
98         column2.getColumn().setText("Description");
99         column2.getColumn().setResizable(false);
100         column2.setLabelProvider(new ColumnLabelProvider() {
101             @Override
102             public String getText(Object element) {
103                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
104                 String description = entry.error.description; 
105                 int p = description.indexOf('\n');
106                 if(p == -1)
107                     return description;
108                 else
109                     return description.substring(0, p);
110             }
111             @Override
112             public String getToolTipText(Object element) {
113                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
114                 return entry.error.description;
115             }
116             @Override
117             public int getToolTipTimeDisplayed(Object object) {
118                 return 15000;
119             }
120             @Override
121             public int getToolTipDisplayDelayTime(Object object) {
122                 return 500;
123             }
124             @Override
125             public Point getToolTipShift(Object object) {
126                 return new Point(0, 0);
127             }
128         });
129         
130         ControlAdapter resizeListener = new ControlAdapter() {
131             @Override
132             public void controlResized(ControlEvent e) {
133                 resizeColumns();
134             }
135         };
136         parent.addControlListener(resizeListener);
137         column1.getColumn().addControlListener(resizeListener);
138
139         tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY);
140         tableViewer.addDoubleClickListener(new IDoubleClickListener() {
141             @Override
142             public void doubleClick(DoubleClickEvent event) {
143                 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
144                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)selection.getFirstElement();
145                 OpenSCLDefinition.openDefinition(entry.moduleName, entry.error.location);
146             }
147         });
148     }
149
150     private void resizeColumns() {
151         Table table = tableViewer.getTable();
152         Rectangle area = parent.getClientArea();
153         Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
154         ScrollBar vBar = table.getVerticalBar();
155         int width = area.width;
156         if (size.y > area.height + table.getHeaderHeight()) {
157             Point vBarSize = vBar.getSize();
158             width -= vBarSize.x;
159         }
160         Point oldSize = table.getSize();
161         if (oldSize.x > area.width) {
162             column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
163             table.setSize(area.width, area.height);
164         } else {
165             table.setSize(area.width, area.height);
166             column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
167         }
168     }
169
170     @Override
171     public void setFocus() {
172         tableViewer.getControl().setFocus();
173     }
174     
175     @Override
176     public void dispose() {
177         super.dispose();
178         issuesContentProvider.dispose();
179     }
180
181 }