KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ant.internal.ui.antsupport.inputhandler;
12
13
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.input.DefaultInputHandler;
16 import org.apache.tools.ant.input.InputRequest;
17 import org.eclipse.ant.internal.ui.antsupport.AntSupportMessages;
18 import org.eclipse.jface.dialogs.IInputValidator;
19 import org.eclipse.jface.dialogs.InputDialog;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Display;
23
24 /**
25  * The default input handler when using Ant within Eclipse.
26  * This is the class that will respond to <input> requests from
27  * within an Ant build file.
28  * If the build is occurring in Ant 1.6.0 and the -noinput option has been specified
29  * this input handler will fail.
30  */

31 public class AntInputHandler extends DefaultInputHandler {
32     
33     /* (non-Javadoc)
34      * @see org.apache.tools.ant.input.InputHandler#handleInput(org.apache.tools.ant.input.InputRequest)
35      */

36     public void handleInput(InputRequest request) throws BuildException {
37         if (System.getProperty("eclipse.ant.noInput") != null) { //$NON-NLS-1$
38
throw new BuildException(AntSupportMessages.AntInputHandler_5);
39         }
40         BuildException[] problem= new BuildException[1];
41         Runnable JavaDoc runnable= getHandleInputRunnable(request, problem);
42         Display.getDefault().syncExec(runnable);
43         if (problem[0] != null) {
44             throw problem[0];
45         }
46     }
47     
48     protected Runnable JavaDoc getHandleInputRunnable(final InputRequest request, final BuildException[] problem) {
49         return new Runnable JavaDoc() {
50             public void run() {
51                 String JavaDoc prompt = getPrompt(request);
52                 String JavaDoc title= AntSupportMessages.AntInputHandler_Ant_Input_Request_1;
53                 IInputValidator validator= new IInputValidator() {
54                     private boolean fFirstValidation =true;
55                     public String JavaDoc isValid(String JavaDoc value) {
56                         request.setInput(value);
57                         if (request.isInputValid()) {
58                             return null;
59                         }
60                         if (fFirstValidation) {
61                             fFirstValidation= false;
62                             return ""; //$NON-NLS-1$
63
}
64                         return AntSupportMessages.AntInputHandler_Invalid_input_2;
65                     }
66                 };
67         
68                 String JavaDoc initialValue = null;
69                 try {
70                     request.getClass().getMethod("getDefaultValue", new Class JavaDoc[0]); //$NON-NLS-1$
71
initialValue = request.getDefaultValue();
72                 } catch (SecurityException JavaDoc e) {
73                 } catch (NoSuchMethodException JavaDoc e) {
74                     //pre Ant 1.7.0
75
}
76                 InputDialog dialog= new InputDialog(null, title, prompt, initialValue, validator) {
77                     protected int getShellStyle() {
78                         return super.getShellStyle() | SWT.RESIZE;
79                     }
80                 };
81                 if (dialog.open() != Window.OK) {
82                     problem[0]= new BuildException(AntSupportMessages.AntInputHandler_Unable_to_respond_to__input__request_4);
83                 }
84             }
85         };
86     }
87 }
88
Popular Tags