1    	package acl2s.plugin.dialogs;
2    	
3    	import java.util.Arrays;
4    	import java.util.HashSet;
5    	import java.util.Set;
6    	
7    	import org.eclipse.swt.SWT;
8    	import org.eclipse.swt.events.SelectionEvent;
9    	import org.eclipse.swt.events.SelectionListener;
10   	import org.eclipse.swt.graphics.Rectangle;
11   	import org.eclipse.swt.layout.GridData;
12   	import org.eclipse.swt.layout.GridLayout;
13   	import org.eclipse.swt.layout.RowLayout;
14   	import org.eclipse.swt.widgets.Button;
15   	import org.eclipse.swt.widgets.Dialog;
16   	import org.eclipse.swt.widgets.Display;
17   	import org.eclipse.swt.widgets.Group;
18   	import org.eclipse.swt.widgets.Shell;
19   	
20   	public class CleanDialog extends Dialog implements SelectionListener {
21   		protected final Set<String> defaultActions =
22   			new HashSet<String>(Arrays.asList(new String[] {
23   				
24   			}));
25   		protected static final String[] allActions =
26   			new String[] {
27   				"acl2s.plugin.editors.session.CleanSession",	
28   				"acl2s.plugin.editors.lisp.ResetLine",
29   				"acl2s.plugin.editors.session.ForgetRedo",
30   				"acl2s.plugin.editors.session.CleanCmdHist",
31   				"acl2s.plugin.editors.lisp.CleanOutputFiles",
32   				"acl2s.plugin.editors.Reparse"
33   			};
34   		protected static final String[] allActionDescriptions =
35   			new String[] {
36   				"Empty session history (and kill session)",
37   				"Move 'todo' and 'done' lines to top",
38   				"Forget REDO information",
39   				"Empty typed command history",
40   				"Delete certification output files (*.o, *.cert, etc.)",
41   				"Reparse in-memory representations (shouldn't be needed)"
42   			};
43   		
44   		
45   		protected final String src;
46   		protected final HashSet<String> chosenActions;
47   		protected Shell sh;
48   		
49   		protected Button[] modeButtons;
50   	
51   		protected Button okButton;
52   		protected Button cancelButton;
53   		
54   		protected final boolean sessionSupported;
55   		protected final boolean lispSupported;
56   		
57   		public CleanDialog(Shell parent, String src, boolean sessionSupported, boolean lispSupported) {
58   			super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
59   			this.src = src;
60   			this.chosenActions = new HashSet<String>();
61   			this.sessionSupported = sessionSupported;
62   			this.lispSupported = lispSupported;
63   			
64   			for (String a : defaultActions) {
65   				if (actionSupported(a)) {
66   					chosenActions.add(a);
67   				}
68   			}
69   			
70   			setText("Select cleaning actions for " + src);
71   		}
72   	
73   		protected boolean actionSupported(String s) {
74   			if (!lispSupported    && s.startsWith("acl2s.plugin.editors.lisp."))
75   				return false;
76   			if (!sessionSupported && s.startsWith("acl2s.plugin.editors.session."))
77   				return false;
78   			return true;
79   		}
80   		
81   		public Set<String> open() {
82   			sh = new Shell(getParent(), getStyle());
83   			sh.setText(getText());
84   			sh.setLayout(new GridLayout(2,true));
85   			
86   			GridData gd;
87   					
88   			Group modeGroup = new Group(sh, 0);
89   			modeGroup.setText("Select a mode for " + src + ":");
90   			gd = new GridData(GridData.FILL_HORIZONTAL);
91   			gd.horizontalSpan = 2;
92   			modeGroup.setLayoutData(gd);
93   			modeGroup.setLayout(new RowLayout(SWT.VERTICAL));
94   			modeButtons = new Button[allActions.length];
95   			for (int i = 0; i < modeButtons.length; i++) {
96   				modeButtons[i] = new Button(modeGroup, SWT.CHECK);
97   				modeButtons[i].setText(allActionDescriptions[i]);
98   				modeButtons[i].setEnabled(actionSupported(allActions[i]));
99   				modeButtons[i].setSelection(chosenActions.contains(allActions[i]));
100  			}
101  	
102  			okButton = new Button(sh, SWT.PUSH);
103  			okButton.setText("OK");
104  			gd = new GridData(GridData.FILL_HORIZONTAL);
105  			okButton.setLayoutData(gd);
106  			okButton.addSelectionListener(this);
107  			
108  			cancelButton = new Button(sh,SWT.PUSH);
109  			cancelButton.setText("Cancel");
110  			gd = new GridData(GridData.FILL_HORIZONTAL);
111  			cancelButton.setLayoutData(gd);
112  			cancelButton.addSelectionListener(this);
113  	
114  			/*
115  			Label l = new Label(sh,0);
116  			l.setText("Note: Any change will take effect when session is restarted.");
117  			gd = new GridData(GridData.FILL_HORIZONTAL);
118  			gd.horizontalSpan = 2;
119  			l.setLayoutData(gd);
120  			*/
121  			
122  			sh.pack();
123  	
124  			Rectangle bounds = getParent().getBounds ();
125  			Rectangle rect = sh.getBounds ();
126  			int x = bounds.x + (bounds.width - rect.width) / 2;
127  			int y = bounds.y + (bounds.height - rect.height) / 2;
128  			sh.setLocation (x, y);
129  			
130  			sh.open();
131  			
132  			Display display = getParent().getDisplay();
133  			while (!sh.isDisposed()) {
134  				if (!display.readAndDispatch()) {
135  					display.sleep();
136  				}
137  			}
138  			
139  			return chosenActions;
140  		}
141  	
142  		public void widgetSelected(SelectionEvent e) {
143  			chosenActions.clear();
Event ref_eq_use: Using the "==" operator.
Also see events: [use_ref_eq][ref_eq_use][ref_eq_use][ref_eq_use][ref_eq_use]
144  			if (e.getSource() == okButton) {
145  				for (int i = 0; i < modeButtons.length; i++) {
146  					if (modeButtons[i].getSelection()) {
147  						chosenActions.add(allActions[i]);
148  					}
149  				}
150  			}
151  			sh.close();
152  		}
153  	
154  		public void widgetDefaultSelected(SelectionEvent e) { }
155  	}