KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui;
21
22
23 import java.util.*;
24 import org.eclipse.jface.dialogs.*;
25 import org.eclipse.jface.viewers.*;
26 import org.eclipse.swt.custom.*;
27 import org.eclipse.swt.events.*;
28 import org.eclipse.swt.widgets.*;
29 import org.eclipse.swt.layout.*;
30 import org.eclipse.swt.*;
31 import ca.mcgill.sable.soot.launching.SavedConfigManager;
32 import ca.mcgill.sable.soot.launching.SootSavedConfiguration;
33 import ca.mcgill.sable.soot.testing.*;
34
35
36 public abstract class AbstractOptionsDialog extends TitleAreaDialog implements ISelectionChangedListener {
37
38     private SashForm sashForm;
39     private TreeViewer treeViewer;
40     private Composite pageContainer;
41     private HashMap config;
42     private String JavaDoc configName;
43     private HashMap editMap;
44     private boolean canRun = true;
45     private HashMap radioGroups;
46     private ArrayList enableGroups;
47     private HashMap eclipseDefList;
48     private HashMap defList;
49     private CheckboxTableViewer tableViewer;
50     private Button addButton;
51     private Button removeButton;
52     private String JavaDoc sootMainClass;
53     
54     /**
55      * Constructor for AbstractOptionsDialog.
56      * @param parentShell
57      */

58     public AbstractOptionsDialog(Shell parentShell) {
59         super(parentShell);
60         this.setShellStyle(SWT.RESIZE);
61     }
62
63     public void addToEclipseDefList(String JavaDoc key, Object JavaDoc val) {
64         if (getEclipseDefList() == null) {
65             setEclipseDefList(new HashMap());
66         }
67         getEclipseDefList().put(key, val);
68         
69         addToDefList(key, val);
70         
71     }
72     
73     public void addToDefList(String JavaDoc key, Object JavaDoc val) {
74         if (getDefList() == null) {
75             setDefList(new HashMap());
76         }
77         getDefList().put(key, val);
78         
79     }
80
81     public boolean isInDefList(String JavaDoc key) {
82         if (getDefList().containsKey(key)) return true;
83         else return false;
84     }
85     
86     public boolean getBoolDef(String JavaDoc key) {
87         Boolean JavaDoc temp = (Boolean JavaDoc)getDefList().get(key);
88         return temp.booleanValue();
89     }
90
91     public String JavaDoc getStringDef(String JavaDoc key) {
92         
93         return (String JavaDoc)getDefList().get(key);
94     }
95
96     public String JavaDoc getArrayDef(String JavaDoc key){
97         String JavaDoc res = "";
98         if (getDefList().get(key) instanceof ArrayList){
99         
100         ArrayList list = (ArrayList)getDefList().get(key);
101         Iterator it = list.iterator();
102         while (it.hasNext()){
103             if (res.equals("")){
104                 res = res + (String JavaDoc)it.next();
105             }
106             else {
107                 res = res + "\r\n" + (String JavaDoc)it.next();
108             }
109         }
110         }
111         else {
112             res = (String JavaDoc)getDefList().get(key);
113         }
114         return res;
115     }
116     
117     // This sets the title in the shell that displays the
118
// options dialog box
119
protected void configureShell(Shell shell){
120         super.configureShell(shell);
121         shell.setText(Messages.getString("AbstractOptionsDialog.Soot_Options")); //$NON-NLS-1$
122
}
123     
124     public boolean isEnableButton(String JavaDoc alias){
125         if (alias.equals("enabled")) return true;
126         return false;
127     }
128     
129     
130     public void handleWidgetSelected(SelectionEvent e){
131         
132         if (getRadioGroups() != null) {
133             Iterator it = getRadioGroups().keySet().iterator();
134             while (it.hasNext()){
135                 Integer JavaDoc key = (Integer JavaDoc)it.next();
136                 if (getRadioGroups().get(key) == null) break;
137                 ArrayList buttons = (ArrayList)getRadioGroups().get(key);
138                 Iterator itButtons = buttons.iterator();
139                 while (itButtons.hasNext()){
140                     if (((BooleanOptionWidget)itButtons.next()).getButton().equals(e.getSource())) {
141                         switchButtons(buttons, (Button)e.getSource());
142                     }
143                 }
144             }
145         }
146         
147         updateAllEnableGroups();
148         
149     }
150     
151     public void updateEnableGroup(Button button){
152         if (getEnableGroups() == null) return;
153         Iterator it = getEnableGroups().iterator();
154         while (it.hasNext()){
155             EnableGroup eGroup = (EnableGroup)it.next();
156             if (eGroup.getLeader().getButton().equals(button)){
157                 // group found
158
eGroup.changeControlState(eGroup.getLeader().getButton().getSelection());
159                 if (eGroup.getControls() != null){
160                     Iterator itCon = eGroup.getControls().iterator();
161                     while (itCon.hasNext()){
162                         Object JavaDoc obj = itCon.next();
163                         if (obj instanceof BooleanOptionWidget){
164                             updateEnableGroup(((BooleanOptionWidget)obj).getButton());
165                         }
166                     }
167                 }
168             }
169         }
170     }
171     
172     public void switchButtons(ArrayList buttons, Button change){
173         if (change.getSelection()){
174             Iterator it = buttons.iterator();
175             while (it.hasNext()){
176                 BooleanOptionWidget nextWidget = (BooleanOptionWidget)it.next();
177                 if (nextWidget.getButton().equals(change)){
178                     nextWidget.getButton().setSelection(true);
179                 }
180                 else {
181                     nextWidget.getButton().setSelection(false);
182                 }
183             }
184         }
185         else {
186             Iterator it = buttons.iterator();
187             while (it.hasNext()){
188                 BooleanOptionWidget defWidget = (BooleanOptionWidget)it.next();
189                 if (defWidget.getData().isDefaultVal()){
190                     defWidget.getButton().setSelection(true);
191                 }
192                 else {
193                     defWidget.getButton().setSelection(false);
194                 }
195             }
196         }
197     }
198     
199     protected void makeNewEnableGroup(String JavaDoc phaseAlias){
200         if (getEnableGroups() == null){
201             setEnableGroups(new ArrayList());
202         }
203         
204         EnableGroup eGroup = new EnableGroup();
205         eGroup.setPhaseAlias(phaseAlias);
206         
207         getEnableGroups().add(eGroup);
208     }
209     
210     protected void makeNewEnableGroup(String JavaDoc phaseAlias, String JavaDoc subPhaseAlias){
211         if (getEnableGroups() == null){
212             setEnableGroups(new ArrayList());
213         }
214         
215         EnableGroup eGroup = new EnableGroup();
216         eGroup.setPhaseAlias(phaseAlias);
217         eGroup.setSubPhaseAlias(subPhaseAlias);
218         
219         getEnableGroups().add(eGroup);
220     }
221     
222     protected void addToEnableGroup(String JavaDoc phaseAlias, ISootOptionWidget widget, String JavaDoc alias){
223         EnableGroup eGroup = findEnableGroup(phaseAlias);
224         if (widget instanceof BooleanOptionWidget){
225             // could be leader
226
if (isEnableButton(alias)){
227                 eGroup.setLeader(((BooleanOptionWidget)widget));
228             }
229             else {
230                 eGroup.addControl(widget);
231             }
232         }
233         else {
234             eGroup.addControl(widget);
235         }
236     }
237     
238     private EnableGroup findEnableGroup(String JavaDoc phaseAlias){
239         Iterator it = getEnableGroups().iterator();
240         while (it.hasNext()){
241             EnableGroup next = (EnableGroup)it.next();
242             if (next.getPhaseAlias().equals(phaseAlias) &&
243                 (next.getSubPhaseAlias() == null)) return next;
244         }
245         return null;
246     }
247     
248     protected void addToEnableGroup(String JavaDoc phaseAlias, String JavaDoc subPhaseAlias, ISootOptionWidget widget, String JavaDoc alias){
249         EnableGroup eGroup = findEnableGroup(phaseAlias, subPhaseAlias);
250         if (widget instanceof BooleanOptionWidget){
251             // could be leader
252

253             if (isEnableButton(alias)){
254                 eGroup.setLeader(((BooleanOptionWidget)widget));
255                 addToEnableGroup(phaseAlias, widget, "");
256             }
257             else {
258                 eGroup.addControl(widget);
259             }
260         }
261         else {
262             eGroup.addControl(widget);
263         }
264     }
265
266     private EnableGroup findEnableGroup(String JavaDoc phaseAlias, String JavaDoc subPhaseAlias){
267         Iterator it = getEnableGroups().iterator();
268         while (it.hasNext()){
269             EnableGroup next = (EnableGroup)it.next();
270             if (next.getSubPhaseAlias() == null) continue;
271             if (next.getPhaseAlias().equals(phaseAlias) &&
272                 next.getSubPhaseAlias().equals(subPhaseAlias)) return next;
273         }
274         return null;
275     }
276     
277     protected void updateAllEnableGroups(){
278         if (getEnableGroups() == null) return;
279         Iterator it = getEnableGroups().iterator();
280         
281         while (it.hasNext()){
282             EnableGroup eGroup = (EnableGroup)it.next();
283             if (eGroup.isPhaseOptType()){
284                 if (eGroup.getLeader() == null){
285                     continue;
286                 }
287                 if (eGroup.getLeader().getButton().getSelection() && eGroup.getLeader().getButton().isEnabled()){
288                     eGroup.changeControlState(true);
289                 }
290                 else {
291                     eGroup.changeControlState(false);
292                 }
293             }
294         }
295         
296         it = getEnableGroups().iterator();
297         
298         while (it.hasNext()){
299             EnableGroup eGroup = (EnableGroup)it.next();
300             if (!eGroup.isPhaseOptType()){
301                 if (eGroup.getLeader() == null){
302                     continue;
303                 }
304                 if (eGroup.getLeader().getButton().getSelection() && eGroup.getLeader().getButton().isEnabled()){
305                     eGroup.changeControlState(true);
306                 }
307                 else {
308                     eGroup.changeControlState(false);
309                 }
310             }
311         }
312     }
313     
314     private void printEnableGroups(){
315         if (getEnableGroups() == null) return;
316         Iterator it = getEnableGroups().iterator();
317         while (it.hasNext()){
318             EnableGroup eGroup = (EnableGroup)it.next();
319             System.out.println(eGroup);
320         }
321     }
322     
323         
324     /**
325      * creates a sash form - one side for a selection tree
326      * and the other for the options
327      */

328     protected Control createDialogArea(Composite parent) {
329         GridData gd;
330         
331         Composite dialogComp = (Composite)super.createDialogArea(parent);
332         Composite topComp = new Composite(dialogComp, SWT.NONE);
333         
334         gd = new GridData(GridData.FILL_BOTH);
335         topComp.setLayoutData(gd);
336         GridLayout topLayout = new GridLayout();
337         topComp.setLayout(topLayout);
338         
339         // Set the things that TitleAreaDialog takes care of
340

341         setTitle(Messages.getString("AbstractOptionsDialog.Soot_Launching_Options")); //$NON-NLS-1$
342
setMessage(""); //$NON-NLS-1$
343

344         // Create the SashForm that contains the selection area on the left,
345
// and the edit area on the right
346
setSashForm(new SashForm(topComp, SWT.NONE));
347         getSashForm().setOrientation(SWT.HORIZONTAL);
348         
349         gd = new GridData(GridData.FILL_BOTH);
350         getSashForm().setLayoutData(gd);
351         
352         Composite selection = createSelectionArea(getSashForm());
353         
354         setPageContainer(createEditArea(getSashForm()));
355
356         initializePageContainer();
357
358         // set general as first page
359
Control [] pages = getPageContainer().getChildren();
360         ((StackLayout)getPageContainer().getLayout()).topControl = pages[0];
361         getPageContainer().layout();
362         
363                 
364         try {
365             getSashForm().setWeights(new int[] {30, 70});
366         }
367         catch(Exception JavaDoc e1) {
368             System.out.println(e1.getMessage());
369         }
370         
371         Label separator = new Label(topComp, SWT.HORIZONTAL | SWT.SEPARATOR);
372         gd = new GridData(GridData.FILL_HORIZONTAL);
373         separator.setLayoutData(gd);
374         
375         dialogComp.layout(true);
376         
377         return dialogComp;
378     }
379
380     // creates buttons Run and Close for a runnable dialog and
381
// buttons Save and Close for a savable one
382
protected void createButtonsForButtonBar(Composite parent) {
383         if (isCanRun()) {
384             createButton(parent, 1, Messages.getString("AbstractOptionsDialog.Run"), true); //$NON-NLS-1$
385
createButton(parent, 2, Messages.getString("AbstractOptionsDialog.Close"), false); //$NON-NLS-1$
386
}
387         else {
388             createButton(parent, 0, Messages.getString("AbstractOptionsDialog.Save"), true); //$NON-NLS-1$
389
createButton(parent, 2, Messages.getString("AbstractOptionsDialog.Close"), false); //$NON-NLS-1$
390
}
391     }
392     
393     
394     protected void buttonPressed(int i){
395         switch(i) {
396             case 0: {
397                 handleSaving();
398                 break;
399             }
400             case 1: {
401                 okPressed();
402                 break;
403             }
404             case 2: {
405                 cancelPressed();
406                 break;
407             }
408         }
409         
410     }
411         
412     protected abstract HashMap savePressed();
413     
414     private void handleSaving() {
415         
416         saveConfigToMap(this.getConfigName());
417         
418         SavedConfigManager scm = new SavedConfigManager();
419         scm.setEditMap(getEditMap());
420         scm.handleEdits();
421         
422         super.okPressed();
423         
424         
425     }
426     
427         
428     private void saveConfigToMap(String JavaDoc name) {
429         
430         SootSavedConfiguration newConfig = new SootSavedConfiguration(name, savePressed());
431         
432         newConfig.setEclipseDefs(getEclipseDefList());
433         if (getEditMap() == null) {
434             setEditMap(new HashMap());
435         }
436         
437         getEditMap().put(name, newConfig.toSaveArray());
438                     
439     }
440         
441     protected abstract void initializePageContainer();
442     protected abstract SootOption getInitialInput();
443     
444         
445     /**
446      * initialize area containing options as a stack layout
447      */

448     private Composite createEditArea(Composite parent) {
449         Composite editArea = new Composite(parent, SWT.NONE);
450         StackLayout layout = new StackLayout();
451         editArea.setLayout(layout);
452         return editArea;
453     }
454     
455     /**
456      * creates the tree of options sections
457      */

458     private Composite createSelectionArea(Composite parent) {
459         Composite comp = new Composite(parent, SWT.NONE);
460         setSelectionArea(comp);
461         
462         GridLayout layout = new GridLayout();
463
464         layout.numColumns = 3;
465         layout.marginHeight = 0;
466         layout.marginWidth = 5;
467         
468         comp.setLayout(layout);
469         
470         GridData gd = new GridData();
471         
472         TreeViewer tree = new TreeViewer(comp);
473         gd = new GridData(GridData.FILL_BOTH);
474         gd.horizontalSpan = 3;
475         gd.widthHint = 0;
476         tree.getControl().setLayoutData(gd);
477         
478         tree.setContentProvider(new SootOptionsContentProvider());
479         tree.setLabelProvider(new SootOptionsLabelProvider());
480         tree.setInput(getInitialInput());
481     
482         
483         setTreeViewer(tree);
484         
485         tree.addSelectionChangedListener(this);
486         
487         tree.getControl().addKeyListener(new KeyAdapter() {
488             public void keyPressed(KeyEvent e) {
489                 handleKeyPressed(e);
490             }
491         });
492          
493         return comp;
494     }
495
496     /*
497      * (non-Javadoc)
498      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
499      */

500     public void selectionChanged(SelectionChangedEvent event) {
501         IStructuredSelection selection = (IStructuredSelection)event.getSelection();
502         if (!selection.isEmpty()) {
503             Object JavaDoc elem = selection.getFirstElement();
504             if (elem instanceof SootOption) {
505                 SootOption sel = (SootOption)elem;
506                 Control [] children = getPageContainer().getChildren();
507                 String JavaDoc childTitle = null;
508                 for (int i = 0; i < children.length; i++) {
509
510                     if( children[i] instanceof Composite) {
511                         if (children[i] instanceof Group) {
512                             childTitle = (String JavaDoc)((Group)children[i]).getData("id");
513                             
514                         }
515                         if (childTitle.compareTo(sel.getAlias()) == 0) {
516                             ((StackLayout)getPageContainer().getLayout()).topControl = children[i];
517                             getPageContainer().layout();
518                             
519                         }
520                         else {
521                             children[i].setVisible(false);
522                         }
523                     }
524                 }
525             }
526         }
527     }
528     
529     public void addOtherBranches(SootOption root){
530         SootOption sootMainClassBranch = new SootOption("Soot Main Class", "sootMainClass");
531         root.addChild(sootMainClassBranch);
532     }
533     
534     public void addOtherPages(Composite parent){
535         Composite mainClassChild = sootMainClassCreate(parent);
536     }
537     
538     private Composite sootMainClassCreate(Composite parent) {
539         
540         Group editGroupSootMainClass = new Group(parent, SWT.NONE);
541         GridLayout layout = new GridLayout();
542         editGroupSootMainClass.setLayout(layout);
543
544         editGroupSootMainClass.setText("Soot Main Class Manager");
545
546         editGroupSootMainClass.setData("id", "sootMainClass");
547
548         String JavaDoc desc = "Specify main class to run.";
549         if (desc.length() > 0) {
550             Label descLabel = new Label(editGroupSootMainClass, SWT.WRAP);
551             descLabel.setText(desc);
552         }
553     
554         String JavaDoc defKey = "sootMainClass";
555         String JavaDoc defaultString;
556         
557         if (isInDefList(defKey)) {
558             defaultString = getStringDef(defKey);
559         }
560         else {
561             defaultString = "";
562         }
563         setSootMainClassWidget(new StringOptionWidget(editGroupSootMainClass, SWT.NONE, new OptionData("Soot Main Class", "", "","sootMainClass", "\nUses specified main class to run Soot.", defaultString)));
564
565         return editGroupSootMainClass;
566     }
567     
568     
569     
570     private StringOptionWidget sootMainClassWidget;
571
572     private void setPageContainer(Composite comp) {
573         pageContainer = comp;
574     }
575     
576     protected Composite getPageContainer() {
577         return pageContainer;
578     }
579
580     /**
581      * Returns the sashForm.
582      * @return SashForm
583      */

584     private SashForm getSashForm() {
585         return sashForm;
586     }
587
588     /**
589      * Sets the sashForm.
590      * @param sashForm The sashForm to set
591      */

592     private void setSashForm(SashForm sashForm) {
593         this.sashForm = sashForm;
594     }
595     
596     protected void handleKeyPressed(KeyEvent event) {
597     }
598     
599     private Composite selectionArea;
600     
601     private void setSelectionArea(Composite comp){
602         selectionArea = comp;
603     }
604
605     private Composite getSelectionArea() {
606         return selectionArea;
607     }
608
609     private void setTreeViewer(TreeViewer tree) {
610         treeViewer = tree;
611     }
612
613     private TreeViewer getTreeViewer() {
614         return treeViewer;
615     }
616
617
618     /**
619      * Returns the defList.
620      * @return HashMap
621      */

622     public HashMap getDefList() {
623         return defList;
624     }
625
626     /**
627      * Sets the defList.
628      * @param defList The defList to set
629      */

630     public void setDefList(HashMap defList) {
631         this.defList = defList;
632     }
633
634     /**
635      * Returns the config.
636      * @return HashMap
637      */

638     public HashMap getConfig() {
639         return config;
640     }
641
642     /**
643      * Sets the config.
644      * @param config The config to set
645      */

646     public void setConfig(HashMap config) {
647         this.config = config;
648     }
649
650     /**
651      * Returns the configName.
652      * @return String
653      */

654     public String JavaDoc getConfigName() {
655         return configName;
656     }
657
658     /**
659      * Sets the configName.
660      * @param configName The configName to set
661      */

662     public void setConfigName(String JavaDoc configName) {
663         this.configName = configName;
664     }
665
666     /**
667      * Returns the eclipseDefList.
668      * @return HashMap
669      */

670     public HashMap getEclipseDefList() {
671         return eclipseDefList;
672     }
673
674     /**
675      * Sets the eclipseDefList.
676      * @param eclipseDefList The eclipseDefList to set
677      */

678     public void setEclipseDefList(HashMap eclipseDefList) {
679         this.eclipseDefList = eclipseDefList;
680     }
681
682
683     /**
684      * Returns the editMap.
685      * @return HashMap
686      */

687     public HashMap getEditMap() {
688         return editMap;
689     }
690
691
692     /**
693      * Sets the editMap.
694      * @param editMap The editMap to set
695      */

696     public void setEditMap(HashMap editMap) {
697         this.editMap = editMap;
698     }
699
700     /**
701      * Returns the canRun.
702      * @return boolean
703      */

704     public boolean isCanRun() {
705         return canRun;
706     }
707
708     /**
709      * Sets the canRun.
710      * @param canRun The canRun to set
711      */

712     public void setCanRun(boolean canRun) {
713         this.canRun = canRun;
714     }
715
716     /**
717      * @return
718      */

719     public HashMap getRadioGroups() {
720         return radioGroups;
721     }
722
723     /**
724      * @param map
725      */

726     public void setRadioGroups(HashMap map) {
727         radioGroups = map;
728     }
729
730     /**
731      * @return
732      */

733     public ArrayList getEnableGroups() {
734         return enableGroups;
735     }
736
737     /**
738      * @param map
739      */

740     public void setEnableGroups(ArrayList list) {
741         enableGroups = list;
742     }
743
744     /**
745      * @return
746      */

747     public Button getAddButton() {
748         return addButton;
749     }
750
751     /**
752      * @return
753      */

754     public Button getRemoveButton() {
755         return removeButton;
756     }
757
758     /**
759      * @param button
760      */

761     public void setAddButton(Button button) {
762         addButton = button;
763     }
764
765     /**
766      * @param button
767      */

768     public void setRemoveButton(Button button) {
769         removeButton = button;
770     }
771
772     /**
773      * @return
774      */

775     public CheckboxTableViewer getTableViewer() {
776         return tableViewer;
777     }
778
779     /**
780      * @param viewer
781      */

782     public void setTableViewer(CheckboxTableViewer viewer) {
783         tableViewer = viewer;
784     }
785
786     /**
787      * @return
788      */

789     public StringOptionWidget getSootMainClassWidget() {
790         return sootMainClassWidget;
791     }
792
793     /**
794      * @param widget
795      */

796     public void setSootMainClassWidget(StringOptionWidget widget) {
797         sootMainClassWidget = widget;
798     }
799
800     /**
801      * @return
802      */

803     public String JavaDoc getSootMainClass() {
804         return sootMainClass;
805     }
806
807     /**
808      * @param string
809      */

810     public void setSootMainClass(String JavaDoc string) {
811         sootMainClass = string;
812     }
813
814 }
815
Popular Tags