KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > resources > SootPartManager


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 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.resources;
21
22 import org.eclipse.ui.*;
23 import org.eclipse.core.resources.*;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.jdt.core.*;
26 import org.eclipse.jface.text.ITextOperationTarget;
27 import org.eclipse.jface.text.source.SourceViewer;
28 import org.eclipse.ui.*;
29 import org.eclipse.ui.texteditor.AbstractTextEditor;
30
31 import ca.mcgill.sable.soot.attributes.*;
32 import ca.mcgill.sable.soot.editors.JimpleEditor;
33 import ca.mcgill.sable.soot.ui.*;
34 import ca.mcgill.sable.soot.*;
35 import java.util.*;
36
37 public class SootPartManager {
38     
39     private boolean updateForOpen;
40     
41     public void updatePart(IEditorPart part){
42         
43         if (part == null) return;
44         
45         if (part instanceof JimpleEditor){
46             AbstractAttributesComputer aac = new JimpleAttributesComputer();
47             SootAttributesJimpleColorer sajc = new SootAttributesJimpleColorer();
48             SootAttrJimpleIconGenerator saji = new SootAttrJimpleIconGenerator();
49             
50             SourceViewer viewer = (SourceViewer)((AbstractTextEditor)part).getAdapter(ITextOperationTarget.class);
51             SootAttributesHandler handler = aac.getAttributesHandler((AbstractTextEditor)part);
52             if (handler != null){
53                 
54                 if (isUpdateForOpen() || handler.isUpdate()){
55                 
56                     sajc.setEditorPart(part);
57                     sajc.setViewer(viewer);
58                     sajc.setHandler(handler);
59                     Thread JavaDoc cThread = new Thread JavaDoc(sajc);
60                     cThread.start();
61                 
62                     
63                     saji.setHandler(handler);
64                     saji.setRec((IFile)aac.getRec());
65                     Thread JavaDoc iThread = new Thread JavaDoc(saji);
66                     iThread.start();
67                     handler.setUpdate(false);
68                 }
69             }
70             handleKeys(handler);
71             handleTypes(handler, (IFile)aac.getRec());
72         }
73         else if (part instanceof AbstractTextEditor){
74             
75             IEditorInput input= ((AbstractTextEditor)part).getEditorInput();
76             IJavaElement jElem = (IJavaElement) ((IAdaptable) input).getAdapter(IJavaElement.class);
77             if (!(jElem instanceof ICompilationUnit)) return;
78             AbstractAttributesComputer aac = new JavaAttributesComputer();
79             
80             SootAttributesJavaColorer sajc = new SootAttributesJavaColorer();
81             SootAttrJavaIconGenerator saji = new SootAttrJavaIconGenerator();
82             
83             SourceViewer viewer = (SourceViewer)((AbstractTextEditor)part).getAdapter(ITextOperationTarget.class);
84             SootAttributesHandler handler = aac.getAttributesHandler((AbstractTextEditor)part);
85             if (handler != null){
86                 if (isUpdateForOpen() || handler.isUpdate()){
87                     sajc.setEditorPart(part);
88                     sajc.setViewer(viewer);
89                     sajc.setHandler(handler);
90                     Thread JavaDoc cThread = new Thread JavaDoc(sajc);
91                     cThread.start();
92                 
93                     saji.setHandler(handler);
94                     saji.setRec((IFile)aac.getRec());
95                     Thread JavaDoc iThread = new Thread JavaDoc(saji);
96                     iThread.start();
97                     handler.setUpdate(false);
98                 }
99                 
100             }
101     
102             handleKeys(handler);
103             handleTypes(handler, (IFile)aac.getRec());
104         }
105         setUpdateForOpen(false);
106     }
107     
108     private void handleTypes(SootAttributesHandler handler, IFile file){
109         IWorkbenchPage page = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
110         ArrayList types = computeTypes(handler);
111         if (!types.isEmpty()){
112             IViewPart view = page.findView(ISootConstants.ANALYSIS_TYPES_VIEW_ID);
113             try {
114                 if (view == null) {
115                     IWorkbenchPart activePart = page.getActivePart();
116                     page.showView(ISootConstants.ANALYSIS_TYPES_VIEW_ID);
117                     //restore focus stolen by the creation of the console
118
IViewPart shownPart = page.findView(ISootConstants.ANALYSIS_TYPES_VIEW_ID);
119         
120                     if (shownPart != null){
121                         ((AnalysisTypeView)shownPart).setFile(file);
122                         ((AnalysisTypeView)shownPart).setAllTypesChecked(handler.isShowAllTypes());
123                         ((AnalysisTypeView)shownPart).setTypesChecked(handler.getTypesToShow());
124                         ((AnalysisTypeView)shownPart).setInputTypes(types);
125                     }
126                     page.activate(activePart);
127                 }
128                 else {
129                     if (view != null){
130                         ((AnalysisTypeView)view).setFile(file);
131                         ((AnalysisTypeView)view).setAllTypesChecked(handler.isShowAllTypes());
132                         ((AnalysisTypeView)view).setTypesChecked(handler.getTypesToShow());
133                         ((AnalysisTypeView)view).setInputTypes(types);
134                     }
135                     page.bringToTop(view);
136                 }
137             }
138             catch (PartInitException pie) {
139                 System.out.println(pie.getMessage());
140             }
141             
142         }
143     }
144     
145     private ArrayList computeTypes(SootAttributesHandler handler){
146         ArrayList types = new ArrayList();
147         if ((handler != null) && (handler.getAttrList() != null)){
148             Iterator attrsIt = handler.getAttrList().iterator();
149             while (attrsIt.hasNext()){
150                 SootAttribute sa = (SootAttribute)attrsIt.next();
151                 Iterator typesIt = sa.getAnalysisTypes().iterator();
152                 while (typesIt.hasNext()){
153                     String JavaDoc val = (String JavaDoc)typesIt.next();
154                     if (!types.contains(val)){
155                         types.add(val);
156                     }
157                 }
158             }
159             
160         }
161         return types;
162     }
163     
164     private void handleKeys(SootAttributesHandler handler){
165         // make a new view and put it in properties
166
// area (bring to top if necessary - make list of keys
167
IWorkbenchPage page = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
168         IViewPart viewPart = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ISootConstants.ANALYSIS_KEY_VIEW_ID);
169             try {
170                 if (viewPart == null) {
171                     IWorkbenchPart activePart = page.getActivePart();
172                     page.showView(ISootConstants.ANALYSIS_KEY_VIEW_ID);
173                     //restore focus stolen by the creation of the console
174
IViewPart shownPart = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ISootConstants.ANALYSIS_KEY_VIEW_ID);
175         
176                     if (shownPart != null){
177                         ((AnalysisKeyView)shownPart).setInputKeys(handler.getKeyList());
178                     }
179                     page.activate(activePart);
180                 }
181                 else {
182                     
183                     if (viewPart != null){
184                         ((AnalysisKeyView)viewPart).setInputKeys(handler.getKeyList());
185                     }
186                     page.bringToTop(viewPart);
187                 }
188             }
189             catch (PartInitException pie) {
190                 System.out.println(pie.getMessage());
191             }
192             if (viewPart != null){
193                 ((AnalysisKeyView)viewPart).setInputKeys(handler.getKeyList());
194             }
195     }
196     /**
197      * @return
198      */

199     public boolean isUpdateForOpen() {
200         return updateForOpen;
201     }
202
203     /**
204      * @param b
205      */

206     public void setUpdateForOpen(boolean b) {
207         updateForOpen = b;
208     }
209
210 }
211
Popular Tags