~ubuntu-branches/ubuntu/raring/ant-contrib/raring

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/antcontrib/input/GUIInputHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-09-26 08:45:47 UTC
  • Revision ID: james.westby@ubuntu.com-20090926084547-ynj34y27mg9dr60c
Tags: upstream-1.0~b3+svn177
ImportĀ upstreamĀ versionĀ 1.0~b3+svn177

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2001-2004 Ant-Contrib project.  All rights reserved.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
package net.sf.antcontrib.input;
 
17
 
 
18
import java.awt.*;
 
19
import javax.swing.*;
 
20
 
 
21
import org.apache.tools.ant.BuildException;
 
22
import org.apache.tools.ant.input.InputHandler;
 
23
import org.apache.tools.ant.input.InputRequest;
 
24
import org.apache.tools.ant.input.MultipleChoiceInputRequest;
 
25
 
 
26
/**
 
27
 * Prompts for user input using a JOptionPane. Developed for use with
 
28
 * Antelope, migrated to ant-contrib Oct 2003.
 
29
 *
 
30
 * @author <a href="mailto:danson@germane-software.com">Dale Anson</a>
 
31
 * @version $Revision: 1.3 $
 
32
 * @since Ant 1.5
 
33
 */
 
34
public class GUIInputHandler implements InputHandler {
 
35
 
 
36
    private Component parent = null;
 
37
 
 
38
    public GUIInputHandler() {}
 
39
 
 
40
    /**
 
41
     * @param parent the parent component to display the input dialog.
 
42
     */
 
43
    public GUIInputHandler( Component parent ) {
 
44
        this.parent = parent;
 
45
    }
 
46
 
 
47
    /**
 
48
     * Prompts and requests input.  May loop until a valid input has
 
49
     * been entered.
 
50
     */
 
51
    public void handleInput( InputRequest request ) throws BuildException {
 
52
 
 
53
        if ( request instanceof MultipleChoiceInputRequest ) {
 
54
            String prompt = request.getPrompt();
 
55
            String title = "Select Input";
 
56
            int optionType = JOptionPane.YES_NO_OPTION;
 
57
            int messageType = JOptionPane.QUESTION_MESSAGE;
 
58
            Icon icon = null;
 
59
            Object[] choices = ( ( MultipleChoiceInputRequest ) request ).getChoices().toArray();
 
60
            Object initialChoice = null;
 
61
            do {
 
62
                Object input = JOptionPane.showInputDialog(parent, prompt, 
 
63
                    title, optionType, icon, choices, initialChoice);
 
64
                if (input == null)
 
65
                   throw new BuildException("User cancelled.");
 
66
                request.setInput(input.toString());
 
67
            } while (!request.isInputValid());
 
68
            
 
69
        }
 
70
        else {
 
71
            do {
 
72
                String input = JOptionPane.showInputDialog( parent, request.getPrompt() );
 
73
                request.setInput( input );
 
74
            } while ( !request.isInputValid() );
 
75
        }
 
76
    }
 
77
 
 
78
}