(refs #7386) Minor SCL tools improvements
[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                         : entry.error.severity == ErrorSeverity.IMPORT_ERROR
94                         ? imageRegistry.get("import_error")
95                         : imageRegistry.get("warning");
96             }
97         });
98         
99         column2 = new TableViewerColumn(tableViewer, SWT.NONE);
100         column2.getColumn().setText("Description");
101         column2.getColumn().setResizable(false);
102         column2.setLabelProvider(new ColumnLabelProvider() {
103             @Override
104             public String getText(Object element) {
105                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
106                 String description = entry.error.description; 
107                 int p = description.indexOf('\n');
108                 if(p == -1)
109                     return description;
110                 else
111                     return description.substring(0, p);
112             }
113             @Override
114             public String getToolTipText(Object element) {
115                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
116                 return entry.error.description;
117             }
118             @Override
119             public int getToolTipTimeDisplayed(Object object) {
120                 return 15000;
121             }
122             @Override
123             public int getToolTipDisplayDelayTime(Object object) {
124                 return 500;
125             }
126             @Override
127             public Point getToolTipShift(Object object) {
128                 return new Point(0, 0);
129             }
130         });
131         
132         ControlAdapter resizeListener = new ControlAdapter() {
133             @Override
134             public void controlResized(ControlEvent e) {
135                 resizeColumns();
136             }
137         };
138         parent.addControlListener(resizeListener);
139         column1.getColumn().addControlListener(resizeListener);
140
141         tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY);
142         tableViewer.addDoubleClickListener(new IDoubleClickListener() {
143             @Override
144             public void doubleClick(DoubleClickEvent event) {
145                 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
146                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)selection.getFirstElement();
147                 OpenSCLDefinition.openDefinition(entry.moduleName, entry.error.location);
148             }
149         });
150     }
151
152     private void resizeColumns() {
153         Table table = tableViewer.getTable();
154         Rectangle area = parent.getClientArea();
155         Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
156         ScrollBar vBar = table.getVerticalBar();
157         int width = area.width;
158         if (size.y > area.height + table.getHeaderHeight()) {
159             Point vBarSize = vBar.getSize();
160             width -= vBarSize.x;
161         }
162         Point oldSize = table.getSize();
163         if (oldSize.x > area.width) {
164             column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
165             table.setSize(area.width, area.height);
166         } else {
167             table.setSize(area.width, area.height);
168             column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
169         }
170     }
171
172     @Override
173     public void setFocus() {
174         tableViewer.getControl().setFocus();
175     }
176     
177     @Override
178     public void dispose() {
179         super.dispose();
180         issuesContentProvider.dispose();
181     }
182
183 }