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

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

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 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="MyGetUserAndPassword" />
29 * and call it like the following:
30 * <getuserandpassword message="a message to display at the top of the dialog"
31 * username="if the username is already specified" if="property name" unless="property name"/>
32 * 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.
33*/
34
35public class MyGetUserAndPassword
36 extends org.apache.tools.ant.Task {
37
38 private String message = null;
39
40 private String username = null;
41 private String password = null;
42 private boolean cancelled = false;
43
44 private Dimension SIZE = new Dimension(300, 150);
45 private JButton ok_button = null;
46 private JTextField name_field = null;
47 private JPasswordField pass_field = null;
48
49 private boolean do_the_task = true;
50 public void setMessage(String m) {
51 message = m;
52 }
53
54 /** specify a property name - the task will only execute if this property is set */
55 public void setIf(String property) {
56 String value = getProject().getProperty(property);
57 if (value== null) {
58 do_the_task = false;
59 }
60 }
61
62 /** specify a property name - the task will only execute if this property is not set */
63 public void setUnless(String property) {
64 String value = getProject().getProperty(property);
65 if (value != null) {
66 do_the_task = false;
67 }
68 }
69
70 public void setUsername(String user) {
71 username = user;
72 }
73
74 public void execute() throws org.apache.tools.ant.BuildException {
75
76 if (!do_the_task) {
77 return;
78 }
79 JFrame hiddenFrame = new JFrame("Username and Password Hidden Frame");
80 final JDialog dialog = new JDialog(hiddenFrame, "Username and Password", true);
81 JPanel content_pane = (JPanel)dialog.getContentPane();
82 content_pane.setLayout(new BorderLayout(5,5));
83 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
84 dialog.setLocation(200, 200);
85 dialog.setSize(SIZE);
86 dialog.addWindowListener(new WindowAdapter() {
87 public void windowClosing(WindowEvent windowEvent) {
88 cancelled = true;
89 dialog.dispose();
90 }
91 });
92
93 JPanel button_pane = new JPanel();
94 button_pane.setLayout(new GridLayout(1,2));
95 JPanel details_pane = new JPanel();
96 details_pane.setLayout(new BorderLayout(5,5));
97 JPanel labels_pane = new JPanel();
98 labels_pane.setLayout(new GridLayout(2,1));
99 JPanel fields_pane = new JPanel();
100 fields_pane.setLayout(new GridLayout(2,1));
101
102 JTextArea instruction_textarea = new JTextArea();
103 instruction_textarea.setEditable(false);
104 instruction_textarea.setLineWrap(true);
105 instruction_textarea.setRows(2);
106 instruction_textarea.setWrapStyleWord(true);
107 String full_message = "Please enter your username and password";
108 if (message != null) {
109 full_message = message + "\n" + full_message;
110 }
111 instruction_textarea.setText(full_message);
112
113 JLabel name_label = new JLabel("User name:");
114 name_field = new JTextField(16);
115 if (username != null) {
116 name_field.setText(username);
117 }
118 JLabel pass_label = new JLabel("Password:");
119 pass_field = new JPasswordField(16);
120
121
122 FieldListener field_listener = new FieldListener();
123 name_field.addActionListener(field_listener);
124 name_field.getDocument().addDocumentListener(field_listener);
125 pass_field.addActionListener(field_listener);
126 pass_field.getDocument().addDocumentListener(field_listener);
127
128 ok_button = new JButton("Ok");
129 ok_button.setEnabled(false);
130 ok_button.addActionListener(new ActionListener() {
131 public void actionPerformed(ActionEvent actionEvent) {
132 username = name_field.getText();
133 password = new String(pass_field.getPassword());
134 dialog.dispose();
135 }
136 });
137
138 JButton cancel_button = new JButton("Cancel");
139 cancel_button.addActionListener(new ActionListener() {
140 public void actionPerformed(ActionEvent actionEvent) {
141 cancelled = true;
142 dialog.dispose();
143 }
144 });
145
146 button_pane.add(ok_button);
147 button_pane.add(cancel_button);
148
149 labels_pane.add(name_label);
150 labels_pane.add(pass_label);
151
152 fields_pane.add(name_field);
153 fields_pane.add(pass_field);
154
155 details_pane.add(labels_pane, BorderLayout.WEST);
156 details_pane.add(fields_pane, BorderLayout.CENTER);
157
158 content_pane.add(instruction_textarea, BorderLayout.NORTH);
159 content_pane.add(details_pane, BorderLayout.CENTER);
160 content_pane.add(button_pane, BorderLayout.SOUTH);
161
162 dialog.show();
163 hiddenFrame.dispose();
164
165 if (cancelled) {
166 throw new org.apache.tools.ant.BuildException("Cancelled by user.");
167 }
168
169 getProject().setProperty("proxy.username", username);
170 getProject().setProperty("proxy.password", password);
171
172
173
174 }
175 private class FieldListener
176 implements ActionListener, DocumentListener {
177
178 public void actionPerformed(ActionEvent actionEvent) {
179 validateOkButton();
180 }
181 public void changedUpdate(DocumentEvent e) {
182
183 validateOkButton();
184 }
185
186 public void insertUpdate(DocumentEvent e) {
187
188 validateOkButton();
189 }
190
191 public void removeUpdate(DocumentEvent e) {
192
193 validateOkButton();
194 }
195
196 private void validateOkButton() {
197 if (name_field.getText().length()!=0 &&
198 pass_field.getPassword().length!=0) {
199 ok_button.setEnabled(true);
200 } else {
201 ok_button.setEnabled(false);
202 }
203 }
204 }
205
206
207 public static void main(String [] args) {
208 MyGetUserAndPassword task = new MyGetUserAndPassword();
209 task.setMessage("hi there kath");
210 task.execute();
211
212 }
213}
Note: See TracBrowser for help on using the repository browser.