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

Last change on this file since 20957 was 18412, checked in by kjdon, 15 years ago

more modifications for RTL GLI, thanks to Amin Hedjazi

File size: 16.1 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;
64import org.greenstone.gatherer.Dictionary;
65
66
67
68import org.greenstone.gatherer.Gatherer;
69import org.greenstone.gatherer.Configuration;
70
71public class FedoraLogin extends JDialog
72{
73 private static final int MAX_ITEMS = 5;
74
75 private static final java.awt.Color LIGHT = new java.awt.Color(244, 244, 224);
76 private static final java.awt.Color TRANSPARENT = new java.awt.Color(0,0,0,0);
77
78 private JPanel m_infoPane;
79 private JComboBox m_serverComboBox;
80 private JComboBox m_protocolComboBox;
81 private JComboBox m_usernameComboBox;
82 private JPasswordField m_passwordField;
83
84 private String m_lastUsername="fedoraAdmin";
85 private String m_lastServer="localhost:8080";
86 private String m_lastProtocol="http";
87 private String m_lastPassword="";
88
89 private boolean login_requested = false;
90
91 public FedoraLogin(String title, boolean can_cancel)
92 {
93 super(Gatherer.g_man, "Login", true);
94 this.setComponentOrientation(Dictionary.getOrientation());
95 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
96
97 JLabel serverLabel=new JLabel("Fedora Server");
98 JLabel protocolLabel=new JLabel("Protocol");
99 JLabel usernameLabel=new JLabel("Username");
100 JLabel passwordLabel=new JLabel("Password");
101
102 serverLabel.setComponentOrientation(Dictionary.getOrientation());
103 protocolLabel.setComponentOrientation(Dictionary.getOrientation());
104 usernameLabel.setComponentOrientation(Dictionary.getOrientation());
105 passwordLabel.setComponentOrientation(Dictionary.getOrientation());
106
107 m_serverComboBox=new JComboBox(new String[]{m_lastServer});
108 m_serverComboBox.setComponentOrientation(Dictionary.getOrientation());
109 m_serverComboBox.setEditable(true);
110 m_protocolComboBox=new JComboBox(new String[]{m_lastProtocol, "https"}); // http and https
111 m_protocolComboBox.setComponentOrientation(Dictionary.getOrientation());
112 m_protocolComboBox.setEditable(true);
113 m_usernameComboBox=new JComboBox(new String[]{m_lastUsername});
114 m_usernameComboBox.setComponentOrientation(Dictionary.getOrientation());
115 m_usernameComboBox.setEditable(true);
116 m_passwordField=new JPasswordField();
117
118 setComboBoxValues();
119
120
121 LoginAction loginAction=new LoginAction(this);
122 JButton loginButton=new JButton(loginAction);
123
124 loginAction.setButton(loginButton);
125 loginButton.setEnabled(false);
126 this.getRootPane().setDefaultButton(loginButton);
127
128 m_passwordField.getDocument().addDocumentListener(
129 new PasswordChangeListener(loginButton, m_passwordField));
130 m_passwordField.setAction(loginAction);
131
132 JPanel inputPane=new JPanel();
133 inputPane.setComponentOrientation(Dictionary.getOrientation());
134 inputPane.setBorder(BorderFactory.createCompoundBorder(
135 BorderFactory.createCompoundBorder(
136 BorderFactory.createEmptyBorder(6, 6, 6, 6),
137 BorderFactory.createEtchedBorder()
138 ),
139 BorderFactory.createEmptyBorder(6,6,6,6)
140 ));
141 GridBagLayout gridBag=new GridBagLayout();
142 inputPane.setLayout(gridBag);
143 addLabelValueRows(new JLabel[] {serverLabel, protocolLabel, usernameLabel, passwordLabel},
144 new JComponent[] {m_serverComboBox, m_protocolComboBox, m_usernameComboBox, m_passwordField},
145 gridBag, inputPane);
146
147 // handling Closing and cancelling events
148 this.addWindowListener(new WindowAdapter() {
149 public void windowClosing(WindowEvent e){
150 // this is important, if we want to stop looping on displaying the dialog:
151 login_requested = false;
152 //dispose(); // defaultCloseOperation of this dialog already set to dispose it
153 }
154 });
155
156 JButton cancelButton=new JButton(new AbstractAction() {
157 private static final long serialVersionUID = 1L;
158 public void actionPerformed(ActionEvent evt) {
159 // this is important, if we want to stop looping on displaying the dialog:
160 login_requested = false;
161 dispose();
162 }
163 });
164
165 cancelButton.setText("Exit"); // if haven't logged in yet
166
167 JPanel buttonPane=new JPanel();
168 buttonPane.setComponentOrientation(Dictionary.getOrientation());
169 buttonPane.add(loginButton);
170 buttonPane.add(cancelButton);
171 Container contentPane=getContentPane();
172 contentPane.setComponentOrientation(Dictionary.getOrientation());
173
174 contentPane.setLayout(new BorderLayout());
175 m_infoPane = new JPanel();
176 m_infoPane.setComponentOrientation(Dictionary.getOrientation());
177 m_infoPane.setBackground(TRANSPARENT);
178 contentPane.add(m_infoPane, BorderLayout.NORTH);
179 contentPane.add(inputPane, BorderLayout.CENTER);
180 contentPane.add(buttonPane, BorderLayout.SOUTH);
181 addWindowListener(new WindowAdapter() {
182 public void windowOpened(WindowEvent evt) {
183 m_passwordField.requestFocus();
184 }
185 });
186 pack();
187
188 // Position
189 Dimension size = getSize();
190 if (Gatherer.g_man != null) {
191 Rectangle frame_bounds = Gatherer.g_man.getBounds();
192 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
193 }
194 else {
195 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
196 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
197 }
198
199
200 setVisible(true);
201 }
202
203
204 public void saveProperties(JComboBox control, String affectedProperty, String editedValue) {
205 //String editedValue = (String)control.getSelectedItem();
206 // Edited value is the value in the combobox editfield, it has not yet been added to the combobox
207 control.insertItemAt(editedValue, 0);
208
209 if(editedValue == null) {
210 editedValue = (String)control.getItemAt(0); // else reuse the first default
211 }
212 Configuration.setString(affectedProperty, true, editedValue);
213
214 // Add the values in the combobox as well.
215 int value_count = 1;
216 for(int i = 0; value_count < MAX_ITEMS && i < control.getItemCount(); i++, value_count++) {
217 String value = (String)control.getItemAt(i);
218 if(value == null || value.equals(editedValue) || value.equals("")) {
219 // skip duplicates of just-edited comboBox value and empty values
220 value_count--;
221 } else {
222 Configuration.setString(affectedProperty+value_count, true, value);
223 } // else retain old value for this affectedProperty (e.g. general.gliserver_url)
224 }
225 }
226
227 private void setComboBoxValues() {
228 // All comboboxes are of the same size
229 Dimension newSize=new Dimension(m_serverComboBox.getPreferredSize().width+20, m_serverComboBox.getPreferredSize().height);
230 m_passwordField.setPreferredSize(newSize);
231
232 setComboBoxValues(m_serverComboBox, "fedora.server", newSize);
233 setComboBoxValues(m_protocolComboBox, "fedora.protocol", newSize);
234 setComboBoxValues(m_usernameComboBox, "fedora.username", newSize);
235 newSize = null;
236 }
237
238 private void setComboBoxValues(JComboBox control, String affectedProperty, Dimension newSize) {
239 // get values from Configuration file, if none, it will go back to using defaultvalues
240 // put them into the combobox
241
242 String value = Configuration.getString(affectedProperty, true);
243 boolean finished = value.equals("");
244 if(!finished) {
245 control.removeAllItems(); // remove defaults, since config file now contains init values
246 control.addItem(value);
247 } // else value and combobox already contains the defaults
248
249
250 for(int i = 1; !finished; i++) {
251 value = Configuration.getString(affectedProperty+i, true);
252 if(value.equals("")) {
253 finished = true;
254 }
255 else { // value is not empty
256 control.addItem(value);
257 }
258 }
259
260 // setting the size of the dropdown control
261 control.setPreferredSize(newSize);
262 }
263
264 public void addLabelValueRows(JLabel[] labels, JComponent[] values,
265 GridBagLayout gridBag, Container container) {
266 GridBagConstraints c=new GridBagConstraints();
267 c.insets=new Insets(0, 6, 6, 6);
268 for (int i=0; i<labels.length; i++) {
269 c.anchor=GridBagConstraints.EAST;
270 c.gridwidth=GridBagConstraints.RELATIVE; //next-to-last
271 c.fill=GridBagConstraints.NONE; //reset to default
272 c.weightx=0.0; //reset to default
273 gridBag.setConstraints(labels[i], c);
274 container.add(labels[i]);
275
276 c.gridwidth=GridBagConstraints.REMAINDER; //end row
277 if (!(values[i] instanceof JComboBox)) {
278 c.fill=GridBagConstraints.HORIZONTAL;
279 } else {
280 c.anchor=GridBagConstraints.WEST;
281 }
282 c.weightx=1.0;
283 gridBag.setConstraints(values[i], c);
284 container.add(values[i]);
285 }
286
287 }
288
289 /** Displays the given errormessage (which is split over several lines)
290 * in the FedoraLogin itself
291 * @param errorLines: the lines of the error message, where each line
292 * will be presented in its own JLabel in the infoPane.
293 */
294 public void setErrorMessage(String[] errorLines) {
295 m_infoPane.removeAll();
296 m_infoPane.setBackground(LIGHT);
297 // n rows and 1 column
298 m_infoPane.setLayout(new java.awt.GridLayout(errorLines.length, 1));
299 for(int i = 0; i < errorLines.length; i++) {
300 JLabel line = new JLabel(" " + errorLines[i] + " ");
301 m_infoPane.add(line);
302 }
303
304 // Adjust and resize this dialog to take into account the
305 // recently added components (labels)
306 m_infoPane.validate();
307 this.pack();
308 }
309
310 public class PasswordChangeListener
311 implements DocumentListener {
312
313 private JButton m_loginButton;
314 private JPasswordField m_passField;
315
316 public PasswordChangeListener(JButton loginButton, JPasswordField pf) {
317 m_loginButton=loginButton;
318 m_passField=pf;
319 }
320
321 public void changedUpdate(DocumentEvent e) {
322 dataChanged();
323 }
324
325 public void insertUpdate(DocumentEvent e) {
326 dataChanged();
327 }
328
329 public void removeUpdate(DocumentEvent e) {
330 dataChanged();
331 }
332
333 public void dataChanged() {
334 if (m_passField.getPassword().length == 0) {
335 m_loginButton.setEnabled(false);
336 } else {
337 m_loginButton.setEnabled(true);
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(m_serverComboBox, "fedora.server", m_lastServer);
401 m_loginDialog.saveProperties(m_protocolComboBox, "fedora.protocol", m_lastProtocol);
402 m_loginDialog.saveProperties(m_usernameComboBox, "fedora.username", m_lastUsername);
403
404 // now save these values to the Configuration file
405 Configuration.save();
406
407 login_requested = true;
408
409 // hiding it instead of disposing it on OK-press allows us to loop
410 // on displaying the dialog in case there's more checking to do
411 m_loginDialog.setVisible(false);
412
413 } catch (Exception e) {
414 //String msg = e.getMessage();
415 //System.err.println(msg);
416
417 e.printStackTrace();
418
419 }
420 }
421 }
422 }
423
424 public boolean loginRequested()
425 {
426 return login_requested;
427 }
428
429 public String getLibraryURL()
430 {
431 String libraryUrl = m_lastProtocol + "://" + m_lastServer + "/fedora/search";
432
433 return libraryUrl;
434 }
435
436 public String getHostname()
437 {
438 String[] s=m_lastServer.split(":");
439 String host=s[0];
440 return host;
441 }
442 public String getPort()
443 {
444 String[] s=m_lastServer.split(":");
445
446 String port = s[1];
447 return port;
448 }
449
450 public String getUsername()
451 {
452 return m_lastUsername;
453 }
454
455 public String getPassword()
456 {
457 return m_lastPassword;
458 }
459
460 public String getProtocol()
461 {
462 return m_lastProtocol;
463 }
464
465
466}
Note: See TracBrowser for help on using the repository browser.