KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > attributes > SootAttributeSelectAction


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package ca.mcgill.sable.soot.attributes;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import org.eclipse.core.resources.*;
26 import org.eclipse.core.runtime.*;
27
28 import org.eclipse.jdt.core.*;
29 import org.eclipse.jface.text.*;
30 import org.eclipse.jface.text.source.*;
31 import org.eclipse.swt.graphics.Rectangle;
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IMarkerResolution;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.texteditor.*;
37
38
39 import ca.mcgill.sable.soot.editors.JimpleEditor;
40 import ca.mcgill.sable.soot.ui.PopupListSelector;
41 import ca.mcgill.sable.soot.*;
42
43 public abstract class SootAttributeSelectAction extends ResourceAction {
44
45     AbstractTextEditor editor;
46     AbstractTextEditor linkToEditor;
47     IVerticalRulerInfo rulerInfo;
48     AbstractMarkerAnnotationModel model;
49     int lineNumber;
50     
51     /**
52      * @param bundle
53      * @param prefix
54      */

55     public SootAttributeSelectAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, IVerticalRulerInfo rulerInfo) {
56         
57         super(bundle, prefix);
58         setEditor((AbstractTextEditor)editor);
59         
60         setRulerInfo(rulerInfo);
61     }
62     
63     
64     public IResource getResource(AbstractTextEditor textEditor) {
65         IEditorInput input= textEditor.getEditorInput();
66         return (IResource) ((IAdaptable) input).getAdapter(IResource.class);
67     }
68     
69     protected IDocument getDocument() {
70         IDocumentProvider provider= getEditor().getDocumentProvider();
71         return provider.getDocument(getEditor().getEditorInput());
72     }
73     
74     public void run() {
75     
76         // need to get list of texts
77
IAnnotationModel model = getEditor().getDocumentProvider().getAnnotationModel(getEditor().getEditorInput());
78         if (model instanceof AbstractMarkerAnnotationModel){
79             setModel((AbstractMarkerAnnotationModel)model);
80         }
81         
82         int markerLine = getRulerInfo().getLineOfLastMouseButtonActivity();
83         IResource rec = getResource(getEditor());
84         try {
85             IMarker [] markers = rec.findMarkers("ca.mcgill.sable.soot.sootattributemarker", true, IResource.DEPTH_INFINITE);
86             for (int i = 0; i < markers.length; i++){
87                 if (getModel().getMarkerPosition(markers[i]) == null) continue;
88                 setLineNumber(getDocument().getLineOfOffset(getModel().getMarkerPosition(markers[i]).getOffset()));
89   
90                 
91                 if (getLineNumber() == markerLine){
92                     
93                     ArrayList JavaDoc links = getMarkerLinks();
94                     Iterator JavaDoc lit = links.iterator();
95                     String JavaDoc [] list = getMarkerLabels(links);
96                     if ((list == null) || (list.length == 0)) {
97                         // show no links
98
}
99                     else {
100                         IWorkbenchWindow window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getWorkbenchWindow();
101                         
102                         PopupListSelector popup = new PopupListSelector(window.getShell());
103                         popup.setItems(list);
104                         
105                         int listWidth = getListWidth(list);
106                         
107                         if (getEditor() instanceof JimpleEditor){
108                             int topIndex = ((JimpleEditor)getEditor()).getViewer().getTopIndex();
109                             Rectangle rect = new Rectangle(320, (getLineNumber()+1-topIndex), listWidth, 45 );
110                             
111                             popup.open(rect);
112                         }
113                         else {
114                             int topIndex = ((ITextViewer)((AbstractTextEditor)getEditor()).getAdapter(ITextOperationTarget.class)).getTopIndex();
115                             int pos = getModel().getMarkerPosition(markers[i]).getOffset();
116                             pos = pos / getLineNumber();
117                             Rectangle rect = new Rectangle(320, getLineNumber()+1-topIndex, listWidth, 45 );
118                             popup.open(rect);
119
120                         }
121                         
122                         handleSelection(popup.getSelected(), links);
123                     }
124                 }
125             }
126         }
127         catch(CoreException e){
128             
129         }
130         catch (BadLocationException e1){
131         }
132         
133     }
134     
135     public int getListWidth(String JavaDoc[] list){
136         int width = 0;
137         for (int i = 0; i < list.length; i++){
138         
139             String JavaDoc next = list[i];
140             width = next.length() > width ? next.length() : width;
141         }
142         
143         return width * 6;
144     }
145     
146     public void handleSelection(String JavaDoc selected, ArrayList JavaDoc links){
147         if (selected == null) return;
148         try {
149             int toShow = 0;
150             String JavaDoc className;
151             Iterator JavaDoc it = links.iterator();
152             while (it.hasNext()){
153                 LinkAttribute la = (LinkAttribute)it.next();
154                 if (la.getLabel().equals(selected)){
155                     toShow = getLinkLine(la) - 1;//.getJimpleLink() - 1;
156

157                     className = la.getClassName();
158                     findClass(className);
159                 }
160             }
161             int selOffset = getLinkToEditor().getDocumentProvider().getDocument(getLinkToEditor().getEditorInput()).getLineOffset(toShow);
162             if ((selOffset != -1) && (selOffset != 0)){
163                 
164                 if (getLinkToEditor() instanceof JimpleEditor){
165                     
166                     ((JimpleEditor)getLinkToEditor()).getViewer().setRangeIndication(selOffset, 1, true);
167                     SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(getLinkToEditor());
168                     
169                 }
170                 else {
171                     SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(getLinkToEditor());
172                     ((AbstractTextEditor)SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()).selectAndReveal(selOffset, 0);
173                     ((AbstractTextEditor)SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()).setHighlightRange(selOffset, 1, true);
174                     
175                 }
176             }
177         }
178         catch(BadLocationException e){
179             System.out.println(e.getMessage());
180         }
181     }
182     
183     protected abstract int getLinkLine(LinkAttribute la);
184     
185     public abstract void findClass(String JavaDoc className);
186
187     
188     public String JavaDoc removeExt(String JavaDoc fileName){
189         return fileName.substring(0, fileName.lastIndexOf("."));
190     }
191     
192     public abstract ArrayList JavaDoc getMarkerLinks();
193     
194     public String JavaDoc [] getMarkerLabels(ArrayList JavaDoc links){
195         
196         if ((links == null) || (links.size() == 0)) return null;
197         ArrayList JavaDoc list = new ArrayList JavaDoc();
198         String JavaDoc [] attributeTexts = new String JavaDoc[links.size()];
199         Iterator JavaDoc it = links.iterator();
200         while (it.hasNext()){
201             list.add(((LinkAttribute)it.next()).getLabel());
202         }
203         list.toArray(attributeTexts);
204         return attributeTexts;
205     
206     }
207     
208     public String JavaDoc [] fixLabels(String JavaDoc [] at){
209         for (int i = 0; i < at.length; i++){
210             String JavaDoc temp = at[i];
211             temp.replaceAll("&lt;", "<");
212             temp.replaceAll("&gt;", ">");
213             at[i] = temp;
214         }
215         return at;
216     }
217     
218     public void getMarkerResolutions(IMarker marker){
219         
220         SootAttributeResolutionGenerator sarg = new SootAttributeResolutionGenerator();
221         if (sarg.hasResolutions(marker)){
222             IMarkerResolution [] res = sarg.getResolutions(marker);
223             for (int i = 0; i < res.length; i++){
224                 //System.out.println("res: "+res[i].getLabel());
225
}
226         }
227     }
228
229     /**
230      * @return
231      */

232     public AbstractTextEditor getEditor() {
233         return editor;
234     }
235
236     /**
237      * @param editor
238      */

239     public void setEditor(AbstractTextEditor editor) {
240         this.editor = editor;
241     }
242
243     /**
244      * @return
245      */

246     public IVerticalRulerInfo getRulerInfo() {
247         return rulerInfo;
248     }
249
250     /**
251      * @param info
252      */

253     public void setRulerInfo(IVerticalRulerInfo info) {
254         rulerInfo = info;
255     }
256
257     /**
258      * @return
259      */

260     public AbstractMarkerAnnotationModel getModel() {
261         return model;
262     }
263
264     /**
265      * @param model
266      */

267     public void setModel(AbstractMarkerAnnotationModel model) {
268         this.model = model;
269     }
270
271     /**
272      * @return
273      */

274     public int getLineNumber() {
275         return lineNumber;
276     }
277
278     /**
279      * @param i
280      */

281     public void setLineNumber(int i) {
282         lineNumber = i;
283     }
284
285     /**
286      * @return
287      */

288     public AbstractTextEditor getLinkToEditor() {
289         return linkToEditor;
290     }
291
292     /**
293      * @param editor
294      */

295     public void setLinkToEditor(AbstractTextEditor editor) {
296         linkToEditor = editor;
297     }
298
299 }
300
Popular Tags