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

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

small changes

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