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