1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 16:58: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); 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) { } catch (CmdLineException e) {
// illegal arguments => just print CLI error message and stop // illegal arguments => just print CLI error message and stop
System.err.println(e.getMessage()); System.err.println(e.getMessage());

View File

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