package no.uio.ifi.refaktor.views; import no.uio.ifi.refaktor.analyze.analyzers.PrefixViewAnalyzer; import no.uio.ifi.refaktor.analyze.collectors.SelectionValidator; import no.uio.ifi.refaktor.change.exceptions.RefaktorChangerException; import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection; import no.uio.ifi.refaktor.utils.DocumentUtils; import no.uio.ifi.refaktor.utils.ParseUtils; import no.uio.ifi.refaktor.utils.RefaktorHandleUtils; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.text.TextViewer; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IPartListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.part.ViewPart; import org.eclipse.ui.texteditor.ITextEditor; /** * This sample class demonstrates how to plug-in a new * workbench view. The view shows data obtained from the * model. The sample creates a dummy model on the fly, * but a real implementation would connect to the model * available either in this or another plug-in (e.g. the workspace). * The view is connected to the model using a content provider. *

* The view uses a label provider to define how model * objects should be presented in the view. Each * view can present the same model objects using * different labels and icons, if needed. Alternatively, * a single label provider can be shared between views * in order to ensure that objects of the same type are * presented in the same way everywhere. *

* TODO: Open automatically instead of having to go to Show->View->... */ public class PrefixView extends ViewPart implements ISelectionChangedListener, IPartListener { /** * The ID of the view as specified by the extension. */ public static final String ID = "no.uio.ifi.refaktor.views.PrefixView"; private TextViewer viewer; private ITextEditor editor; private Document doc; /** * The constructor. */ public PrefixView() { } /** * This is a callback that will allow us * to create the viewer and initialize it. */ public void createPartControl(Composite parent) { viewer = new TextViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); doc = new Document(); viewer.setDocument(doc); viewer.setInput(getViewSite()); getSite().getPage().addPartListener(this); } /** * Passing the focus request to the viewer's control. */ public void setFocus() { viewer.getControl().setFocus(); } @Override public void partActivated(IWorkbenchPart part) { if (!(part instanceof ITextEditor)) return; editor = (ITextEditor) part; editor.getEditorSite().getSelectionProvider().addSelectionChangedListener(this); } @Override public void partBroughtToTop(IWorkbenchPart part) { } @Override public void partClosed(IWorkbenchPart part) { } @Override public void partDeactivated(IWorkbenchPart part) { } @Override public void partOpened(IWorkbenchPart part) { } @Override public void selectionChanged(SelectionChangedEvent event) { if (!(event.getSelection() instanceof TextSelection)) return; if (editor == null) return; final TextSelection sel = (TextSelection) event.getSelection(); final IDocument document = DocumentUtils.getDocumentFrom(editor); final ITextSelection strippedTextSelection; try { strippedTextSelection = ParseUtils.stripTextSelection(document, sel); if (strippedTextSelection == null) return; } catch (RuntimeException r) { r.printStackTrace(); doc.set(r.getMessage()); return; } ICompilationUnit compilationUnit = RefaktorHandleUtils.getCompilationUnitFromEditor(editor); CompilationUnitTextSelection compilationUnitTextSelection = new CompilationUnitTextSelection(compilationUnit, strippedTextSelection); final PrefixViewAnalyzer analyzer = new PrefixViewAnalyzer(compilationUnitTextSelection); try { SelectionValidator.checkIfSelectionIsValid(compilationUnitTextSelection); analyzer.analyze(); String dialogText = analyzer.toString(); doc.set(dialogText); } catch (RefaktorChangerException e) { doc.set(e.getMessage()); } } }