Changeset 16337 for gli/trunk


Ignore:
Timestamp:
2008-07-10T13:36:30+12:00 (16 years ago)
Author:
ak19
Message:

Changed to let user-provided Fedora connection details be written into the existing fedora-config.xml rather than an additional file (properties file) as previously used.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/gui/FedoraLogin.java

    r15505 r16337  
    6666
    6767import org.greenstone.gatherer.Gatherer;
     68import org.greenstone.gatherer.Configuration;
    6869
    6970public class FedoraLogin extends JDialog
    7071{
     72    private static final int MAX_ITEMS = 5;
     73
    7174    private static final java.awt.Color LIGHT = new java.awt.Color(244, 244, 224);
    7275    private static final java.awt.Color TRANSPARENT = new java.awt.Color(0,0,0,0);
     76
    7377    private JPanel m_infoPane;
    7478    private JComboBox m_serverComboBox;
     
    8286    private String m_lastPassword="";
    8387
    84     private HashMap m_usernames;
    85     private HashMap m_servers;
    86     private HashMap m_protocols;
    87 
    8888    private boolean login_requested = false;
    8989
     
    9393    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    9494       
    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");
    10397        JLabel usernameLabel=new JLabel("Username");
    10498        JLabel passwordLabel=new JLabel("Password");
    10599
    106         m_serverComboBox=new JComboBox();
     100        m_serverComboBox=new JComboBox(new String[]{m_lastServer});
    107101        m_serverComboBox.setEditable(true);
    108         m_protocolComboBox=new JComboBox();
    109         m_protocolComboBox.setEditable(true);
    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});
    111105        m_usernameComboBox.setEditable(true);
    112106        m_passwordField=new JPasswordField();
     
    193187    }
    194188
    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    }
    198223   
    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);
    300248    }
    301249
    302250    public void addLabelValueRows(JLabel[] labels, JComponent[] values,
    303             GridBagLayout gridBag, Container container) {
     251                  GridBagLayout gridBag, Container container) {
    304252        GridBagConstraints c=new GridBagConstraints();
    305253        c.insets=new Insets(0, 6, 6, 6);
     
    311259            gridBag.setConstraints(labels[i], c);
    312260            container.add(labels[i]);
    313 
     261       
    314262            c.gridwidth=GridBagConstraints.REMAINDER;     //end row
    315263            if (!(values[i] instanceof JComboBox)) {
     
    322270            container.add(values[i]);
    323271        }
    324 
     272   
    325273    }
    326274
     
    368316            dataChanged();
    369317        }
    370 
     318   
    371319        public void dataChanged() {
    372             if (m_passField.getPassword().length == 0) {
     320        if (m_passField.getPassword().length == 0) {
    373321                m_loginButton.setEnabled(false);
    374322            } else {
     
    431379
    432380                    // all looks ok...just save stuff and exit now
    433                     m_lastServer=host + ":" + port;
     381            m_lastServer=host + ":" + port;
    434382                    m_lastProtocol=protocol;
    435383                    m_lastUsername=username;
    436384            m_lastPassword=pass;
    437385
    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();                   
    440392
    441393            login_requested = true;
     
    450402
    451403            e.printStackTrace();
    452            
    453404
    454405                }
Note: See TracChangeset for help on using the changeset viewer.