Changeset 16337
- Timestamp:
- 2008-07-10T13:36:30+12:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/trunk/src/org/greenstone/gatherer/gui/FedoraLogin.java
r15505 r16337 66 66 67 67 import org.greenstone.gatherer.Gatherer; 68 import org.greenstone.gatherer.Configuration; 68 69 69 70 public class FedoraLogin extends JDialog 70 71 { 72 private static final int MAX_ITEMS = 5; 73 71 74 private static final java.awt.Color LIGHT = new java.awt.Color(244, 244, 224); 72 75 private static final java.awt.Color TRANSPARENT = new java.awt.Color(0,0,0,0); 76 73 77 private JPanel m_infoPane; 74 78 private JComboBox m_serverComboBox; … … 82 86 private String m_lastPassword=""; 83 87 84 private HashMap m_usernames;85 private HashMap m_servers;86 private HashMap m_protocols;87 88 88 private boolean login_requested = false; 89 89 … … 93 93 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 94 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"); 95 JLabel serverLabel=new JLabel("Fedora Server"); 96 JLabel protocolLabel=new JLabel("Protocol"); 103 97 JLabel usernameLabel=new JLabel("Username"); 104 98 JLabel passwordLabel=new JLabel("Password"); 105 99 106 m_serverComboBox=new JComboBox( );100 m_serverComboBox=new JComboBox(new String[]{m_lastServer}); 107 101 m_serverComboBox.setEditable(true); 108 m_protocolComboBox=new JComboBox();109 110 m_usernameComboBox=new JComboBox( );102 m_protocolComboBox=new JComboBox(new String[]{m_lastProtocol, "https"}); // http and https 103 m_protocolComboBox.setEditable(true); 104 m_usernameComboBox=new JComboBox(new String[]{m_lastUsername}); 111 105 m_usernameComboBox.setEditable(true); 112 106 m_passwordField=new JPasswordField(); … … 193 187 } 194 188 195 // re-writes fedora-admin.properties with latest values for servers 196 // and usernames 197 189 190 public void saveProperties(JComboBox control, String affectedProperty, String editedValue) { 191 //String editedValue = (String)control.getSelectedItem(); 192 // Edited value is the value in the combobox editfield, it has not yet been added to the combobox 193 control.insertItemAt(editedValue, 0); 194 195 if(editedValue == null) { 196 editedValue = (String)control.getItemAt(0); // else reuse the first default 197 } 198 Configuration.setString(affectedProperty, true, editedValue); 199 200 // Add the values in the combobox as well. 201 int value_count = 1; 202 for(int i = 0; value_count < MAX_ITEMS && i < control.getItemCount(); i++, value_count++) { 203 String value = (String)control.getItemAt(i); 204 if(value == null || value.equals(editedValue) || value.equals("")) { 205 // skip duplicates of just-edited comboBox value and empty values 206 value_count--; 207 } else { 208 Configuration.setString(affectedProperty+value_count, true, value); 209 } // else retain old value for this affectedProperty (e.g. general.gliserver_url) 210 } 211 } 212 213 private void setComboBoxValues() { 214 // All comboboxes are of the same size 215 Dimension newSize=new Dimension(m_serverComboBox.getPreferredSize().width+20, m_serverComboBox.getPreferredSize().height); 216 m_passwordField.setPreferredSize(newSize); 217 218 setComboBoxValues(m_serverComboBox, "fedora.server", newSize); 219 setComboBoxValues(m_protocolComboBox, "fedora.protocol", newSize); 220 setComboBoxValues(m_usernameComboBox, "fedora.username", newSize); 221 newSize = null; 222 } 198 223 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); 224 private void setComboBoxValues(JComboBox control, String affectedProperty, Dimension newSize) { 225 // get values from Configuration file, if none, it will go back to using defaultvalues 226 // put them into the combobox 227 228 String value = Configuration.getString(affectedProperty, true); 229 boolean finished = value.equals(""); 230 if(!finished) { 231 control.removeAllItems(); // remove defaults, since config file now contains init values 232 control.addItem(value); 233 } // else value and combobox already contains the defaults 234 235 236 for(int i = 1; !finished; i++) { 237 value = Configuration.getString(affectedProperty+i, true); 238 if(value.equals("")) { 239 finished = true; 240 } 241 else { // value is not empty 242 control.addItem(value); 243 } 244 } 245 246 // setting the size of the dropdown control 247 control.setPreferredSize(newSize); 300 248 } 301 249 302 250 public void addLabelValueRows(JLabel[] labels, JComponent[] values, 303 251 GridBagLayout gridBag, Container container) { 304 252 GridBagConstraints c=new GridBagConstraints(); 305 253 c.insets=new Insets(0, 6, 6, 6); … … 311 259 gridBag.setConstraints(labels[i], c); 312 260 container.add(labels[i]); 313 261 314 262 c.gridwidth=GridBagConstraints.REMAINDER; //end row 315 263 if (!(values[i] instanceof JComboBox)) { … … 322 270 container.add(values[i]); 323 271 } 324 272 325 273 } 326 274 … … 368 316 dataChanged(); 369 317 } 370 318 371 319 public void dataChanged() { 372 320 if (m_passField.getPassword().length == 0) { 373 321 m_loginButton.setEnabled(false); 374 322 } else { … … 431 379 432 380 // all looks ok...just save stuff and exit now 433 381 m_lastServer=host + ":" + port; 434 382 m_lastProtocol=protocol; 435 383 m_lastUsername=username; 436 384 m_lastPassword=pass; 437 385 438 m_loginDialog.saveProperties(); 439 386 m_loginDialog.saveProperties(m_serverComboBox, "fedora.server", m_lastServer); 387 m_loginDialog.saveProperties(m_protocolComboBox, "fedora.protocol", m_lastProtocol); 388 m_loginDialog.saveProperties(m_usernameComboBox, "fedora.username", m_lastUsername); 389 390 // now save these values to the Configuration file 391 Configuration.save(); 440 392 441 393 login_requested = true; … … 450 402 451 403 e.printStackTrace(); 452 453 404 454 405 }
Note:
See TracChangeset
for help on using the changeset viewer.