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

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

made the username property optional

  • 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 (optional)"
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 (pword_property == null) {
93 throw new org.apache.tools.ant.BuildException("pwordproperty attribute 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 if (user_property == null ) {
136 name_field.setEditable(false);
137 }
138 JLabel pass_label = new JLabel("Password:");
139 pass_field = new JPasswordField(16);
140
141
142 FieldListener field_listener = new FieldListener();
143 name_field.addActionListener(field_listener);
144 name_field.getDocument().addDocumentListener(field_listener);
145 pass_field.addActionListener(field_listener);
146 pass_field.getDocument().addDocumentListener(field_listener);
147
148 ok_button = new JButton("Ok");
149 ok_button.setEnabled(false);
150 ok_button.addActionListener(new ActionListener() {
151 public void actionPerformed(ActionEvent actionEvent) {
152 username = name_field.getText();
153 password = new String(pass_field.getPassword());
154 dialog.dispose();
155 }
156 });
157
158 JButton cancel_button = new JButton("Cancel");
159 cancel_button.addActionListener(new ActionListener() {
160 public void actionPerformed(ActionEvent actionEvent) {
161 cancelled = true;
162 dialog.dispose();
163 }
164 });
165
166 button_pane.add(ok_button);
167 button_pane.add(cancel_button);
168
169 labels_pane.add(name_label);
170 labels_pane.add(pass_label);
171
172 fields_pane.add(name_field);
173 fields_pane.add(pass_field);
174
175 details_pane.add(labels_pane, BorderLayout.WEST);
176 details_pane.add(fields_pane, BorderLayout.CENTER);
177
178 content_pane.add(instruction_textarea, BorderLayout.NORTH);
179 content_pane.add(details_pane, BorderLayout.CENTER);
180 content_pane.add(button_pane, BorderLayout.SOUTH);
181
182 dialog.show();
183 hiddenFrame.dispose();
184
185 if (cancelled) {
186 throw new org.apache.tools.ant.BuildException("Cancelled by user.");
187 }
188
189 if (user_property != null) {
190 getProject().setProperty(user_property, username);
191 }
192 getProject().setProperty(pword_property, password);
193
194
195
196 }
197 private class FieldListener
198 implements ActionListener, DocumentListener {
199
200 public void actionPerformed(ActionEvent actionEvent) {
201 validateOkButton();
202 }
203 public void changedUpdate(DocumentEvent e) {
204
205 validateOkButton();
206 }
207
208 public void insertUpdate(DocumentEvent e) {
209
210 validateOkButton();
211 }
212
213 public void removeUpdate(DocumentEvent e) {
214
215 validateOkButton();
216 }
217
218 private void validateOkButton() {
219 if (name_field.getText().length()!=0 &&
220 pass_field.getPassword().length!=0) {
221 ok_button.setEnabled(true);
222 } else {
223 ok_button.setEnabled(false);
224 }
225 }
226 }
227
228
229 public static void main(String [] args) {
230 MyGetUserAndPassword task = new MyGetUserAndPassword();
231 task.setMessage("hi there kath");
232 task.setUserproperty("username");
233 task.setPwordproperty("password");
234 task.execute();
235
236 }
237}
Note: See TracBrowser for help on using the repository browser.