1 package org.simantics.scl.ui.issues;
3 import java.util.Iterator;
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;
41 public class SCLIssuesView extends ViewPart {
43 private static final int COLUMN1_DEFAULT_WIDTH = 300;
44 private static final int COLUMN2_MIN_WIDTH = 100;
47 TableViewer tableViewer;
48 TableViewerColumn column1;
49 TableViewerColumn column2;
51 ImageRegistry imageRegistry;
53 SCLIssuesContentProvider issuesContentProvider = new SCLIssuesContentProvider();
57 public SCLIssuesView() {
59 imageRegistry = Activator.getInstance().getImageRegistry();
63 public void init(IViewSite site, IMemento memento)
64 throws PartInitException {
65 super.init(site, memento);
66 IAction action = new Action(Messages.SCLIssuesView_Refresh) {
69 tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY);
70 // issuesContentProvider.refresh()
73 action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh")); //$NON-NLS-1$
74 site.getActionBars().getToolBarManager().add(action);
78 public void createPartControl(Composite parent) {
79 this.clipboard = new Clipboard(parent.getDisplay());
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);
88 Table table = tableViewer.getTable();
89 table.setHeaderVisible(true);
90 table.setLinesVisible(true);
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() {
98 public String getText(Object element) {
99 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
100 return entry.moduleName;
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$
113 column2 = new TableViewerColumn(tableViewer, SWT.NONE);
114 column2.getColumn().setText(Messages.SCLIssuesView_Description);
115 column2.getColumn().setResizable(false);
116 column2.setLabelProvider(new ColumnLabelProvider() {
118 public String getText(Object element) {
119 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
120 String description = entry.error.description;
121 int p = description.indexOf('\n');
125 return description.substring(0, p);
128 public String getToolTipText(Object element) {
129 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element;
130 return entry.error.description;
133 public int getToolTipTimeDisplayed(Object object) {
137 public int getToolTipDisplayDelayTime(Object object) {
141 public Point getToolTipShift(Object object) {
142 return new Point(0, 0);
146 ControlAdapter resizeListener = new ControlAdapter() {
148 public void controlResized(ControlEvent e) {
152 parent.addControlListener(resizeListener);
153 column1.getColumn().addControlListener(resizeListener);
155 tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY);
156 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
158 public void doubleClick(DoubleClickEvent event) {
159 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
160 SCLIssuesTableEntry entry = (SCLIssuesTableEntry)selection.getFirstElement();
161 entry.openLocation();
165 IHandlerService serv = (IHandlerService) getSite().getService(IHandlerService.class);
166 serv.activateHandler(org.eclipse.ui.IWorkbenchCommandConstants.EDIT_COPY, new CopyHandler());
169 class CopyHandler extends AbstractHandler {
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');
179 clipboard.setContents(new Object[] { b.toString() }, new Transfer[] { TextTransfer.getInstance() });
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();
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);
200 table.setSize(area.width, area.height);
201 column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth()));
206 public void setFocus() {
207 tableViewer.getControl().setFocus();
211 public void dispose() {
213 issuesContentProvider.dispose();
214 if(clipboard != null) {