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

Last change on this file since 6582 was 6582, checked in by jmt12, 20 years ago

A whole bunch of files right in the middle of being edited because Michael is being awkward and expecting me to commit stuff regular like.

  • Property svn:keywords set to Author Date Id Revision
File size: 35.8 KB
Line 
1/**
2 *#########################################################################
3 *
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.msm.MSMUtils;
47import org.greenstone.gatherer.util.StaticStrings;
48import org.greenstone.gatherer.util.Utility;
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 final private String CONFIG_XML = "config.xml";
70 /** 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). */
71 static final private String GATHERER_CONFIG = "GathererConfig";
72 /** The string identifying an argument element. */
73 static final private String GATHERER_CONFIG_ARGUMENT = "Argument";
74
75 static final private String GENERAL_EMAIL_SETTING = "general.email";
76 /** The name of a Name Element. */
77 static final private String NAME = "Name";
78 /** The name of the other arguments element. */
79 static final private String OTHER = "Other";
80 /** The name of an information Element within the Other subtree. */
81 static final private String OTHER_INFO = "Info";
82 /** The name of the general Gatherer configuration template. */
83 static final private String TEMPLATE_CONFIG_XML = "xml/config.xml";
84 /** The first of three patterns used during tokenization, this pattern handles a comma separated list. */
85 static final private String TOKENIZER_PATTERN1 = " ,\n\t";
86 /** The second of three patterns used during tokenization, this pattern handles an underscore separated list. */
87 static final private String TOKENIZER_PATTERN2 = "_\n\t";
88 /** The last of three patterns used during tokenization, this pattern handles an comma separated list containing spaces. */
89 static final private String TOKENIZER_PATTERN3 = ",\n\t";
90
91 public File exec_file;
92 /** The path (or url) to the webserver which is serving the Greenstone collection. */
93 public String exec_path = null;
94 /** The path to the Greenstone Suite installation directory. */
95 public String gsdl_path = "";
96 /** The path to the PERL executable, up to and including Perl.exe. */
97 public String perl_path = "";
98 /** The password for the proxy server indicated above. */
99 public String proxy_pass = null;
100 /** The username for the proxy server indicated above. */
101 public String proxy_user = null;
102 /** The screen size of the desktop the Gatherer will be displayed on. */
103 public Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
104 /** Collection level configuration (which in some cases overrides general configuration. */
105 private Document collection_config;
106 /** The general configuration settings. */
107 private Document general_config;
108
109 /** The element, from glis config file, that contains the various directory mappings. */
110 private Element directory_mappings_element;
111
112 private int cache_hit = 0;
113 private int cache_miss = 0;
114 public URL exec_address = null;
115
116 /** Constructor.
117 * @param gsdl_path The path to the Greenstone directory as a <strong>String</strong>.
118 * @param exec_path A <strong>String</strong> containing the path or url to the webserver serving the greenstone collections.
119 * @param perl_path The path to the PERL executable, as a <strong>String</strong>.
120 */
121 public Configuration(String gsdl_path, String exec_path, String perl_path) {
122 super();
123 this.gsdl_path = gsdl_path;
124 this.exec_path = exec_path;
125 // The exec_path may contain an url address, in which case we blindly use that and leave it up to the user to worry about settings and resetting.
126 Gatherer.println("EXEC_PATH = " + exec_path);
127 if(exec_path != null && exec_path.length() > 0) {
128 try {
129 exec_address = new URL(exec_path);
130 }
131 catch (MalformedURLException error) {
132 Gatherer.println("Not an address.");
133 Gatherer.printStackTrace(error);
134 }
135 }
136 // If the above failed, then its up to us to try and figure out what to do.
137 if(exec_address == null) {
138 // Try building a file from the given exec_path
139 try {
140 File local_file = new File(exec_path);
141 if(local_file.exists()) {
142 // All good. I hope.
143 exec_file = local_file;
144 }
145 else {
146 Gatherer.println("No local library at given file path.");
147 }
148 }
149 // All sorts of errors might be thrown by a bogus file path.
150 catch (Exception error) {
151 Gatherer.println("Library url does not indicate the server.exe file.");
152 ///atherer.printStackTrace(error);
153 }
154 // We can generate the path to where the local library should be and use that if it is there.
155 if(exec_file == null) {
156 File server_exe = new File(gsdl_path + Utility.SERVER_EXE);
157 if(server_exe.exists()) {
158 exec_file = server_exe;
159 }
160 else {
161 Gatherer.println("No local library.");
162 }
163 }
164 // If we get to here with no exec_address nor an exec_file its just plain not going to work.
165 }
166 else {
167 Gatherer.println("exec_address != null -> " + exec_address);
168 }
169 ///ystem.err.println("Perl path.");
170 this.perl_path = perl_path;
171 // Ensure the perl path includes exe under windoze
172 if(Utility.isWindows() && !perl_path.toLowerCase().endsWith(".exe")) {
173 if(!perl_path.endsWith(File.separator)) {
174 perl_path = perl_path + File.separator;
175 }
176 perl_path = perl_path + "perl.exe";
177 }
178
179 // Try to reload the configuration. The first place we look is the user specific location
180 File user_config_xml = null;
181 // Windows. What to do, what to do?
182 if(Utility.isWindows()) {
183
184 }
185 // Unix based systems. This one I know. Look for .gli/config.xml in the users home folder
186 else {
187 user_config_xml = new File(Utility.getGLIUserFolder(), CONFIG_XML);
188 }
189 if(user_config_xml != null && user_config_xml.exists()) {
190 general_config = Utility.parse(user_config_xml.getAbsolutePath(), false);
191 }
192
193 // We then try within the gli directory itself
194 if(general_config == null) {
195 File config_xml = new File(CONFIG_XML);
196 if(config_xml.exists()) {
197 general_config = Utility.parse(CONFIG_XML, false);
198 }
199 }
200
201 // And if that fails retrieve the default configuration file from our xml library, which I'll personally guarantee to work.
202 if(general_config == null) {
203 general_config = Utility.parse(TEMPLATE_CONFIG_XML, true);
204 Gatherer.println("Loaded default Gatherer configuration template.");
205 }
206 else {
207 Gatherer.println("Loaded current Gatherer configuration.");
208 }
209
210 // Determine the Directory Mappings element
211 directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(general_config.getDocumentElement(), StaticStrings.DIRECTORY_MAPPINGS_ELEMENT);
212
213 // Re-establish the color settings.
214 updateUI();
215
216 // If we have no exec_address, see if one was specified in the config file
217 if (exec_address == null) {
218 String exec_address_string = getString("general.exec_address", true);
219 if (!exec_address_string.equals("")) {
220 try {
221 exec_address = new URL(exec_address_string);
222 }
223 catch (MalformedURLException error) {
224 ///ystem.err.println("Error: Bad address: " + exec_address_string);
225 }
226 }
227 }
228
229 Gatherer.println("EXEC_FILE = " + exec_file);
230 Gatherer.println("EXEC_ADDRESS = " + exec_address);
231 }
232
233 /** Add a special directory mapping. */
234 public boolean addDirectoryMapping(String name, File file) {
235 boolean result = false;
236 try {
237 // Ensure the name isn't already in use.
238 boolean found = false;
239 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
240 for(int i = 0; !found && i < mappings.getLength(); i++) {
241 Element mapping_element = (Element) mappings.item(i);
242 if(mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
243 found = true;
244 }
245 mapping_element = null;
246 }
247 // Otherwise add the mapping.
248 if(!found) {
249 Element mapping_element = general_config.createElement(StaticStrings.MAPPING_ELEMENT);
250 mapping_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
251 mapping_element.setAttribute(StaticStrings.FILE_ATTRIBUTE, file.toString());
252 directory_mappings_element.appendChild(mapping_element);
253 result = true;
254 mapping_element = null;
255 }
256 mappings = null;
257 }
258 catch (Exception exception) {
259 Gatherer.printStackTrace(exception);
260 }
261 return result;
262 } /** addDirectoryMapping(String name, String file) **/
263
264 /** The default get action retrieves the named property from the desired configuration, and returns a true or false. */
265 public boolean get(String property, boolean general) {
266 String raw = getString(property, general);
267 return (raw != null && raw.equalsIgnoreCase("true"));
268 }
269
270 /** Retrieve all of the configuration preferences which match a certain string. They are returned as a hash map of property names to String objects. */
271 public HashMap getAll(String property_pattern, boolean general) {
272 HashMap properties = new HashMap();
273 try {
274 // Locate the appropriate element
275 Element document_element = null;
276 if(general) {
277 document_element = general_config.getDocumentElement();
278 }
279 else if(collection_config != null) {
280 document_element = collection_config.getDocumentElement();
281 }
282 if(document_element != null) {
283 // Retrieve the Gatherer element
284 Element gatherer_element = (Element) MSMUtils.getNodeFromNamed(document_element, GATHERER_CONFIG);
285 NodeList arguments = gatherer_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
286 for(int i = 0; i < arguments.getLength(); i++) {
287 Element argument_element = (Element) arguments.item(i);
288 if(argument_element.getAttribute(ARGUMENT_NAME).matches(property_pattern)) {
289 String result = MSMUtils.getValue(argument_element);
290 // 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.
291 put(argument_element.getAttribute(ARGUMENT_NAME) + general, new SoftReference(argument_element));
292 // Add mapping to the properties we're going to return
293 properties.put(argument_element.getAttribute(ARGUMENT_NAME), result);
294 }
295 }
296 }
297 }
298 catch(Exception error) {
299 }
300 return properties;
301 }
302
303 /** Retrieve the information subtree containing the arguments for the desired external program. If the program has marked superclasses append their arguments as well. */
304 public Element getArguments(String filename) {
305 Element argument_element = null;
306 try {
307 // Retrieve the other information subtree.
308 Element document_element = general_config.getDocumentElement();
309 Element other_element = (Element) MSMUtils.getNodeFromNamed(document_element, OTHER);
310 NodeList argument_elements = other_element.getElementsByTagName(OTHER_INFO);
311 for(int i = 0; argument_element == null && i < argument_elements.getLength(); i++) {
312 Element possible_element = (Element) argument_elements.item(i);
313 Element possible_name_element = (Element) MSMUtils.getNodeFromNamed(possible_element, NAME);
314 String possible_name = MSMUtils.getValue(possible_name_element);
315 ///ystem.err.println("Does " + possible_name + " equal " + filename);
316 if(possible_name.equalsIgnoreCase(filename)) {
317 argument_element = possible_element;
318 }
319 possible_name = null;
320 possible_name_element = null;
321 possible_element = null;
322 }
323 argument_elements = null;
324 other_element = null;
325 document_element = null;
326 }
327 catch(Exception error) {
328 }
329 return argument_element;
330 }
331
332 /** Retrieve the value of the named property as a Rectangle. */
333 public Rectangle getBounds(String property, boolean general) {
334 Rectangle result = null;
335 try {
336 String raw = getString(property, general);
337 // Rectangle is (x, y, width, height)
338 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
339 int x = Integer.parseInt(tokenizer.nextToken());
340 int y = Integer.parseInt(tokenizer.nextToken());
341 int width = Integer.parseInt(tokenizer.nextToken());
342 int height = Integer.parseInt(tokenizer.nextToken());
343 result = new Rectangle(x, y, width, height);
344 }
345 catch(Exception error) {
346 Gatherer.printStackTrace(error);
347 }
348 return result;
349 }
350
351 /** Retrieve the value of the named property as a Color. */
352 public Color getColor(String property, boolean general) {
353 Color result = Color.white; // Default
354 try {
355 String raw = getString(property, general);
356 // Color is a RGB triplet list, comma separated (also remove whitespace)
357 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
358 int red = Integer.parseInt(tokenizer.nextToken());
359 int green = Integer.parseInt(tokenizer.nextToken());
360 int blue = Integer.parseInt(tokenizer.nextToken());
361 result = new Color(red, green, blue);
362 }
363 catch(Exception error) {
364 Gatherer.printStackTrace(error);
365 }
366 return result;
367 }
368
369 /** Retrieve the value of the named property as a Dimension. */
370 /* private Dimension getDimension(String property, boolean general) {
371 Dimension result = new Dimension(100, 100); // Default
372 try {
373 String raw = getString(property, general);
374 // Dimension is a width by height pair, comma separated (also remove whitespace)
375 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN1);
376 int width = Integer.parseInt(tokenizer.nextToken());
377 int height = Integer.parseInt(tokenizer.nextToken());
378 result = new Dimension(width, height);
379 }
380 catch(Exception error) {
381 Gatherer.printStackTrace(error);
382 }
383 return result;
384 } */
385
386 /** Retrieve the special directory mappings associated with this collection.
387 * @return A <strong>HashMap</strong> containing mappings from names to directories.
388 */
389 public HashMap getDirectoryMappings() {
390 HashMap special_directories = new HashMap();
391 try {
392 // Ensure the name isn't already in use.
393 boolean found = false;
394 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
395 for(int i = 0; !found && i < mappings.getLength(); i++) {
396 Element mapping_element = (Element) mappings.item(i);
397 String name = mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
398 File file = new File(mapping_element.getAttribute(StaticStrings.FILE_ATTRIBUTE));
399 special_directories.put(name, file);
400 file = null;
401 name = null;
402 mapping_element = null;
403 }
404 mappings = null;
405 }
406 catch(Exception exception) {
407 Gatherer.printStackTrace(exception);
408 }
409 return special_directories;
410 } /** getDirectoryMappings() */
411
412 /** Retrieve the current users email. These are always stored in the general settings.
413 * @return the email address, if it is set, as a String
414 */
415 public String getEmail() {
416 String email = getString(GENERAL_EMAIL_SETTING, true);
417 return (email.length() > 0 ? email : null);
418 }
419
420 /** Retrieve the value of the named property as a FontUIResource. */
421 public FontUIResource getFont(String property, boolean general) {
422 FontUIResource result = new FontUIResource("Times New Roman", Font.PLAIN, 10);
423 try {
424 String raw = getString(property, general);
425 // Font is a face, style, size triplet.
426 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN3);
427 String face = tokenizer.nextToken().trim();
428 System.err.println("Face: " + face);
429 int style = Font.PLAIN;
430 String temp = tokenizer.nextToken().toUpperCase();
431 if(temp.equals("BOLD")) {
432 style = Font.BOLD;
433 }
434 else if(temp.equals("ITALIC")) {
435 style = Font.ITALIC;
436 }
437 int size = Integer.parseInt(tokenizer.nextToken().trim());
438 result = new FontUIResource(face, style, size);
439 }
440 catch(Exception error) {
441 Gatherer.printStackTrace(error);
442 }
443 return result;
444 }
445
446 /** Retrieve the value of the named property as an integer. */
447 public int getInt(String property, boolean general) {
448 int result = 0;
449 try {
450 String raw = getString(property, general);
451 result = Integer.parseInt(raw);
452 }
453 catch(Exception error) {
454 Gatherer.printStackTrace(error);
455 }
456 return result;
457 }
458
459 /** Retrieves the current interface language two letter code. */
460 public String getLanguage() {
461 Locale locale = getLocale("general.locale", GENERAL_SETTING);
462 String code = "en"; // Default
463 if(locale != null) {
464 code = locale.getLanguage();
465 }
466 return code;
467 }
468
469 /** Retrieve the value of the named property as a Locale. */
470 public Locale getLocale(String property, boolean general) {
471 Locale result = Locale.getDefault();
472 try {
473 String raw = getString(property, general);
474 // Locale is a underscore separated code.
475 StringTokenizer tokenizer = new StringTokenizer(raw, TOKENIZER_PATTERN2);
476 String language = tokenizer.nextToken();
477 if(tokenizer.hasMoreTokens()) {
478 String country = tokenizer.nextToken();
479 result = new Locale(language, country);
480 }
481 else {
482 result = new Locale(language);
483 }
484 }
485 catch(Exception error) {
486 Gatherer.printStackTrace(error);
487 }
488 return result;
489 }
490
491 /** 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.
492 * @return an int representing the mode
493 */
494 public int getMode() {
495 return getInt("general.mode", GENERAL_SETTING);
496 }
497
498 /** Return the current mode as a string for use in title bar etc
499 * @return the mode as a String
500 */
501 public String getModeAsString() {
502 String result;
503 switch(getInt("general.mode", GENERAL_SETTING)) {
504 case ASSISTANT_MODE:
505 result = Dictionary.get("Preferences.Mode.Assistant");
506 break;
507 case SYSTEMS_MODE:
508 result = Dictionary.get("Preferences.Mode.Systems");
509 break;
510 case EXPERT_MODE:
511 result = Dictionary.get("Preferences.Mode.Expert");
512 break;
513 default:
514 result = Dictionary.get("Preferences.Mode.Librarian");
515 }
516 return result;
517 }
518
519 public String getPreviewCommand() {
520 return getString("general.preview_program", GENERAL_SETTING);
521 }
522
523 /** Retrieve the value of the named property, and noting whether we consult the general or collection specific configuration. */
524 public String getString(String property, boolean general) {
525 // Its up to this method to find the appropriate node and retrieve the data itself.
526 String result = "";
527 try {
528 // First of all we look in the cache to see if we have a match.
529 SoftReference reference = (SoftReference) get(property + general);
530 if(reference != null) {
531 Element argument_element = (Element) reference.get();
532 if(argument_element != null) {
533 cache_hit++;
534 result = MSMUtils.getValue(argument_element);
535 }
536 }
537 // We may have missed in the cache, or the reference may have been consumed.
538 if(result.length() == 0) {
539 cache_miss++;
540 // Locate the appropriate element
541 Element document_element = null;
542 if(general) {
543 document_element = general_config.getDocumentElement();
544 }
545 else if(collection_config != null) {
546 document_element = collection_config.getDocumentElement();
547 }
548 if(document_element != null) {
549 // Retrieve the Gatherer element
550 Element gatherer_element = (Element) MSMUtils.getNodeFromNamed(document_element, GATHERER_CONFIG);
551 NodeList arguments = gatherer_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
552 for(int i = 0; result.length() == 0 && i < arguments.getLength(); i++) {
553 Element argument_element = (Element) arguments.item(i);
554 if(argument_element.getAttribute(ARGUMENT_NAME).equalsIgnoreCase(property)) {
555 result = MSMUtils.getValue(argument_element);
556 // 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.
557 put(property + general, new SoftReference(argument_element));
558 }
559 }
560 }
561 }
562 }
563 catch (Exception error) {
564 Gatherer.printStackTrace(error);
565 }
566 // If we still have no result, and the search was made in the collection configuration, retrieve the general one instead.
567 if(result.length() == 0 && !general) {
568 result = getString(property, true);
569 }
570 return result;
571 }
572
573 /** Retrieve the path to the PERL scripts within the Greenstone directory.
574 * @return A <strong>String</strong> containing the path.
575 */
576 public String getScriptPath() {
577 return gsdl_path + "bin" + File.separator + "script" + File.separator;
578 }
579
580
581 public String getWGetPath() {
582 return "/usr/bin/wget";
583 }
584
585 /** Remove a previously defined special directory mapping.
586 * @param name The name of the mapping to remove as a <strong>String</strong>.
587 * @return The <strong>File</strong> of the mapping removed.
588 */
589 public File removeDirectoryMapping(String name) {
590 File file = null;
591 try {
592 // Ensure the name isn't already in use.
593 boolean found = false;
594 NodeList mappings = directory_mappings_element.getElementsByTagName(StaticStrings.MAPPING_ELEMENT);
595 for(int i = 0; !found && i < mappings.getLength(); i++) {
596 Element mapping_element = (Element) mappings.item(i);
597 if(mapping_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
598 file = new File(MSMUtils.getValue(mapping_element));
599 directory_mappings_element.removeChild(mapping_element);
600 found = true;
601 }
602 mapping_element = null;
603 }
604 mappings = null;
605 }
606 catch(Exception error) {
607 Gatherer.printStackTrace(error);
608 }
609 return file;
610 } /** removeDirectoryMapping(String name) */
611
612 /** Export the general configuration to file. */
613 public void save() {
614 ///ystem.err.println("Hits " + cache_hit + " vs Misses " + cache_miss);
615 // We first try exporting to a user specific place
616 File user_config_xml = null;
617 try {
618 if(Utility.isWindows()) {
619
620 }
621 // Unix systems save user preferences in . folders in the home directory.
622 else {
623 user_config_xml = new File(Utility.getGLIUserFolder(), CONFIG_XML);
624 ///ystem.err.println("Trying to save to: " + user_config_xml.getAbsolutePath());
625 ///ystem.err.println("Writing.");
626 user_config_xml.getParentFile().mkdirs();
627 Utility.export(general_config, user_config_xml.getAbsolutePath());
628 }
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);
637 }
638 }
639
640 /** Set the named property, from the specified configuration, using the given boolean value. */
641 public void set(String property, boolean general, boolean value) {
642 if(property.startsWith("workflow")) {
643 Gatherer.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 public void setArguments(Element arguments_element) {
650 try {
651 Element document_element = general_config.getDocumentElement();
652 Element other_element = (Element) MSMUtils.getNodeFromNamed(document_element, OTHER);
653 // Retrieve the name of the information
654 Element arguments_name_element = (Element)MSMUtils.getNodeFromNamed(arguments_element, NAME);
655 String filename = MSMUtils.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 Gatherer.println("Error in Configuration.setArguments(): " + error);
676 Gatherer.printStackTrace(error);
677 }
678 }
679
680 /** Set the collection configuration. */
681 public void setCollectionConfiguration(Document collection_config) {
682 // clear the cached values
683 clear();
684 this.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 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 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 /** Set the named property, from the specified configuration, using the given Dimension value. */
714 /* private void setDimension(String property, boolean general, Dimension value) {
715 StringBuffer text = new StringBuffer("");
716 text.append(value.width);
717 text.append(", ");
718 text.append(value.height);
719 setString(property, general, text.toString());
720 } */
721
722 /** Establish the current users email.
723 * @param email the email as a String
724 */
725 public void setEmail(String email) {
726 setString(GENERAL_EMAIL_SETTING, true, email);
727 }
728
729 /** Set the named property, from the specified configuration, using the given Font value. */
730 public void setFont(String property, boolean general, Font value) {
731 StringBuffer text = new StringBuffer("");
732 text.append(value.getName());
733 text.append(", ");
734 switch(value.getStyle()) {
735 case Font.BOLD:
736 text.append("BOLD");
737 break;
738 case Font.ITALIC:
739 text.append("ITALIC");
740 break;
741 default:
742 text.append("PLAIN");
743 }
744 text.append(", ");
745 text.append(value.getSize());
746 setString(property, general, text.toString());
747 }
748
749 /** Set the named property, from the specified configuration, using the given integer value. */
750 public void setInt(String property, boolean general, int value) {
751 setString(property, general, String.valueOf(value));
752 }
753
754 /** Set the named property, from the specified configuration, using the given Locale value. */
755 public void setLocale(String property, boolean general, Locale value) {
756 StringBuffer text = new StringBuffer("");
757 text.append(value.getLanguage());
758 String country = value.getCountry();
759 if(country != null && country.length() > 0) {
760 text.append("_");
761 text.append(country);
762 }
763 country = null;
764 setString(property, general, text.toString());
765 }
766
767 /** 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.
768 * @param value the new value for mode
769 */
770 public void setMode(int value) {
771 setInt("general.mode", GENERAL_SETTING, value);
772 }
773
774 public void setPreviewCommand(String value) {
775 setString("general.preview_program", GENERAL_SETTING, value);
776 }
777
778 /** Sets the value of the named property argument using the given string. */
779 public void setString(String property, boolean general, String value) {
780 Gatherer.println("Set configuration property: " + property + " = " + value + (general ? "" : " [Collection]"));
781 try {
782 Document document = general_config;
783 if(!general && collection_config != null) {
784 document = collection_config;
785 }
786 if(document != null) {
787 Element argument_element = null;
788 // Try to retrieve from cache
789 SoftReference reference = (SoftReference) get(property + general);
790 if(reference != null) {
791 argument_element = (Element) reference.get();
792 }
793 if(argument_element == null) {
794 Element document_element = document.getDocumentElement();
795 Element gatherer_element = (Element) MSMUtils.getNodeFromNamed(document_element, GATHERER_CONFIG);
796 NodeList arguments = document_element.getElementsByTagName(GATHERER_CONFIG_ARGUMENT);
797 boolean found = false;
798 for(int i = 0; argument_element == null && i < arguments.getLength(); i++) {
799 Element possible_element = (Element) arguments.item(i);
800 if(possible_element.getAttribute(ARGUMENT_NAME).equalsIgnoreCase(property)) {
801 argument_element = possible_element;
802 }
803 }
804 // If argument element is still null, create it in the target document.
805 if(argument_element == null) {
806 argument_element = document.createElement(GATHERER_CONFIG_ARGUMENT);
807 argument_element.setAttribute(ARGUMENT_NAME, property);
808 gatherer_element.appendChild(argument_element);
809 }
810 // Update cache
811 put(property + general, new SoftReference(argument_element));
812
813 }
814 if(value == null) {
815 value = "";
816 }
817 // Now remove any current text node children.
818 NodeList children = argument_element.getChildNodes();
819 for(int i = 0; i < children.getLength(); i++) {
820 argument_element.removeChild(children.item(i));
821 }
822 // Add a new text node child with the new value
823 argument_element.appendChild(document.createTextNode(value));
824 }
825 }
826 catch (Exception error) {
827 }
828 }
829
830 private void updateUI() {
831 // Buttons
832 UIManager.put("Button.select", new ColorUIResource(getColor("coloring.button_selected_background", false)));
833 UIManager.put("Button.background", new ColorUIResource(getColor("coloring.button_background", false)));
834 UIManager.put("Button.foreground", new ColorUIResource(getColor("coloring.button_foreground", false)));
835
836 UIManager.put("ToggleButton.background", new ColorUIResource(getColor("coloring.button_background", false)));
837 UIManager.put("ToggleButton.foreground", new ColorUIResource(getColor("coloring.button_foreground", false)));
838 UIManager.put("ToggleButton.select", new ColorUIResource(getColor("coloring.button_selected_background", false)));
839
840 // All the things with a lovely Collection green background
841 UIManager.put("OptionPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
842 UIManager.put("Panel.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
843 UIManager.put("Label.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
844 UIManager.put("TabbedPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
845 UIManager.put("SplitPane.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
846 UIManager.put("CheckBox.background", new ColorUIResource(getColor("coloring.collection_heading_background", false)));
847
848
849 // Editable coloring
850 UIManager.put("ComboBox.background", new ColorUIResource(getColor("coloring.collection_tree_background", false))); // Indicate clickable
851 UIManager.put("Tree.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
852 UIManager.put("Tree.textBackground", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
853 UIManager.put("ProgressBar.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
854 UIManager.put("TextArea.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
855 UIManager.put("TextField.background", new ColorUIResource(getColor("coloring.editable_background", false)));
856 UIManager.put("Table.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
857 UIManager.put("List.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
858 UIManager.put("RadioButton.background", new ColorUIResource(getColor("coloring.collection_tree_background", false)));
859
860 // Selection color
861 UIManager.put("TabbedPane.selected", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
862 UIManager.put("Tree.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
863 UIManager.put("ComboBox.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
864 UIManager.put("ProgressBar.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
865 UIManager.put("TextArea.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
866 UIManager.put("TextField.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
867 UIManager.put("List.selectionBackground", new ColorUIResource(getColor("coloring.collection_selection_background", false)));
868
869 // Scroll bar stuff
870 UIManager.put("ScrollBar.background", new ColorUIResource(getColor("coloring.scrollbar_background", false)));
871 UIManager.put("ScrollBar.thumb", new ColorUIResource(getColor("coloring.scrollbar_foreground", false)));
872 if (Gatherer.g_man != null) {
873 JPanel pane = (JPanel) Gatherer.g_man.getContentPane();
874 pane.updateUI();
875 // Also update all of the tabs according to workflow.
876 Gatherer.g_man.workflowUpdate("Hunt", get("workflow.browse", false));
877 Gatherer.g_man.workflowUpdate("Mirror", get("workflow.mirror", false));
878 Gatherer.g_man.workflowUpdate("Gather", get("workflow.gather", false));
879 Gatherer.g_man.workflowUpdate("Enrich", get("workflow.enrich", false));
880 Gatherer.g_man.workflowUpdate("Design", get("workflow.design", false));
881 Gatherer.g_man.workflowUpdate("Export", get("workflow.export", false));
882 Gatherer.g_man.workflowUpdate("Create", get("workflow.create", false));
883 Gatherer.g_man.workflowUpdate("Preview", get("workflow.preview", false));
884 }
885 }
886}
Note: See TracBrowser for help on using the repository browser.