KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > antsupport > inputhandler > SWTInputHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 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  * Brock Janiczak (brockj@tpg.com.au) - Bug 145736
11  *******************************************************************************/

12
13 package org.eclipse.ant.internal.ui.antsupport.inputhandler;
14
15 import java.util.Iterator JavaDoc;
16
17 import org.apache.tools.ant.BuildException;
18 import org.apache.tools.ant.input.DefaultInputHandler;
19 import org.apache.tools.ant.input.InputRequest;
20 import org.apache.tools.ant.input.MultipleChoiceInputRequest;
21 import org.eclipse.ant.internal.ui.antsupport.RemoteAntMessages;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.FontMetrics;
28 import org.eclipse.swt.graphics.GC;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Combo;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Display;
37 import org.eclipse.swt.widgets.Event;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Listener;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Text;
42
43 public class SWTInputHandler extends DefaultInputHandler {
44     
45     private Text fText;
46     private Combo fCombo;
47     private Text fErrorMessageText;
48     private Button fOkButton;
49     private Shell fDialog;
50     private FontMetrics fFontMetrics;
51     protected InputRequest fRequest;
52     private boolean fFirstValidation= true;
53     
54     /* (non-Javadoc)
55      * @see org.apache.tools.ant.input.InputHandler#handleInput(org.apache.tools.ant.input.InputRequest)
56      */

57     public void handleInput(InputRequest request) throws BuildException {
58         if (System.getProperty("eclipse.ant.noInput") != null) { //$NON-NLS-1$
59
throw new BuildException(RemoteAntMessages.getString("SWTInputHandler.0")); //$NON-NLS-1$
60
}
61         fFirstValidation= true;
62         fRequest= request;
63         BuildException[] problem= new BuildException[1];
64         Runnable JavaDoc runnable= getHandleInputRunnable(problem);
65         Display.getDefault().syncExec(runnable);
66         if (problem[0] != null) {
67             throw problem[0];
68         }
69     }
70     
71     protected Runnable JavaDoc getHandleInputRunnable(final BuildException[] problem) {
72         return new Runnable JavaDoc() {
73             public void run() {
74                 String JavaDoc prompt;
75                 if (fRequest instanceof MultipleChoiceInputRequest) {
76                     prompt = fRequest.getPrompt();
77                 } else {
78                     prompt = getPrompt(fRequest);
79                 }
80                 String JavaDoc title= RemoteAntMessages.getString("SWTInputHandler.1"); //$NON-NLS-1$
81
boolean[] result = new boolean[1];
82                 open(title, prompt, result);
83         
84                 if (!result[0]) {
85                     problem[0]= new BuildException(RemoteAntMessages.getString("SWTInputHandler.2")); //$NON-NLS-1$
86
}
87             }
88         };
89     }
90     
91     protected void open(String JavaDoc title, String JavaDoc prompt, boolean[] result) {
92         createDialog(title, prompt, result);
93         validateInput();
94         fDialog.open();
95
96         while (!fDialog.isDisposed()) {
97             if (!fDialog.getDisplay().readAndDispatch()) fDialog.getDisplay().sleep();
98         }
99         Display.getDefault().dispose();
100     }
101
102     private void createDialog(String JavaDoc title, String JavaDoc prompt, boolean[] result) {
103         Display display= Display.getDefault();
104         fDialog = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
105         initializeDialogUnits(fDialog);
106         fDialog.setLayout(new GridLayout());
107         
108         GridData gd= new GridData(SWT.FILL);
109         gd.horizontalSpan= 2;
110         fDialog.setLayoutData(gd);
111         fDialog.setText(title);
112         Label label= new Label(fDialog, SWT.WRAP);
113         label.setText(prompt);
114         GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
115         
116         data.widthHint = convertHorizontalDLUsToPixels(300); //from IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH
117
label.setLayoutData(data);
118         label.setFont(fDialog.getFont());
119         
120         if (fRequest instanceof MultipleChoiceInputRequest) {
121             fCombo = new Combo(fDialog, SWT.BORDER | SWT.READ_ONLY);
122             fCombo.add(""); //$NON-NLS-1$
123
for (Iterator JavaDoc i = ((MultipleChoiceInputRequest)fRequest).getChoices().iterator();i.hasNext();) {
124                 fCombo.add((String JavaDoc)i.next());
125                 fCombo.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL| GridData.HORIZONTAL_ALIGN_FILL));
126                 fCombo.addSelectionListener(new SelectionAdapter() {
127                     public void widgetSelected(SelectionEvent e) {
128                         validateInput();
129                     }
130                 });
131             }
132         } else {
133             fText = new Text(fDialog, SWT.SINGLE | SWT.BORDER);
134             fText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL| GridData.HORIZONTAL_ALIGN_FILL));
135             fText.addModifyListener(new ModifyListener() {
136                 public void modifyText(ModifyEvent e) {
137                     validateInput();
138                 }
139             });
140         }
141         
142         String JavaDoc value = null;
143         try {
144             fRequest.getClass().getMethod("getDefaultValue", new Class JavaDoc[0]); //$NON-NLS-1$
145
value = fRequest.getDefaultValue();
146         } catch (SecurityException JavaDoc e) {
147         } catch (NoSuchMethodException JavaDoc e) {
148             //pre Ant 1.7.0
149
}
150         
151         fErrorMessageText = new Text(fDialog, SWT.READ_ONLY);
152         fErrorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
153         fErrorMessageText.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
154         
155         createButtonBar(fDialog, result);
156         
157         if (value != null) {
158             if (fCombo != null) {
159                 fCombo.select(fCombo.indexOf(value));
160             } else {
161                 fText.setText(value);
162                 fText.selectAll();
163             }
164         }
165         fDialog.pack();
166     }
167
168     protected void setErrorMessage(String JavaDoc errorMessage) {
169         fErrorMessageText.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
170
fOkButton.setEnabled(errorMessage == null);
171         fErrorMessageText.getParent().update();
172     }
173     
174     protected void validateInput() {
175         String JavaDoc errorMessage = null;
176         if (fRequest instanceof MultipleChoiceInputRequest) {
177             fRequest.setInput(fCombo.getText());
178         } else {
179             fRequest.setInput(fText.getText());
180         }
181         if (!fRequest.isInputValid()) {
182             if (fFirstValidation) {
183                 errorMessage= ""; //$NON-NLS-1$
184
fFirstValidation= false;
185             } else {
186                 errorMessage= RemoteAntMessages.getString("SWTInputHandler.3"); //$NON-NLS-1$
187
}
188        }
189        
190         setErrorMessage(errorMessage);
191     }
192     
193     protected Control createButtonBar(Composite parent, boolean[] result) {
194         Composite composite = new Composite(parent, SWT.NONE);
195         GridLayout layout = new GridLayout();
196         layout.numColumns = 2;
197         layout.makeColumnsEqualWidth = true;
198         composite.setLayout(layout);
199         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_CENTER);
200         composite.setLayoutData(data);
201         composite.setFont(parent.getFont());
202        
203         createButtonsForButtonBar(composite, result);
204         return composite;
205     }
206     
207     protected void createButtonsForButtonBar(Composite parent, final boolean[] result) {
208         fOkButton = new Button(parent, SWT.PUSH);
209         fOkButton.setText(RemoteAntMessages.getString("SWTInputHandler.4")); //$NON-NLS-1$
210
setButtonLayoutData(fOkButton);
211         
212         Button cancel = new Button(parent, SWT.PUSH);
213         cancel.setText(RemoteAntMessages.getString("SWTInputHandler.5")); //$NON-NLS-1$
214
Listener listener = new Listener() {
215             public void handleEvent(Event event) {
216                 result[0] = event.widget == fOkButton;
217                 fDialog.close();
218             }
219         };
220         setButtonLayoutData(cancel);
221         fOkButton.addListener(SWT.Selection, listener);
222         fDialog.setDefaultButton(fOkButton);
223         cancel.addListener(SWT.Selection, listener);
224         //do this here because setting the text will set enablement on the ok button
225
if (fRequest instanceof MultipleChoiceInputRequest) {
226             fCombo.setFocus();
227         } else {
228             fText.setFocus();
229         }
230     }
231     
232     private void setButtonLayoutData(Button button) {
233         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
234         int widthHint = convertHorizontalDLUsToPixels(61); //from IDialogConstants.BUTTON_WIDTH
235
Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
236         data.widthHint = Math.max(widthHint, minSize.x);
237         button.setLayoutData(data);
238     }
239
240     private int convertHorizontalDLUsToPixels(int dlus) {
241         // round to the nearest pixel
242
return (fFontMetrics.getAverageCharWidth() * dlus + 4 / 2) / 4;
243     }
244
245     protected void initializeDialogUnits(Control control) {
246         // Compute and store a font metric
247
GC gc = new GC(control);
248         gc.setFont(control.getFont());
249         fFontMetrics = gc.getFontMetrics();
250         gc.dispose();
251     }
252 }
Popular Tags