]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/views/PrefixView.java
f6fc95a2e49f63e67cffc395fd8a637bdf832324
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / views / PrefixView.java
1 package no.uio.ifi.refaktor.views;
2
3
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;
8
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;
22
23
24 /**
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.
31  * <p>
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.
39  * <p>
40  * TODO: Open automatically instead of having to go to Show->View->...
41  */
42
43 public class PrefixView extends ViewPart implements ISelectionChangedListener, IPartListener {
44
45         /**
46          * The ID of the view as specified by the extension.
47          */
48         public static final String ID = "no.uio.ifi.refaktor.views.PrefixView";
49
50         private TextViewer viewer;
51
52         private ITextEditor editor;
53         private Document doc;
54
55         /**
56          * The constructor.
57          */
58         public PrefixView() {
59         }
60
61         /**
62          * This is a callback that will allow us
63          * to create the viewer and initialize it.
64          */
65         public void createPartControl(Composite parent) {
66                 viewer = new TextViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
67                 doc = new Document();
68                 viewer.setDocument(doc);
69                 viewer.setInput(getViewSite());
70                 getSite().getPage().addPartListener(this);
71         }
72
73         /**
74          * Passing the focus request to the viewer's control.
75          */
76         public void setFocus() {
77                 viewer.getControl().setFocus();
78         }
79
80         @Override
81         public void partActivated(IWorkbenchPart part) {
82                 if (!(part instanceof ITextEditor))
83                         return;
84                 editor = (ITextEditor) part;
85                 editor.getEditorSite().getSelectionProvider().addSelectionChangedListener(this);
86         }
87
88         @Override
89         public void partBroughtToTop(IWorkbenchPart part) {     }
90
91         @Override
92         public void partClosed(IWorkbenchPart part) {   }
93
94         @Override
95         public void partDeactivated(IWorkbenchPart part) {      }
96
97         @Override
98         public void partOpened(IWorkbenchPart part) {   }
99
100         @Override
101         public void selectionChanged(SelectionChangedEvent event) {
102                 if (!(event.getSelection() instanceof TextSelection))
103                         return;
104                 if (editor == null)
105                         return;
106                 final TextSelection sel = (TextSelection) event.getSelection();
107                 final IDocument document = DocumentUtils.getDocumentFrom(editor);
108                 final ITextSelection strippedTextSelection;
109                 try {
110                         strippedTextSelection = ParseUtils.stripTextSelection(document, sel);
111                         if (strippedTextSelection == null)
112                                 return;
113                 } catch (RuntimeException r) {
114                         r.printStackTrace();
115                         doc.set(r.getMessage());
116                         return;
117                 }
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();
123                         doc.set(dialogText);
124                 } else
125                         doc.set("invalid selection");
126         }
127
128 }