1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00

* Each application instance will get its own temp folder now

* moved MessageBus registration of each Panels MessageHandler to FileBotWindow
This commit is contained in:
Reinhard Pointner 2008-10-22 22:07:02 +00:00
parent d0725404ef
commit e6df0141c7
8 changed files with 92 additions and 37 deletions

View File

@ -5,6 +5,8 @@ package net.sourceforge.filebot.ui;
import javax.swing.Icon;
import javax.swing.JPanel;
import net.sourceforge.tuned.MessageHandler;
public class FileBotPanel extends JPanel {
@ -29,4 +31,9 @@ public class FileBotPanel extends JPanel {
return name;
}
public MessageHandler getMessageHandler() {
return null;
}
}

View File

@ -36,23 +36,25 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
private JPanel pagePanel = new JPanel(new CardLayout());
private FileBotPanelSelectionList selectionListPanel = new FileBotPanelSelectionList();
private FileBotPanelSelectionList panelSelectionList = new FileBotPanelSelectionList();
private HeaderPanel headerPanel = new HeaderPanel();
public FileBotWindow() {
super(FileBotUtil.getApplicationName());
setLocationByPlatform(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// set taskbar / taskswitch icons
ArrayList<Image> icons = new ArrayList<Image>(2);
icons.add(ResourceManager.getImage("window.icon.small"));
icons.add(ResourceManager.getImage("window.icon.big"));
setIconImages(icons);
selectionListPanel.getPanelModel().addAll(createPanels());
selectionListPanel.addListSelectionListener(this);
panelSelectionList.getPanelModel().addAll(createPanels());
panelSelectionList.addListSelectionListener(this);
JComponent contentPane = createContentPane();
@ -63,14 +65,19 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
// restore the panel selection from last time,
// switch to EpisodeListPanel by default (e.g. first start)
int selectedPanel = Preferences.userNodeForPackage(getClass()).getInt("selectedPanel", 3);
selectionListPanel.setSelectedIndex(selectedPanel);
panelSelectionList.setSelectedIndex(selectedPanel);
// connect message handlers to message bus
MessageBus.getDefault().addMessageHandler("panel", panelSelectMessageHandler);
for (FileBotPanel panel : panelSelectionList.getPanelModel()) {
MessageBus.getDefault().addMessageHandler(panel.getPanelName(), panel.getMessageHandler());
}
}
private List<FileBotPanel> createPanels() {
List<FileBotPanel> panels = new ArrayList<FileBotPanel>();
List<FileBotPanel> panels = new ArrayList<FileBotPanel>(6);
panels.add(new ListPanel());
panels.add(new RenamePanel());
@ -84,7 +91,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
FileBotPanel currentPanel = (FileBotPanel) selectionListPanel.getSelectedValue();
FileBotPanel currentPanel = (FileBotPanel) panelSelectionList.getSelectedValue();
headerPanel.setTitle(currentPanel.getPanelName());
CardLayout cardLayout = (CardLayout) pagePanel.getLayout();
@ -95,7 +102,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
c.revalidate();
c.repaint();
Preferences.userNodeForPackage(getClass()).putInt("selectedPanel", selectionListPanel.getSelectedIndex());
Preferences.userNodeForPackage(getClass()).putInt("selectedPanel", panelSelectionList.getSelectedIndex());
}
@ -106,7 +113,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
JPanel shadowBorderPanel = new JPanel(new BorderLayout());
shadowBorderPanel.setOpaque(false);
JScrollPane selectListScrollPane = new JScrollPane(selectionListPanel);
JScrollPane selectListScrollPane = new JScrollPane(panelSelectionList);
selectListScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
selectListScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
shadowBorderPanel.add(selectListScrollPane, BorderLayout.CENTER);
@ -130,7 +137,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
pageLayer.add(headerPanel, BorderLayout.NORTH);
pageLayer.add(pagePanel, BorderLayout.CENTER);
for (FileBotPanel panel : selectionListPanel.getPanelModel()) {
for (FileBotPanel panel : panelSelectionList.getPanelModel()) {
pagePanel.add(panel, panel.getPanelName());
}
@ -161,7 +168,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
// switch to this panel
if (panel instanceof FileBotPanel)
selectionListPanel.setSelectedValue(panel, true);
panelSelectionList.setSelectedValue(panel, true);
}
}

View File

@ -18,7 +18,7 @@ import net.sourceforge.filebot.ui.FileTransferableMessageHandler;
import net.sourceforge.filebot.ui.panel.analyze.tools.SplitPanel;
import net.sourceforge.filebot.ui.panel.analyze.tools.ToolPanel;
import net.sourceforge.filebot.ui.panel.analyze.tools.TypePanel;
import net.sourceforge.tuned.MessageBus;
import net.sourceforge.tuned.MessageHandler;
public class AnalyzePanel extends FileBotPanel {
@ -26,6 +26,8 @@ public class AnalyzePanel extends FileBotPanel {
private final FileTreePanel fileTreePanel = new FileTreePanel();
private final JTabbedPane toolsPanel = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
private final MessageHandler messageHandler = new FileTransferableMessageHandler(this, fileTreePanel.getFileTree().getTransferablePolicy());
public AnalyzePanel() {
super("Analyze", ResourceManager.getIcon("panel.analyze"));
@ -41,8 +43,12 @@ public class AnalyzePanel extends FileBotPanel {
addTool(new SplitPanel());
fileTreePanel.getFileTree().addPropertyChangeListener(FileTree.CONTENT_PROPERTY, fileTreeChangeListener);
MessageBus.getDefault().addMessageHandler(getPanelName(), new FileTransferableMessageHandler(this, fileTreePanel.getFileTree().getTransferablePolicy()));
}
@Override
public MessageHandler getMessageHandler() {
return messageHandler;
}

View File

@ -27,7 +27,7 @@ import net.sourceforge.filebot.ui.FileTransferableMessageHandler;
import net.sourceforge.filebot.ui.MessageManager;
import net.sourceforge.filebot.ui.transfer.LoadAction;
import net.sourceforge.filebot.ui.transfer.SaveAction;
import net.sourceforge.tuned.MessageBus;
import net.sourceforge.tuned.MessageHandler;
import net.sourceforge.tuned.ui.TunedUtil;
@ -41,6 +41,8 @@ public class ListPanel extends FileBotPanel {
private SpinnerNumberModel fromSpinnerModel = new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1);
private SpinnerNumberModel toSpinnerModel = new SpinnerNumberModel(20, 0, Integer.MAX_VALUE, 1);
private final MessageHandler messageHandler = new FileTransferableMessageHandler(this, list.getTransferablePolicy());
public ListPanel() {
super("List", ResourceManager.getIcon("panel.list"));
@ -78,8 +80,12 @@ public class ListPanel extends FileBotPanel {
list.add(buttonPanel, BorderLayout.SOUTH);
TunedUtil.putActionForKeystroke(this, KeyStroke.getKeyStroke("ENTER"), createAction);
MessageBus.getDefault().addMessageHandler(getPanelName(), new FileTransferableMessageHandler(this, list.getTransferablePolicy()));
}
@Override
public MessageHandler getMessageHandler() {
return messageHandler;
}
private AbstractAction createAction = new AbstractAction("Create") {

View File

@ -22,15 +22,17 @@ import net.sourceforge.filebot.ui.SelectDialog;
import net.sourceforge.filebot.ui.transfer.LoadAction;
import net.sourceforge.filebot.ui.transfer.SaveAction;
import net.sourceforge.tuned.FileUtil;
import net.sourceforge.tuned.MessageBus;
import net.sourceforge.tuned.MessageHandler;
import net.sourceforge.tuned.ui.TunedUtil;
public class SfvPanel extends FileBotPanel {
private SfvTable sfvTable = new SfvTable();
private final SfvTable sfvTable = new SfvTable();
private TotalProgressPanel totalProgressPanel = new TotalProgressPanel(sfvTable.getChecksumComputationService());
private final TotalProgressPanel totalProgressPanel = new TotalProgressPanel(sfvTable.getChecksumComputationService());
private final MessageHandler messageHandler = new FileTransferableMessageHandler(this, sfvTable.getTransferablePolicy());
public SfvPanel() {
@ -50,8 +52,12 @@ public class SfvPanel extends FileBotPanel {
// Shortcut DELETE
TunedUtil.putActionForKeystroke(this, KeyStroke.getKeyStroke("pressed DELETE"), removeAction);
MessageBus.getDefault().addMessageHandler(getPanelName(), new FileTransferableMessageHandler(this, sfvTable.getTransferablePolicy()));
}
@Override
public MessageHandler getMessageHandler() {
return messageHandler;
}
private final SaveAction saveAction = new ChecksumTableSaveAction();

View File

@ -52,10 +52,10 @@ public class LazyTextFileTransferable implements Transferable {
private FileTransferable createFileTransferable() throws IOException {
// remove invalid characters from file name
String filename = FileBotUtil.validateFileName(defaultFileName);
String validFileName = FileBotUtil.validateFileName(defaultFileName);
// create new temporary file
File temporaryFile = TemporaryFolder.getFolder(FileBotUtil.getApplicationName().toLowerCase()).createFile(filename);
// create new temporary file in TEMP/APP_NAME [UUID]/dnd
File temporaryFile = TemporaryFolder.getFolder(FileBotUtil.getApplicationName().toLowerCase()).createFolder("dnd").createFile(validFileName);
// write text to file
FileChannel fileChannel = new FileOutputStream(temporaryFile).getChannel();

View File

@ -23,6 +23,8 @@ public class MessageBus {
public synchronized void addMessageHandler(String topic, MessageHandler handler) {
if (handler == null)
return;
List<MessageHandler> list = handlers.get(topic.toLowerCase());
@ -55,15 +57,24 @@ public class MessageBus {
public void publish(final String topic, final Object... messages) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
for (MessageHandler handler : getHandlers(topic.toLowerCase())) {
handler.handle(topic.toLowerCase(), messages);
if (SwingUtilities.isEventDispatchThread()) {
publishDirect(topic, messages);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
publishDirect(topic, messages);
}
}
});
});
}
}
private void publishDirect(String topic, Object... messages) {
for (MessageHandler handler : getHandlers(topic.toLowerCase())) {
handler.handle(topic.toLowerCase(), messages);
}
}
}

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public final class TemporaryFolder {
@ -17,12 +18,21 @@ public final class TemporaryFolder {
private static final Map<String, TemporaryFolder> folders = new HashMap<String, TemporaryFolder>();
/**
* Get a {@link TemporaryFolder} instance for a given name. The actual directory will be
* created lazily (e.g. when a file is created). The directories name will start with the
* given name and contain a unique id, so multiple application instances may run at the
* same time without the risk of interference.
*
* @param name case-insensitive name of a temporary folder (e.g. application name)
* @return temporary folder for this name
*/
public static TemporaryFolder getFolder(String name) {
synchronized (folders) {
TemporaryFolder folder = folders.get(name.toLowerCase());
if (folder == null) {
folder = new TemporaryFolder(new File(tmpdir, name));
folder = new TemporaryFolder(new File(tmpdir, String.format("%s [%s]", name, UUID.randomUUID())));
folders.put(name.toLowerCase(), folder);
}
@ -56,7 +66,7 @@ public final class TemporaryFolder {
/**
* Create an empty file in this temporary folder
* Create an empty file in this temporary folder.
*
* @param name name of the file
* @return newly created file
@ -64,11 +74,11 @@ public final class TemporaryFolder {
*/
public File createFile(String name) throws IOException {
// if the directory does not exist it will be created
File file = new File(getFolder(), name);
file.createNewFile();
return file;
}
@ -95,13 +105,15 @@ public final class TemporaryFolder {
/**
* Retrieve the {@link File} object for this {@link TemporaryFolder}
* Retrieve the {@link File} object for this {@link TemporaryFolder}. The actual directory
* for the {@link TemporaryFolder} instance will be created by this method.
*
* @return the {@link File} object for this {@link TemporaryFolder}
*/
public File getFolder() {
if (!root.exists())
if (!root.exists()) {
root.mkdirs();
}
return root;
}