ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ivan Ivanov <rambiusparkisan...@yahoo.com>
Subject Re: AW: Creating custom InputHandler
Date Wed, 14 Jul 2004 07:09:48 GMT
Jan, 
besides PropertyExpandInputHandler I also wrote
another InputHandler and I am thinking that I bumped
into a limitation of <input> task. The new
InputHandler is called PasswordInputHandler and it
shows a JDialog where the user the enters some text in
a JPasswordField and her input is masked.
Here is the code:

package org.apache.tools.ant.input;

import java.util.Enumeration;

import java.awt.Dimension;
import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;


public class PasswordInputHandler implements
InputHandler {

    /**
     * Default non-arg constructor
     */
    public PasswordInputHandler() {
    }

    /**
     * Prompts and requests input. The user enters the
     * input in a JPasswordField where the input is
masked.
     * @param request the request to handle
     * @throws BuildException if not possible to read
from console
     */
    public void handleInput(final InputRequest
request) {
        String prompt = getPrompt(request);
        final JDialog dlgPassword = new JDialog();
        dlgPassword.setTitle("Enter Password");
        dlgPassword.setModal(true);
        final JPasswordField fldPassword = new
JPasswordField();
        fldPassword.setPreferredSize(new
Dimension(150,18));
        JLabel lblPassword = new JLabel(prompt);
        JButton btnOk = new JButton("OK");
        btnOk.setPreferredSize(new Dimension(120,20));
        JPanel pan = new JPanel();
        BoxLayout layout1 = new BoxLayout(pan ,
BoxLayout.X_AXIS);
        pan.setLayout(layout1);
       
pan.setBorder(BorderFactory.createEmptyBorder(5,
5,0,5));
        pan.add(lblPassword);
        pan.add(Box.createHorizontalGlue());
        pan.add(fldPassword);
        BoxLayout layout2  = new
BoxLayout(dlgPassword.getContentPane(),
BoxLayout.Y_AXIS);
       
dlgPassword.getContentPane().setLayout(layout2);
        dlgPassword.getContentPane().add(pan);
        JPanel pan2 = new JPanel();
        BoxLayout layout3 = new BoxLayout(pan2 ,
BoxLayout.X_AXIS);
        pan2.setLayout(layout3);
        pan2.add(btnOk);
       
dlgPassword.getContentPane().add(Box.createVerticalStrut(5));
        dlgPassword.getContentPane().add(pan2);
        dlgPassword.pack();
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
{
                String input = fldPassword.getText();
                request.setInput(input);
                dlgPassword.setVisible(true);
                dlgPassword.dispose();
            }
        });
        dlgPassword.setVisible(true);
    }

    /**
     * Constructs user prompt from a request.
     *
     * <p>This implementation adds
(choice1,choice2,choice3,...) to the
     * prompt for
<code>MultipleChoiceInputRequest</code>s.</p>
     *
     * @param request the request to construct the
prompt for.
     *                Must not be <code>null</code>.
     * @return the prompt to ask the user
     */
    protected String getPrompt(InputRequest request) {
        String prompt = request.getPrompt();
        if (request instanceof
MultipleChoiceInputRequest) {
            StringBuffer sb = new
StringBuffer(prompt);
            sb.append("(");
            Enumeration e =
                ((MultipleChoiceInputRequest)
request).getChoices().elements();
            boolean first = true;
            while (e.hasMoreElements()) {
                if (!first) {
                    sb.append(",");
                }
                sb.append(e.nextElement());
                first = false;
            }
            sb.append(")");
            prompt = sb.toString();
        }
        return prompt;
    }
}

My questions and proposition are:
1) Suppose that besides enetering a password we want
to ask the user for a user name (say its cvs
username). Setting the inputhandler globally to a
project means that the username inputted will also be
masked and this is not good practice. So I think that
inputhandlers must be set to concrete <input>s and to
the whole project.
2) While <input> task breaks the automation, it can
make Ant a decent installer, because we can collect
information from the user as where she wants to
install the product, etc. If we replace JPasswordField
in the above code with simple JTextField we can do it.
However, this opens the issue that one might want not
a JTextField but a JComboBox, and not with the default
Look & Feel, but with his own. Can you recommend me a
way to make the above code more extensible in terms of
the used Swing Components?

Again thank you very much for your advice

Regards Ivan

P.S. I wish all projects I worked on were as clearly
written as Ant.




		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message