JAL-3253-applet JAL-3423 Windows TestNG
[jalview.git] / src / jalview / io / FileFormats.java
index a18d4e8..e83b87c 100644 (file)
@@ -1,5 +1,28 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.io;
 
+import jalview.bin.ApplicationSingletonProvider;
+import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
+
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -16,24 +39,11 @@ import java.util.Set;
  * @author gmcarstairs
  *
  */
-public class FileFormats
+public class FileFormats implements ApplicationSingletonI
 {
-  private static FileFormats instance = new FileFormats();
-
-  /*
-   * A lookup map of file formats by upper-cased name
-   */
-  private static Map<String, FileFormatI> formats;
-
-  /*
-   * Formats in this set are built in to Jalview and instantiated
-   * on startup, any others are added dynamically 
-   */
-  private static Set<FileFormatI> builtIn;
-
   public static FileFormats getInstance()
   {
-    return instance;
+    return (FileFormats) ApplicationSingletonProvider.getInstance(FileFormats.class);
   }
 
   /**
@@ -44,6 +54,16 @@ public class FileFormats
     reset();
   }
 
+  /*
+   * A lookup map of file formats by upper-cased name
+   */
+  private Map<String, FileFormatI> formats;
+
+  /*
+   * Formats in this set are capable of being identified by IdentifyFile 
+   */
+  private Set<FileFormatI> identifiable;
+
   /**
    * Reset to just the built-in file formats packaged with Jalview. These are
    * added (and will be shown in menus) in the order of their declaration in the
@@ -51,22 +71,21 @@ public class FileFormats
    */
   public synchronized void reset()
   {
-    formats = new LinkedHashMap<String, FileFormatI>();
-    builtIn = new HashSet<FileFormatI>();
+    formats = new LinkedHashMap<>();
+    identifiable = new HashSet<>();
     for (FileFormat format : FileFormat.values())
     {
-      registerFileFormat(format, true);
+      registerFileFormat(format, format.isIdentifiable());
     }
   }
 
   /**
-   * Answers false if the format is one 'built in' to Jalview, or true if not
-   * (meaning it has been added dynamically at runtime). Only built-in formats
-   * can be validated by IdentifyFile. Answers true for a null input.
+   * Answers true if the format is one that can be identified by IdentifyFile.
+   * Answers false for a null value.
    */
-  public boolean isDynamic(FileFormatI f)
+  public boolean isIdentifiable(FileFormatI f)
   {
-    return !builtIn.contains(f);
+    return identifiable.contains(f);
   }
 
   /**
@@ -76,11 +95,13 @@ public class FileFormats
    */
   public void registerFileFormat(FileFormatI format)
   {
-    registerFileFormat(format, false);
+    boolean isIdentifiable = format instanceof FileFormat
+            && ((FileFormat) format).isIdentifiable();
+    registerFileFormat(format, isIdentifiable);
   }
 
   protected void registerFileFormat(FileFormatI format,
-          boolean isBuiltIn)
+          boolean isIdentifiable)
   {
     String name = format.getName().toUpperCase();
     if (formats.containsKey(name))
@@ -88,9 +109,9 @@ public class FileFormats
       System.err.println("Overwriting file format: " + format.getName());
     }
     formats.put(name, format);
-    if (isBuiltIn)
+    if (isIdentifiable)
     {
-      builtIn.add(format);
+      identifiable.add(format);
     }
   }
 
@@ -102,7 +123,7 @@ public class FileFormats
   public void deregisterFileFormat(String name)
   {
     FileFormatI ff = formats.remove(name.toUpperCase());
-    builtIn.remove(ff);
+    identifiable.remove(ff);
   }
 
   /**
@@ -115,7 +136,7 @@ public class FileFormats
    */
   public List<String> getWritableFormats(boolean textOnly)
   {
-    List<String> l = new ArrayList<String>();
+    List<String> l = new ArrayList<>();
     for (FileFormatI ff : formats.values())
     {
       if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
@@ -134,7 +155,7 @@ public class FileFormats
    */
   public List<String> getReadableFormats()
   {
-    List<String> l = new ArrayList<String>();
+    List<String> l = new ArrayList<>();
     for (FileFormatI ff : formats.values())
     {
       if (ff.isReadable())