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

Last change on this file since 14975 was 14975, checked in by davidb, 16 years ago

New files added to support Fedora in GLI

File size: 14.5 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
72 private JComboBox m_serverComboBox;
73 private JComboBox m_protocolComboBox;
74 private JComboBox m_usernameComboBox;
75 private JPasswordField m_passwordField;
76
77 private String m_lastUsername="fedoraAdmin";
78 private String m_lastServer="localhost:8080";
79 private String m_lastProtocol="http";
80 private String m_lastPassword="";
81
82 private HashMap m_usernames;
83 private HashMap m_servers;
84 private HashMap m_protocols;
85
86 private boolean login_requested = false;
87
88 public FedoraLogin(String title, boolean can_cancel)
89 {
90 super(Gatherer.g_man, "Login", true);
91
92 m_servers=new HashMap();
93 m_protocols=new HashMap();
94 m_protocols.put("http", "");
95 m_protocols.put("https", "");
96 m_usernames=new HashMap();
97
98 JLabel serverLabel=new JLabel("Fedora Server");
99 JLabel protocolLabel=new JLabel("Protocol");
100 JLabel usernameLabel=new JLabel("Username");
101 JLabel passwordLabel=new JLabel("Password");
102
103 m_serverComboBox=new JComboBox();
104 m_serverComboBox.setEditable(true);
105 m_protocolComboBox=new JComboBox();
106 m_protocolComboBox.setEditable(true);
107 m_usernameComboBox=new JComboBox();
108 m_usernameComboBox.setEditable(true);
109 m_passwordField=new JPasswordField();
110
111 setComboBoxValues();
112
113
114 LoginAction loginAction=new LoginAction(this);
115 JButton loginButton=new JButton(loginAction);
116
117 loginAction.setButton(loginButton);
118 loginButton.setEnabled(false);
119
120
121 m_passwordField.getDocument().addDocumentListener(
122 new PasswordChangeListener(loginButton, m_passwordField));
123 m_passwordField.setAction(loginAction);
124
125 JPanel inputPane=new JPanel();
126 inputPane.setBorder(BorderFactory.createCompoundBorder(
127 BorderFactory.createCompoundBorder(
128 BorderFactory.createEmptyBorder(6, 6, 6, 6),
129 BorderFactory.createEtchedBorder()
130 ),
131 BorderFactory.createEmptyBorder(6,6,6,6)
132 ));
133 GridBagLayout gridBag=new GridBagLayout();
134 inputPane.setLayout(gridBag);
135 addLabelValueRows(new JLabel[] {serverLabel, protocolLabel, usernameLabel, passwordLabel},
136 new JComponent[] {m_serverComboBox, m_protocolComboBox, m_usernameComboBox, m_passwordField},
137 gridBag, inputPane);
138
139 JButton cancelButton=new JButton(new AbstractAction() {
140 private static final long serialVersionUID = 1L;
141 public void actionPerformed(ActionEvent evt) {
142 dispose();
143 }
144 });
145
146 cancelButton.setText("Exit"); // if haven't logged in yet
147
148 JPanel buttonPane=new JPanel();
149 buttonPane.add(loginButton);
150 buttonPane.add(cancelButton);
151 Container contentPane=getContentPane();
152 contentPane.setLayout(new BorderLayout());
153 contentPane.add(inputPane, BorderLayout.CENTER);
154 contentPane.add(buttonPane, BorderLayout.SOUTH);
155 addWindowListener(new WindowAdapter() {
156 public void windowOpened(WindowEvent evt) {
157 m_passwordField.requestFocus();
158 }
159 });
160 pack();
161
162 // Position
163 Dimension size = getSize();
164 if (Gatherer.g_man != null) {
165 Rectangle frame_bounds = Gatherer.g_man.getBounds();
166 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
167 }
168 else {
169 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
170 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
171 }
172
173
174 setVisible(true);
175 }
176
177 // re-writes fedora-admin.properties with latest values for servers
178 // and usernames
179
180
181 public void saveProperties() {
182 try {
183 Properties props=new Properties();
184 props.setProperty("lastServer", m_lastServer);
185 props.setProperty("lastProtocol", m_lastProtocol);
186 props.setProperty("lastUsername", m_lastUsername);
187 Iterator iter;
188 int i;
189 iter=m_servers.keySet().iterator();
190 i=0;
191 while (iter.hasNext()) {
192 String name=(String) iter.next();
193 props.setProperty("server" + i, name);
194 i++;
195 }
196 iter=m_protocols.keySet().iterator();
197 i=0;
198 while (iter.hasNext()) {
199 String name=(String) iter.next();
200 props.setProperty("protocol" + i, name);
201 i++;
202 }
203 iter=m_usernames.keySet().iterator();
204 i=0;
205 while (iter.hasNext()) {
206 String name=(String) iter.next();
207 props.setProperty("username" + i, name);
208 i++;
209 }
210 props.store(new FileOutputStream(new File(Gatherer.getGLIUserDirectoryPath(), "fedora-admin.properties")), "Fedora Administrator saved settings");
211 } catch (Exception e) {
212 System.err.println("Warning: Error writing properties: "
213 + e.getClass().getName() + ": " + e.getMessage());
214 }
215 }
216
217
218 private void setComboBoxValues() {
219 // get values from prop file, or use localhost:8080/fedoraAdmin if none
220 try {
221 Properties props=new Properties();
222 props.load(new FileInputStream(new File(Gatherer.getGLIUserDirectoryPath(), "fedora-admin.properties")));
223 Enumeration names=props.propertyNames();
224 while (names.hasMoreElements()) {
225 String prop=(String) names.nextElement();
226 if (prop.equals("lastServer")) {
227 m_lastServer=props.getProperty(prop);
228 } else if (prop.equals("lastProtocol")) {
229 m_lastProtocol=props.getProperty(prop);
230 } else if (prop.equals("lastUsername")) {
231 m_lastUsername=props.getProperty(prop);
232 } else if (prop.startsWith("server")) {
233 m_servers.put(props.getProperty(prop), "");
234 } else if (prop.startsWith("protocol")) {
235 m_protocols.put(props.getProperty(prop), "");
236 } else if (prop.startsWith("username")) {
237 m_usernames.put(props.getProperty(prop), "");
238 }
239 }
240 } catch (Exception e) {
241 // no problem if props file doesn't exist...we have defaults
242 }
243 // finally, populate them
244 m_serverComboBox.addItem(m_lastServer);
245 Iterator sIter=m_servers.keySet().iterator();
246 while (sIter.hasNext()) {
247 String a=(String) sIter.next();
248 if (!a.equals(m_lastServer)) {
249 m_serverComboBox.addItem(a);
250 }
251 }
252 m_servers.put(m_lastServer, "");
253
254 m_protocolComboBox.addItem(m_lastProtocol);
255 Iterator protocolIter=m_protocols.keySet().iterator();
256 while (protocolIter.hasNext()) {
257 String a=(String) protocolIter.next();
258 if (!a.equals(m_lastProtocol)) {
259 m_protocolComboBox.addItem(a);
260 }
261 }
262 m_protocols.put(m_lastProtocol, "");
263
264 m_usernameComboBox.addItem(m_lastUsername);
265 Iterator uIter=m_usernames.keySet().iterator();
266 while (uIter.hasNext()) {
267 String a=(String) uIter.next();
268 if (!a.equals(m_lastUsername)) {
269 m_usernameComboBox.addItem(a);
270 }
271 }
272 m_usernames.put(m_lastUsername, "");
273
274 // make all entry widgets same size
275 Dimension newSize=new Dimension(
276 m_serverComboBox.getPreferredSize().width+20,
277 m_serverComboBox.getPreferredSize().height);
278 m_serverComboBox.setPreferredSize(newSize);
279 m_protocolComboBox.setPreferredSize(newSize);
280 m_usernameComboBox.setPreferredSize(newSize);
281 m_passwordField.setPreferredSize(newSize);
282 }
283
284 public void addLabelValueRows(JLabel[] labels, JComponent[] values,
285 GridBagLayout gridBag, Container container) {
286 GridBagConstraints c=new GridBagConstraints();
287 c.insets=new Insets(0, 6, 6, 6);
288 for (int i=0; i<labels.length; i++) {
289 c.anchor=GridBagConstraints.EAST;
290 c.gridwidth=GridBagConstraints.RELATIVE; //next-to-last
291 c.fill=GridBagConstraints.NONE; //reset to default
292 c.weightx=0.0; //reset to default
293 gridBag.setConstraints(labels[i], c);
294 container.add(labels[i]);
295
296 c.gridwidth=GridBagConstraints.REMAINDER; //end row
297 if (!(values[i] instanceof JComboBox)) {
298 c.fill=GridBagConstraints.HORIZONTAL;
299 } else {
300 c.anchor=GridBagConstraints.WEST;
301 }
302 c.weightx=1.0;
303 gridBag.setConstraints(values[i], c);
304 container.add(values[i]);
305 }
306
307 }
308
309 public class PasswordChangeListener
310 implements DocumentListener {
311
312 private JButton m_loginButton;
313 private JPasswordField m_passField;
314
315 public PasswordChangeListener(JButton loginButton, JPasswordField pf) {
316 m_loginButton=loginButton;
317 m_passField=pf;
318 }
319
320 public void changedUpdate(DocumentEvent e) {
321 dataChanged();
322 }
323
324 public void insertUpdate(DocumentEvent e) {
325 dataChanged();
326 }
327
328 public void removeUpdate(DocumentEvent e) {
329 dataChanged();
330 }
331
332 public void dataChanged() {
333 if (m_passField.getPassword().length == 0) {
334 m_loginButton.setEnabled(false);
335 } else {
336 m_loginButton.setEnabled(true);
337 }
338 }
339
340 }
341
342
343
344 public class LoginAction
345 extends AbstractAction {
346
347 FedoraLogin m_loginDialog;
348 JButton m_button;
349
350 public LoginAction(FedoraLogin loginDialog) {
351 super("Login");
352 m_loginDialog=loginDialog;
353 }
354
355 public void setButton(JButton button) {
356 m_button=button;
357 }
358
359 public void actionPerformed(ActionEvent evt) {
360
361
362 if (m_button.isEnabled()) {
363
364 try {
365 // pull out values and do a quick syntax check
366 String hostPort=(String) m_serverComboBox.getSelectedItem();
367 int colonPos=hostPort.indexOf(":");
368 if (colonPos==-1) {
369 throw new IOException("Server must be specified as host:port");
370 }
371 String[] s=hostPort.split(":");
372 String host=s[0];
373 if (host.length()==0) {
374 throw new IOException("No server name provided.");
375 }
376 int port=0;
377 try {
378 port=Integer.parseInt(s[1]);
379 } catch (NumberFormatException nfe) {
380 throw new IOException("Server port must be an integer.");
381 }
382 String protocol=(String) m_protocolComboBox.getSelectedItem();
383 if (protocol.equals("")) {
384 throw new IOException("No protocol provided.");
385 }
386
387 String username=(String) m_usernameComboBox.getSelectedItem();
388 if (username.equals("")) {
389 throw new IOException("No username provided.");
390 }
391 String pass = new String(m_passwordField.getPassword());
392
393
394 // all looks ok...just save stuff and exit now
395 m_lastServer=host + ":" + port;
396 m_lastProtocol=protocol;
397 m_lastUsername=username;
398 m_lastPassword=pass;
399
400 m_loginDialog.saveProperties();
401
402
403 login_requested = true;
404
405 m_loginDialog.dispose();
406 } catch (Exception e) {
407 //String msg = e.getMessage();
408 //System.err.println(msg);
409
410 e.printStackTrace();
411
412
413 }
414 }
415 }
416
417
418 }
419
420 public boolean loginRequested()
421 {
422 return login_requested;
423 }
424
425 public String getLibraryURL()
426 {
427 String libraryUrl = m_lastProtocol + "://" + m_lastServer + "/fedora/search";
428
429 return libraryUrl;
430 }
431
432 public String getHostname()
433 {
434 String[] s=m_lastServer.split(":");
435 String host=s[0];
436 return host;
437 }
438 public String getPort()
439 {
440 String[] s=m_lastServer.split(":");
441
442 String port = s[1];
443 return port;
444 }
445
446 public String getUsername()
447 {
448 return m_lastUsername;
449 }
450
451 public String getPassword()
452 {
453 return m_lastPassword;
454 }
455
456 public String getProtocol()
457 {
458 return m_lastProtocol;
459 }
460
461
462}
Note: See TracBrowser for help on using the repository browser.