KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > ui > AnalysisVisManipDialog


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.ui;
21
22 import org.eclipse.jface.dialogs.TitleAreaDialog;
23 import org.eclipse.jface.viewers.*;
24 import org.eclipse.swt.widgets.*;
25 import org.eclipse.swt.events.*;
26 import org.eclipse.swt.layout.*;
27 import org.eclipse.swt.custom.*;
28 import org.eclipse.swt.*;
29 import java.util.*;
30 import org.eclipse.core.resources.*;
31 import ca.mcgill.sable.soot.attributes.*;
32
33 public class AnalysisVisManipDialog
34     extends TitleAreaDialog implements ISelectionChangedListener {
35
36     private HashMap dataMap;
37     private ArrayList fileList;
38     private IProject proj;
39     private ArrayList allSelected;
40     private HashMap currentSettingsMap;
41     
42     
43     /**
44      * @param parentShell
45      */

46     public AnalysisVisManipDialog(Shell parentShell) {
47         super(parentShell);
48     }
49
50     
51     
52     protected void configureShell(Shell shell){
53         super.configureShell(shell);
54         shell.setText(Messages.getString("AnalysisVisManDialog.Title"));
55     }
56     
57     public void setDataMap(HashMap map){
58         dataMap = map;
59     }
60     
61     protected Control createDialogArea(Composite parent) {
62         GridData gd;
63         
64         Composite dialogComp = (Composite)super.createDialogArea(parent);
65         Composite topComp = new Composite(dialogComp, SWT.NONE);
66         gd = new GridData(GridData.FILL_BOTH);
67         topComp.setLayoutData(gd);
68         GridLayout topLayout = new GridLayout();
69         topLayout.numColumns = 2;
70         topComp.setLayout(topLayout);
71         
72         setTitle("Project: "+getProj().getName());
73         setMessage("");
74         
75         TabFolder tabFolder = new TabFolder(topComp, SWT.NONE);
76         gd = new GridData(GridData.FILL_BOTH);
77         tabFolder.setLayoutData(gd);
78         
79         TabItem singleFileItem = new TabItem(tabFolder, SWT.NONE);
80         singleFileItem.setText("By File");
81         TabItem globalItem = new TabItem(tabFolder, SWT.NONE);
82         globalItem.setText("By Project");
83         
84         /* create single file tab */
85         SashForm sash = new SashForm(tabFolder, SWT.NONE);
86         singleFileItem.setControl(sash);
87         sash.setOrientation(SWT.HORIZONTAL);
88         
89         gd = new GridData(GridData.FILL_BOTH);
90         sash.setLayoutData(gd);
91         
92         Composite selection = createSelectionArea(sash);
93         gd = new GridData(GridData.FILL_VERTICAL);
94         gd.horizontalSpan = 1;
95         
96         selection.setLayoutData(gd);
97         
98         Composite types = createCheckArea(sash);
99         gd = new GridData(GridData.FILL_VERTICAL);
100         gd.horizontalSpan = 1;
101         
102         types.setLayoutData(gd);
103         
104         /* create global tab */
105         Composite global = new Composite(tabFolder, SWT.NONE);
106         GridLayout globalLayout = new GridLayout();
107         
108         global.setLayout(globalLayout);
109         
110         Table allTable = new Table(global, SWT.CHECK);
111         TableViewer allTypesList = new TableViewer(allTable);
112         allTypesList.setContentProvider(new ArrayContentProvider());
113         allTypesList.setLabelProvider(new LabelProvider());
114         
115         gd = new GridData(GridData.FILL_BOTH);
116         allTypesList.getControl().setLayoutData(gd);
117         
118         Composite buttonPanel = new Composite(global, SWT.NONE);
119         GridLayout bpLayout = new GridLayout();
120         bpLayout.numColumns = 2;
121         buttonPanel.setLayout(bpLayout);
122         
123         Button selectAll = new Button(buttonPanel, SWT.PUSH);
124         selectAll.setText("Select All");
125         gd = new GridData();
126         gd.horizontalSpan = 1;
127         selectAll.setLayoutData(gd);
128         
129         Button deselectAll = new Button(buttonPanel, SWT.PUSH);
130         deselectAll.setText("De-select All");
131         gd = new GridData();
132         gd.horizontalSpan = 1;
133         deselectAll.setLayoutData(gd);
134         
135         globalItem.setControl(global);
136         
137         return dialogComp;
138     }
139     
140     private CheckboxTableViewer checkTypes;
141     
142     private Composite createCheckArea(Composite parent){
143         Composite comp = new Composite(parent, SWT.NONE);
144         GridLayout layout = new GridLayout();
145     
146         layout.numColumns = 1;
147     
148         
149         comp.setLayout(layout);
150         
151         GridData gd = new GridData();
152         Table table = new Table(comp, SWT.CHECK);
153         checkTypes = new CheckboxTableViewer(table);
154         
155         gd = new GridData(GridData.FILL_BOTH);
156     
157         checkTypes.getControl().setLayoutData(gd);
158         
159         checkTypes.setContentProvider(new ArrayContentProvider());
160         checkTypes.setLabelProvider(new LabelProvider());
161         return comp;
162     }
163     
164
165     
166     private Composite createSelectionArea(Composite parent) {
167         Composite comp = new Composite(parent, SWT.NONE);
168         
169         GridLayout layout = new GridLayout();
170     
171         layout.numColumns = 1;
172     
173         
174         comp.setLayout(layout);
175         
176         GridData gd = new GridData();
177         
178         TreeViewer files = new TreeViewer(comp);
179         gd = new GridData(GridData.FILL_BOTH);
180     
181         files.getControl().setLayoutData(gd);
182         
183         files.setContentProvider(new VisManContentProvider());
184         files.setLabelProvider(new VisManLabelProvider());
185         files.setInput(getInitialInput());
186     
187         files.addSelectionChangedListener(this);
188     
189         
190         files.getControl().addKeyListener(new KeyAdapter() {
191             public void keyPressed(KeyEvent e) {
192                 handleKeyPressed(e);
193             }
194         });
195          
196
197         return comp;
198     }
199     
200
201
202     public void selectionChanged(SelectionChangedEvent event) {
203         IStructuredSelection selection = (IStructuredSelection)event.getSelection();
204         if (selection.isEmpty()) {
205             checkTypes.setInput(getCheckInput(""));
206         }
207         else {
208             Object JavaDoc elem = selection.getFirstElement();
209             if (!(elem instanceof IFile)) return;
210             ArrayList list = (ArrayList)getCheckInput(elem);
211             handleLast();
212             checkTypes.setInput(list);
213             Object JavaDoc [] checkElems;
214             if ((currentSettingsMap != null) && (currentSettingsMap.containsKey(elem))){
215                 checkElems = (Object JavaDoc [])currentSettingsMap.get(elem);
216                 checkTypes.setCheckedElements(checkElems);
217             }
218             else {
219                 SootAttributesHandler handler = getHandlerForFile((IFile)elem);
220                 if (handler != null) {
221                     if (handler.isShowAllTypes()){
222                         if (list != null){
223                             Object JavaDoc [] elems = new Object JavaDoc[list.size()];
224                 
225                             for (int i = 0; i < list.size(); i++){
226                                 elems[i] = checkTypes.getElementAt(i);
227                     
228                             }
229                             checkTypes.setCheckedElements(elems);
230                         }
231                     }
232                     else {
233                         Iterator it = handler.getTypesToShow().iterator();
234                         Object JavaDoc [] elems = new Object JavaDoc[handler.getTypesToShow().size()];
235                         int i = 0;
236                         while (it.hasNext()){
237                             elems[i] = it.next();
238                             i++;
239                         }
240                         checkTypes.setCheckedElements(elems);
241                         
242                         
243                     }
244                 }
245             }
246             if (getAllSelected() == null){
247                 setAllSelected(new ArrayList());
248             }
249             getAllSelected().add(elem);
250         }
251     }
252     
253     protected void okPressed(){
254         handleLast();
255         super.okPressed();
256     }
257     
258     private void handleLast(){
259         if (getAllSelected() == null) return;
260         Object JavaDoc lastElem = getAllSelected().get(getAllSelected().size()-1);
261         if (lastElem != null){
262             Object JavaDoc [] checkElems = checkTypes.getCheckedElements();
263             if (currentSettingsMap == null){
264                 currentSettingsMap = new HashMap();
265             }
266             currentSettingsMap.put(lastElem, checkElems);
267             
268         }
269     }
270     
271     private IContainer getInitialInput(){
272         IContainer proj = getProj();
273         return proj;
274     }
275     
276     private SootAttributesHandler getHandlerForFile(IFile next){
277         SootAttributesHandler handler = null;
278         if (next.getFileExtension().equals("java")){
279             JavaAttributesComputer jac = new JavaAttributesComputer();
280             jac.setProj(getProj());
281             jac.setRec(next);
282             handler = jac.getAttributesHandler(next);
283         }
284         else {
285             JimpleAttributesComputer jac = new JimpleAttributesComputer();
286             jac.setProj(getProj());
287             jac.setRec(next);
288             handler = jac.getAttributesHandler(next);
289         }
290         return handler;
291     }
292     
293     private ArrayList getAllTypesInput(){
294         ArrayList allTypes = new ArrayList();
295         Iterator it = getFileList().iterator();
296         while (it.hasNext()){
297             SootAttributesHandler handler = getHandlerForFile((IFile)it.next());
298             if ((handler != null) && (handler.getAttrList() != null)){
299                 Iterator attrsIt = handler.getAttrList().iterator();
300             
301                 while (attrsIt.hasNext()){
302                     SootAttribute sa = (SootAttribute)attrsIt.next();
303                     Iterator typesIt = sa.getAnalysisTypes().iterator();
304                     while (typesIt.hasNext()){
305                         String JavaDoc val = (String JavaDoc)typesIt.next();
306                         if (!allTypes.contains(val)){
307                             allTypes.add(val);
308                         }
309                     }
310                 }
311             }
312             
313         }
314         
315         return allTypes;
316     }
317     
318     private ArrayList getCheckInput(Object JavaDoc key){
319         ArrayList list = new ArrayList();
320         if (!(key instanceof IFile)) return list;
321         IFile next = (IFile)key;
322         SootAttributesHandler handler = getHandlerForFile(next);
323         
324         if ((handler != null) && (handler.getAttrList() != null)){
325             Iterator attrsIt = handler.getAttrList().iterator();
326             ArrayList types = new ArrayList();
327             while (attrsIt.hasNext()){
328                 SootAttribute sa = (SootAttribute)attrsIt.next();
329                 Iterator typesIt = sa.getAnalysisTypes().iterator();
330                 while (typesIt.hasNext()){
331                     String JavaDoc val = (String JavaDoc)typesIt.next();
332                     if (!types.contains(val)){
333                         types.add(val);
334                     }
335                 }
336             }
337             return types;
338         }
339         else {
340             return null;
341         }
342     }
343     
344     private void handleKeyPressed(KeyEvent e){
345     }
346     
347     /**
348      * @return
349      */

350     public ArrayList getFileList() {
351         return fileList;
352     }
353
354     /**
355      * @param list
356      */

357     public void setFileList(ArrayList list) {
358         fileList = list;
359     }
360
361     /**
362      * @return
363      */

364     public IProject getProj() {
365         return proj;
366     }
367
368     /**
369      * @param project
370      */

371     public void setProj(IProject project) {
372         proj = project;
373     }
374
375     /**
376      * @return
377      */

378     public ArrayList getAllSelected() {
379         return allSelected;
380     }
381
382     /**
383      * @param list
384      */

385     public void setAllSelected(ArrayList list) {
386         allSelected = list;
387     }
388
389     /**
390      * @return
391      */

392     public HashMap getCurrentSettingsMap() {
393         return currentSettingsMap;
394     }
395
396     /**
397      * @param map
398      */

399     public void setCurrentSettingsMap(HashMap map) {
400         currentSettingsMap = map;
401     }
402
403     /**
404      * @return
405      */

406     public CheckboxTableViewer getCheckTypes() {
407         return checkTypes;
408     }
409
410     /**
411      * @param viewer
412      */

413     public void setCheckTypes(CheckboxTableViewer viewer) {
414         checkTypes = viewer;
415     }
416
417 }
418
Popular Tags