3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.LinkedHashMap;
11 * A singleton registry of alignment file formats known to Jalview. On startup,
12 * the 'built-in' formats are added (from the FileFormat enum). Additional
13 * formats can be registered (or formats deregistered) programmatically, for
14 * example with a Groovy script.
19 public class FileFormats
21 private static FileFormats instance = new FileFormats();
24 * A lookup map of file formats by upper-cased name
26 private static Map<String, FileFormatI> formats;
29 * Formats in this set are capable of being identified by IdentifyFile
31 private static Set<FileFormatI> identifiable;
33 public static FileFormats getInstance()
39 * Private constructor registers Jalview's built-in file formats
47 * Reset to just the built-in file formats packaged with Jalview. These are
48 * added (and will be shown in menus) in the order of their declaration in the
51 public synchronized void reset()
53 formats = new LinkedHashMap<String, FileFormatI>();
54 identifiable = new HashSet<FileFormatI>();
55 for (FileFormat format : FileFormat.values())
57 registerFileFormat(format, format.isIdentifiable());
62 * Answers true if the format is one that can be identified by IdentifyFile.
63 * Answers false for a null value.
65 public boolean isIdentifiable(FileFormatI f)
67 return identifiable.contains(f);
71 * Registers a file format for case-insensitive lookup by name
75 public void registerFileFormat(FileFormatI format)
77 boolean isIdentifiable = format instanceof FileFormat
78 && ((FileFormat) format).isIdentifiable();
79 registerFileFormat(format, isIdentifiable);
82 protected void registerFileFormat(FileFormatI format,
83 boolean isIdentifiable)
85 String name = format.getName().toUpperCase();
86 if (formats.containsKey(name))
88 System.err.println("Overwriting file format: " + format.getName());
90 formats.put(name, format);
93 identifiable.add(format);
98 * Deregisters a file format so it is no longer shown in menus
102 public void deregisterFileFormat(String name)
104 FileFormatI ff = formats.remove(name.toUpperCase());
105 identifiable.remove(ff);
109 * Answers a list of writeable file formats (as strings, corresponding to the
110 * getName() and forName() methods)
113 * if true, only text (not binary) formats are included
116 public List<String> getWritableFormats(boolean textOnly)
118 List<String> l = new ArrayList<String>();
119 for (FileFormatI ff : formats.values())
121 if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
130 * Answers a list of readable file formats (as strings, corresponding to the
131 * getName() and forName() methods)
135 public List<String> getReadableFormats()
137 List<String> l = new ArrayList<String>();
138 for (FileFormatI ff : formats.values())
149 * Returns the file format with the given name, or null if format is null or
150 * invalid. This is not case-sensitive.
155 public FileFormatI forName(String format)
157 return format == null ? null : formats.get(format.toUpperCase());
161 * Returns an iterable collection of registered file formats (in the order in
162 * which they were registered)
166 public Iterable<FileFormatI> getFormats()
168 return formats.values();