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