greenstone.org greenstone wiki greenstone trac planet greenstone

Changeset 16338

Show
Ignore:
Timestamp:
2008-07-10 13:42:34 (4 months ago)
Author:
ak19
Message:

Completely changed in order to still work with WarningDialog? but now deal with not only the regular JTextField and URLField.Text field, but also a URLField.Dropdown combobox that stores a certain number of previously-entered gliserver URLs and also the associated last-opened collection files. URLField is now a class with static methods now in order to deal with JTextField, URL TextFields? and URL Comboxes. Therefore it now contains serveral inner classes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gli/trunk/src/org/greenstone/gatherer/gui/URLField.java

    r10575 r16338  
    44import java.net.*; 
    55import javax.swing.*; 
    6  
    7  
     6import java.util.*; 
     7import java.io.*; 
     8import org.greenstone.gatherer.Gatherer; 
     9import org.greenstone.gatherer.Configuration; 
     10 
     11/** Modified.  
     12 * Class used by WarningDialog and Gatherer which represents a textual input control  
     13 * (TextField or Combobox) into which a URL can be entered. Provides some static methods 
     14 * shared by these controls and by a general JTextField control which can all be used  
     15 * in a WarningDialog. Class URLField also contains an inner Interface class and two  
     16 * static inner classes that implement this interface: Text and DropDown.  
     17 * Finally, another static inner class URLCollectionPair associates each Gliserver URL 
     18 * (or Library URL) that can be displayed in the DropDown with the last opened collection 
     19 * file for the Greenstone server at that URL.  
     20*/ 
    821public class URLField 
    9     extends JTextField 
    1022{ 
    11     public URLField(Color foreground, Color background) 
    12     { 
    13         super(); 
    14         setBackground(background); 
    15         setForeground(foreground); 
    16     } 
    17  
    18  
    19     public boolean validateURL() 
    20     { 
    21         String url_string = getText(); 
     23    static public String getText(JComponent control) 
     24    { 
     25        if(control instanceof JTextField) { // not specifically a URL Field, therefore, we're not meant to do checking 
     26            return ((JTextField)control).getText(); 
     27        }  
     28        else if(control instanceof URLField.DropDown) { 
     29            return (String)((JComboBox)control).getSelectedItem(); 
     30        } 
     31 
     32        // unknown control, shouldn't happen 
     33        return ""; 
     34    } 
     35 
     36    static public String setText(JComponent control, String text) 
     37    { 
     38        if(control instanceof JTextField) { // not specifically a URL Field, therefore, we're not meant to do checking 
     39            ((JTextField)control).setText(text); 
     40        }  
     41        else if(control instanceof URLField.DropDown) { 
     42            ((JComboBox)control).setSelectedItem(text);      
     43        } 
     44 
     45        // unknown control, shouldn't happen 
     46        return ""; 
     47    } 
     48 
     49    static public boolean validateURL(JComponent control) 
     50    { 
     51        if(control instanceof URLField.Text) { 
     52            return ((URLField.Text)control).validateURL(); 
     53        }  
     54        else if (control instanceof JTextField) { 
     55            return true; // not specifically a URL Field, therefore, we're not meant to do checking 
     56        } 
     57        else { // control is a DropDown 
     58            return ((URLField.DropDown)control).validateURL(); 
     59        } 
     60    } 
     61 
     62    static public boolean validateURL(String url_string) 
     63    { 
    2264        if (!url_string.equals("")) { 
    2365            // Check the URL string is valid by trying to create a URL object from it 
     
    3476        return true; 
    3577    } 
     78 
     79    static public void store(JComponent control, String affected_property) { 
     80        if(control instanceof JTextField) {  
     81            String value = ((JTextField)control).getText(); 
     82            Configuration.setString(affected_property, true, value); 
     83        }  
     84        else { // DropDown 
     85            ((DropDown)control).saveProperties(); 
     86        } 
     87    } 
     88 
     89     
     90    /** INTERFACE CLASS IMPLEMENTED BY Static inner classes Text and DropDown */ 
     91    public static interface URLFieldControl { 
     92        public boolean validateURL(); 
     93    } 
     94 
     95 
     96    /** STATIC INNER CLASS URLTextField */ 
     97    public static class Text  
     98        extends JTextField implements URLFieldControl  
     99    { 
     100        public Text(Color foreground, Color background) 
     101        { 
     102            super(); 
     103            setBackground(background); 
     104            setForeground(foreground); 
     105        } 
     106 
     107        public boolean validateURL() { 
     108            String url_string = getText(); 
     109            return URLField.validateURL(url_string); 
     110        } 
     111    } 
     112 
     113    
     114    /** STATIC INNER CLASS DropDown, an editable URLComboBox */ 
     115    public static class DropDown  
     116        extends JComboBox implements URLFieldControl 
     117    { 
     118        private String checkString = null; // optional string to check the URLs items against to see whether the URLs contain this 
     119        private String affectedProperty = null; // the Configuration property associated with this DropDown 
     120        private String coaffectedProperty = null; // the Configuration property whose suffix should change along with the affectedProperty 
     121         
     122        private static final int MAX_URLS = 5; 
     123 
     124        public DropDown(Color foreground, Color background, String[] defaultURLs,  
     125                        String affectedProperty, String coaffectedProperty) 
     126        { 
     127            super(URLCollectionPair.createDefaultsArray(defaultURLs)); 
     128            this.setEditable(true); 
     129            this.getEditor().selectAll(); 
     130             
     131            setBackground(background); 
     132            setForeground(foreground); 
     133             
     134            this.affectedProperty = affectedProperty; 
     135            this.coaffectedProperty = coaffectedProperty; 
     136            // read any URLS specified in the config file 
     137            setComboBoxValues(); 
     138             
     139        } 
     140 
     141        public DropDown(Color foreground, Color background, String[] defaultURLs,  
     142                        String affectedProperty, String coaffectedProperty, String checkString) 
     143        { 
     144            this(foreground, background, defaultURLs, affectedProperty, coaffectedProperty); 
     145            this.checkString = checkString; 
     146        }        
     147 
     148        public boolean validateURL() { 
     149            //String url_string = (String)this.getEditor().getItem(); 
     150            String url_string = this.getSelectedItem().toString(); // returns url of the combobox item 
     151                 
     152            if(checkString != null && !url_string.endsWith(checkString)) { 
     153                return false; 
     154            } else { 
     155                return URLField.validateURL(url_string); 
     156            } 
     157        } 
     158 
     159        public void saveProperties() { 
     160            // 1. Add the first url just edited (if any)  
     161 
     162            // The user might have created a new STRING, or selected an existing URLCollectionPair 
     163            // We don't know the type! 
     164            Object o = this.getSelectedItem();  
     165            URLCollectionPair editedItem = (o instanceof URLCollectionPair)  
     166                ? (URLCollectionPair)o : new URLCollectionPair(o.toString()); 
     167                     
     168            if(editedItem != null) { 
     169                insertItemAt(editedItem, 0); 
     170            } else { 
     171                editedItem = (URLCollectionPair)getItemAt(0); // else reuse the first default: 
     172            } 
     173            Configuration.setString(affectedProperty, true, editedItem.getURL()); 
     174             
     175            // concurrently set the last opened collection for this url 
     176            String lastOpenedCollection = editedItem.getCollection(); 
     177            if(lastOpenedCollection.equals("")) { 
     178                Configuration.setString(coaffectedProperty, true, null); 
     179            } else { 
     180                Configuration.setString(coaffectedProperty, true, lastOpenedCollection); 
     181            } 
     182 
     183 
     184            // 2. Add the remaining urls into the combobox 
     185            int url_count = 1; // remain under MAX_URLS AND under the number of actual URLs in the dropdown 
     186            for(int i = 0; url_count < MAX_URLS && i < getItemCount(); i++, url_count++) { 
     187                 
     188                URLCollectionPair item = (URLCollectionPair)getItemAt(i); 
     189                String url = item.getURL(); 
     190                 
     191                if(item == null || url.equals(editedItem.getURL()) || url.equals("")) {  
     192                    // skip any duplicates of the just-edited URL and skip empty URLs 
     193                    url_count--; 
     194                }  
     195                else {  
     196                    Configuration.setString(affectedProperty+url_count, true, url); 
     197                     
     198                    // concurrently save the name of the corresponding open collection 
     199                    if(item.getCollection().equals("")) { 
     200                        Configuration.setString(coaffectedProperty+url_count, true, null); 
     201                    } else { 
     202                        Configuration.setString(coaffectedProperty+url_count, true, item.getCollection()); 
     203                    } 
     204 
     205                } // else retain old value for this affectedProperty (general.gliserver_url) 
     206            } 
     207            Configuration.save(); 
     208        } 
     209 
     210         
     211        // Get values from Configuration file, or (if none) use defaultURLs. 
     212        // Put them into the combobox 
     213        private void setComboBoxValues() { 
     214 
     215            String url = Configuration.getString(affectedProperty, true); 
     216            String collection = Configuration.getString(coaffectedProperty, true); 
     217            boolean finished = url.equals(""); 
     218            if(!finished) { 
     219                this.removeAllItems(); // remove defaults, since config file now contains init values 
     220                this.addItem(new URLCollectionPair(url, collection)); 
     221            } // else urls and combobox already contains the defaults 
     222                     
     223 
     224            for(int i = 1; !finished; i++) { 
     225                url = Configuration.getString(affectedProperty+i, true); 
     226                collection = Configuration.getString(coaffectedProperty+i, true); 
     227                if(url.equals("")) { 
     228                    finished = true; 
     229                } 
     230                else { // url is not empty 
     231                    this.addItem(new URLCollectionPair(url, collection)); 
     232                } 
     233            } 
     234             
     235            // setting the size of the dropdown control 
     236            Dimension newSize=new Dimension(getPreferredSize().width+20, getPreferredSize().height); 
     237            setPreferredSize(newSize); 
     238        } 
     239    } 
     240 
     241 
     242    /** STATIC INNER CLASS URLCollectionPair.  
     243     * Since the configuration for a (remote) Greenstone2 and Greenstone3 is now 
     244     * stored in a single file for both, we can have GLI connecting to different servers  
     245     * while for each server, a different collection may have been left open last time. 
     246     * This class URLCollectionPair will associate a GLIServer_URL or Library_URL with 
     247     * the last opened collection file for it. It is used by URLField.DropDown combobox. */ 
     248    public static class URLCollectionPair { 
     249        String url;        // gliserverURL  
     250        String collection; // last opened collection for the corresponding gliserver 
     251 
     252         
     253        public static URLCollectionPair[] createDefaultsArray(String[] defaultURLs) { 
     254            URLCollectionPair[] pairs = new URLCollectionPair[defaultURLs.length]; 
     255            for(int i = 0; i < pairs.length; i++) { 
     256                pairs[i] = new URLCollectionPair(defaultURLs[i]); // no associated open collection 
     257            } 
     258            return pairs; 
     259        } 
     260 
     261        public URLCollectionPair(String url) { 
     262            this.set(url, ""); 
     263        } 
     264 
     265        public URLCollectionPair(String url, String collection) { 
     266            this.set(url, collection); 
     267        } 
     268 
     269        public void set(String url, String collection) { 
     270            this.url = url; 
     271            this.collection = collection; 
     272        } 
     273 
     274        public void setURL(String url) { 
     275            this.url = url; 
     276        } 
     277 
     278        public void setCollection(String collection) { 
     279            this.collection = collection; 
     280        } 
     281 
     282        public String getCollection() { 
     283            return collection; 
     284        } 
     285 
     286        public String getURL() { 
     287            return url; 
     288        } 
     289 
     290        /** For display in comboboxes */ 
     291        public String toString() { 
     292            return url; 
     293        } 
     294 
     295        /** @returns the number that is suffixed to the gliserver_url */ 
     296        public String getSuffixNumber() { 
     297            String numericSuffix = ""; 
     298            boolean stop = false; 
     299            for(int index = url.length()-1; index >= 0 && !stop; index--) { // work from the end 
     300                char character = url.charAt(index); 
     301                if(Character.isDigit(character)) { 
     302                    numericSuffix = Character.toString(character) + numericSuffix; 
     303                } else { 
     304                    stop = true; 
     305                } 
     306            } 
     307 
     308            return numericSuffix; 
     309        } 
     310    } 
     311 
    36312}