2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.bin.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.LinkedHashMap;
29 import java.util.List;
34 * A singleton registry of alignment file formats known to Jalview. On startup,
35 * the 'built-in' formats are added (from the FileFormat enum). Additional
36 * formats can be registered (or formats deregistered) programmatically, for
37 * example with a Groovy script.
42 public class FileFormats implements ApplicationSingletonI
44 public static FileFormats getInstance()
46 return (FileFormats) ApplicationSingletonProvider.getInstance(FileFormats.class);
50 * Private constructor registers Jalview's built-in file formats
58 * A lookup map of file formats by upper-cased name
60 private Map<String, FileFormatI> formats;
63 * Formats in this set are capable of being identified by IdentifyFile
65 private Set<FileFormatI> identifiable;
68 * Reset to just the built-in file formats packaged with Jalview. These are
69 * added (and will be shown in menus) in the order of their declaration in the
72 public synchronized void reset()
74 formats = new LinkedHashMap<>();
75 identifiable = new HashSet<>();
76 for (FileFormat format : FileFormat.values())
78 registerFileFormat(format, format.isIdentifiable());
83 * Answers true if the format is one that can be identified by IdentifyFile.
84 * Answers false for a null value.
86 public boolean isIdentifiable(FileFormatI f)
88 return identifiable.contains(f);
92 * Registers a file format for case-insensitive lookup by name
96 public void registerFileFormat(FileFormatI format)
98 boolean isIdentifiable = format instanceof FileFormat
99 && ((FileFormat) format).isIdentifiable();
100 registerFileFormat(format, isIdentifiable);
103 protected void registerFileFormat(FileFormatI format,
104 boolean isIdentifiable)
106 String name = format.getName().toUpperCase();
107 if (formats.containsKey(name))
109 System.err.println("Overwriting file format: " + format.getName());
111 formats.put(name, format);
114 identifiable.add(format);
119 * Deregisters a file format so it is no longer shown in menus
123 public void deregisterFileFormat(String name)
125 FileFormatI ff = formats.remove(name.toUpperCase());
126 identifiable.remove(ff);
130 * Answers a list of writeable file formats (as strings, corresponding to the
131 * getName() and forName() methods)
134 * if true, only text (not binary) formats are included
137 public List<String> getWritableFormats(boolean textOnly)
139 List<String> l = new ArrayList<>();
140 for (FileFormatI ff : formats.values())
142 if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
151 * Answers a list of readable file formats (as strings, corresponding to the
152 * getName() and forName() methods)
156 public List<String> getReadableFormats()
158 List<String> l = new ArrayList<>();
159 for (FileFormatI ff : formats.values())
170 * Returns the file format with the given name, or null if format is null or
171 * invalid. This is not case-sensitive.
176 public FileFormatI forName(String format)
178 return format == null ? null : formats.get(format.toUpperCase());
182 * Returns an iterable collection of registered file formats (in the order in
183 * which they were registered)
187 public Iterable<FileFormatI> getFormats()
189 return formats.values();