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

Last change on this file since 8853 was 8850, checked in by mdewsnip, 20 years ago

Commented out the obsolete configuration dialog again.

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