JAL-1152 menu tweaks plus new option to put Autocalc at top or below.
[jalview.git] / src / jalview / bin / Cache.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
24 import jalview.ws.dbsources.das.datamodel.DasSourceRegistry;
25
26 import java.awt.Color;
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.Properties;
37
38 import org.apache.log4j.ConsoleAppender;
39 import org.apache.log4j.Level;
40 import org.apache.log4j.Logger;
41 import org.apache.log4j.SimpleLayout;
42
43 /**
44  * Stores and retrieves Jalview Application Properties Lists and fields within
45  * list entries are separated by '|' symbols unless otherwise stated (|) clauses
46  * are alternative values for a tag. <br>
47  * <br>
48  * Current properties include:
49  * <ul>
50  * <br>
51  * logs.Axis.Level - one of the stringified Levels for log4j controlling the
52  * logging level for axis (used for web services) <br>
53  * </li>
54  * <li>logs.Castor.Level - one of the stringified Levels for log4j controlling
55  * the logging level for castor (used for serialization) <br>
56  * </li>
57  * <li>logs.Jalview.Level - Cache.log stringified level. <br>
58  * </li>
59  * <li>SCREEN_WIDTH</li>
60  * <li>SCREEN_HEIGHT</li>
61  * <li>SCREEN_Y=285</li>
62  * <li>SCREEN_X=371</li>
63  * <li>SHOW_FULLSCREEN boolean</li>
64  * <li>FONT_NAME java font name for alignment text display</li>
65  * <li>FONT_SIZE size of displayed alignment text</li>
66  * <li>FONT_STYLE style of font displayed (sequence labels are always italic)</li>
67  * <li>GAP_SYMBOL character to treat as gap symbol (usually -,.,' ')</li>
68  * <li>LAST_DIRECTORY last directory for browsing alignment</li>
69  * <li>USER_DEFINED_COLOURS list of user defined colour scheme files</li>
70  * <li>SHOW_FULL_ID show id with '/start-end' numbers appended</li>
71  * <li>SHOW_IDENTITY show percentage identity annotation</li>
72  * <li>SHOW_QUALITY show alignment quality annotation</li>
73  * <li>SHOW_ANNOTATIONS show alignment annotation rows</li>
74  * <li>SHOW_CONSERVATION show alignment conservation annotation</li>
75  * <li>SORT_ANNOTATIONS currently either SEQUENCE_AND_LABEL or
76  * LABEL_AND_SEQUENCE</li>
77  * <li>SHOW_AUTOCALC_ABOVE true to show autocalculated annotations above
78  * sequence annotations</li>
79  * <li>CENTRE_COLUMN_LABELS centre the labels at each column in a displayed
80  * annotation row</li>
81  * <li>DEFAULT_COLOUR default colour scheme to apply for a new alignment</li>
82  * <li>DEFAULT_FILE_FORMAT file format used to save</li>
83  * <li>STARTUP_FILE file loaded on startup (may be a fully qualified url)</li>
84  * <li>SHOW_STARTUP_FILE flag to control loading of startup file</li>
85  * <li>VERSION the version of the jalview build</li>
86  * <li>BUILD_DATE date of this build</li>
87  * <li>LATEST_VERSION the latest jalview version advertised on the
88  * www.jalview.org</li>
89  * <li>PIR_MODELLER boolean indicating if PIR files are written with MODELLER
90  * descriptions</li>
91  * <li>(FASTA,MSF,PILEUP,CLUSTAL,BLC,PIR,PFAM)_JVSUFFIX boolean for adding jv
92  * suffix to file</li>
93  * <li>RECENT_URL list of recently retrieved URLs</li>
94  * <li>RECENT_FILE list of recently opened files</li>
95  * <li>USE_PROXY flag for whether a http proxy is to be used</li>
96  * <li>PROXY_SERVER the proxy</li>
97  * <li>PROXY_PORT</li>
98  * <li>NOQUESTIONNAIRES true to prevent jalview from checking the questionnaire
99  * service</li>
100  * <li>QUESTIONNAIRE last questionnaire:responder id string from questionnaire
101  * service</li>
102  * <li>USAGESTATS (false - user prompted) Enable google analytics tracker for
103  * collecting usage statistics</li>
104  * <li>DAS_LOCAL_SOURCE list of local das sources</li>
105  * <li>SHOW_OVERVIEW boolean for overview window display</li>
106  * <li>ANTI_ALIAS boolean for smooth fonts</li>
107  * <li>RIGHT_ALIGN_IDS boolean</li>
108  * <li>AUTO_CALC_CONSENSUS boolean for automatic recalculation of consensus</li>
109  * <li>PAD_GAPS boolean</li>
110  * <li>ID_ITALICS boolean</li>
111  * <li>SHOW_JV_SUFFIX</li>
112  * <li>WRAP_ALIGNMENT</li>
113  * <li>EPS_RENDERING (Prompt each time|Lineart|Text) default for EPS rendering
114  * style check</li>
115  * <li>SORT_ALIGNMENT (No sort|Id|Pairwise Identity)</li>
116  * <li>SEQUENCE_LINKS list of name|URL pairs for opening a url with
117  * $SEQUENCE_ID$</li>
118  * <li>GROUP_LINKS list of name|URL[|&lt;separator&gt;] tuples - see
119  * jalview.utils.GroupURLLink for more info</li>
120  * <li>DAS_REGISTRY_URL the registry to query</li>
121  * <li>DEFAULT_BROWSER for unix</li>
122  * <li>DAS_ACTIVE_SOURCE list of active sources</li>
123  * <li>SHOW_MEMUSAGE boolean show memory usage and warning indicator on desktop
124  * (false)</li>
125  * <li>VERSION_CHECK (true) check for the latest release version from
126  * www.jalview.org (or the alias given by the www.jalview.org property)</li>
127  * <li>SHOW_NPFEATS_TOOLTIP (true) show non-positional features in the Sequence
128  * ID tooltip</li>
129  * <li>SHOW_DBREFS_TOOLTIP (true) show Database Cross References in the Sequence
130  * ID tooltip</li>
131  * <li>SHOW_UNCONSERVED (false) only render unconserved residues - conserved
132  * displayed as '.'</li>
133  * <li>SORT_BY_TREE (false) sort the current alignment view according to the
134  * order of a newly displayed tree</li>
135  * <li>DBFETCH_USEPICR (false) use PICR to recover valid DB references from
136  * sequence ID strings before attempting retrieval from any datasource</li>
137  * <li>SHOW_GROUP_CONSENSUS (false) Show consensus annotation for groups in the
138  * alignment.</li>
139  * <li>SHOW_GROUP_CONSERVATION (false) Show conservation annotation for groups
140  * in the alignment.</li>
141  * <li>SHOW_CONSENSUS_HISTOGRAM (false) Show consensus annotation row's
142  * histogram.</li>
143  * <li>SHOW_CONSENSUS_LOGO (false) Show consensus annotation row's sequence
144  * logo.</li>
145  * <li>NORMALISE_CONSENSUS_LOGO (false) Show consensus annotation row's sequence
146  * logo normalised to row height rather than histogram height.</li>
147  * <li>FOLLOW_SELECTIONS (true) Controls whether a new alignment view should
148  * respond to selections made in other alignments containing the same sequences.
149  * </li>
150  * <li>SHOW_WSDISCOVERY_ERRORS (true) Controls if the web service URL discovery
151  * warning dialog box is displayed.</li>
152  * <li>ANNOTATIONCOLOUR_MIN (orange) Shade used for minimum value of annotation
153  * when shading by annotation</li>
154  * <li>ANNOTATIONCOLOUR_MAX (red) Shade used for maximum value of annotation
155  * when shading by annotation</li>
156  * <li>www.jalview.org (http://www.jalview.org) a property enabling all HTTP
157  * requests to be redirected to a mirror of http://www.jalview.org</li>
158  * 
159  * <li>FIGURE_AUTOIDWIDTH (false) Expand the left hand column of an exported
160  * alignment figure to accommodate even the longest sequence ID or annotation
161  * label.</li>
162  * <li>FIGURE_FIXEDIDWIDTH Specifies the width to use for the left-hand column
163  * when exporting an alignment as a figure (setting FIGURE_AUTOIDWIDTH to true
164  * will override this).</li>
165  * <li></li>
166  * 
167  * </ul>
168  * Deprecated settings:
169  * <ul>
170  * *
171  * <li>DISCOVERY_START - Boolean - controls if discovery services are queried on
172  * startup (JWS1 services only)</li>
173  * <li>DISCOVERY_URLS - comma separated list of Discovery Service endpoints.
174  * (JWS1 services only)</li>
175  * <li>SHOW_JWS1_SERVICES (true) enable or disable the original Jalview 2
176  * services in the desktop GUI</li>
177  * <li>ENABLE_RSBS_EDITOR (false for 2.7 release) enable or disable RSBS editing
178  * panel in web service preferences</li>
179  * </ul>
180  * 
181  * @author $author$
182  * @version $Revision$
183  */
184 public class Cache
185 {
186   /**
187    * property giving log4j level for CASTOR loggers
188    */
189   public static final String CASTORLOGLEVEL = "logs.Castor.level";
190
191   /**
192    * property giving log4j level for AXIS loggers
193    */
194   public static final String AXISLOGLEVEL = "logs.Axis.level";
195
196   /**
197    * property giving log4j level for Jalview Log
198    */
199   public static final String JALVIEWLOGLEVEL = "logs.Jalview.level";
200
201   public static final String DAS_LOCAL_SOURCE = "DAS_LOCAL_SOURCE";
202
203   public static final String DAS_REGISTRY_URL = "DAS_REGISTRY_URL";
204
205   public static final String DAS_ACTIVE_SOURCE = "DAS_ACTIVE_SOURCE";
206
207   /**
208    * Initialises the Jalview Application Log
209    */
210   public static Logger log;
211
212   /** Jalview Properties */
213   public static Properties applicationProperties = new Properties();
214
215   /** Default file is ~/.jalview_properties */
216   static String propertiesFile;
217
218   private static boolean propsAreReadOnly = false;
219
220   public static void initLogger()
221   {
222     if (log != null)
223     {
224       return;
225     }
226     try
227     {
228       // TODO: redirect stdout and stderr here in order to grab the output of
229       // the log
230
231       ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
232               "System.err");
233       ap.setName("JalviewLogger");
234       org.apache.log4j.Logger.getRootLogger().addAppender(ap); // catch all for
235       // log output
236       Logger laxis = Logger.getLogger("org.apache.axis");
237       Logger lcastor = Logger.getLogger("org.exolab.castor");
238       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
239
240       laxis.setLevel(Level.toLevel(Cache.getDefault("logs.Axis.Level",
241               Level.INFO.toString())));
242       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
243               Level.INFO.toString())));
244       lcastor = Logger.getLogger("org.exolab.castor.xml");
245       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
246               Level.INFO.toString())));
247       // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller");
248       // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
249       // Level.INFO.toString())));
250       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache.getDefault(
251               "logs.Jalview.level", Level.INFO.toString())));
252       // laxis.addAppender(ap);
253       // lcastor.addAppender(ap);
254       // jalview.bin.Cache.log.addAppender(ap);
255       // Tell the user that debug is enabled
256       jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
257     } catch (Exception ex)
258     {
259       System.err.println("Problems initializing the log4j system\n");
260       ex.printStackTrace(System.err);
261     }
262   }
263
264   /** Called when Jalview is started */
265   public static void loadProperties(String propsFile)
266   {
267     propertiesFile = propsFile;
268     if (propsFile == null)
269     {
270       propertiesFile = System.getProperty("user.home") + File.separatorChar
271               + ".jalview_properties";
272     }
273     else
274     {
275       // don't corrupt the file we've been given.
276       propsAreReadOnly = true;
277     }
278
279     try
280     {
281       InputStream fis;
282       try
283       {
284         fis = new java.net.URL(propertiesFile).openStream();
285         System.out.println("Loading jalview properties from : "
286                 + propertiesFile);
287         System.out
288                 .println("Disabling Jalview writing to user's local properties file.");
289         propsAreReadOnly = true;
290
291       } catch (Exception ex)
292       {
293         fis = null;
294       }
295       if (fis == null)
296       {
297         fis = new FileInputStream(propertiesFile);
298       }
299       applicationProperties.load(fis);
300       
301       // remove any old build properties
302       
303       deleteBuildProperties();
304       fis.close();
305     } catch (Exception ex)
306     {
307       System.out.println("Error reading properties file: " + ex);
308     }
309
310     if (getDefault("USE_PROXY", false))
311     {
312       String proxyServer = getDefault("PROXY_SERVER", ""), proxyPort = getDefault(
313               "PROXY_PORT", "8080");
314
315       System.out.println("Using proxyServer: " + proxyServer
316               + " proxyPort: " + proxyPort);
317
318       System.setProperty("http.proxyHost", proxyServer);
319       System.setProperty("http.proxyPort", proxyPort);
320     }
321
322     // LOAD THE AUTHORS FROM THE authors.props file
323     try
324     {
325       String authorDetails = "jar:".concat(Cache.class
326               .getProtectionDomain().getCodeSource().getLocation()
327               .toString().concat("!/authors.props"));
328
329       java.net.URL localJarFileURL = new java.net.URL(authorDetails);
330
331       InputStream in = localJarFileURL.openStream();
332       applicationProperties.load(in);
333       in.close();
334     } catch (Exception ex)
335     {
336       System.out.println("Error reading author details: " + ex);
337       applicationProperties.remove("AUTHORS");
338       applicationProperties.remove("AUTHORFNAMES");
339       applicationProperties.remove("YEAR");
340     }
341
342     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
343     // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE
344     // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW
345     try
346     {
347       String buildDetails = "jar:".concat(Cache.class.getProtectionDomain()
348               .getCodeSource().getLocation().toString()
349               .concat("!/.build_properties"));
350
351       java.net.URL localJarFileURL = new java.net.URL(buildDetails);
352
353       InputStream in = localJarFileURL.openStream();
354       applicationProperties.load(in);
355       in.close();
356     } catch (Exception ex)
357     {
358       System.out.println("Error reading build details: " + ex);
359       applicationProperties.remove("VERSION");
360     }
361
362     String jnlpVersion = System.getProperty("jalview.version");
363     String codeVersion = getProperty("VERSION");
364
365     if (codeVersion == null)
366     {
367       // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!
368       codeVersion = "Test";
369       jnlpVersion = "Test";
370     }
371
372     System.out.println("Jalview Version: " + codeVersion);
373
374     // jnlpVersion will be null if we're using InstallAnywhere
375     // Dont do this check if running in headless mode
376     if (jnlpVersion == null
377             && getDefault("VERSION_CHECK", true)
378             && (System.getProperty("java.awt.headless") == null || System
379                     .getProperty("java.awt.headless").equals("false")))
380     {
381
382       class VersionChecker extends Thread
383       {
384         public void run()
385         {
386           String orgtimeout = System
387                   .getProperty("sun.net.client.defaultConnectTimeout");
388           if (orgtimeout == null)
389           {
390             orgtimeout = "30";
391             System.out.println("# INFO: Setting default net timeout to "
392                     + orgtimeout + " seconds.");
393           }
394           String jnlpVersion = null;
395           try
396           {
397             System.setProperty("sun.net.client.defaultConnectTimeout",
398                     "5000");
399             java.net.URL url = new java.net.URL(Cache.getDefault(
400                     "www.jalview.org", "http://www.jalview.org")
401                     + "/webstart/jalview.jnlp");
402             BufferedReader in = new BufferedReader(new InputStreamReader(
403                     url.openStream()));
404             String line = null;
405             while ((line = in.readLine()) != null)
406             {
407               if (line.indexOf("jalview.version") == -1)
408               {
409                 continue;
410               }
411
412               line = line.substring(line.indexOf("value=") + 7);
413               line = line.substring(0, line.lastIndexOf("\""));
414               jnlpVersion = line;
415               break;
416             }
417           } catch (Exception ex)
418           {
419             System.out
420                     .println("Non-fatal exceptions when checking version at www.jalview.org :");
421             System.out.println(ex);
422             jnlpVersion = getProperty("VERSION");
423           }
424           System.setProperty("sun.net.client.defaultConnectTimeout",
425                   orgtimeout);
426
427           setProperty("LATEST_VERSION", jnlpVersion);
428         }
429       }
430
431       VersionChecker vc = new VersionChecker();
432       vc.start();
433     }
434     else
435     {
436       if (jnlpVersion != null)
437       {
438         setProperty("LATEST_VERSION", jnlpVersion);
439       }
440       else
441       {
442         applicationProperties.remove("LATEST_VERSION");
443       }
444     }
445
446     setProperty("VERSION", codeVersion);
447
448     // LOAD USERDEFINED COLOURS
449     jalview.gui.UserDefinedColours
450             .initUserColourSchemes(getProperty("USER_DEFINED_COLOURS"));
451     jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER",
452             false);
453   }
454
455   private static void deleteBuildProperties()
456   {
457     applicationProperties.remove("LATEST_VERSION");
458     applicationProperties.remove("VERSION");
459     applicationProperties.remove("AUTHORS");
460     applicationProperties.remove("AUTHORFNAMES");
461     applicationProperties.remove("YEAR");
462   }
463
464   /**
465    * Gets Jalview application property of given key. Returns null if key not
466    * found
467    * 
468    * @param key
469    *          Name of property
470    * 
471    * @return Property value
472    */
473   public static String getProperty(String key)
474   {
475     return applicationProperties.getProperty(key);
476   }
477
478   /**
479    * These methods are used when checking if the saved preference is different
480    * to the default setting
481    */
482
483   public static boolean getDefault(String property, boolean def)
484   {
485     String string = getProperty(property);
486     if (string != null)
487     {
488       def = Boolean.valueOf(string).booleanValue();
489     }
490
491     return def;
492   }
493
494   /**
495    * These methods are used when checking if the saved preference is different
496    * to the default setting
497    */
498   public static String getDefault(String property, String def)
499   {
500     String string = getProperty(property);
501     if (string != null)
502     {
503       return string;
504     }
505
506     return def;
507   }
508
509   /**
510    * Stores property in the file "HOME_DIR/.jalview_properties"
511    * 
512    * @param key
513    *          Name of object
514    * @param obj
515    *          String value of property
516    * 
517    * @return String value of property
518    */
519   public static String setProperty(String key, String obj)
520   {
521
522     try
523     {
524       applicationProperties.setProperty(key, obj);
525       if (!propsAreReadOnly)
526       {
527         FileOutputStream out = new FileOutputStream(propertiesFile);
528         applicationProperties.store(out, "---JalviewX Properties File---");
529         out.close();
530       }
531     } catch (Exception ex)
532     {
533       System.out.println("Error setting property: " + key + " " + obj
534               + "\n" + ex);
535     }
536     return obj;
537   }
538
539   /**
540    * remove the specified property from the jalview properties file
541    * 
542    * @param string
543    */
544   public static void removeProperty(String string)
545   {
546     applicationProperties.remove(string);
547     saveProperties();
548   }
549
550   /**
551    * save the properties to the jalview properties path
552    */
553   public static void saveProperties()
554   {
555     if (!propsAreReadOnly)
556     {
557       try
558       {
559         FileOutputStream out = new FileOutputStream(propertiesFile);
560         applicationProperties.store(out, "---JalviewX Properties File---");
561         out.close();
562       } catch (Exception ex)
563       {
564         System.out.println("Error saving properties: " + ex);
565       }
566     }
567   }
568
569   /**
570    * internal vamsas class discovery state
571    */
572   private static int vamsasJarsArePresent = -1;
573
574   /**
575    * Searches for vamsas client classes on class path.
576    * 
577    * @return true if vamsas client is present on classpath
578    */
579   public static boolean vamsasJarsPresent()
580   {
581     if (vamsasJarsArePresent == -1)
582     {
583       try
584       {
585         if (jalview.jbgui.GDesktop.class.getClassLoader().loadClass(
586                 "uk.ac.vamsas.client.VorbaId") != null)
587         {
588           jalview.bin.Cache.log
589                   .debug("Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
590           vamsasJarsArePresent = 1;
591           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
592           lvclient.setLevel(Level.toLevel(Cache.getDefault(
593                   "logs.Vamsas.Level", Level.INFO.toString())));
594
595           lvclient.addAppender(log.getAppender("JalviewLogger"));
596           // Tell the user that debug is enabled
597           lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
598         }
599       } catch (Exception e)
600       {
601         vamsasJarsArePresent = 0;
602         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
603       }
604     }
605     return (vamsasJarsArePresent > 0);
606   }
607
608   /**
609    * internal vamsas class discovery state
610    */
611   private static int groovyJarsArePresent = -1;
612
613   /**
614    * Searches for vamsas client classes on class path.
615    * 
616    * @return true if vamsas client is present on classpath
617    */
618   public static boolean groovyJarsPresent()
619   {
620     if (groovyJarsArePresent == -1)
621     {
622       try
623       {
624         if (Cache.class.getClassLoader().loadClass(
625                 "groovy.lang.GroovyObject") != null)
626         {
627           jalview.bin.Cache.log
628                   .debug("Found Groovy (groovy.lang.GroovyObject can be loaded)");
629           groovyJarsArePresent = 1;
630           Logger lgclient = Logger.getLogger("groovy");
631           lgclient.setLevel(Level.toLevel(Cache.getDefault(
632                   "logs.Groovy.Level", Level.INFO.toString())));
633
634           lgclient.addAppender(log.getAppender("JalviewLogger"));
635           // Tell the user that debug is enabled
636           lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
637         }
638       } catch (Error e)
639       {
640         groovyJarsArePresent = 0;
641         jalview.bin.Cache.log.debug("Groovy Classes are not present", e);
642       } catch (Exception e)
643       {
644         groovyJarsArePresent = 0;
645         jalview.bin.Cache.log.debug("Groovy Classes are not present");
646       }
647     }
648     return (groovyJarsArePresent > 0);
649   }
650
651   /**
652    * GA tracker object - actually JGoogleAnalyticsTracker null if tracking not
653    * enabled.
654    */
655   protected static Object tracker = null;
656
657   protected static Class trackerfocus = null;
658
659   protected static Class jgoogleanalyticstracker = null;
660
661   /**
662    * Initialise the google tracker if it is not done already.
663    */
664   public static void initGoogleTracker()
665   {
666     if (tracker == null)
667     {
668       if (jgoogleanalyticstracker == null)
669       {
670         // try to get the tracker class
671         try
672         {
673           jgoogleanalyticstracker = Cache.class
674                   .getClassLoader()
675                   .loadClass(
676                           "com.boxysystems.jgoogleanalytics.JGoogleAnalyticsTracker");
677           trackerfocus = Cache.class.getClassLoader().loadClass(
678                   "com.boxysystems.jgoogleanalytics.FocusPoint");
679         } catch (Exception e)
680         {
681           log.debug("com.boxysystems.jgoogleanalytics package is not present - tracking not enabled.");
682           tracker = null;
683           jgoogleanalyticstracker = null;
684           trackerfocus = null;
685           return;
686         }
687       }
688       // now initialise tracker
689       Exception re = null, ex = null;
690       Error err = null;
691       String vrs = "No Version Accessible";
692       try
693       {
694         // Google analytics tracking code for Library Finder
695         tracker = jgoogleanalyticstracker.getConstructor(new Class[]
696         { String.class, String.class, String.class }).newInstance(
697                 new Object[]
698                 {
699                     "Jalview Desktop",
700                     (vrs = jalview.bin.Cache.getProperty("VERSION")
701                             + "_"
702                             + jalview.bin.Cache.getDefault("BUILD_DATE",
703                                     "unknown")), "UA-9060947-1" });
704         jgoogleanalyticstracker.getMethod("trackAsynchronously",
705                 new Class[]
706                 { trackerfocus }).invoke(tracker, new Object[]
707         { trackerfocus.getConstructor(new Class[]
708         { String.class }).newInstance(new Object[]
709         { "Application Started." }) });
710       } catch (RuntimeException e)
711       {
712         re = e;
713       } catch (Exception e)
714       {
715         ex = e;
716       } catch (Error e)
717       {
718         err = e;
719       }
720       if (re != null || ex != null || err != null)
721       {
722         if (log != null)
723         {
724           if (re != null)
725           {
726             log.debug("Caught runtime exception in googletracker init:", re);
727           }
728           if (ex != null)
729           {
730             log.warn(
731                     "Failed to initialise GoogleTracker for Jalview Desktop with version "
732                             + vrs, ex);
733           }
734           if (err != null)
735           {
736             log.error(
737                     "Whilst initing GoogleTracker for Jalview Desktop version "
738                             + vrs, err);
739           }
740         }
741         else
742         {
743           if (re != null)
744           {
745             System.err
746                     .println("Debug: Caught runtime exception in googletracker init:"
747                             + vrs);
748             re.printStackTrace();
749           }
750           if (ex != null)
751           {
752             System.err
753                     .println("Warning:  Failed to initialise GoogleTracker for Jalview Desktop with version "
754                             + vrs);
755             ex.printStackTrace();
756           }
757
758           if (err != null)
759           {
760             System.err
761                     .println("ERROR: Whilst initing GoogleTracker for Jalview Desktop version "
762                             + vrs);
763             err.printStackTrace();
764           }
765         }
766       }
767       else
768       {
769         log.debug("Successfully initialised tracker.");
770       }
771     }
772   }
773
774   /**
775    * get the user's default colour if available
776    * 
777    * @param property
778    * @param defcolour
779    * @return
780    */
781   public static Color getDefaultColour(String property, Color defcolour)
782   {
783     String colprop = getProperty(property);
784     if (colprop == null)
785     {
786       return defcolour;
787     }
788     Color col = jalview.schemes.ColourSchemeProperty
789             .getAWTColorFromName(colprop);
790     if (col == null)
791     {
792       try
793       {
794         col = new jalview.schemes.UserColourScheme(colprop).findColour('A');
795       } catch (Exception ex)
796       {
797         log.warn("Couldn't parse '" + colprop + "' as a colour for "
798                 + property);
799         col = null;
800       }
801     }
802     return (col == null) ? defcolour : col;
803   }
804
805   /**
806    * store a colour as a Jalview user default property
807    * 
808    * @param property
809    * @param colour
810    */
811   public static void setColourProperty(String property, Color colour)
812   {
813     setProperty(property, jalview.util.Format.getHexString(colour));
814   }
815
816   public static final DateFormat date_format = SimpleDateFormat
817           .getDateTimeInstance();
818
819   /**
820    * store a date in a jalview property
821    * 
822    * @param string
823    * @param time
824    */
825   public static void setDateProperty(String property, Date time)
826   {
827     setProperty(property, date_format.format(time));
828   }
829
830   /**
831    * read a date stored in a jalview property
832    * 
833    * @param property
834    * @return valid date as stored by setDateProperty, or null
835    * 
836    */
837   public static Date getDateProperty(String property)
838   {
839     String val = getProperty(property);
840     if (val != null)
841     {
842       try
843       {
844         return date_format.parse(val);
845       } catch (Exception ex)
846       {
847         System.err.println("Invalid or corrupt date in property '"
848                 + property + "' : value was '" + val + "'");
849       }
850     }
851     return null;
852   }
853
854   /**
855    * get and parse a property as an integer. send any parsing problems to
856    * System.err
857    * 
858    * @param property
859    * @return null or Integer
860    */
861   public static Integer getIntegerProperty(String property)
862   {
863     String val = getProperty(property);
864     if (val != null && (val = val.trim()).length() > 0)
865     {
866       try
867       {
868         return Integer.valueOf(val);
869       } catch (NumberFormatException x)
870       {
871         System.err.println("Invalid integer in property '" + property
872                 + "' (value was '" + val + "')");
873       }
874     }
875     return null;
876   }
877
878   private static DasSourceRegistryI sourceRegistry = null;
879
880   /**
881    * initialise and ..
882    * 
883    * @return instance of the das source registry
884    */
885   public static DasSourceRegistryI getDasSourceRegistry()
886   {
887     if (sourceRegistry == null)
888     {
889       sourceRegistry = new DasSourceRegistry();
890     }
891     return sourceRegistry;
892   }
893 }