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