1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28: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());
@ -99,7 +102,7 @@ public class Main {
}
}
private static void startUserInterface(ArgumentBean args) {
JFrame frame;
@ -126,7 +129,7 @@ public class Main {
frame.setVisible(true);
}
private static void restoreWindowBounds(final JFrame window, final Settings settings) {
// store bounds on close
window.addWindowListener(new WindowAdapter() {
@ -151,7 +154,7 @@ public class Main {
window.setBounds(x, y, width, height);
}
/**
* Shutdown ehcache properly, so that disk-persistent stores can actually be saved to disk
*/
@ -165,7 +168,7 @@ public class Main {
});
}
/**
* Initialize default SecurityManager and grant all permissions via security policy.
* Initialization is required in order to run {@link ExpressionFormat} in a secure sandbox.
@ -182,7 +185,7 @@ public class Main {
return true;
}
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
// VisualVM can't connect if this method does return

View File

@ -3,70 +3,71 @@ 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 {
@XmlAttribute(name = "name")
private String name;
@XmlElement(name = "extension")
private String[] extensions;
}
private static final MediaTypes defaultInstance = unmarshal();
private static MediaTypes unmarshal() {
private static MediaTypes parseDefault() {
try {
Unmarshaller unmarshaller = JAXBContext.newInstance(MediaTypes.class).createUnmarshaller();
return (MediaTypes) unmarshaller.unmarshal(MediaTypes.class.getResource("media.types"));
} catch (JAXBException e) {
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(MediaTypes.class.getResourceAsStream("media.types"));
Map<String, List<String>> types = new HashMap<String, List<String>>();
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);
}
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());
}
}
return list;
}
public FileFilter getFilter(String name) {
public ExtensionFileFilter getFilter(String name) {
ExtensionFileFilter filter = filters.get(name);
if (filter == null) {
@ -77,19 +78,19 @@ public class MediaTypes {
return filter;
}
public static MediaTypes getDefault() {
return defaultInstance;
}
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");
}