source: trunk/gli/src/org/greenstone/gatherer/Configuration.java@ 9437

Last change on this file since 9437 was 9437, checked in by mdewsnip, 19 years ago

Updated version numbers in preparation for the 2.60 release.

  • Property svn:keywords set to Author Date Id Revision
File size: 35.9 KB
Line 
1/**
2 *#########################################################################
3 * Configuration
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer;
38
39import java.awt.*;
40import java.io.*;
41import java.lang.ref.*;
42import java.net.*;
43import java.util.*;
44import javax.swing.*;
45import javax.swing.plaf.*;
46import org.greenstone.gatherer.util.StaticStrings;
47import org.greenstone.gatherer.util.Utility;
48import org.greenstone.gatherer.util.XMLTools;
49import org.w3c.dom.*;
50
51/** This class stores the various configurable settings inside the Gatherer, both during a session, and between sessions in the form of XML. However not all data members are retained during xml serialization. To further improve efficiency, the property-name -> DOM Element pairs are stored in a SoftReferenced Hashtable.
52 * @author John Thompson, Greenstone Digital Library, University of Waikato
53 * @version 2.3
54 */
55public class Configuration
56 extends Hashtable {
57
58 static final public boolean COLLECTION_SPECIFIC = true;
59 static final public boolean GENERAL_SETTING = true;
60
61 static final public int ASSISTANT_MODE = 1;
62 static final public int LIBRARIAN_MODE = 2;
63 static final public int SYSTEMS_MODE = 3;
64 static final public int EXPERT_MODE = 4;
65
66 /** The string identifying an argument's name attribute. */
67 static final private String ARGUMENT_NAME = "name";
68 /** The name of the general Gatherer configuration file. */
69 static public String CONFIG_XML = "config.xml";
70 /** The name of the general Gatherer configuration file for running with GS3. */
71 static public String GS3_CONFIG_XML = "config3.xml";
72
73 static final private String CURRENT_CONFIGURATION_VERSION = "2.60";
74 /** The name of the root element of the subtree containing gatherer configuration options. This is required as the document itself may contain several other subtrees of settings (such as in the case of a '.col' file). */
75 static final private String GATHERER_CONFIG = "GathererConfig";
76 /** The string identifying an argument element. */
77 static final private String GATHERER_CONFIG_ARGUMENT = "Argument";
78
79 static final private String GENERAL_EMAIL_SETTING = "general.email";
80 /** the lang attribute of the other element */
81 static final private String LANGUAGE = "lang";
82 /** The name of a Name Element. */
83 static final private String NAME = "Name";
84 /** The name of the other arguments element. */
85 static final private String OTHER = "Other";
86 /** The name of an information Element within the Other subtree. */
87 static final private String OTHER_INFO = "Info";
88 /** The name of the general Gatherer configuration template. */
89 static public String TEMPLATE_CONFIG_XML = "xml/config.xml";
90 /** The first of three patterns used during tokenization, this pattern handles a comma separated list. */
91 static final private String TOKENIZER_PATTERN1 = " ,\n\t";
92 /** The second of three patterns used during tokenization, this pattern handles an underscore separated list. */
93 static final private String TOKENIZER_PATTERN2 = "_\n\t";
94 /** The last of three patterns used during tokenization, this pattern handles an comma separated list containing spaces. */
95 static final private String TOKENIZER_PATTERN3 = ",\n\t";
96
97 static public Configuration self = null;
98
99 static public boolean just_created_new_config_file = false;
100 /** The path to the Greenstone Suite installation directory. */
101 static public String gsdl_path = "";
102 /** If we are using GLI in Greenstone 3, the path to gsdl3 directory */
103 static public String gsdl3_path = "";
104 /** The path to the PERL executable, up to and including Perl.exe. */
105 static public String perl_path = "";
106 /** The password for the proxy server indicated above. */
107 static public String proxy_pass = null;
108 /** The username for the proxy server indicated above. */
109 static public String proxy_user = null;
110 /** The screen size of the desktop the Gatherer will be displayed on. */
111 static public Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
112 /** The site name if we are using GS3 */
113 static public String site_name = "";
114 /** The servlet path if we are using GS3 */
115 static public String servlet_path = "";
116 /** Collection level configuration (which in some cases overrides general configuration. */
117 static private Document collection_config;
118 /** The general configuration settings. */
119 static private Document general_config;
120
121 /** The element, from glis config file, that contains the various directory mappings. */
122 static private Element directory_mappings_element;
123
124 static private int cache_hit = 0;
125 static private int cache_miss = 0;
126
127 static private String wget_path = null;
128 static private String wget_version_str = StaticStrings.NO_WGET_STR;
129
130 static public URL library_url = null;
131
132
133 /** Constructor.
134 * @param gsdl_path The path to the Greenstone directory as a <strong>String</strong>.
135 * @param gsdl3_path The path to the Greenstone 3 directory as a <strong>String</strong>.
136 * @param site_name The name of the Greenstone 3 site currently in use.
137 */
138 public Configuration(String gsdl_path, String gsdl3_path, String site_name)
139 {
140 super();
141 self = this;
142
143 this.gsdl_path = gsdl_path;
144 this.gsdl3_path = gsdl3_path;
145 this.site_name = site_name;
146
147 // Try to load the configuration file from the user specific location
148 String config_xml_file_name = CONFIG_XML;
149 if (gsdl3_path != null) {
150 config_xml_file_name = GS3_CONFIG_XML;
151 }
152 File config_xml_file = new File(Utility.getGLIUserFolder(), config_xml_file_name);
153 if (config_xml_file != null && config_xml_file.exists()) {
154 general_config = Utility.parse(config_xml_file.getAbsolutePath(), false);
155 }
156
157 // If the version of the config we've loaded isn't recent enough, backup the old version and create a new one
158 just_created_new_config_file = false;
159 if (general_config != null) {
160 Element configuration_element = general_config.getDocumentElement();
161 String configuration_version = configuration_element.getAttribute(StaticStrings.VERSION_ATTRIBUTE);
162 if (!configuration_version.equals(CURRENT_CONFIGURATION_VERSION)) {
163 // !!! JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Configuration.Obsolete_Config"), Dictionary.get("Configuration.Obsolete_Config_Title"), JOptionPane.ERROR_MESSAGE);
164 File backup = new File(config_xml_file.getAbsolutePath() + ".bak");
165 config_xml_file.renameTo(backup);
166 general_config = null;
167 }
168 }
169
170 // Create a new config from the default configuration file from our xml library
171 if (general_config == null) {
172 general_config = Utility.parse(TEMPLATE_CONFIG_XML, true);
173 just_created_new_config_file = true;
174 }
175
176 // Determine the Directory Mappings element
177 directory_mappings_element = (Element) XMLTools.getNodeFromNamed(general_config.getDocumentElement(), StaticStrings.DIRECTORY_MAPPINGS_ELEMENT);
178
179 // Re-establish the color settings.
180 updateUI();
181
182 // Read the Greenstone library URL from the config file
183 String library_url_string = getString("general.library_url", true);
184 if (!library_url_string.equals("")) {
185 try {
186 library_url = new URL(library_url_string);
187 }
188 catch (MalformedURLException exception) {
189 DebugStream.printStackTrace(exception);
190 }
191 }
192
193 if (gsdl3_path != null) {
194 if (this.site_name == null || this.site_name.equals("")) {
195 this.site_name = getString("general.site_name", true);
196 this.servlet_path = getString("general.servlet_path", true);
197 if (this.site_name.equals("")) {
198 this.site_name = "localsite"; // can we assume these??
199 this.servlet_path = "/library";
200 setString("general.site_name", true, this.site_name);
201 setString("general.servlet_path", true, this.servlet_path);
202 }
203
204 } else {
205 // we set the current one in the config
206 setString("general.site_name", true, this.site_name);
207 if (this.servlet_path != null && !this.servlet_path.equals("")) {
208 setString("general.servlet_path", true, this.servlet_path);
209 }
210 }
211 }
212 }
213
214
215 /** Add a special directory mapping. */
216 static public boolean addDirectoryMapping(String name, File file) {
217 boolean result = false;
218 try {
219 // Ensure the name isn't already in use.
220 boolean found = false;
221 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
222 for(int i = 0; !found && i < mappings.getLength(); i++) {
223 Element mapping_element = (Element) mappings.item(i);
224 if(mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
225 found = true;
226 }
227 mapping_element = null;
228 }
229 // Otherwise add the mapping.
230 if(!found) {
231 Element mapping_element = general_config.createElement(StaticStrings.MAPPING_ELEMENT);
232 mapping_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
233 mapping_element.setAttribute(StaticStrings.FILE_ATTRIBUTE, file.toString());
234 directory_mappings_element.appendChild(mapping_element);
235 result = true;
236 mapping_element = null;
237 }
238 mappings = null;
239 }
240 catch (Exception exception) {
241 DebugStream.printStackTrace(exception);
242 }
243 return result;
244 } /** addDirectoryMapping(String name, String file) **/
245
246 /** The default get action retrieves the named property from the desired configuration, and returns a true or false. */
247 static public boolean get(String property, boolean general) {
248 String raw = getString(property, general);
249 return (raw != null && raw.equalsIgnoreCase("true"));
250 }
251
252 /** Retrieve all of the configuration preferences which match a certain string. They are returned as a hash map of property names to String objects. */
253 static public HashMap getAll(String property_pattern, boolean general) {
254 HashMap properties = new HashMap();
255 try {
256 // Locate the appropriate element
257 Element document_element = null;
258 if(general) {
259 document_element = general_config.getDocumentElement();
260 }
261 else if(collection_config != null) {
262 document_element = collection_config.getDocumentElement();
263 }
264 if(document_element != null) {
265 // Retrieve the Gatherer element
266 Element gatherer_element = (Element) XMLTools.getNodeFromNamed(document_element, GATHERER_CONFIG);
267 NodeList arguments = gatherer_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
268 for(int i = 0; i < arguments.getLength(); i++) {
269 Element argument_element = (Element) arguments.item(i);
270 if(argument_element.getAttribute(ARGUMENT_NAME).matches(property_pattern)) {
271 String result = XMLTools.getValue(argument_element);
272 // Store a mapping in the cache. Sometimes we will overwrite an existing value (say for collection and general level workflow options) but the processing overhead of detecting these clashes far exceeds any savings.
273 self.put(argument_element.getAttribute(ARGUMENT_NAME) + general, new SoftReference(argument_element));
274 // Add mapping to the properties we're going to return
275 properties.put(argument_element.getAttribute(ARGUMENT_NAME), result);
276 }
277 }
278 }
279 }
280 catch(Exception error) {
281 }
282 return properties;
283 }
284
285 /** Retrieve the information subtree containing the arguments for the desired external program. If the program has marked superclasses append their arguments as well. */
286 static public Element getArguments(String filename) {
287 Element argument_element = null;
288 try {
289 // Retrieve the other information subtree.
290 Element document_element = general_config.getDocumentElement();
291 Element other_element = (Element) XMLTools.getNodeFromNamed(document_element, OTHER);
292 NodeList argument_elements = other_element.getElementsByTagName(OTHER_INFO);
293 for(int i = 0; argument_element == null && i < argument_elements.getLength(); i++) {
294 Element possible_element = (Element) argument_elements.item(i);
295 Element possible_name_element = (Element) XMLTools.getNodeFromNamed(possible_element, NAME);
296 String possible_name = XMLTools.getValue(possible_name_element);
297 ///ystem.err.println("Does " + possible_name + " equal " + filename);
298 if(possible_name.equalsIgnoreCase(filename)) {
299 argument_element = possible_element;
300 }
301 possible_name = null;
302 possible_name_element = null;
303 possible_element = null;
304 }
305 argument_elements = null;
306 other_element = null;
307 document_element = null;
308 }
309 catch(Exception error) {
310 }
311 return argument_element;
312 }
313
314 /** Gets the language for the other arguments subtree
315 @return empty string if no language specified */
316 static public String getArgumentsLanguage() {
317 Element document_element = general_config.getDocumentElement();
318 Element other_element = (Element) XMLTools.getNodeFromNamed(document_element, OTHER);
319 String lang = other_element.getAttribute(LANGUAGE);
320 if (lang == null) {
321 lang = "en";
322 }
323 return lang;
324 }
325
326 /** Retrieve the value of the named property as a Rectangle. */
327 static public Rectangle getBounds(String property, boolean general) {
328 Rectangle result = null;
329 try {
330 String raw = getString(property, general);
331 // Rectangle is (x, y, width, height)
332 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
333 int x = Integer.parseInt(tokenizer.nextToken());
334 int y = Integer.parseInt(tokenizer.nextToken());
335 int width = Integer.parseInt(tokenizer.nextToken());
336 int height = Integer.parseInt(tokenizer.nextToken());
337 result = new Rectangle(x, y, width, height);
338 }
339 catch(Exception error) {
340 DebugStream.printStackTrace(error);
341 }
342 return result;
343 }
344
345 /** Retrieve the value of the named property as a Color. */
346 static public Color getColor(String property, boolean general) {
347 Color result = Color.white; // Default
348 try {
349 String raw = getString(property, general);
350 // Color is a RGB triplet list, comma separated (also remove whitespace)
351 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
352 int red = Integer.parseInt(tokenizer.nextToken());
353 int green = Integer.parseInt(tokenizer.nextToken());
354 int blue = Integer.parseInt(tokenizer.nextToken());
355 result = new Color(red, green, blue);
356 }
357 catch(Exception error) {
358 error.printStackTrace();
359 }
360 return result;
361 }
362
363
364 /** Retrieve the special directory mappings associated with this collection.
365 * @return A <strong>HashMap</strong> containing mappings from names to directories.
366 */
367 static public HashMap getDirectoryMappings() {
368 HashMap special_directories = new HashMap();
369 try {
370 // Ensure the name isn't already in use.
371 boolean found = false;
372 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
373 for(int i = 0; !found && i < mappings.getLength(); i++) {
374 Element mapping_element = (Element) mappings.item(i);
375 String name = mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
376 File file = new File(mapping_element.getAttribute(StaticStrings.FILE_ATTRIBUTE));
377 special_directories.put(name, file);
378 file = null;
379 name = null;
380 mapping_element = null;
381 }
382 mappings = null;
383 }
384 catch(Exception exception) {
385 DebugStream.printStackTrace(exception);
386 }
387 return special_directories;
388 } /** getDirectoryMappings() */
389
390 /** Retrieve the current users email. These are always stored in the general settings.
391 * @return the email address, if it is set, as a String
392 */
393 static public String getEmail() {
394 String email = getString(GENERAL_EMAIL_SETTING, true);
395 return (email.length() > 0 ? email : null);
396 }
397
398 static public Element getFileAssociations() {
399 NodeList file_association_elements = general_config.getDocumentElement().getElementsByTagName(StaticStrings.ASSOCIATIONS_ELEMENT);
400 return (Element) file_association_elements.item(0);
401 }
402
403 /** Retrieve the value of the named property as a FontUIResource. */
404 static public FontUIResource getFont(String property, boolean general) {
405 FontUIResource result = new FontUIResource("Times New Roman", Font.PLAIN, 10);
406 try {
407 String raw = getString(property, general);
408 // Font is a face, style, size triplet.
409 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN3);
410 String face = tokenizer.nextToken().trim();
411 ///ystem.err.println("Face: " + face);
412 int style = Font.PLAIN;
413 String temp = tokenizer.nextToken().toUpperCase().trim();
414 ///ystem.err.println("Style: " + temp);
415 if(temp.equals("BOLD")) {
416 style = Font.BOLD;
417 }
418 else if(temp.equals("ITALIC")) {
419 style = Font.ITALIC;
420 }
421 int size = Integer.parseInt(tokenizer.nextToken().trim());
422 ///ystem.err.println("Size: " + size);
423 result = new FontUIResource(face, style, size);
424 }
425 catch(Exception error) {
426 DebugStream.printStackTrace(error);
427 }
428 return result;
429 }
430
431 /** Retrieve the value of the named property as an integer. */
432 static public int getInt(String property, boolean general) {
433 int result = 0;
434 try {
435 String raw = getString(property, general);
436 result = Integer.parseInt(raw);
437 }
438 catch(Exception error) {
439 DebugStream.printStackTrace(error);
440 }
441 return result;
442 }
443
444 /** Retrieves the current interface language two letter code. */
445 static public String getLanguage() {
446 Locale locale = getLocale("general.locale", GENERAL_SETTING);
447 String code = "en"; // Default
448 if(locale != null) {
449 code = locale.getLanguage();
450 }
451 return code;
452 }
453
454 /** Retrieve the value of the named property as a Locale. */
455 static public Locale getLocale(String property, boolean general) {
456 Locale result = Locale.getDefault();
457 try {
458 String raw = getString(property, general);
459 // Locale is a underscore separated code.
460 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN2);
461 String language = tokenizer.nextToken();
462 if(tokenizer.hasMoreTokens()) {
463 String country = tokenizer.nextToken();
464 result = new Locale(language, country);
465 }
466 else {
467 result = new Locale(language);
468 }
469 }
470 catch(Exception error) {
471 DebugStream.printStackTrace(error);
472 }
473 return result;
474 }
475
476 /** Because modes will soon be an integral part of GLI, they get their own easy to remember methods such as this one to get the mode.
477 * @return an int representing the mode
478 */
479 static public int getMode() {
480 return getInt("general.mode", GENERAL_SETTING);
481 }
482
483 /** Return the current mode as a string for use in title bar etc
484 * @return the mode as a String
485 */
486 static public String getModeAsString() {
487 String result;
488 switch(getInt("general.mode", GENERAL_SETTING)) {
489 case ASSISTANT_MODE:
490 result = Dictionary.get("Preferences.Mode.Assistant");
491 break;
492 case SYSTEMS_MODE:
493 result = Dictionary.get("Preferences.Mode.Systems");
494 break;
495 case EXPERT_MODE:
496 result = Dictionary.get("Preferences.Mode.Expert");
497 break;
498 default:
499 result = Dictionary.get("Preferences.Mode.Librarian");
500 }
501 return result;
502 }
503
504 static public String getPreviewCommand() {
505 return getString("general.preview_program", GENERAL_SETTING);
506 }
507
508 static public String getServletPath() {
509 return servlet_path;
510 }
511
512 /** Retrieve the value of the named property, and noting whether we consult the general or collection specific configuration. */
513 static public String getString(String property, boolean general) {
514 // Its up to this method to find the appropriate node and retrieve the data itself.
515 String result = "";
516 try {
517 // First of all we look in the cache to see if we have a match.
518 SoftReference reference = (SoftReference) self.get(property + general);
519 if(reference != null) {
520 Element argument_element = (Element) reference.get();
521 if(argument_element != null) {
522 cache_hit++;
523 result = XMLTools.getValue(argument_element);
524 }
525 }
526 // We may have missed in the cache, or the reference may have been consumed.
527 if(result.length() == 0) {
528 cache_miss++;
529 // Locate the appropriate element
530 Element document_element = null;
531 if(general) {
532 document_element = general_config.getDocumentElement();
533 }
534 else if(collection_config != null) {
535 document_element = collection_config.getDocumentElement();
536 }
537 if(document_element != null) {
538 // Retrieve the Gatherer element
539 Element gatherer_element = (Element) XMLTools.getNodeFromNamed(document_element, GATHERER_CONFIG);
540 NodeList arguments = gatherer_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
541 for(int i = 0; result.length() == 0 && i < arguments.getLength(); i++) {
542 Element argument_element = (Element) arguments.item(i);
543 if(argument_element.getAttribute(ARGUMENT_NAME).equalsIgnoreCase(property)) {
544 result = XMLTools.getValue(argument_element);
545 // Store a mapping in the cache. Sometimes we will overwrite an existing value (say for collection and general level workflow options) but the processing overhead of detecting these clashes far exceeds any savings.
546 self.put(property + general, new SoftReference(argument_element));
547 }
548 }
549 }
550 }
551 }
552 catch (Exception error) {
553 error.printStackTrace();
554 }
555 // If we still have no result, and the search was made in the collection configuration, retrieve the general one instead.
556 if(result.length() == 0 && !general) {
557 result = getString(property, true);
558 }
559 return result;
560 }
561
562 /** Retrieve the path to the PERL scripts within the Greenstone directory.
563 * @return A <strong>String</strong> containing the path.
564 */
565 static public String getScriptPath() {
566 return gsdl_path + "bin" + File.separator + "script" + File.separator;
567 }
568
569 static public String getGS3ScriptPath() {
570 return gsdl3_path + "bin" + File.separator + "script" + File.separator;
571 }
572
573
574 static public String getWGetPath() {
575 return (wget_path != null ? wget_path : "");
576 }
577
578 static public String getWGetVersion() {
579 return (wget_version_str != null ? wget_version_str : StaticStrings.NO_WGET_STR);
580 }
581
582 /** Remove a previously defined special directory mapping.
583 * @param name The name of the mapping to remove as a <strong>String</strong>.
584 * @return The <strong>File</strong> of the mapping removed.
585 */
586 static public File removeDirectoryMapping(String name) {
587 File file = null;
588 try {
589 // Ensure the name isn't already in use.
590 boolean found = false;
591 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
592 for(int i = 0; !found && i < mappings.getLength(); i++) {
593 Element mapping_element = (Element) mappings.item(i);
594 if(mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
595 file = new File(XMLTools.getValue(mapping_element));
596 directory_mappings_element.removeChild(mapping_element);
597 found = true;
598 }
599 mapping_element = null;
600 }
601 mappings = null;
602 }
603 catch(Exception error) {
604 DebugStream.printStackTrace(error);
605 }
606 return file;
607 } /** removeDirectoryMapping(String name) */
608
609 /** Export the general configuration to file. */
610 static public void save() {
611 ///ystem.err.println("Hits " + cache_hit + " vs Misses " + cache_miss);
612 // We first try exporting to a user specific place
613 File user_config_xml = null;
614 String config_xml_name = CONFIG_XML;
615 if (gsdl3_path!=null) {
616 config_xml_name = GS3_CONFIG_XML;
617 }
618 try {
619 user_config_xml = new File(Utility.getGLIUserFolder(), config_xml_name);
620 ///ystem.err.println("Trying to save to: " + user_config_xml.getAbsolutePath());
621 ///ystem.err.println("Writing.");
622 user_config_xml.getParentFile().mkdirs();
623 XMLTools.writeXMLFile(new File(user_config_xml.getAbsolutePath()), general_config);
624 }
625 catch(Exception exception) {
626 user_config_xml = null;
627 }
628 // Always do this if the above fails.
629 if(user_config_xml == null || !user_config_xml.exists()) {
630 // But failing that we produce a general copy
631 XMLTools.writeXMLFile(new File(Utility.BASE_DIR + config_xml_name), general_config);
632 }
633 }
634
635 /** Set the named property, from the specified configuration, using the given boolean value. */
636 static public void set(String property, boolean general, boolean value) {
637 if(property.startsWith("workflow")) {
638 DebugStream.println("Set property: " + property + ", general=" + general + ", value=" + value);
639 }
640 setString(property, general, (value ? "true" : "false"));
641 }
642
643 /** Add a subtree of argument information to the other arguments part of the general configuration. This overwrites any such existing subtree. */
644 static public void setArguments(Element arguments_element) {
645 try {
646 Element document_element = general_config.getDocumentElement();
647 Element other_element = (Element) XMLTools.getNodeFromNamed(document_element, OTHER);
648 // Retrieve the name of the information
649 Element arguments_name_element = (Element)XMLTools.getNodeFromNamed(arguments_element, NAME);
650 String filename = XMLTools.getValue(arguments_element);
651 // Find any argument information subtree starting with the same name
652 Element obsolete_arguments_element = getArguments(filename);
653 // Create a copy of the arguments_element within our tree (import).
654 Element our_arguments_element = (Element) general_config.importNode(arguments_element, true);
655 // Now we insert this new node into the tree. If a previous node exists we replace it instead.
656 if(obsolete_arguments_element == null) {
657 other_element.appendChild(our_arguments_element);
658 }
659 else {
660 other_element.replaceChild(our_arguments_element, obsolete_arguments_element);
661 }
662 our_arguments_element = null;
663 obsolete_arguments_element = null;
664 filename = null;
665 arguments_name_element = null;
666 other_element = null;
667 document_element = null;
668 }
669 catch (Exception error) {
670 DebugStream.println("Error in Configuration.setArguments(): " + error);
671 DebugStream.printStackTrace(error);
672 }
673 }
674
675 /** Sets the language for the other arguments subtree */
676 static public void setArgumentsLanguage(String lang) {
677 Element document_element = general_config.getDocumentElement();
678 Element other_element = (Element) XMLTools.getNodeFromNamed(document_element, OTHER);
679 other_element.setAttribute(LANGUAGE, lang);
680 }
681
682
683 /** Set the collection configuration. */
684 static public void setCollectionConfiguration(Document collection_config) {
685 // clear the cached values
686 self.clear();
687 collection_config = collection_config;
688 updateUI();
689 ///atherer.println("Collection configuration set.");
690 }
691
692 /** Set the named property, from the specified configuration, using the given Rectangle value. */
693 static public void setBounds(String property, boolean general, Rectangle value) {
694 StringBuffer text = new StringBuffer("");
695 text.append(value.x);
696 text.append(", ");
697 text.append(value.y);
698 text.append(", ");
699 text.append(value.width);
700 text.append(", ");
701 text.append(value.height);
702 setString(property, general, text.toString());
703 }
704
705 /** Set the named property, from the specified configuration, using the given Color value. */
706 static public void setColor(String property, boolean general, Color value) {
707 StringBuffer text = new StringBuffer("");
708 text.append(value.getRed());
709 text.append(", ");
710 text.append(value.getGreen());
711 text.append(", ");
712 text.append(value.getBlue());
713 setString(property, general, text.toString());
714 }
715
716 /** Establish the current users email.
717 * @param email the email as a String
718 */
719 static public void setEmail(String email) {
720 setString(GENERAL_EMAIL_SETTING, true, email);
721 }
722
723 /** Set the named property, from the specified configuration, using the given Font value. */
724 static public void setFont(String property, boolean general, Font value) {
725 StringBuffer text = new StringBuffer("");
726 text.append(value.getName());
727 text.append(", ");
728 switch(value.getStyle()) {
729 case Font.BOLD:
730 text.append("BOLD");
731 break;
732 case Font.ITALIC:
733 text.append("ITALIC");
734 break;
735 default:
736 text.append("PLAIN");
737 }
738 text.append(", ");
739 text.append(value.getSize());
740 setString(property, general, text.toString());
741 }
742
743 /** Set the named property, from the specified configuration, using the given integer value. */
744 static public void setInt(String property, boolean general, int value) {
745 setString(property, general, String.valueOf(value));
746 }
747
748 /** Set the named property, from the specified configuration, using the given Locale value. */
749 static public void setLocale(String property, boolean general, Locale value) {
750 StringBuffer text = new StringBuffer("");
751 text.append(value.getLanguage());
752 String country = value.getCountry();
753 if(country != null && country.length() > 0) {
754 text.append("_");
755 text.append(country);
756 }
757 country = null;
758 setString(property, general, text.toString());
759 }
760
761 /** Because modes will soon be an integral part of GLI, they get their own easy to remember methods such as this one to set the mode.
762 * @param value the new value for mode
763 */
764 static public void setMode(int value) {
765 setInt("general.mode", GENERAL_SETTING, value);
766 }
767
768 static public void setPreviewCommand(String value) {
769 setString("general.preview_program", GENERAL_SETTING, value);
770 }
771
772 static public void setSiteAndServlet(String site, String servlet) {
773 site_name = site;
774 servlet_path = servlet;
775 setString("general.site_name", GENERAL_SETTING, site);
776 setString("general.servlet_path", GENERAL_SETTING, servlet);
777
778 }
779 /** Sets the value of the named property argument using the given string. */
780 static public void setString(String property, boolean general, String value) {
781 DebugStream.println("Set configuration property: " + property + " = " + value + (general ? "" : " [Collection]"));
782 try {
783 Document document = general_config;
784 if(!general && collection_config != null) {
785 document = collection_config;
786 }
787 if(document != null) {
788 Element argument_element = null;
789 // Try to retrieve from cache
790 SoftReference reference = (SoftReference) self.get(property + general);
791 if(reference != null) {
792 argument_element = (Element) reference.get();
793 }
794 if(argument_element == null) {
795 Element document_element = document.getDocumentElement();
796 Element gatherer_element = (Element) XMLTools.getNodeFromNamed(document_element, GATHERER_CONFIG);
797 NodeList arguments = document_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
798 boolean found = false;
799 for(int i = 0; argument_element == null && i < arguments.getLength(); i++) {
800 Element possible_element = (Element) arguments.item(i);
801 if(possible_element.getAttribute(ARGUMENT_NAME).equalsIgnoreCase(property)) {
802 argument_element = possible_element;
803 }
804 }
805 // If argument element is still null, create it in the target document.
806 if(argument_element == null) {
807 argument_element = document.createElement(GATHERER_CONFIG_ARGUMENT);
808 argument_element.setAttribute(ARGUMENT_NAME, property);
809 gatherer_element.appendChild(argument_element);
810 }
811 // Update cache
812 self.put(property + general, new SoftReference(argument_element));
813
814 }
815 if(value == null) {
816 value = "";
817 }
818 // Now remove any current text node children.
819 NodeList children = argument_element.getChildNodes();
820 for(int i = 0; i < children.getLength(); i++) {
821 argument_element.removeChild(children.item(i));
822 }
823 // Add a new text node child with the new value
824 argument_element.appendChild(document.createTextNode(value));
825 }
826 }
827 catch (Exception error) {
828 }
829 }
830
831 static public void setWGetPath(String path) {
832 wget_path = path;
833 }
834
835 static public void setWGetVersion(String version) {
836 wget_version_str = version;
837 }
838
839 static private void updateUI() {
840 // Buttons
841 UIManager.put("Button.select", new ColorUIResource(getColor("coloring.button_selected_background", false)));
842 UIManager.put("Button.background", new ColorUIResource(getColor("coloring.button_background", false)));
843 UIManager.put("Button.foreground", new ColorUIResource(getColor("coloring.button_foreground", false)));
844
845 UIManager.put("ToggleButton.background", new ColorUIResource(getColor("coloring.button_background", false)));
846 UIManager.put("ToggleButton.foreground", new ColorUIResource(getColor("coloring.button_foreground", false)));
847 UIManager.put("ToggleButton.select", new ColorUIResource(getColor("coloring.button_selected_background", false)));
848
849 // All the things with a lovely Collection green background
850 UIManager.put("OptionPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
851 UIManager.put("Panel.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
852 UIManager.put("Label.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
853 UIManager.put("TabbedPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
854 UIManager.put("SplitPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
855 UIManager.put("CheckBox.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
856
857
858 // Editable coloring
859 UIManager.put("ComboBox.background", new ColorUIResource(getColor("coloring.collection_tree_background", false))); // Indicate clickable
860 UIManager.put("Tree.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
861 UIManager.put("Tree.textBackground", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
862 UIManager.put("ProgressBar.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
863 UIManager.put("TextArea.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
864 UIManager.put("TextField.background", new ColorUIResource(getColor("coloring.editable_background", false)));
865 UIManager.put("Table.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
866 UIManager.put("List.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
867 UIManager.put("RadioButton.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
868
869 // Selection color
870 UIManager.put("TabbedPane.selected", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
871 UIManager.put("Tree.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
872 UIManager.put("ComboBox.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
873 UIManager.put("ProgressBar.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
874 UIManager.put("TextArea.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
875 UIManager.put("TextField.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
876 UIManager.put("List.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
877
878 // Scroll bar stuff
879 UIManager.put("ScrollBar.background", new ColorUIResource(getColor("coloring.scrollbar_background", false)));
880 UIManager.put("ScrollBar.thumb", new ColorUIResource(getColor("coloring.scrollbar_foreground", false)));
881 }
882}
Note: See TracBrowser for help on using the repository browser.