* removed Settings singleton, always use Preferences directly

* simplified HistoryPanel by using MigLayout
* added MigLayout jar to libs
* improved decimal format for file count and size in AnalyzePanel
* put application name and version getters into FileBotUtil
This commit is contained in:
Reinhard Pointner 2008-10-11 20:42:03 +00:00
parent 661e079f2b
commit a0a43b0e03
17 changed files with 97 additions and 162 deletions

BIN
lib/miglayout-3.6-swing.jar Normal file

Binary file not shown.

View File

@ -1,11 +1,13 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import net.sourceforge.filebot.ArgumentBean;
import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.ui.FileBotWindow;
import org.kohsuke.args4j.CmdLineException;
@ -21,8 +23,13 @@ public class Main {
final ArgumentBean argumentBean = parseArguments(args);
if (argumentBean.isClear())
Settings.getSettings().clear();
if (argumentBean.isClear()) {
try {
Preferences.userNodeForPackage(FileBotUtil.class).removeNode();
} catch (BackingStoreException e) {
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
}
}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -67,4 +74,5 @@ public class Main {
return argumentBean;
}
}

View File

@ -13,6 +13,15 @@ import net.sourceforge.tuned.FileUtil;
public final class FileBotUtil {
public static final String getApplicationName() {
return "FileBot";
};
public static final String getApplicationVersion() {
return "1.9";
};
/**
* Invalid characters in filenames: \, /, :, *, ?, ", <, >, |, \r and \n
*/

View File

@ -1,72 +0,0 @@
package net.sourceforge.filebot;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import net.sourceforge.tuned.PreferencesList;
import net.sourceforge.tuned.PreferencesMap;
public class Settings {
public static final String NAME = "FileBot";
public static final String VERSION = "2.0";
public static final String ROOT = NAME.toLowerCase();
public static final String SELECTED_PANEL = "panel";
public static final String SEARCH_HISTORY = "search/history";
public static final String SUBTITLE_HISTORY = "subtitle/history";
private static final Settings settings = new Settings();
public static Settings getSettings() {
return settings;
}
private final Preferences prefs;
private Settings() {
prefs = Preferences.userRoot().node(ROOT);
}
public void putInt(String key, int value) {
prefs.putInt(key, value);
}
public int getInt(String key, int def) {
return prefs.getInt(key, def);
}
public List<String> asStringList(String key) {
return PreferencesList.map(prefs.node(key), String.class);
}
public Map<String, Boolean> asBooleanMap(String key) {
return PreferencesMap.map(prefs.node(key), Boolean.class);
}
public void clear() {
try {
for (String child : prefs.childrenNames()) {
prefs.node(child).removeNode();
}
} catch (BackingStoreException e) {
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
}
}
}

View File

@ -72,7 +72,7 @@ public abstract class AbstractSearchPanel<S, E, T extends JComponent> extends Fi
centerPanel.add(tabbedPane, BorderLayout.CENTER);
historyPanel.setColumnHeader3("Duration");
historyPanel.setColumnHeader(2, "Duration");
JScrollPane historyScrollPane = new JScrollPane(historyPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
historyScrollPane.setBorder(BorderFactory.createEmptyBorder());

View File

@ -2,8 +2,8 @@
package net.sourceforge.filebot.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
@ -53,7 +53,10 @@ class FileBotPanelSelectionList extends JList {
private static class PanelCellRenderer extends DefaultFancyListCellRenderer {
public PanelCellRenderer() {
super(BorderLayout.CENTER, 10, 0, new Color(0x163264));
super(10, 0, new Color(0x163264));
// center labels in list
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
setHighlightingEnabled(false);

View File

@ -7,6 +7,7 @@ import java.awt.CardLayout;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.JComponent;
import javax.swing.JFrame;
@ -18,8 +19,8 @@ import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.ui.panel.analyze.AnalyzePanel;
import net.sourceforge.filebot.ui.panel.list.ListPanel;
import net.sourceforge.filebot.ui.panel.rename.RenamePanel;
@ -41,7 +42,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
public FileBotWindow() {
super(Settings.NAME);
super(FileBotUtil.getApplicationName());
setLocationByPlatform(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
@ -59,7 +60,10 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
setSize(760, 615);
selectionListPanel.setSelectedIndex(Settings.getSettings().getInt(Settings.SELECTED_PANEL, 3));
// restore the panel selection from last time,
// switch to SearchPanel by default (e.g. first start)
int selectedPanel = Preferences.userNodeForPackage(getClass()).getInt("selectedPanel", 3);
selectionListPanel.setSelectedIndex(selectedPanel);
MessageBus.getDefault().addMessageHandler("panel", panelSelectMessageHandler);
}
@ -91,7 +95,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
c.revalidate();
c.repaint();
Settings.getSettings().putInt(Settings.SELECTED_PANEL, selectionListPanel.getSelectedIndex());
Preferences.userNodeForPackage(getClass()).putInt("selectedPanel", selectionListPanel.getSelectedIndex());
}
@ -121,7 +125,6 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
private JComponent createPageLayer() {
JPanel pageLayer = new JPanel(new BorderLayout());
pagePanel.setBorder(new EmptyBorder(10, 110, 10, 10));
pageLayer.add(headerPanel, BorderLayout.NORTH);
@ -139,7 +142,7 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
private JComponent createContentPane() {
JPanel contentPane = new JPanel();
JPanel contentPane = new JPanel(null);
contentPane.setLayout(new OverlayLayout(contentPane));
contentPane.add(createSelectionListLayer());
@ -153,8 +156,10 @@ public class FileBotWindow extends JFrame implements ListSelectionListener {
@Override
public void handle(String topic, Object... messages) {
if (messages.length >= 1) {
// get last element in array
Object panel = messages[messages.length - 1];
// switch to this panel
if (panel instanceof FileBotPanel)
selectionListPanel.setSelectedValue(panel, true);
}

View File

@ -3,85 +3,66 @@ package net.sourceforge.filebot.ui;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import net.sourceforge.tuned.ui.HyperlinkLabel;
public class HistoryPanel extends JPanel {
private final JPanel grid = new JPanel(new GridLayout(0, 3, 15, 10));
private final JLabel columnHeader1 = new JLabel();
private final JLabel columnHeader2 = new JLabel();
private final JLabel columnHeader3 = new JLabel();
private final List<JLabel> columnHeaders = new ArrayList<JLabel>(3);
public HistoryPanel() {
super(new FlowLayout(FlowLayout.CENTER));
super(new MigLayout("fillx, insets 10 30 10 50, wrap 3"));
setBackground(Color.WHITE);
setOpaque(true);
grid.setOpaque(false);
Font font = columnHeader1.getFont().deriveFont(Font.BOLD);
columnHeader1.setHorizontalAlignment(SwingConstants.CENTER);
columnHeader2.setHorizontalAlignment(SwingConstants.CENTER);
columnHeader3.setHorizontalAlignment(SwingConstants.RIGHT);
columnHeader1.setFont(font);
columnHeader2.setFont(font);
columnHeader3.setFont(font);
grid.add(columnHeader1);
grid.add(columnHeader2);
grid.add(columnHeader3);
add(grid);
setupHeader();
}
public void setColumnHeader1(String text) {
columnHeader1.setText(text);
protected void setupHeader() {
for (int i = 0; i < 3; i++) {
JLabel columnHeader = new JLabel();
columnHeader.setFont(columnHeader.getFont().deriveFont(Font.BOLD));
columnHeaders.add(columnHeader);
add(columnHeader, (i == 0) ? "align left, gapbefore 20" : "align right, gapafter 20");
}
}
public void setColumnHeader2(String text) {
columnHeader2.setText(text);
}
public void setColumnHeader3(String text) {
columnHeader3.setText(text);
public void setColumnHeader(int index, String text) {
columnHeaders.get(index).setText(text);
}
public void add(String column1, URI link, Icon icon, String column2, String column3) {
JLabel label1 = (link != null) ? new HyperlinkLabel(column1, link) : new JLabel(column1);
JLabel label2 = new JLabel(column2);
JLabel label3 = new JLabel(column3);
label1.setHorizontalAlignment(SwingConstants.LEFT);
label2.setHorizontalAlignment(SwingConstants.RIGHT);
label3.setHorizontalAlignment(SwingConstants.RIGHT);
JLabel label2 = new JLabel(column2, SwingConstants.RIGHT);
JLabel label3 = new JLabel(column3, SwingConstants.RIGHT);
label1.setIcon(icon);
label1.setIconTextGap(7);
label2.setBorder(new EmptyBorder(0, 0, 0, 10));
add(label1, "align left");
grid.add(label1);
grid.add(label2);
grid.add(label3);
// set minimum with to 100px so the text is aligned to the right,
// even though the whole label is centered
add(label2, "align center, wmin 100");
add(label3, "align right");
}
}

View File

@ -5,8 +5,8 @@ package net.sourceforge.filebot.ui;
import javax.swing.Icon;
import javax.swing.SwingConstants;
import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings;
import net.sourceforge.tuned.ui.notification.MessageNotification;
import net.sourceforge.tuned.ui.notification.NotificationManager;
import net.sourceforge.tuned.ui.notification.QueueNotificationLayout;
@ -29,7 +29,7 @@ public class MessageManager {
private static void show(String message, Icon icon, int timeout) {
manager.show(new MessageNotification(Settings.NAME, message, icon, timeout));
manager.show(new MessageNotification(FileBotUtil.getApplicationName(), message, icon, timeout));
}
}

View File

@ -29,7 +29,7 @@ public abstract class ToolPanel extends JComponent {
public abstract void update(Collection<File> list);
protected static DefaultMutableTreeNode createTreeNode(String name, Collection<File> files) {
protected DefaultMutableTreeNode createTreeNode(String name, Collection<File> files) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
long totalSize = 0;
@ -39,15 +39,11 @@ public abstract class ToolPanel extends JComponent {
totalSize += file.length();
}
String count = null;
// format the number of files string (e.g. 1 file, 2 files, ...)
String numberOfFiles = String.format("%,d %s", files.size(), files.size() == 1 ? "file" : "files");
if (files.size() == 1) {
count = String.format("%d file", files.size());
} else {
count = String.format("%d files", files.size());
}
node.setUserObject(String.format("%s (%s, %s)", name, count, FileUtil.formatSize(totalSize)));
// set node text (e.g. txt (1 file, 42 Byte))
node.setUserObject(String.format("%s (%s, %s)", name, numberOfFiles, FileUtil.formatSize(totalSize)));
return node;
}

View File

@ -67,7 +67,7 @@ public class SearchPanel extends FileBotPanel {
public SearchPanel() {
super("Search", ResourceManager.getIcon("panel.search"));
super("Episodes", ResourceManager.getIcon("panel.search"));
searchField = new SelectButtonTextField<EpisodeListClient>();
@ -76,9 +76,9 @@ public class SearchPanel extends FileBotPanel {
searchField.getSelectButton().addPropertyChangeListener(SelectButton.SELECTED_VALUE, selectButtonListener);
historyPanel.setColumnHeader1("Show");
historyPanel.setColumnHeader2("Number of Episodes");
historyPanel.setColumnHeader3("Duration");
historyPanel.setColumnHeader(0, "Show");
historyPanel.setColumnHeader(1, "Number of Episodes");
historyPanel.setColumnHeader(2, "Duration");
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));

View File

@ -7,9 +7,9 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.prefs.Preferences;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.ui.AbstractSearchPanel;
import net.sourceforge.filebot.ui.SelectDialog;
import net.sourceforge.filebot.web.OpenSubtitlesSubtitleClient;
@ -18,6 +18,7 @@ import net.sourceforge.filebot.web.SubsceneSubtitleClient;
import net.sourceforge.filebot.web.SubtitleClient;
import net.sourceforge.filebot.web.SubtitleDescriptor;
import net.sourceforge.tuned.ListChangeSynchronizer;
import net.sourceforge.tuned.PreferencesList;
import net.sourceforge.tuned.ui.LabelProvider;
import net.sourceforge.tuned.ui.SimpleLabelProvider;
@ -25,15 +26,21 @@ import net.sourceforge.tuned.ui.SimpleLabelProvider;
public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitlePackage, SubtitleDownloadPanel> {
public SubtitlePanel() {
super("Subtitle", ResourceManager.getIcon("panel.subtitle"));
super("Subtitles", ResourceManager.getIcon("panel.subtitle"));
getHistoryPanel().setColumnHeader1("Show / Movie");
getHistoryPanel().setColumnHeader2("Number of Subtitles");
getHistoryPanel().setColumnHeader(0, "Show / Movie");
getHistoryPanel().setColumnHeader(1, "Number of Subtitles");
List<String> persistentSearchHistory = Settings.getSettings().asStringList(Settings.SUBTITLE_HISTORY);
// get preferences node that contains the history entries
Preferences historyNode = Preferences.systemNodeForPackage(getClass()).node("history");
// get a StringList that read and writes directly from and to the preferences
List<String> persistentSearchHistory = PreferencesList.map(historyNode, String.class);
// add history from the preferences to the current in-memory history (for completion)
getSearchHistory().addAll(persistentSearchHistory);
// perform all insert/add/remove operations on the in-memory history on the preferences node as well
ListChangeSynchronizer.syncEventListToList(getSearchHistory(), persistentSearchHistory);
}

View File

@ -12,7 +12,6 @@ import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.Settings;
import net.sourceforge.tuned.TemporaryFolder;
@ -56,7 +55,7 @@ public class TextFileTransferable implements Transferable {
String filename = FileBotUtil.validateFileName(defaultFileName);
// create new temporary file
File temporaryFile = TemporaryFolder.getFolder(Settings.ROOT).createFile(filename);
File temporaryFile = TemporaryFolder.getFolder(FileBotUtil.getApplicationName().toLowerCase()).createFile(filename);
// write text to file
FileChannel fileChannel = new FileOutputStream(temporaryFile).getChannel();

View File

@ -13,8 +13,8 @@ import java.util.logging.Logger;
import javax.swing.Icon;
import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings;
/**
@ -22,7 +22,7 @@ import net.sourceforge.filebot.Settings;
*/
public class OpenSubtitlesSubtitleClient implements SubtitleClient {
private final OpenSubtitlesClient client = new OpenSubtitlesClient(String.format("%s v%s", Settings.NAME, Settings.VERSION));
private final OpenSubtitlesClient client = new OpenSubtitlesClient(String.format("%s v%s", FileBotUtil.getApplicationName(), FileBotUtil.getApplicationVersion()));
private final LogoutTimer logoutTimer = new LogoutTimer();

View File

@ -16,11 +16,11 @@ public final class FileUtil {
public static String formatSize(long size) {
if (size >= MEGA)
return String.format("%d MB", size / MEGA);
return String.format("%,d MB", size / MEGA);
else if (size >= KILO)
return String.format("%d KB", size / KILO);
return String.format("%,d KB", size / KILO);
else
return String.format("%d Byte", size);
return String.format("%,d Byte", size);
}

View File

@ -2,9 +2,9 @@
package net.sourceforge.tuned.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
@ -55,7 +55,7 @@ public abstract class AbstractFancyListCellRenderer extends JComponent implement
public AbstractFancyListCellRenderer(Insets padding, Insets margin, Color borderColor) {
this.setLayout(new BorderLayout());
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
Border border = null;

View File

@ -2,7 +2,6 @@
package net.sourceforge.tuned.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
@ -17,19 +16,19 @@ public class DefaultFancyListCellRenderer extends AbstractFancyListCellRenderer
public DefaultFancyListCellRenderer() {
add(label, BorderLayout.WEST);
add(label);
}
public DefaultFancyListCellRenderer(int padding) {
super(new Insets(padding, padding, padding, padding));
add(label, BorderLayout.WEST);
add(label);
}
protected DefaultFancyListCellRenderer(Object constraint, int padding, int margin, Color selectedBorderColor) {
protected DefaultFancyListCellRenderer(int padding, int margin, Color selectedBorderColor) {
super(new Insets(padding, padding, padding, padding), new Insets(margin, margin, margin, margin), selectedBorderColor);
add(label, constraint);
add(label);
}