1 package no.uio.ifi.refaktor.views;
4 import no.uio.ifi.refaktor.extractors.UnionOfLongestCommonPrefixesExtractor;
5 import no.uio.ifi.refaktor.utils.DocumentUtils;
6 import no.uio.ifi.refaktor.utils.ParseUtils;
7 import no.uio.ifi.refaktor.utils.SmartTextSelection;
9 import org.eclipse.jface.text.Document;
10 import org.eclipse.jface.text.IDocument;
11 import org.eclipse.jface.text.ITextSelection;
12 import org.eclipse.jface.text.TextSelection;
13 import org.eclipse.jface.text.TextViewer;
14 import org.eclipse.jface.viewers.ISelectionChangedListener;
15 import org.eclipse.jface.viewers.SelectionChangedEvent;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.ui.IPartListener;
19 import org.eclipse.ui.IWorkbenchPart;
20 import org.eclipse.ui.part.ViewPart;
21 import org.eclipse.ui.texteditor.ITextEditor;
25 * This sample class demonstrates how to plug-in a new
26 * workbench view. The view shows data obtained from the
27 * model. The sample creates a dummy model on the fly,
28 * but a real implementation would connect to the model
29 * available either in this or another plug-in (e.g. the workspace).
30 * The view is connected to the model using a content provider.
32 * The view uses a label provider to define how model
33 * objects should be presented in the view. Each
34 * view can present the same model objects using
35 * different labels and icons, if needed. Alternatively,
36 * a single label provider can be shared between views
37 * in order to ensure that objects of the same type are
38 * presented in the same way everywhere.
40 * TODO: Open automatically instead of having to go to Show->View->...
43 public class PrefixView extends ViewPart implements ISelectionChangedListener, IPartListener {
46 * The ID of the view as specified by the extension.
48 public static final String ID = "no.uio.ifi.refaktor.views.PrefixView";
50 private TextViewer viewer;
52 private ITextEditor editor;
62 * This is a callback that will allow us
63 * to create the viewer and initialize it.
65 public void createPartControl(Composite parent) {
66 viewer = new TextViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
68 viewer.setDocument(doc);
69 viewer.setInput(getViewSite());
70 getSite().getPage().addPartListener(this);
74 * Passing the focus request to the viewer's control.
76 public void setFocus() {
77 viewer.getControl().setFocus();
81 public void partActivated(IWorkbenchPart part) {
82 if (!(part instanceof ITextEditor))
84 editor = (ITextEditor) part;
85 editor.getEditorSite().getSelectionProvider().addSelectionChangedListener(this);
89 public void partBroughtToTop(IWorkbenchPart part) { }
92 public void partClosed(IWorkbenchPart part) { }
95 public void partDeactivated(IWorkbenchPart part) { }
98 public void partOpened(IWorkbenchPart part) { }
101 public void selectionChanged(SelectionChangedEvent event) {
102 if (!(event.getSelection() instanceof TextSelection))
106 final TextSelection sel = (TextSelection) event.getSelection();
107 final IDocument document = DocumentUtils.getDocumentFrom(editor);
108 final ITextSelection strippedTextSelection;
110 strippedTextSelection = ParseUtils.stripTextSelection(document, sel);
111 if (strippedTextSelection == null)
113 } catch (RuntimeException r) {
115 doc.set(r.getMessage());
118 SmartTextSelection smartTextSelection = new SmartTextSelection(document, strippedTextSelection);
119 final UnionOfLongestCommonPrefixesExtractor extractor = new UnionOfLongestCommonPrefixesExtractor(smartTextSelection);
120 if (extractor.selectionIsValid()) {
121 extractor.extractProperty();
122 String dialogText = extractor.toString();
125 doc.set("invalid selection");