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