1 package no.uio.ifi.refaktor.views;
4 import no.uio.ifi.refaktor.analyze.analyzers.PrefixViewAnalyzer;
5 import no.uio.ifi.refaktor.analyze.analyzers.SelectionValidator;
6 import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection;
7 import no.uio.ifi.refaktor.utils.DocumentUtils;
8 import no.uio.ifi.refaktor.utils.ParseUtils;
9 import no.uio.ifi.refaktor.utils.RefaktorHandleUtils;
11 import org.eclipse.jdt.core.ICompilationUnit;
12 import org.eclipse.jface.text.Document;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.ITextSelection;
15 import org.eclipse.jface.text.TextSelection;
16 import org.eclipse.jface.text.TextViewer;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.ui.IPartListener;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.part.ViewPart;
24 import org.eclipse.ui.texteditor.ITextEditor;
28 * This sample class demonstrates how to plug-in a new
29 * workbench view. The view shows data obtained from the
30 * model. The sample creates a dummy model on the fly,
31 * but a real implementation would connect to the model
32 * available either in this or another plug-in (e.g. the workspace).
33 * The view is connected to the model using a content provider.
35 * The view uses a label provider to define how model
36 * objects should be presented in the view. Each
37 * view can present the same model objects using
38 * different labels and icons, if needed. Alternatively,
39 * a single label provider can be shared between views
40 * in order to ensure that objects of the same type are
41 * presented in the same way everywhere.
43 * TODO: Open automatically instead of having to go to Show->View->...
46 public class PrefixView extends ViewPart implements ISelectionChangedListener, IPartListener {
49 * The ID of the view as specified by the extension.
51 public static final String ID = "no.uio.ifi.refaktor.views.PrefixView";
53 private TextViewer viewer;
55 private ITextEditor editor;
65 * This is a callback that will allow us
66 * to create the viewer and initialize it.
68 public void createPartControl(Composite parent) {
69 viewer = new TextViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
71 viewer.setDocument(doc);
72 viewer.setInput(getViewSite());
73 getSite().getPage().addPartListener(this);
77 * Passing the focus request to the viewer's control.
79 public void setFocus() {
80 viewer.getControl().setFocus();
84 public void partActivated(IWorkbenchPart part) {
85 if (!(part instanceof ITextEditor))
87 editor = (ITextEditor) part;
88 editor.getEditorSite().getSelectionProvider().addSelectionChangedListener(this);
92 public void partBroughtToTop(IWorkbenchPart part) { }
95 public void partClosed(IWorkbenchPart part) { }
98 public void partDeactivated(IWorkbenchPart part) { }
101 public void partOpened(IWorkbenchPart part) { }
104 public void selectionChanged(SelectionChangedEvent event) {
105 if (!(event.getSelection() instanceof TextSelection))
109 final TextSelection sel = (TextSelection) event.getSelection();
110 final IDocument document = DocumentUtils.getDocumentFrom(editor);
111 final ITextSelection strippedTextSelection;
113 strippedTextSelection = ParseUtils.stripTextSelection(document, sel);
114 if (strippedTextSelection == null)
116 } catch (RuntimeException r) {
118 doc.set(r.getMessage());
123 ICompilationUnit compilationUnit = RefaktorHandleUtils.getCompilationUnitFromEditor(editor);
124 CompilationUnitTextSelection compilationUnitTextSelection = new CompilationUnitTextSelection(compilationUnit, strippedTextSelection);
125 final PrefixViewAnalyzer analyzer = new PrefixViewAnalyzer(compilationUnitTextSelection);
126 SelectionValidator.checkIfSelectionIsValid(compilationUnitTextSelection);
128 String dialogText = analyzer.toString();
130 } catch (Throwable e) {
131 doc.set(e.getMessage());