package org.simantics.scl.ui.issues; import java.util.Iterator; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.ColumnViewerToolTipSupport; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.events.ControlAdapter; import org.eclipse.swt.events.ControlEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.ScrollBar; import org.eclipse.swt.widgets.Table; import org.eclipse.ui.IMemento; import org.eclipse.ui.IViewSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.handlers.IHandlerService; import org.eclipse.ui.part.ViewPart; import org.simantics.scl.compiler.errors.ErrorSeverity; import org.simantics.scl.osgi.SCLOsgi; import org.simantics.scl.osgi.issues.SCLIssuesTableEntry; import org.simantics.scl.ui.Activator; public class SCLIssuesView extends ViewPart { private static final int COLUMN1_DEFAULT_WIDTH = 300; private static final int COLUMN2_MIN_WIDTH = 100; Composite parent; TableViewer tableViewer; TableViewerColumn column1; TableViewerColumn column2; ImageRegistry imageRegistry; SCLIssuesContentProvider issuesContentProvider = new SCLIssuesContentProvider(); Clipboard clipboard; public SCLIssuesView() { super(); imageRegistry = Activator.getInstance().getImageRegistry(); } @Override public void init(IViewSite site, IMemento memento) throws PartInitException { super.init(site, memento); IAction action = new Action(Messages.SCLIssuesView_Refresh) { @Override public void run() { tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY); // issuesContentProvider.refresh() } }; action.setImageDescriptor(imageRegistry.getDescriptor("arrow_refresh")); //$NON-NLS-1$ site.getActionBars().getToolBarManager().add(action); } @Override public void createPartControl(Composite parent) { this.clipboard = new Clipboard(parent.getDisplay()); this.parent = parent; parent.setLayout(new FillLayout()); tableViewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); ColumnViewerToolTipSupport.enableFor(tableViewer); tableViewer.setContentProvider(issuesContentProvider); Table table = tableViewer.getTable(); table.setHeaderVisible(true); table.setLinesVisible(true); column1 = new TableViewerColumn(tableViewer, SWT.NONE); column1.getColumn().setText(Messages.SCLIssuesView_Module); column1.getColumn().setWidth(COLUMN1_DEFAULT_WIDTH); column1.getColumn().setResizable(true); column1.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element; return entry.moduleName; } @Override public Image getImage(Object element) { SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element; return entry.error.severity == ErrorSeverity.ERROR ? imageRegistry.get("error") //$NON-NLS-1$ : entry.error.severity == ErrorSeverity.IMPORT_ERROR ? imageRegistry.get("import_error") //$NON-NLS-1$ : imageRegistry.get("warning"); //$NON-NLS-1$ } }); column2 = new TableViewerColumn(tableViewer, SWT.NONE); column2.getColumn().setText(Messages.SCLIssuesView_Description); column2.getColumn().setResizable(false); column2.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element; String description = entry.error.description; int p = description.indexOf('\n'); if(p == -1) return description; else return description.substring(0, p); } @Override public String getToolTipText(Object element) { SCLIssuesTableEntry entry = (SCLIssuesTableEntry)element; return entry.error.description; } @Override public int getToolTipTimeDisplayed(Object object) { return 15000; } @Override public int getToolTipDisplayDelayTime(Object object) { return 500; } @Override public Point getToolTipShift(Object object) { return new Point(0, 0); } }); ControlAdapter resizeListener = new ControlAdapter() { @Override public void controlResized(ControlEvent e) { resizeColumns(); } }; parent.addControlListener(resizeListener); column1.getColumn().addControlListener(resizeListener); tableViewer.setInput(SCLOsgi.MODULE_REPOSITORY); tableViewer.addDoubleClickListener(new IDoubleClickListener() { @Override public void doubleClick(DoubleClickEvent event) { IStructuredSelection selection = (IStructuredSelection)event.getSelection(); SCLIssuesTableEntry entry = (SCLIssuesTableEntry)selection.getFirstElement(); entry.openLocation(); } }); IHandlerService serv = (IHandlerService) getSite().getService(IHandlerService.class); serv.activateHandler(org.eclipse.ui.IWorkbenchCommandConstants.EDIT_COPY, new CopyHandler()); } class CopyHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { IStructuredSelection selection = tableViewer.getStructuredSelection(); if(!selection.isEmpty()) { StringBuilder b = new StringBuilder(); for(Iterator it=selection.iterator();it.hasNext();) { SCLIssuesTableEntry entry = (SCLIssuesTableEntry)it.next(); b.append(entry.moduleName).append('\t').append(entry.error.description).append('\n'); } clipboard.setContents(new Object[] { b.toString() }, new Transfer[] { TextTransfer.getInstance() }); } return null; } } private void resizeColumns() { Table table = tableViewer.getTable(); Rectangle area = parent.getClientArea(); Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT); ScrollBar vBar = table.getVerticalBar(); int width = area.width; if (size.y > area.height + table.getHeaderHeight()) { Point vBarSize = vBar.getSize(); width -= vBarSize.x; } Point oldSize = table.getSize(); if (oldSize.x > area.width) { column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth())); table.setSize(area.width, area.height); } else { table.setSize(area.width, area.height); column2.getColumn().setWidth(Math.max(COLUMN2_MIN_WIDTH, width - column1.getColumn().getWidth())); } } @Override public void setFocus() { tableViewer.getControl().setFocus(); } @Override public void dispose() { super.dispose(); issuesContentProvider.dispose(); if(clipboard != null) { clipboard.dispose(); clipboard = null; } } }