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