1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 08:48:51 -05:00

* fixed UI jitters during DnD caused by loading JAXB

This commit is contained in:
Reinhard Pointner 2011-12-13 13:32:44 +00:00
parent 484996ac1f
commit 7863e8cf43
2 changed files with 50 additions and 46 deletions

View File

@ -92,6 +92,9 @@ public class Main {
startUserInterface(args);
}
});
// pre-load media.types (when loaded during DnD it will freeze the UI for a few hundred milliseconds)
MediaTypes.getDefault();
} catch (CmdLineException e) {
// illegal arguments => just print CLI error message and stop
System.err.println(e.getMessage());

View File

@ -3,62 +3,63 @@ package net.sourceforge.filebot;
import static java.util.Collections.*;
import static net.sourceforge.tuned.XPathUtilities.*;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import net.sourceforge.tuned.FileUtilities.ExtensionFileFilter;
@XmlRootElement(name = "media-types")
public class MediaTypes {
@XmlElement(name = "type")
private Type[] types;
private static final MediaTypes defaultInstance = parseDefault();
private static class Type {
private static MediaTypes parseDefault() {
try {
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(MediaTypes.class.getResourceAsStream("media.types"));
Map<String, List<String>> types = new HashMap<String, List<String>>();
@XmlAttribute(name = "name")
private String name;
@XmlElement(name = "extension")
private String[] extensions;
for (Node it : getChildren("type", dom.getFirstChild())) {
List<String> extensions = new ArrayList<String>(2);
for (Node ie : getChildren("extension", it)) {
extensions.add(getTextContent(ie));
}
types.put(getAttribute("name", it), extensions);
}
private static final MediaTypes defaultInstance = unmarshal();
private static MediaTypes unmarshal() {
try {
Unmarshaller unmarshaller = JAXBContext.newInstance(MediaTypes.class).createUnmarshaller();
return (MediaTypes) unmarshaller.unmarshal(MediaTypes.class.getResource("media.types"));
} catch (JAXBException e) {
return new MediaTypes(types);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Map<String, List<String>> types;
private Map<String, ExtensionFileFilter> filters = synchronizedMap(new HashMap<String, ExtensionFileFilter>());
public MediaTypes(Map<String, List<String>> types) {
this.types = types;
}
public List<String> getExtensionList(String name) {
List<String> list = new ArrayList<String>();
for (Type type : defaultInstance.types) {
if (type.name.startsWith(name)) {
addAll(list, type.extensions);
for (Entry<String, List<String>> type : types.entrySet()) {
if (type.getKey().startsWith(name)) {
list.addAll(type.getValue());
}
}
@ -66,7 +67,7 @@ public class MediaTypes {
}
public FileFilter getFilter(String name) {
public ExtensionFileFilter getFilter(String name) {
ExtensionFileFilter filter = filters.get(name);
if (filter == null) {
@ -84,12 +85,12 @@ public class MediaTypes {
public static ExtensionFileFilter getDefaultFilter(String name) {
return new ExtensionFileFilter(getDefault().getExtensionList(name));
return defaultInstance.getFilter(name);
}
// some convenience filters
public static final ExtensionFileFilter AUDIO_FILES = MediaTypes.getDefaultFilter("audio");
public static final ExtensionFileFilter VIDEO_FILES = MediaTypes.getDefaultFilter("video");
public static final ExtensionFileFilter SUBTITLE_FILES = MediaTypes.getDefaultFilter("subtitle");
public static final ExtensionFileFilter AUDIO_FILES = getDefaultFilter("audio");
public static final ExtensionFileFilter VIDEO_FILES = getDefaultFilter("video");
public static final ExtensionFileFilter SUBTITLE_FILES = getDefaultFilter("subtitle");
}