* allow "unselection" of subtitle options

This commit is contained in:
Reinhard Pointner 2014-04-05 16:25:38 +00:00
parent 9cbef475c6
commit d8c56c637a
1 changed files with 56 additions and 28 deletions

View File

@ -52,6 +52,7 @@ import javax.swing.JTable;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.SwingWorker.StateValue; import javax.swing.SwingWorker.StateValue;
import javax.swing.border.Border; import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableCellRenderer;
@ -63,6 +64,7 @@ import net.sourceforge.filebot.similarity.MetricCascade;
import net.sourceforge.filebot.similarity.SimilarityMetric; import net.sourceforge.filebot.similarity.SimilarityMetric;
import net.sourceforge.filebot.subtitle.SubtitleNaming; import net.sourceforge.filebot.subtitle.SubtitleNaming;
import net.sourceforge.filebot.util.ui.AbstractBean; import net.sourceforge.filebot.util.ui.AbstractBean;
import net.sourceforge.filebot.util.ui.DashedSeparator;
import net.sourceforge.filebot.util.ui.EmptySelectionModel; import net.sourceforge.filebot.util.ui.EmptySelectionModel;
import net.sourceforge.filebot.util.ui.LinkButton; import net.sourceforge.filebot.util.ui.LinkButton;
import net.sourceforge.filebot.util.ui.RoundBorder; import net.sourceforge.filebot.util.ui.RoundBorder;
@ -127,7 +129,7 @@ class SubtitleAutoMatchDialog extends JDialog {
table.setFillsViewportHeight(true); table.setFillsViewportHeight(true);
JComboBox editor = new SimpleComboBox(); JComboBox editor = new SimpleComboBox();
editor.setRenderer(new SubtitleOptionRenderer()); editor.setRenderer(new SubtitleOptionRenderer(true));
// disable selection // disable selection
table.setSelectionModel(new EmptySelectionModel()); table.setSelectionModel(new EmptySelectionModel());
@ -138,11 +140,13 @@ class SubtitleAutoMatchDialog extends JDialog {
@Override @Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JComboBox editor = (JComboBox) super.getTableCellEditorComponent(table, value, isSelected, row, column); JComboBox editor = (JComboBox) super.getTableCellEditorComponent(table, value, isSelected, row, column);
SubtitleMapping mapping = (SubtitleMapping) value; SubtitleMapping mapping = (SubtitleMapping) value;
editor.setModel(new DefaultComboBoxModel(mapping.getOptions()));
editor.setSelectedItem(mapping.getSelectedOption());
DefaultComboBoxModel model = new DefaultComboBoxModel(mapping.getOptions());
model.addElement(null); // option to cancel selection
editor.setModel(model);
editor.setSelectedItem(mapping.getSelectedOption());
return editor; return editor;
} }
}); });
@ -347,7 +351,7 @@ class SubtitleAutoMatchDialog extends JDialog {
private final JComboBox optionComboBox = new SimpleComboBox(); private final JComboBox optionComboBox = new SimpleComboBox();
public SubtitleMappingOptionRenderer() { public SubtitleMappingOptionRenderer() {
optionComboBox.setRenderer(new SubtitleOptionRenderer()); optionComboBox.setRenderer(new SubtitleOptionRenderer(false));
} }
@Override @Override
@ -356,7 +360,7 @@ class SubtitleAutoMatchDialog extends JDialog {
SubtitleDescriptorBean subtitleBean = mapping.getSelectedOption(); SubtitleDescriptorBean subtitleBean = mapping.getSelectedOption();
// render combobox for subtitle options // render combobox for subtitle options
if (mapping.isEditable()) { if (mapping.isEditable() && subtitleBean != null) {
optionComboBox.setModel(new DefaultComboBoxModel(new Object[] { subtitleBean })); optionComboBox.setModel(new DefaultComboBoxModel(new Object[] { subtitleBean }));
return optionComboBox; return optionComboBox;
} }
@ -366,10 +370,17 @@ class SubtitleAutoMatchDialog extends JDialog {
setForeground(table.getForeground()); setForeground(table.getForeground());
if (subtitleBean == null) { if (subtitleBean == null) {
if (mapping.getOptions().length == 0) {
// no subtitles found // no subtitles found
setText("No subtitles found"); setText("No subtitles found");
setIcon(null); setIcon(null);
setForeground(Color.gray); setForeground(Color.gray);
} else {
// no subtitles found
setText("No subtitles selected");
setIcon(null);
setForeground(Color.gray);
}
} else if (subtitleBean.getState() == StateValue.PENDING) { } else if (subtitleBean.getState() == StateValue.PENDING) {
// download in the queue // download in the queue
setText(subtitleBean.getText()); setText(subtitleBean.getText());
@ -394,6 +405,11 @@ class SubtitleAutoMatchDialog extends JDialog {
private static class SubtitleOptionRenderer extends DefaultListCellRenderer { private static class SubtitleOptionRenderer extends DefaultListCellRenderer {
private final Border padding = createEmptyBorder(3, 3, 3, 3); private final Border padding = createEmptyBorder(3, 3, 3, 3);
private final boolean isEditor;
public SubtitleOptionRenderer(boolean isEditor) {
this.isEditor = isEditor;
}
@Override @Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
@ -401,6 +417,16 @@ class SubtitleAutoMatchDialog extends JDialog {
setBorder(padding); setBorder(padding);
SubtitleDescriptorBean subtitleBean = (SubtitleDescriptorBean) value; SubtitleDescriptorBean subtitleBean = (SubtitleDescriptorBean) value;
if (isEditor && index == list.getModel().getSize() - 2) {
setBorder(new CompoundBorder(new DashedSeparator(10, 4, Color.lightGray, list.getBackground()), getBorder())); // this element is always the last one
}
if (value == null) {
setText("Cancel selection");
setIcon(ResourceManager.getIcon("dialog.cancel"));
setToolTipText(null);
} else {
setText(subtitleBean.getText()); setText(subtitleBean.getText());
if (subtitleBean.getError() == null) { if (subtitleBean.getError() == null) {
@ -422,6 +448,7 @@ class SubtitleAutoMatchDialog extends JDialog {
setBackground(derive(Color.RED, (1 - f) * 0.5f)); setBackground(derive(Color.RED, (1 - f) * 0.5f));
} }
} }
}
return this; return this;
} }
@ -570,7 +597,7 @@ class SubtitleAutoMatchDialog extends JDialog {
} }
public boolean isEditable() { public boolean isEditable() {
return subtitleFile == null && selectedOption != null && (selectedOption.getState() == null || selectedOption.getError() != null); return subtitleFile == null && options.size() > 0 && (selectedOption == null || (selectedOption.getState() == null || selectedOption.getError() != null));
} }
public SubtitleDescriptorBean getSelectedOption() { public SubtitleDescriptorBean getSelectedOption() {
@ -583,8 +610,9 @@ class SubtitleAutoMatchDialog extends JDialog {
} }
this.selectedOption = selectedOption; this.selectedOption = selectedOption;
if (this.selectedOption != null) {
this.selectedOption.addPropertyChangeListener(selectedOptionListener); this.selectedOption.addPropertyChangeListener(selectedOptionListener);
}
firePropertyChange("selectedOption", null, this.selectedOption); firePropertyChange("selectedOption", null, this.selectedOption);
} }