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