Platform fix, missing check for jsutil == null
[jalview.git] / src / jalview / util / Platform.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.util;
22
23 import jalview.javascript.json.JSON;
24
25 import java.awt.Component;
26 import java.awt.Dimension;
27 import java.awt.Toolkit;
28 import java.awt.event.MouseEvent;
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.Reader;
37 import java.net.URL;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Properties;
41 import java.util.Set;
42 import java.util.logging.ConsoleHandler;
43 import java.util.logging.Level;
44 import java.util.logging.Logger;
45
46 import javax.swing.SwingUtilities;
47
48 import org.json.simple.parser.JSONParser;
49 import org.json.simple.parser.ParseException;
50
51 import com.stevesoft.pat.Regex;
52
53 import swingjs.api.JSUtilI;
54
55 /**
56  * System platform information used by Applet and Application
57  * 
58  * @author Jim Procter
59  */
60 public class Platform
61 {
62
63   private static boolean isJS = /** @j2sNative true || */
64           false;
65
66   private static JSUtilI jsutil = /**
67                                    * @j2sNative new Clazz.new_("swingjs.JSUtil")
68                                    *            ||
69                                    */
70           null;
71
72
73   private static Boolean isNoJSMac = null, isNoJSWin = null, isMac = null,
74           isWin = null;
75
76   // private static Boolean isHeadless = null;
77
78   /**
79    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
80    * 
81    * @return
82    */
83   public static boolean isMac()
84   {
85     return (isMac == null
86             ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0))
87             : isMac);
88   }
89
90   /**
91    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
92    * 
93    * @return
94    */
95   public static boolean isWin()
96   {
97     return (isWin == null
98             ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0))
99             : isWin);
100   }
101
102   /**
103    * 
104    * @return true if HTML5 JavaScript
105    */
106   public static boolean isJS()
107   {
108     return isJS;
109   }
110
111   /**
112    * sorry folks - Macs really are different
113    * 
114    * BH: disabled for SwingJS -- will need to check key-press issues
115    * 
116    * @return true if we do things in a special way.
117    */
118   public static boolean isAMacAndNotJS()
119   {
120     return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
121   }
122
123   /**
124    * Check if we are on a Microsoft plaform...
125    * 
126    * @return true if we have to cope with another platform variation
127    */
128   public static boolean isWindowsAndNotJS()
129   {
130     return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
131   }
132
133   // /**
134   // *
135   // * @return true if we are running in non-interactive no UI mode
136   // */
137   // public static boolean isHeadless()
138   // {
139   // if (isHeadless == null)
140   // {
141   // isHeadless = "true".equals(System.getProperty("java.awt.headless"));
142   // }
143   // return isHeadless;
144   // }
145
146   /**
147    * 
148    * @return nominal maximum command line length for this platform
149    */
150   public static int getMaxCommandLineLength()
151   {
152     // TODO: determine nominal limits for most platforms.
153     return 2046; // this is the max length for a windows NT system.
154   }
155
156   /**
157    * escape a string according to the local platform's escape character
158    * 
159    * @param file
160    * @return escaped file
161    */
162   public static String escapeString(String file)
163   {
164     StringBuffer f = new StringBuffer();
165     int p = 0, lastp = 0;
166     while ((p = file.indexOf('\\', lastp)) > -1)
167     {
168       f.append(file.subSequence(lastp, p));
169       f.append("\\\\");
170       lastp = p + 1;
171     }
172     f.append(file.substring(lastp));
173     return f.toString();
174   }
175
176   /**
177    * Answers true if the mouse event has Meta-down (Command key on Mac) or
178    * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
179    * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on
180    * Mac, you can use e.isPopupTrigger().
181    * 
182    * @param e
183    * @return
184    */
185   public static boolean isControlDown(MouseEvent e)
186   {
187     return isControlDown(e, isMac());
188   }
189
190   /**
191    * Overloaded version of method (to allow unit testing)
192    * 
193    * @param e
194    * @param aMac
195    * @return
196    */
197   protected static boolean isControlDown(MouseEvent e, boolean aMac)
198   {
199     if (!aMac)
200     {
201       return e.isControlDown();
202     }
203     // answer false for right mouse button
204     // shortcut key will be META for a Mac
205     return !e.isPopupTrigger()
206             && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
207                     & e.getModifiers()) != 0;
208     // could we use e.isMetaDown() here?
209   }
210
211   // BH: I don't know about that previous method. Here is what SwingJS uses.
212   // Notice the distinction in mouse events. (BUTTON3_MASK == META)
213   //
214   // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
215   // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
216   // if (isWin) {
217   // if (id != MouseEvent.MOUSE_RELEASED)
218   // return false;
219   ////
220   //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
221   //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
222   ////
223   //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
224   // because
225   //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
226   ////
227   // return rt;
228   // } else {
229   // // mac, linux, unix
230   // if (id != MouseEvent.MOUSE_PRESSED)
231   // return false;
232   // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
233   // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
234   // return rt || (ctrl && lt);
235   // }
236   // }
237   //
238
239   /**
240    * Windows (not Mac, Linux, or Unix) and right button to test for the
241    * right-mouse pressed event in Windows that would have opened a menu or a
242    * Mac.
243    * 
244    * @param e
245    * @return
246    */
247   public static boolean isWinRightButton(MouseEvent e)
248   {
249     // was !isAMac(), but that is true also for Linux and Unix and JS,
250
251     return isWin() && SwingUtilities.isRightMouseButton(e);
252   }
253
254   /**
255    * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
256    * without pressing the button.
257    * 
258    * @param e
259    * @return
260    */
261   public static boolean isWinMiddleButton(MouseEvent e)
262   {
263     // was !isAMac(), but that is true also for Linux and Unix and JS
264     return isWin() && SwingUtilities.isMiddleMouseButton(e);
265   }
266
267   public static boolean allowMnemonics()
268   {
269     return !isMac();
270   }
271
272   public final static int TIME_RESET = 0;
273
274   public final static int TIME_MARK = 1;
275
276   public static final int TIME_SET = 2;
277
278   public static final int TIME_GET = 3;
279
280   public static long time, mark, set, duration;
281
282   /**
283    * typical usage:
284    * 
285    * Platform.timeCheck(null, Platform.TIME_MARK);
286    * 
287    * ...
288    * 
289    * Platform.timeCheck("some message", Platform.TIME_MARK);
290    * 
291    * reset...[set/mark]n...get
292    * 
293    * @param msg
294    * @param mode
295    */
296   public static void timeCheck(String msg, int mode)
297   {
298     long t = System.currentTimeMillis();
299     switch (mode)
300     {
301     case TIME_RESET:
302       time = mark = t;
303       duration = 0;
304       if (msg != null)
305       {
306         System.err.println("Platform: timer reset\t\t\t" + msg);
307       }
308       break;
309     case TIME_MARK:
310       if (set > 0)
311       {
312         // total time between set/mark points
313         duration += (t - set);
314       }
315       else
316       {
317         if (time == 0)
318         {
319           time = mark = t;
320         }
321         if (msg != null)
322         {
323           System.err.println("Platform: timer mark\t" + ((t - time) / 1000f)
324                   + "\t" + ((t - mark) / 1000f) + "\t" + msg);
325         }
326         mark = t;
327       }
328       break;
329     case TIME_SET:
330       set = t;
331       break;
332     case TIME_GET:
333       if (msg != null)
334       {
335         System.err.println("Platform: timer get\t" + ((t - time) / 1000f)
336                 + "\t" + ((duration) / 1000f) + "\t" + msg);
337       }
338       set = 0;
339       break;
340     }
341   }
342
343   /**
344    * Encode the URI using JavaScript encodeURIComponent
345    * 
346    * @param value
347    * @return encoded value
348    */
349   public static String encodeURI(String value)
350   {
351     /**
352      * @j2sNative value = encodeURIComponent(value);
353      */
354     return value;
355   }
356
357   /**
358    * Open the URL using a simple window call if this is JavaScript
359    * 
360    * @param url
361    * @return true if window has been opened
362    */
363   public static boolean openURL(String url) throws IOException
364   {
365     if (!isJS())
366     {
367       BrowserLauncher.openURL(url);
368       return false;
369     }
370     /**
371      * @j2sNative
372      * 
373      * 
374      *            window.open(url);
375      */
376     return true;
377   }
378
379   public static void stackTrace()
380   {
381     new NullPointerException("testing only").printStackTrace();
382   }
383
384   public static void cacheFileData(String path, Object data)
385   {
386     if (isJS())
387     {
388       jsutil.cachePathData(path, data);
389     }
390   }
391
392   public static void cacheFileData(File file)
393   {
394     if (isJS())
395     {
396       byte[] bytes = getFileBytes(file);
397       if (bytes != null)
398       {
399         cacheFileData(file.toString(), bytes);
400       }
401     }
402   }
403
404   public static byte[] getFileBytes(File f)
405   {
406     return (isJS() && f != null ? jsutil.getBytes(f) : null);
407   }
408
409   public static byte[] getFileAsBytes(String fileStr)
410   {
411     byte[] bytes = (isJS() && fileStr != null
412             ? (byte[]) jsutil.getFile(fileStr, false)
413             : null);
414     if (bytes != null)
415     {
416       cacheFileData(fileStr, bytes);
417     }
418     return bytes;
419   }
420
421   public static String getFileAsString(String url)
422   {
423     String ret = null;
424     if (isJS())
425     {
426       ret = (String) jsutil.getFile(url, true);
427       if (ret != null)
428       {
429         cacheFileData(url, ret);
430       }
431     }
432     return ret;
433   }
434
435   public static boolean setFileBytes(File f, String urlstring)
436   {
437     if (!isJS())
438     {
439       return false;
440     }
441     byte[] bytes = getFileAsBytes(urlstring);
442     boolean ok = false;
443     try
444     {
445       jsutil.setFileBytes(f, bytes);
446     } catch (Throwable t)
447     {
448       System.out.println("Platform.setFileBytes failed: " + t);
449     }
450     return ok;
451   }
452
453   public static void addJ2SBinaryType(String ext)
454   {
455
456     jsutil.addBinaryFileType(ext);
457   }
458
459   /**
460    * Read the Info block for this applet.
461    * 
462    * @param prefix
463    *          "jalview_"
464    * @param p
465    * @return unique id for this applet
466    */
467   public static void readInfoProperties(String prefix, Properties p)
468   {
469     if (isJS())
470     {
471       jsutil.readInfoProperties(prefix, p);
472     }
473   }
474
475   public static void setAjaxJSON(URL url)
476   {
477     if (isJS())
478     {
479       JSON.setAjax(url);
480     }
481   }
482
483   public static Object parseJSON(InputStream response)
484           throws IOException, ParseException
485   {
486     if (isJS())
487     {
488       return JSON.parse(response);
489     }
490
491     BufferedReader br = null;
492     try
493     {
494       br = new BufferedReader(new InputStreamReader(response, "UTF-8"));
495       return new JSONParser().parse(br);
496     } finally
497     {
498       if (br != null)
499       {
500         try
501         {
502           br.close();
503         } catch (IOException e)
504         {
505           // ignore
506         }
507       }
508     }
509   }
510
511   public static Object parseJSON(String json) throws ParseException
512   {
513     return (isJS() ? JSON.parse(json)
514             : new JSONParser().parse(json));
515   }
516
517   public static Object parseJSON(Reader r)
518           throws IOException, ParseException
519   {
520     if (r == null)
521     {
522       return null;
523     }
524
525     if (!isJS())
526     {
527       return new JSONParser().parse(r);
528     }
529     // Using a file reader is not currently supported in SwingJS JavaScript
530
531     if (r instanceof FileReader)
532     {
533       throw new IOException(
534               "StringJS does not support FileReader parsing for JSON -- but it could...");
535     }
536     return JSON.parse(r);
537
538   }
539
540   /**
541    * Dump the input stream to an output file.
542    * 
543    * @param is
544    * @param outFile
545    * @throws IOException
546    *           if the file cannot be created or there is a problem reading the
547    *           input stream.
548    */
549   public static void streamToFile(InputStream is, File outFile)
550           throws IOException
551   {
552     if (isJS() && jsutil.streamToFile(is, outFile))
553     {
554       return;
555     }
556     FileOutputStream fio = new FileOutputStream(outFile);
557     try
558     {
559       byte[] bb = new byte[32 * 1024];
560       int l;
561       while ((l = is.read(bb)) > 0)
562       {
563         fio.write(bb, 0, l);
564       }
565     } finally
566     {
567       fio.close();
568     }
569   }
570
571   /**
572    * Add a known domain that implements access-control-allow-origin:*
573    * 
574    * These should be reviewed periodically.
575    * 
576    * @param domain
577    *          for a service that is not allowing ajax
578    * 
579    * @author hansonr@stolaf.edu
580    * 
581    */
582   public static void addJ2SDirectDatabaseCall(String domain)
583   {
584
585     if (isJS())
586     {
587       jsutil.addDirectDatabaseCall(domain);
588     }
589   }
590
591   /**
592    * Retrieve the first query field as command arguments to Jalview. Include
593    * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's __Info.args
594    * element to this value.
595    */
596
597   @SuppressWarnings("unused")
598   public static void getURLCommandArguments()
599   {
600     if (!isJS())
601     {
602       return;
603     }
604     String[] args = null;
605     /**
606      * @j2sNative args =
607      *            decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
608      *            + "?").split("?")[1].split("#")[0]); args && (args =
609      *            args.split(" "));
610      */
611     if (args != null)
612     {
613       jsutil.setAppletInfo("args", args);
614     }
615
616   }
617
618   public static URL getDocumentBase()
619   {
620     return (isJS() ? jsutil.getDocumentBase() : null);
621   }
622
623   public static URL getCodeBase()
624   {
625     return (isJS() ? jsutil.getCodeBase() : null);
626   }
627
628   public static void ensureJmol()
629   {
630     if (!isJS())
631     {
632       return;
633     }
634     jsutil.loadResourceIfClassUnknown("core/core_jvjmol.z.js",
635             "org.jmol.viewer.Viewer");
636   }
637
638   public static void ensureRegex()
639   {
640     if (!isJS())
641     {
642       return;
643     }
644     jsutil.loadResourceIfClassUnknown("core/core_stevesoft.z.js",
645             "com.stevesoft.pat.Regex");
646   }
647
648   public static Regex newRegex(String searchString, String replaceString)
649   {
650     ensureRegex();
651     return (replaceString == null ? new Regex(searchString)
652             : new Regex(searchString, replaceString));
653   }
654
655   public static Regex newRegexPerl(String code)
656   {
657     ensureRegex();
658     return Regex.perlCode(code);
659   }
660
661   /**
662    * Initialize Java debug logging. A representative sample -- adapt as desired.
663    */
664   public static void startJavaLogging()
665   {
666     /**
667      * @j2sIgnore
668      */
669     {
670       logClass("java.awt.EventDispatchThread", "java.awt.EventQueue",
671               "java.awt.Component", "java.awt.focus.Component",
672               "java.awt.event.Component",
673               "java.awt.focus.DefaultKeyboardFocusManager");
674     }
675   }
676
677   /**
678    * Initiate Java logging for a given class. Only for Java, not JavaScript;
679    * Allows debugging of complex event processing.
680    * 
681    * @param className
682    */
683   public static void logClass(String... classNames)
684   {
685     /**
686      * @j2sIgnore
687      * 
688      * 
689      */
690     {
691       Logger rootLogger = Logger.getLogger("");
692       rootLogger.setLevel(Level.ALL);
693       ConsoleHandler consoleHandler = new ConsoleHandler();
694       consoleHandler.setLevel(Level.ALL);
695       for (int i = classNames.length; --i >= 0;)
696       {
697         Logger logger = Logger.getLogger(classNames[i]);
698         logger.setLevel(Level.ALL);
699         logger.addHandler(consoleHandler);
700       }
701     }
702   }
703
704   /**
705    * Set the "app" property of the HTML5 applet object, for example,
706    * "testApplet.app", to point to the Jalview instance. This will be the object
707    * that page developers use that is similar to the original Java applet object
708    * that was accessed via LiveConnect.
709    * 
710    * @param app
711    */
712   public static void setAppClass(Object app)
713   {
714     if (isJS())
715     {
716       jsutil.setAppletAttribute("app", app);
717     }
718   }
719
720   /**
721    * Retrieve the object's embedded size from a div's style on a page if
722    * embedded in SwingJS.
723    * 
724    * @param frame
725    *          JFrame or JInternalFrame
726    * @param defaultWidth
727    *          use -1 to return null (no default size)
728    * @param defaultHeight
729    * @return the embedded dimensions or null (no default size or not embedded)
730    */
731   public static Dimension getDimIfEmbedded(Component frame,
732           int defaultWidth, int defaultHeight)
733   {
734     Dimension d = (Dimension) getEmbeddedAttribute(frame, "dim");
735     return (d == null && defaultWidth >= 0
736             ? new Dimension(defaultWidth, defaultHeight)
737             : d);
738   }
739
740   /**
741    *
742    * If this frame Is this frame embedded in a web page, return a known type.
743    * 
744    * @param frame
745    *          a JFrame or JInternalFrame
746    * @param type
747    * @return null if frame is not embedded.
748    */
749   public static Object getEmbeddedAttribute(Component frame, String type)
750   {
751     return (isJS() ? jsutil.getEmbeddedAttribute(frame, type) : null);
752   }
753
754   /**
755    * Only called for JavaScript.
756    * 
757    * @return Map for static singleton classes unique to a given applet
758    */
759   public static HashMap<?,?> getJSSingletons()
760   {
761     return (isJS() ? jsutil.getJSContext("jssingletons") : null);
762   }
763
764   /**
765    * By designating initialCapacity and loadFactor, we tell SwingJS to use a
766    * standard (slower) Java HashMap to back this HashSet, thus providing exactly
767    * the same iterator order (until a new Java version changes it!)
768    * 
769    * @return a standard Java HashSet
770    */
771   public static Set<String> getJavaOrderedHashSet()
772   {
773     return new HashSet<>(16, 0.75f);
774   }
775   
776   /**
777    * Switch the flag in SwingJS to use or not use the JavaScript Map object in
778    * any Hashtable, HashMap, or HashSet. Default is enabled.
779    * 
780    * For testing purposes only.
781    * 
782    */
783   public static boolean setJavaScriptMapObjectEnabled(boolean enabled)
784   {
785     if (!isJS())
786     {
787       return false;
788     }
789     jsutil.setJavaScriptMapObjectEnabled(enabled);
790     HashSet<String> hs = new HashSet<>();
791     // Java hash table iterator in HashMap will return "one" before "two"
792     // because of its hash code;
793     // JavaScript Map object will return "two" first because it was added first.
794     hs.add("two");
795     hs.add("one");
796     return (hs.iterator().next() == (enabled ? "two" : "one"));
797   }
798 }