/* * MyGetUserAndPassword.java * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.anttasks; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** A new ant task to prompt the user for username and password. * To use this task, the compiled class must be put in the classpath. add a taskdef line to the build.xml: * * and call it like the following: * * the if and unless attributes are used to control whether this task runs or not. If the 'if' attribute is set, the task will only run if the specified property is defined. If the unless attribute is set, the task will only run if the property is not defined. */ public class MyGetUserAndPassword extends org.apache.tools.ant.Task { private String message = null; private String user_property = null; private String pword_property = null; private String username = null; private String password = null; private boolean cancelled = false; private Dimension SIZE = new Dimension(300, 150); private JButton ok_button = null; private JTextField name_field = null; private JPasswordField pass_field = null; private boolean do_the_task = true; public void setMessage(String m) { message = m; } /** specify a property name - the task will only execute if this property is set */ public void setIf(String property) { String value = getProject().getProperty(property); if (value== null) { do_the_task = false; } } public void setPwordproperty(String p_prop) { pword_property = p_prop; } /** specify a property name - the task will only execute if this property is not set */ public void setUnless(String property) { String value = getProject().getProperty(property); if (value != null) { do_the_task = false; } } public void setUsername(String user) { username = user; } public void setUserproperty(String u_prop) { user_property = u_prop; } public void execute() throws org.apache.tools.ant.BuildException { if (!do_the_task) { return; } if (pword_property == null) { throw new org.apache.tools.ant.BuildException("pwordproperty attribute must be specified"); } JFrame hiddenFrame = new JFrame("Username and Password Hidden Frame"); final JDialog dialog = new JDialog(hiddenFrame, "Username and Password", true); JPanel content_pane = (JPanel)dialog.getContentPane(); content_pane.setLayout(new BorderLayout(5,5)); content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); dialog.setLocation(200, 200); dialog.setSize(SIZE); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { cancelled = true; dialog.dispose(); } }); JPanel button_pane = new JPanel(); button_pane.setLayout(new GridLayout(1,2)); JPanel details_pane = new JPanel(); details_pane.setLayout(new BorderLayout(5,5)); JPanel labels_pane = new JPanel(); labels_pane.setLayout(new GridLayout(2,1)); JPanel fields_pane = new JPanel(); fields_pane.setLayout(new GridLayout(2,1)); JTextArea instruction_textarea = new JTextArea(); instruction_textarea.setEditable(false); instruction_textarea.setLineWrap(true); instruction_textarea.setRows(2); instruction_textarea.setWrapStyleWord(true); String full_message = "Please enter your username and password"; if (message != null) { full_message = message + "\n" + full_message; } instruction_textarea.setText(full_message); JLabel name_label = new JLabel("User name:"); name_field = new JTextField(16); if (username != null) { name_field.setText(username); } if (user_property == null ) { name_field.setEditable(false); } JLabel pass_label = new JLabel("Password:"); pass_field = new JPasswordField(16); FieldListener field_listener = new FieldListener(); name_field.addActionListener(field_listener); name_field.getDocument().addDocumentListener(field_listener); pass_field.addActionListener(field_listener); pass_field.getDocument().addDocumentListener(field_listener); ok_button = new JButton("Ok"); ok_button.setEnabled(false); ok_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { username = name_field.getText(); password = new String(pass_field.getPassword()); dialog.dispose(); } }); JButton cancel_button = new JButton("Cancel"); cancel_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { cancelled = true; dialog.dispose(); } }); button_pane.add(ok_button); button_pane.add(cancel_button); labels_pane.add(name_label); labels_pane.add(pass_label); fields_pane.add(name_field); fields_pane.add(pass_field); details_pane.add(labels_pane, BorderLayout.WEST); details_pane.add(fields_pane, BorderLayout.CENTER); content_pane.add(instruction_textarea, BorderLayout.NORTH); content_pane.add(details_pane, BorderLayout.CENTER); content_pane.add(button_pane, BorderLayout.SOUTH); dialog.show(); hiddenFrame.dispose(); if (cancelled) { throw new org.apache.tools.ant.BuildException("Cancelled by user."); } if (user_property != null) { getProject().setProperty(user_property, username); } getProject().setProperty(pword_property, password); } private class FieldListener implements ActionListener, DocumentListener { public void actionPerformed(ActionEvent actionEvent) { validateOkButton(); } public void changedUpdate(DocumentEvent e) { validateOkButton(); } public void insertUpdate(DocumentEvent e) { validateOkButton(); } public void removeUpdate(DocumentEvent e) { validateOkButton(); } private void validateOkButton() { if (name_field.getText().length()!=0 && pass_field.getPassword().length!=0) { ok_button.setEnabled(true); } else { ok_button.setEnabled(false); } } } public static void main(String [] args) { MyGetUserAndPassword task = new MyGetUserAndPassword(); task.setMessage("hi there kath"); task.setUserproperty("username"); task.setPwordproperty("password"); task.execute(); } }