KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > PromptDialog


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.layout.*;
15 import org.eclipse.swt.widgets.*;
16
17 class PromptDialog extends Dialog {
18     
19     public PromptDialog(Shell parent, int style) {
20         super(parent, style);
21     }
22     
23     public PromptDialog(Shell parent) {
24         this(parent, 0);
25     }
26     
27     public void alertCheck(String JavaDoc title, String JavaDoc text, String JavaDoc check, final int[] checkValue) {
28         Shell parent = getParent();
29         final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
30         if (title != null) shell.setText(title);
31         GridLayout gridLayout = new GridLayout();
32         shell.setLayout(gridLayout);
33         Label label = new Label(shell, SWT.WRAP);
34         label.setText(text);
35         GridData data = new GridData();
36         data.horizontalAlignment = GridData.FILL;
37         data.grabExcessHorizontalSpace = true;
38         label.setLayoutData (data);
39
40         final Button checkButton = check != null ? new Button(shell, SWT.CHECK) : null;
41         if (checkButton != null) {
42             checkButton.setText(check);
43             checkButton.setSelection(checkValue[0] != 0);
44             data = new GridData ();
45             data.horizontalAlignment = GridData.BEGINNING;
46             checkButton.setLayoutData (data);
47         }
48         Button okButton = new Button(shell, SWT.PUSH);
49         okButton.setText(SWT.getMessage("SWT_OK")); //$NON-NLS-1$
50
data = new GridData ();
51         data.horizontalAlignment = GridData.CENTER;
52         okButton.setLayoutData (data);
53         okButton.addListener(SWT.Selection, new Listener() {
54             public void handleEvent(Event event) {
55                 if (checkButton != null) checkValue[0] = checkButton.getSelection() ? 1 : 0;
56                 shell.close();
57             }
58         });
59
60         shell.pack();
61         shell.open();
62         Display display = parent.getDisplay();
63         while (!shell.isDisposed()) {
64             if (!display.readAndDispatch()) display.sleep();
65         }
66     }
67
68     public void confirmEx(String JavaDoc title, String JavaDoc text, String JavaDoc check, String JavaDoc button0, String JavaDoc button1, String JavaDoc button2, int defaultIndex, final int[] checkValue, final int[] result) {
69         Shell parent = getParent();
70         final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
71         shell.setText(title);
72         GridLayout gridLayout = new GridLayout();
73         shell.setLayout(gridLayout);
74         Label label = new Label(shell, SWT.WRAP);
75         label.setText(text);
76         GridData data = new GridData();
77         data.horizontalAlignment = GridData.FILL;
78         data.grabExcessHorizontalSpace = true;
79         label.setLayoutData (data);
80         
81         final Button[] buttons = new Button[4];
82         Listener listener = new Listener() {
83             public void handleEvent(Event event) {
84                 if (buttons[0] != null) checkValue[0] = buttons[0].getSelection() ? 1 : 0;
85                 Widget widget = event.widget;
86                 for (int i = 1; i < buttons.length; i++) {
87                     if (widget == buttons[i]) {
88                         result[0] = i - 1;
89                         break;
90                     }
91                 }
92                 shell.close();
93             }
94         };
95         if (check != null) {
96             buttons[0] = new Button(shell, SWT.CHECK);
97             buttons[0].setText(check);
98             buttons[0].setSelection(checkValue[0] != 0);
99             data = new GridData ();
100             data.horizontalAlignment = GridData.BEGINNING;
101             buttons[0].setLayoutData (data);
102         }
103         Composite composite = new Composite(shell, SWT.NONE);
104         data = new GridData();
105         data.horizontalAlignment = GridData.END;
106         composite.setLayoutData (data);
107         composite.setLayout(new RowLayout());
108         if (button0 != null) {
109             buttons[1] = new Button(composite, SWT.PUSH);
110             buttons[1].setText(button0);
111             buttons[1].addListener(SWT.Selection, listener);
112         }
113         if (button1 != null) {
114             buttons[2] = new Button(composite, SWT.PUSH);
115             buttons[2].setText(button1);
116             buttons[2].addListener(SWT.Selection, listener);
117         }
118         if (button2 != null) {
119             buttons[3] = new Button(composite, SWT.PUSH);
120             buttons[3].setText(button2);
121             buttons[3].addListener(SWT.Selection, listener);
122         }
123         Button defaultButton = buttons [defaultIndex + 1];
124         if (defaultButton != null) shell.setDefaultButton (defaultButton);
125
126         shell.pack();
127         shell.open();
128         Display display = parent.getDisplay();
129         while (!shell.isDisposed()) {
130             if (!display.readAndDispatch()) display.sleep();
131         }
132     }
133     
134     public void prompt(String JavaDoc title, String JavaDoc text, String JavaDoc check, final String JavaDoc[] value, final int[] checkValue, final int[] result) {
135         Shell parent = getParent();
136         final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
137         if (title != null) shell.setText(title);
138         GridLayout gridLayout = new GridLayout();
139         shell.setLayout(gridLayout);
140         Label label = new Label(shell, SWT.WRAP);
141         label.setText(text);
142         GridData data = new GridData();
143         data.horizontalAlignment = GridData.FILL;
144         data.grabExcessHorizontalSpace = true;
145         label.setLayoutData (data);
146                 
147         final Text valueText = new Text(shell, SWT.BORDER);
148         if (value[0] != null) valueText.setText(value[0]);
149         data = new GridData();
150         data.horizontalAlignment = GridData.FILL;
151         data.grabExcessHorizontalSpace = true;
152         valueText.setLayoutData(data);
153                 
154         final Button[] buttons = new Button[3];
155         Listener listener = new Listener() {
156             public void handleEvent(Event event) {
157                 if (buttons[0] != null) checkValue[0] = buttons[0].getSelection() ? 1 : 0;
158                 value[0] = valueText.getText();
159                 result[0] = event.widget == buttons[1] ? 1 : 0;
160                 shell.close();
161             }
162         };
163         if (check != null) {
164             buttons[0] = new Button(shell, SWT.CHECK);
165             buttons[0].setText(check);
166             buttons[0].setSelection(checkValue[0] != 0);
167             data = new GridData ();
168             data.horizontalAlignment = GridData.BEGINNING;
169             buttons[0].setLayoutData (data);
170         }
171         Composite composite = new Composite(shell, SWT.NONE);
172         data = new GridData();
173         data.horizontalAlignment = GridData.END;
174         composite.setLayoutData (data);
175         composite.setLayout(new GridLayout(2, true));
176         buttons[1] = new Button(composite, SWT.PUSH);
177         buttons[1].setText(SWT.getMessage("SWT_OK")); //$NON-NLS-1$
178
buttons[1].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
179         buttons[1].addListener(SWT.Selection, listener);
180         buttons[2] = new Button(composite, SWT.PUSH);
181         buttons[2].setText(SWT.getMessage("SWT_Cancel")); //$NON-NLS-1$
182
buttons[2].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
183         buttons[2].addListener(SWT.Selection, listener);
184
185         shell.pack();
186         shell.open();
187         Display display = parent.getDisplay();
188         while (!shell.isDisposed()) {
189             if (!display.readAndDispatch()) display.sleep();
190         }
191     }
192
193     public void promptUsernameAndPassword(String JavaDoc title, String JavaDoc text, String JavaDoc check, final String JavaDoc[] user, final String JavaDoc[] pass, final int[] checkValue, final int[] result) {
194         Shell parent = getParent();
195         final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
196         shell.setText(title);
197         GridLayout gridLayout = new GridLayout();
198         shell.setLayout(gridLayout);
199         Label label = new Label(shell, SWT.WRAP);
200         label.setText(text);
201         GridData data = new GridData();
202         data.horizontalAlignment = GridData.FILL;
203         data.grabExcessHorizontalSpace = true;
204         label.setLayoutData (data);
205         
206         Label userLabel = new Label(shell, SWT.NONE);
207         userLabel.setText(SWT.getMessage("SWT_Username")); //$NON-NLS-1$
208

209         final Text userText = new Text(shell, SWT.BORDER);
210         if (user[0] != null) userText.setText(user[0]);
211         data = new GridData();
212         data.horizontalAlignment = GridData.FILL;
213         data.grabExcessHorizontalSpace = true;
214         userText.setLayoutData(data);
215         
216         Label passwordLabel = new Label(shell, SWT.NONE);
217         passwordLabel.setText(SWT.getMessage("SWT_Password")); //$NON-NLS-1$
218

219         final Text passwordText = new Text(shell, SWT.PASSWORD | SWT.BORDER);
220         if (pass[0] != null) passwordText.setText(pass[0]);
221         data = new GridData();
222         data.horizontalAlignment = GridData.FILL;
223         data.grabExcessHorizontalSpace = true;
224         passwordText.setLayoutData(data);
225
226         final Button[] buttons = new Button[3];
227         Listener listener = new Listener() {
228             public void handleEvent(Event event) {
229                 if (buttons[0] != null) checkValue[0] = buttons[0].getSelection() ? 1 : 0;
230                 user[0] = userText.getText();
231                 pass[0] = passwordText.getText();
232                 result[0] = event.widget == buttons[1] ? 1 : 0;
233                 shell.close();
234             }
235         };
236         if (check != null) {
237             buttons[0] = new Button(shell, SWT.CHECK);
238             buttons[0].setText(check);
239             buttons[0].setSelection(checkValue[0] != 0);
240             data = new GridData ();
241             data.horizontalAlignment = GridData.BEGINNING;
242             buttons[0].setLayoutData (data);
243         }
244         Composite composite = new Composite(shell, SWT.NONE);
245         data = new GridData();
246         data.horizontalAlignment = GridData.END;
247         composite.setLayoutData (data);
248         composite.setLayout(new GridLayout(2, true));
249         buttons[1] = new Button(composite, SWT.PUSH);
250         buttons[1].setText(SWT.getMessage("SWT_OK")); //$NON-NLS-1$
251
buttons[1].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
252         buttons[1].addListener(SWT.Selection, listener);
253         buttons[2] = new Button(composite, SWT.PUSH);
254         buttons[2].setText(SWT.getMessage("SWT_Cancel")); //$NON-NLS-1$
255
buttons[2].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
256         buttons[2].addListener(SWT.Selection, listener);
257
258         shell.setDefaultButton(buttons[1]);
259         shell.pack();
260         shell.open();
261         Display display = parent.getDisplay();
262         while (!shell.isDisposed()) {
263             if (!display.readAndDispatch()) display.sleep();
264         }
265     }
266 }
267
Popular Tags