source: trunk/gsdl3/src/java/org/greenstone/anttasks/MyGetUserAndPassword.java@ 10299

Last change on this file since 10299 was 10299, checked in by kjdon, 19 years ago

made this more general - now specify what user and pword properties should be set

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/*
2 * MyGetUserAndPassword.java
3 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.anttasks;
20
21import java.awt.*;
22import java.awt.event.*;
23import javax.swing.*;
24import javax.swing.event.*;
25
26/** A new ant task to prompt the user for username and password.
27 * To use this task, the compiled class must be put in the classpath. add a taskdef line to the build.xml:
28 * <taskdef name="getuserandpassword" classname="org.greenstone.anttasks.MyGetUserAndPassword" />
29 * and call it like the following:
30 * <getuserandpassword
31 * message="a message to display at the top of the dialog"
32 * userproperty="the name of the username property to set"
33 * pwordproperty="the name of the password property to set"
34 * username="default value for username (optional)"
35 * if="property name (optional)"
36 * unless="property name (optional)"/>
37 * 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.
38*/
39
40public class MyGetUserAndPassword
41 extends org.apache.tools.ant.Task {
42
43 private String message = null;
44 private String user_property = null;
45 private String pword_property = null;
46 private String username = null;
47 private String password = null;
48 private boolean cancelled = false;
49
50 private Dimension SIZE = new Dimension(300, 150);
51 private JButton ok_button = null;
52 private JTextField name_field = null;
53 private JPasswordField pass_field = null;
54
55 private boolean do_the_task = true;
56 public void setMessage(String m) {
57 message = m;
58 }
59
60 /** specify a property name - the task will only execute if this property is set */
61 public void setIf(String property) {
62 String value = getProject().getProperty(property);
63 if (value== null) {
64 do_the_task = false;
65 }
66 }
67
68 public void setPwordproperty(String p_prop) {
69 pword_property = p_prop;
70 }
71
72 /** specify a property name - the task will only execute if this property is not set */
73 public void setUnless(String property) {
74 String value = getProject().getProperty(property);
75 if (value != null) {
76 do_the_task = false;
77 }
78 }
79
80 public void setUsername(String user) {
81 username = user;
82 }
83
84 public void setUserproperty(String u_prop) {
85 user_property = u_prop;
86 }
87 public void execute() throws org.apache.tools.ant.BuildException {
88
89 if (!do_the_task) {
90 return;
91 }
92 if (user_property == null || pword_property == null) {
93 throw new org.apache.tools.ant.BuildException("userproperty and pwordproperty attributes must be specified");
94 }
95
96 JFrame hiddenFrame = new JFrame("Username and Password Hidden Frame");
97 final JDialog dialog = new JDialog(hiddenFrame, "Username and Password", true);
98 JPanel content_pane = (JPanel)dialog.getContentPane();
99 content_pane.setLayout(new BorderLayout(5,5));
100 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
101 dialog.setLocation(200, 200);
102 dialog.setSize(SIZE);
103 dialog.addWindowListener(new WindowAdapter() {
104 public void windowClosing(WindowEvent windowEvent) {
105 cancelled = true;
106 dialog.dispose();
107 }
108 });
109
110 JPanel button_pane = new JPanel();
111 button_pane.setLayout(new GridLayout(1,2));
112 JPanel details_pane = new JPanel();
113 details_pane.setLayout(new BorderLayout(5,5));
114 JPanel labels_pane = new JPanel();
115 labels_pane.setLayout(new GridLayout(2,1));
116 JPanel fields_pane = new JPanel();
117 fields_pane.setLayout(new GridLayout(2,1));
118
119 JTextArea instruction_textarea = new JTextArea();
120 instruction_textarea.setEditable(false);
121 instruction_textarea.setLineWrap(true);
122 instruction_textarea.setRows(2);
123 instruction_textarea.setWrapStyleWord(true);
124 String full_message = "Please enter your username and password";
125 if (message != null) {
126 full_message = message + "\n" + full_message;
127 }
128 instruction_textarea.setText(full_message);
129
130 JLabel name_label = new JLabel("User name:");
131 name_field = new JTextField(16);
132 if (username != null) {
133 name_field.setText(username);
134 }
135 JLabel pass_label = new JLabel("Password:");
136 pass_field = new JPasswordField(16);
137
138
139 FieldListener field_listener = new FieldListener();
140 name_field.addActionListener(field_listener);
141 name_field.getDocument().addDocumentListener(field_listener);
142 pass_field.addActionListener(field_listener);
143 pass_field.getDocument().addDocumentListener(field_listener);
144
145 ok_button = new JButton("Ok");
146 ok_button.setEnabled(false);
147 ok_button.addActionListener(new ActionListener() {
148 public void actionPerformed(ActionEvent actionEvent) {
149 username = name_field.getText();
150 password = new String(pass_field.getPassword());
151 dialog.dispose();
152 }
153 });
154
155 JButton cancel_button = new JButton("Cancel");
156 cancel_button.addActionListener(new ActionListener() {
157 public void actionPerformed(ActionEvent actionEvent) {
158 cancelled = true;
159 dialog.dispose();
160 }
161 });
162
163 button_pane.add(ok_button);
164 button_pane.add(cancel_button);
165
166 labels_pane.add(name_label);
167 labels_pane.add(pass_label);
168
169 fields_pane.add(name_field);
170 fields_pane.add(pass_field);
171
172 details_pane.add(labels_pane, BorderLayout.WEST);
173 details_pane.add(fields_pane, BorderLayout.CENTER);
174
175 content_pane.add(instruction_textarea, BorderLayout.NORTH);
176 content_pane.add(details_pane, BorderLayout.CENTER);
177 content_pane.add(button_pane, BorderLayout.SOUTH);
178
179 dialog.show();
180 hiddenFrame.dispose();
181
182 if (cancelled) {
183 throw new org.apache.tools.ant.BuildException("Cancelled by user.");
184 }
185
186 getProject().setProperty(user_property, username);
187 getProject().setProperty(pword_property, password);
188
189
190
191 }
192 private class FieldListener
193 implements ActionListener, DocumentListener {
194
195 public void actionPerformed(ActionEvent actionEvent) {
196 validateOkButton();
197 }
198 public void changedUpdate(DocumentEvent e) {
199
200 validateOkButton();
201 }
202
203 public void insertUpdate(DocumentEvent e) {
204
205 validateOkButton();
206 }
207
208 public void removeUpdate(DocumentEvent e) {
209
210 validateOkButton();
211 }
212
213 private void validateOkButton() {
214 if (name_field.getText().length()!=0 &&
215 pass_field.getPassword().length!=0) {
216 ok_button.setEnabled(true);
217 } else {
218 ok_button.setEnabled(false);
219 }
220 }
221 }
222
223
224 public static void main(String [] args) {
225 MyGetUserAndPassword task = new MyGetUserAndPassword();
226 task.setMessage("hi there kath");
227 task.setUserproperty("username");
228 task.setPwordproperty("password");
229 task.execute();
230
231 }
232}
Note: See TracBrowser for help on using the repository browser.