1    	package acl2s.plugin.dialogs;
2    	
3    	import org.eclipse.swt.SWT;
4    	import org.eclipse.swt.events.SelectionEvent;
5    	import org.eclipse.swt.events.SelectionListener;
6    	import org.eclipse.swt.graphics.Rectangle;
7    	import org.eclipse.swt.layout.GridData;
8    	import org.eclipse.swt.layout.GridLayout;
9    	import org.eclipse.swt.layout.RowLayout;
10   	import org.eclipse.swt.widgets.Button;
11   	import org.eclipse.swt.widgets.Dialog;
12   	import org.eclipse.swt.widgets.Display;
13   	import org.eclipse.swt.widgets.Group;
14   	import org.eclipse.swt.widgets.Shell;
15   	
16   	import acl2s.plugin.editors.lisp.LineMode;
17   	
18   	public class LineDialog extends Dialog implements SelectionListener {
19   		protected final String src;
20   		protected final LineMode old;
21   		protected final LineMode[] modes;
22   		
23   		protected LineMode result;
24   		protected Shell sh;
25   		
26   		protected Button[] modeButtons;
27   	
28   		protected Button okButton;
29   		protected Button cancelButton;
30   		
31   		public LineDialog(Shell parent, String src, LineMode old) {
32   			super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
33   			this.src = src;
34   			this.old = old;
35   			setText("Pick a \"Line\"-tracking mode");
36   			
37   			modes = LineMode.modes;
38   			for (int i = 1; i < modes.length; i++) {
39   				if (modes[i].equals(LineMode.getDefault())) {
40   					modes[i] = modes[0];
41   					modes[0] = LineMode.getDefault();
42   					break;
43   				}
44   			}
45   		}
46   	
47   		public LineMode open() {
48   			sh = new Shell(getParent(), getStyle());
49   			sh.setText(getText());
50   			sh.setLayout(new GridLayout(2,true));
51   			
52   			GridData gd;
53   					
54   			Group modeGroup = new Group(sh, 0);
55   			modeGroup.setText("Select a mode for " + src + ":");
56   			gd = new GridData(GridData.FILL_HORIZONTAL);
57   			gd.horizontalSpan = 2;
58   			modeGroup.setLayoutData(gd);
59   			modeGroup.setLayout(new RowLayout(SWT.VERTICAL));
60   			modeButtons = new Button[modes.length];
61   			for (int i = 0; i < modeButtons.length; i++) {
62   				modeButtons[i] = new Button(modeGroup, SWT.RADIO);
63   				modeButtons[i].setText(modes[i].getShortDesc());
64   				modeButtons[i].setSelection(modes[i].equals(old));
65   			}
66   	
67   			okButton = new Button(sh, SWT.PUSH);
68   			okButton.setText("OK");
69   			gd = new GridData(GridData.FILL_HORIZONTAL);
70   			okButton.setLayoutData(gd);
71   			okButton.addSelectionListener(this);
72   			
73   			cancelButton = new Button(sh,SWT.PUSH);
74   			cancelButton.setText("Cancel");
75   			gd = new GridData(GridData.FILL_HORIZONTAL);
76   			cancelButton.setLayoutData(gd);
77   			cancelButton.addSelectionListener(this);
78   	
79   			/*
80   			Label l = new Label(sh,0);
81   			l.setText("Note: Any change will take effect when session is restarted.");
82   			gd = new GridData(GridData.FILL_HORIZONTAL);
83   			gd.horizontalSpan = 2;
84   			l.setLayoutData(gd);
85   			*/
86   			
87   			sh.pack();
88   	
89   			Rectangle bounds = getParent().getBounds ();
90   			Rectangle rect = sh.getBounds ();
91   			int x = bounds.x + (bounds.width - rect.width) / 2;
92   			int y = bounds.y + (bounds.height - rect.height) / 2;
93   			sh.setLocation (x, y);
94   			
95   			sh.open();
96   			
97   			Display display = getParent().getDisplay();
98   			while (!sh.isDisposed()) {
99   				if (!display.readAndDispatch()) {
100  					display.sleep();
101  				}
102  			}
103  			
104  			return result;
105  		}
106  	
107  		public void widgetSelected(SelectionEvent e) {
108  			result = null;
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]
109  			if (e.getSource() == okButton) {
110  				for (int i = 0; i < modeButtons.length; i++) {
111  					if (modeButtons[i].getSelection()) {
112  						result = modes[i];
113  						break;
114  					}
115  				}
116  			}
117  			sh.close();
118  		}
119  	
120  		public void widgetDefaultSelected(SelectionEvent e) { }
121  	}