source: gli/trunk/src/org/greenstone/gatherer/gui/FedoraLogin.java@ 15505

Last change on this file since 15505 was 15505, checked in by ak19, 16 years ago

Login error message has a yellow background

File size: 16.7 KB
Line 
1/*
2 * -----------------------------------------------------------------------------
3 *
4 * <p><b>License and Copyright: </b>The contents of this file are subject
5 * to the Educational Community License (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy
7 * of the License at <a href="http://www.opensource.org/licenses/ecl1.txt">
8 * http://www.opensource.org/licenses/ecl1.txt.</a></p>
9 *
10 * <p>Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
12 * License for the specific language governing rights and limitations under
13 * the License.</p>
14 *
15 * <p>The entire file consists of original code. Copyright &copy;
16 * 2002-2007 by The Rector and Visitors of the University of Virginia and
17 * Cornell University. All rights reserved.</p>
18 *
19 * -----------------------------------------------------------------------------
20 */
21
22
23/* Based on Fedora's Client LoginDialog.java code */
24/* Modified to work with Greenstone: 22 Jan 2008 */
25
26package org.greenstone.gatherer.gui;
27
28import java.awt.BorderLayout;
29import java.awt.Container;
30import java.awt.Dimension;
31import java.awt.GridBagConstraints;
32import java.awt.GridBagLayout;
33import java.awt.Insets;
34import java.awt.event.ActionEvent;
35import java.awt.event.WindowAdapter;
36import java.awt.event.WindowEvent;
37
38import java.io.File;
39import java.io.FileInputStream;
40import java.io.FileOutputStream;
41import java.io.IOException;
42
43import java.util.Enumeration;
44import java.util.HashMap;
45import java.util.Iterator;
46import java.util.List;
47import java.util.Properties;
48
49import javax.swing.AbstractAction;
50import javax.swing.BorderFactory;
51import javax.swing.JButton;
52import javax.swing.JComboBox;
53import javax.swing.JComponent;
54import javax.swing.JDialog;
55import javax.swing.JLabel;
56import javax.swing.JOptionPane;
57import javax.swing.JPanel;
58import javax.swing.JPasswordField;
59import javax.swing.event.DocumentEvent;
60import javax.swing.event.DocumentListener;
61
62import java.awt.Rectangle;
63import java.awt.Toolkit;
64
65
66
67import org.greenstone.gatherer.Gatherer;
68
69public class FedoraLogin extends JDialog
70{
71 private static final java.awt.Color LIGHT = new java.awt.Color(244, 244, 224);
72 private static final java.awt.Color TRANSPARENT = new java.awt.Color(0,0,0,0);
73 private JPanel m_infoPane;
74 private JComboBox m_serverComboBox;
75 private JComboBox m_protocolComboBox;
76 private JComboBox m_usernameComboBox;
77 private JPasswordField m_passwordField;
78
79 private String m_lastUsername="fedoraAdmin";
80 private String m_lastServer="localhost:8080";
81 private String m_lastProtocol="http";
82 private String m_lastPassword="";
83
84 private HashMap m_usernames;
85 private HashMap m_servers;
86 private HashMap m_protocols;
87
88 private boolean login_requested = false;
89
90 public FedoraLogin(String title, boolean can_cancel)
91 {
92 super(Gatherer.g_man, "Login", true);
93 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
94
95 m_servers=new HashMap();
96 m_protocols=new HashMap();
97 m_protocols.put("http", "");
98 m_protocols.put("https", "");
99 m_usernames=new HashMap();
100
101 JLabel serverLabel=new JLabel("Fedora Server");
102 JLabel protocolLabel=new JLabel("Protocol");
103 JLabel usernameLabel=new JLabel("Username");
104 JLabel passwordLabel=new JLabel("Password");
105
106 m_serverComboBox=new JComboBox();
107 m_serverComboBox.setEditable(true);
108 m_protocolComboBox=new JComboBox();
109 m_protocolComboBox.setEditable(true);
110 m_usernameComboBox=new JComboBox();
111 m_usernameComboBox.setEditable(true);
112 m_passwordField=new JPasswordField();
113
114 setComboBoxValues();
115
116
117 LoginAction loginAction=new LoginAction(this);
118 JButton loginButton=new JButton(loginAction);
119
120 loginAction.setButton(loginButton);
121 loginButton.setEnabled(false);
122
123
124 m_passwordField.getDocument().addDocumentListener(
125 new PasswordChangeListener(loginButton, m_passwordField));
126 m_passwordField.setAction(loginAction);
127
128 JPanel inputPane=new JPanel();
129 inputPane.setBorder(BorderFactory.createCompoundBorder(
130 BorderFactory.createCompoundBorder(
131 BorderFactory.createEmptyBorder(6, 6, 6, 6),
132 BorderFactory.createEtchedBorder()
133 ),
134 BorderFactory.createEmptyBorder(6,6,6,6)
135 ));
136 GridBagLayout gridBag=new GridBagLayout();
137 inputPane.setLayout(gridBag);
138 addLabelValueRows(new JLabel[] {serverLabel, protocolLabel, usernameLabel, passwordLabel},
139 new JComponent[] {m_serverComboBox, m_protocolComboBox, m_usernameComboBox, m_passwordField},
140 gridBag, inputPane);
141
142 // handling Closing and cancelling events
143 this.addWindowListener(new WindowAdapter() {
144 public void windowClosing(WindowEvent e){
145 // this is important, if we want to stop looping on displaying the dialog:
146 login_requested = false;
147 //dispose(); // defaultCloseOperation of this dialog already set to dispose it
148 }
149 });
150
151 JButton cancelButton=new JButton(new AbstractAction() {
152 private static final long serialVersionUID = 1L;
153 public void actionPerformed(ActionEvent evt) {
154 // this is important, if we want to stop looping on displaying the dialog:
155 login_requested = false;
156 dispose();
157 }
158 });
159
160 cancelButton.setText("Exit"); // if haven't logged in yet
161
162 JPanel buttonPane=new JPanel();
163 buttonPane.add(loginButton);
164 buttonPane.add(cancelButton);
165 Container contentPane=getContentPane();
166
167 contentPane.setLayout(new BorderLayout());
168 m_infoPane = new JPanel();
169 m_infoPane.setBackground(TRANSPARENT);
170 contentPane.add(m_infoPane, BorderLayout.NORTH);
171 contentPane.add(inputPane, BorderLayout.CENTER);
172 contentPane.add(buttonPane, BorderLayout.SOUTH);
173 addWindowListener(new WindowAdapter() {
174 public void windowOpened(WindowEvent evt) {
175 m_passwordField.requestFocus();
176 }
177 });
178 pack();
179
180 // Position
181 Dimension size = getSize();
182 if (Gatherer.g_man != null) {
183 Rectangle frame_bounds = Gatherer.g_man.getBounds();
184 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
185 }
186 else {
187 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
188 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
189 }
190
191
192 setVisible(true);
193 }
194
195 // re-writes fedora-admin.properties with latest values for servers
196 // and usernames
197
198
199 public void saveProperties() {
200 try {
201 Properties props=new Properties();
202 props.setProperty("lastServer", m_lastServer);
203 props.setProperty("lastProtocol", m_lastProtocol);
204 props.setProperty("lastUsername", m_lastUsername);
205 Iterator iter;
206 int i;
207 iter=m_servers.keySet().iterator();
208 i=0;
209 while (iter.hasNext()) {
210 String name=(String) iter.next();
211 props.setProperty("server" + i, name);
212 i++;
213 }
214 iter=m_protocols.keySet().iterator();
215 i=0;
216 while (iter.hasNext()) {
217 String name=(String) iter.next();
218 props.setProperty("protocol" + i, name);
219 i++;
220 }
221 iter=m_usernames.keySet().iterator();
222 i=0;
223 while (iter.hasNext()) {
224 String name=(String) iter.next();
225 props.setProperty("username" + i, name);
226 i++;
227 }
228 props.store(new FileOutputStream(new File(Gatherer.getGLIUserDirectoryPath(), "fedora-admin.properties")), "Fedora Administrator saved settings");
229 } catch (Exception e) {
230 System.err.println("Warning: Error writing properties: "
231 + e.getClass().getName() + ": " + e.getMessage());
232 }
233 }
234
235
236 private void setComboBoxValues() {
237 // get values from prop file, or use localhost:8080/fedoraAdmin if none
238 try {
239 Properties props=new Properties();
240 props.load(new FileInputStream(new File(Gatherer.getGLIUserDirectoryPath(), "fedora-admin.properties")));
241 Enumeration names=props.propertyNames();
242 while (names.hasMoreElements()) {
243 String prop=(String) names.nextElement();
244 if (prop.equals("lastServer")) {
245 m_lastServer=props.getProperty(prop);
246 } else if (prop.equals("lastProtocol")) {
247 m_lastProtocol=props.getProperty(prop);
248 } else if (prop.equals("lastUsername")) {
249 m_lastUsername=props.getProperty(prop);
250 } else if (prop.startsWith("server")) {
251 m_servers.put(props.getProperty(prop), "");
252 } else if (prop.startsWith("protocol")) {
253 m_protocols.put(props.getProperty(prop), "");
254 } else if (prop.startsWith("username")) {
255 m_usernames.put(props.getProperty(prop), "");
256 }
257 }
258 } catch (Exception e) {
259 // no problem if props file doesn't exist...we have defaults
260 }
261 // finally, populate them
262 m_serverComboBox.addItem(m_lastServer);
263 Iterator sIter=m_servers.keySet().iterator();
264 while (sIter.hasNext()) {
265 String a=(String) sIter.next();
266 if (!a.equals(m_lastServer)) {
267 m_serverComboBox.addItem(a);
268 }
269 }
270 m_servers.put(m_lastServer, "");
271
272 m_protocolComboBox.addItem(m_lastProtocol);
273 Iterator protocolIter=m_protocols.keySet().iterator();
274 while (protocolIter.hasNext()) {
275 String a=(String) protocolIter.next();
276 if (!a.equals(m_lastProtocol)) {
277 m_protocolComboBox.addItem(a);
278 }
279 }
280 m_protocols.put(m_lastProtocol, "");
281
282 m_usernameComboBox.addItem(m_lastUsername);
283 Iterator uIter=m_usernames.keySet().iterator();
284 while (uIter.hasNext()) {
285 String a=(String) uIter.next();
286 if (!a.equals(m_lastUsername)) {
287 m_usernameComboBox.addItem(a);
288 }
289 }
290 m_usernames.put(m_lastUsername, "");
291
292 // make all entry widgets same size
293 Dimension newSize=new Dimension(
294 m_serverComboBox.getPreferredSize().width+20,
295 m_serverComboBox.getPreferredSize().height);
296 m_serverComboBox.setPreferredSize(newSize);
297 m_protocolComboBox.setPreferredSize(newSize);
298 m_usernameComboBox.setPreferredSize(newSize);
299 m_passwordField.setPreferredSize(newSize);
300 }
301
302 public void addLabelValueRows(JLabel[] labels, JComponent[] values,
303 GridBagLayout gridBag, Container container) {
304 GridBagConstraints c=new GridBagConstraints();
305 c.insets=new Insets(0, 6, 6, 6);
306 for (int i=0; i<labels.length; i++) {
307 c.anchor=GridBagConstraints.EAST;
308 c.gridwidth=GridBagConstraints.RELATIVE; //next-to-last
309 c.fill=GridBagConstraints.NONE; //reset to default
310 c.weightx=0.0; //reset to default
311 gridBag.setConstraints(labels[i], c);
312 container.add(labels[i]);
313
314 c.gridwidth=GridBagConstraints.REMAINDER; //end row
315 if (!(values[i] instanceof JComboBox)) {
316 c.fill=GridBagConstraints.HORIZONTAL;
317 } else {
318 c.anchor=GridBagConstraints.WEST;
319 }
320 c.weightx=1.0;
321 gridBag.setConstraints(values[i], c);
322 container.add(values[i]);
323 }
324
325 }
326
327 /** Displays the given errormessage (which is split over several lines)
328 * in the FedoraLogin itself
329 * @param errorLines: the lines of the error message, where each line
330 * will be presented in its own JLabel in the infoPane.
331 */
332 public void setErrorMessage(String[] errorLines) {
333 m_infoPane.removeAll();
334 m_infoPane.setBackground(LIGHT);
335 // n rows and 1 column
336 m_infoPane.setLayout(new java.awt.GridLayout(errorLines.length, 1));
337 for(int i = 0; i < errorLines.length; i++) {
338 JLabel line = new JLabel(" " + errorLines[i] + " ");
339 m_infoPane.add(line);
340 }
341
342 // Adjust and resize this dialog to take into account the
343 // recently added components (labels)
344 m_infoPane.validate();
345 this.pack();
346 }
347
348 public class PasswordChangeListener
349 implements DocumentListener {
350
351 private JButton m_loginButton;
352 private JPasswordField m_passField;
353
354 public PasswordChangeListener(JButton loginButton, JPasswordField pf) {
355 m_loginButton=loginButton;
356 m_passField=pf;
357 }
358
359 public void changedUpdate(DocumentEvent e) {
360 dataChanged();
361 }
362
363 public void insertUpdate(DocumentEvent e) {
364 dataChanged();
365 }
366
367 public void removeUpdate(DocumentEvent e) {
368 dataChanged();
369 }
370
371 public void dataChanged() {
372 if (m_passField.getPassword().length == 0) {
373 m_loginButton.setEnabled(false);
374 } else {
375 m_loginButton.setEnabled(true);
376 }
377 }
378
379 }
380
381
382 public class LoginAction
383 extends AbstractAction {
384
385 FedoraLogin m_loginDialog;
386 JButton m_button;
387
388 public LoginAction(FedoraLogin loginDialog) {
389 super("Login");
390 m_loginDialog=loginDialog;
391 }
392
393 public void setButton(JButton button) {
394 m_button=button;
395 }
396
397 public void actionPerformed(ActionEvent evt) {
398
399
400 if (m_button.isEnabled()) {
401
402 try {
403 // pull out values and do a quick syntax check
404 String hostPort=(String) m_serverComboBox.getSelectedItem();
405 int colonPos=hostPort.indexOf(":");
406 if (colonPos==-1) {
407 throw new IOException("Server must be specified as host:port");
408 }
409 String[] s=hostPort.split(":");
410 String host=s[0];
411 if (host.length()==0) {
412 throw new IOException("No server name provided.");
413 }
414 int port=0;
415 try {
416 port=Integer.parseInt(s[1]);
417 } catch (NumberFormatException nfe) {
418 throw new IOException("Server port must be an integer.");
419 }
420 String protocol=(String) m_protocolComboBox.getSelectedItem();
421 if (protocol.equals("")) {
422 throw new IOException("No protocol provided.");
423 }
424
425 String username=(String) m_usernameComboBox.getSelectedItem();
426 if (username.equals("")) {
427 throw new IOException("No username provided.");
428 }
429 String pass = new String(m_passwordField.getPassword());
430
431
432 // all looks ok...just save stuff and exit now
433 m_lastServer=host + ":" + port;
434 m_lastProtocol=protocol;
435 m_lastUsername=username;
436 m_lastPassword=pass;
437
438 m_loginDialog.saveProperties();
439
440
441 login_requested = true;
442
443 // hiding it instead of disposing it on OK-press allows us to loop
444 // on displaying the dialog in case there's more checking to do
445 m_loginDialog.setVisible(false);
446
447 } catch (Exception e) {
448 //String msg = e.getMessage();
449 //System.err.println(msg);
450
451 e.printStackTrace();
452
453
454 }
455 }
456 }
457 }
458
459 public boolean loginRequested()
460 {
461 return login_requested;
462 }
463
464 public String getLibraryURL()
465 {
466 String libraryUrl = m_lastProtocol + "://" + m_lastServer + "/fedora/search";
467
468 return libraryUrl;
469 }
470
471 public String getHostname()
472 {
473 String[] s=m_lastServer.split(":");
474 String host=s[0];
475 return host;
476 }
477 public String getPort()
478 {
479 String[] s=m_lastServer.split(":");
480
481 String port = s[1];
482 return port;
483 }
484
485 public String getUsername()
486 {
487 return m_lastUsername;
488 }
489
490 public String getPassword()
491 {
492 return m_lastPassword;
493 }
494
495 public String getProtocol()
496 {
497 return m_lastProtocol;
498 }
499
500
501}
Note: See TracBrowser for help on using the repository browser.