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

Last change on this file since 8629 was 8627, checked in by mdewsnip, 20 years ago

Replaced exec_address with library_url, which is much more descriptive (in my mind, anyway). Also tidied up a few things related to this.

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