]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/issues/SCLIssuesView.java
Prime SCL BindingRegistry to shave ~0.5s from startup
[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 java.util.Iterator;
4
5 import org.eclipse.core.commands.AbstractHandler;
6 import org.eclipse.core.commands.ExecutionEvent;
7 import org.eclipse.core.commands.ExecutionException;
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.IAction;
10 import org.eclipse.jface.resource.ImageRegistry;
11 import org.eclipse.jface.viewers.ColumnLabelProvider;
12 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
13 import org.eclipse.jface.viewers.DoubleClickEvent;
14 import org.eclipse.jface.viewers.IDoubleClickListener;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.TableViewer;
17 import org.eclipse.jface.viewers.TableViewerColumn;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.dnd.Clipboard;
20 import org.eclipse.swt.dnd.TextTransfer;
21 import org.eclipse.swt.dnd.Transfer;
22 import org.eclipse.swt.events.ControlAdapter;
23 import org.eclipse.swt.events.ControlEvent;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.layout.FillLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.ScrollBar;
30 import org.eclipse.swt.widgets.Table;
31 import org.eclipse.ui.IMemento;
32 import org.eclipse.ui.IViewSite;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.handlers.IHandlerService;
35 import org.eclipse.ui.part.ViewPart;
36 import org.simantics.scl.compiler.errors.ErrorSeverity;
37 import org.simantics.scl.osgi.SCLOsgi;
38 import org.simantics.scl.osgi.issues.SCLIssuesTableEntry;
39 import org.simantics.scl.ui.Activator;
40
41 public class SCLIssuesView extends ViewPart {
42
43     private static final int COLUMN1_DEFAULT_WIDTH = 300;
44     private static final int COLUMN2_MIN_WIDTH = 100;
45     
46     Composite parent;
47     TableViewer tableViewer;
48     TableViewerColumn column1;
49     TableViewerColumn column2;
50
51     ImageRegistry imageRegistry;
52     
53     SCLIssuesContentProvider issuesContentProvider = new SCLIssuesContentProvider();
54     
55     Clipboard clipboard;
56     
57     public SCLIssuesView() {
58         super();
59         imageRegistry = Activator.getInstance().getImageRegistry();
60     }
61     
62     @Override
63     public void init(IViewSite site, IMemento memento)
64             throws PartInitException {
65         super.init(site, memento);
66         IAction action = new Action(Messages.SCLIssuesView_Refresh) {
67             @Override
68             public void run() {
69                 tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY);
70 //                issuesContentProvider.refresh()
71             }
72         };
73         action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh")); //$NON-NLS-1$
74         site.getActionBars().getToolBarManager().add(action);
75     }
76     
77     @Override
78     public void createPartControl(Composite parent) {
79         this.clipboard = new Clipboard(parent.getDisplay());
80         
81         this.parent = parent;
82         parent.setLayout(new FillLayout());
83         tableViewer = new TableViewer(parent,
84                 SWT.FULL_SELECTION | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
85         ColumnViewerToolTipSupport.enableFor(tableViewer);
86         tableViewer.setContentProvider(issuesContentProvider);
87
88         Table table = tableViewer.getTable();
89         table.setHeaderVisible(true);
90         table.setLinesVisible(true);
91
92         column1 = new TableViewerColumn(tableViewer, SWT.NONE);
93         column1.getColumn().setText(Messages.SCLIssuesView_Module);
94         column1.getColumn().setWidth(COLUMN1_DEFAULT_WIDTH);
95         column1.getColumn().setResizable(true);
96         column1.setLabelProvider(new ColumnLabelProvider() {
97             @Override
98             public String getText(Object element) {
99                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
100                 return entry.moduleName;
101             }
102             @Override
103             public Image getImage(Object element) {
104                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
105                 return entry.error.severity == ErrorSeverity.ERROR
106                         ? imageRegistry.get("error") //$NON-NLS-1$
107                         : entry.error.severity == ErrorSeverity.IMPORT_ERROR
108                         ? imageRegistry.get("import_error") //$NON-NLS-1$
109                         : imageRegistry.get("warning"); //$NON-NLS-1$
110             }
111         });
112         
113         column2 = new TableViewerColumn(tableViewer, SWT.NONE);
114         column2.getColumn().setText(Messages.SCLIssuesView_Description);
115         column2.getColumn().setResizable(false);
116         column2.setLabelProvider(new ColumnLabelProvider() {
117             @Override
118             public String getText(Object element) {
119                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
120                 String description = entry.error.description; 
121                 int p = description.indexOf('\n');
122                 if(p == -1)
123                     return description;
124                 else
125                     return description.substring(0, p);
126             }
127             @Override
128             public String getToolTipText(Object element) {
129                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
130                 return entry.error.description;
131             }
132             @Override
133             public int getToolTipTimeDisplayed(Object object) {
134                 return 15000;
135             }
136             @Override
137             public int getToolTipDisplayDelayTime(Object object) {
138                 return 500;
139             }
140             @Override
141             public Point getToolTipShift(Object object) {
142                 return new Point(0, 0);
143             }
144         });
145         
146         ControlAdapter resizeListener = new ControlAdapter() {
147             @Override
148             public void controlResized(ControlEvent e) {
149                 resizeColumns();
150             }
151         };
152         parent.addControlListener(resizeListener);
153         column1.getColumn().addControlListener(resizeListener);
154
155         tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY);
156         tableViewer.addDoubleClickListener(new IDoubleClickListener() {
157             @Override
158             public void doubleClick(DoubleClickEvent event) {
159                 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
160                 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)selection.getFirstElement();
161                 entry.openLocation();
162             }
163         });
164         
165         IHandlerService serv = (IHandlerService) getSite().getService(IHandlerService.class);
166         serv.activateHandler(org.eclipse.ui.IWorkbenchCommandConstants.EDIT_COPY, new CopyHandler());
167     }
168     
169     class CopyHandler extends AbstractHandler {
170         @Override
171         public Object execute(ExecutionEvent event) throws ExecutionException {
172             IStructuredSelection selection = tableViewer.getStructuredSelection();
173             if(!selection.isEmpty()) {
174                 StringBuilder b = new StringBuilder();
175                 for(Iterator it=selection.iterator();it.hasNext();) {
176                     SCLIssuesTableEntry entry = (SCLIssuesTableEntry)it.next();
177                     b.append(entry.moduleName).append('\t').append(entry.error.description).append('\n');
178                 }
179                 clipboard.setContents(new Object[] { b.toString() }, new Transfer[] { TextTransfer.getInstance() });
180             }
181             return null;
182         }
183     }
184
185     private void resizeColumns() {
186         Table table = tableViewer.getTable();
187         Rectangle area = parent.getClientArea();
188         Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
189         ScrollBar vBar = table.getVerticalBar();
190         int width = area.width;
191         if (size.y > area.height + table.getHeaderHeight()) {
192             Point vBarSize = vBar.getSize();
193             width -= vBarSize.x;
194         }
195         Point oldSize = table.getSize();
196         if (oldSize.x > area.width) {
197             column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
198             table.setSize(area.width, area.height);
199         } else {
200             table.setSize(area.width, area.height);
201             column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
202         }
203     }
204
205     @Override
206     public void setFocus() {
207         tableViewer.getControl().setFocus();
208     }
209     
210     @Override
211     public void dispose() {
212         super.dispose();
213         issuesContentProvider.dispose();
214         if(clipboard != null) {
215             clipboard.dispose();
216             clipboard = null;
217         }
218     }
219
220 }