* added language selection combobox in episodelist panel

This commit is contained in:
Reinhard Pointner 2011-11-04 03:04:49 +00:00
parent 0e885c12f8
commit 15b90ebf73
11 changed files with 176 additions and 150 deletions

View File

@ -71,7 +71,7 @@ public abstract class AbstractSearchPanel<S, E> extends JComponent {
setLayout(new MigLayout("nogrid, fill, insets 10px 10px 15px 10px", "align center", "[pref!]10px[fill]"));
add(searchTextField, "gapafter indent");
add(searchTextField);
add(new JButton(searchAction), "gap 18px, wrap");
add(tabbedPaneGroup, "grow");

View File

@ -0,0 +1,126 @@
package net.sourceforge.filebot.ui;
import static java.awt.event.ItemEvent.*;
import static net.sourceforge.filebot.ui.Language.*;
import static net.sourceforge.filebot.ui.LanguageComboBoxModel.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.AbstractList;
import java.util.Locale;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import net.sourceforge.filebot.Settings;
import net.sourceforge.tuned.PreferencesList;
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
public class LanguageComboBox extends JComboBox {
private final PreferencesEntry<String> persistentSelectedLanguage;
private final PreferencesList<String> persistentFavoriteLanguages;
public LanguageComboBox(JComponent parent, Language initialSelection) {
super(new LanguageComboBoxModel(initialSelection != ALL_LANGUAGES, initialSelection));
setRenderer(new LanguageComboBoxCellRenderer(super.getRenderer()));
persistentSelectedLanguage = Settings.forPackage(parent.getClass()).entry("language.selected");
persistentFavoriteLanguages = Settings.forPackage(parent.getClass()).node("language.favorites").asList();
// restore selected language
getModel().setSelectedItem(Language.getLanguage(persistentSelectedLanguage.getValue()));
// restore favorite languages
for (String favoriteLanguage : persistentFavoriteLanguages) {
getModel().favorites().add(getModel().favorites().size(), getLanguage(favoriteLanguage));
}
// guess favorite languages
if (getModel().favorites().isEmpty()) {
for (Locale locale : new Locale[] { Locale.getDefault(), Locale.ENGLISH }) {
getModel().favorites().add(getLanguage(locale.getLanguage()));
}
}
// update favorites on change
addPopupMenuListener(new PopupSelectionListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Language language = (Language) e.getItem();
if (getModel().favorites().add(language)) {
persistentFavoriteLanguages.set(new AbstractList<String>() {
@Override
public String get(int index) {
return getModel().favorites().get(index).getCode();
}
@Override
public int size() {
return getModel().favorites().size();
}
});
}
persistentSelectedLanguage.setValue(language.getCode());
}
});
}
@Override
public LanguageComboBoxModel getModel() {
return (LanguageComboBoxModel) super.getModel();
}
private static class PopupSelectionListener implements PopupMenuListener, ItemListener {
private Object selected = null;
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
// selected item before popup
selected = comboBox.getSelectedItem();
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
// check selected item after popup
if (selected != comboBox.getSelectedItem()) {
itemStateChanged(new ItemEvent(comboBox, ITEM_STATE_CHANGED, comboBox.getSelectedItem(), SELECTED));
}
selected = null;
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
selected = null;
}
@Override
public void itemStateChanged(ItemEvent e) {
}
}
}

View File

@ -1,5 +1,5 @@
package net.sourceforge.filebot.ui.subtitle;
package net.sourceforge.filebot.ui;
import java.awt.Color;
@ -13,10 +13,10 @@ import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.ui.Language;
import net.sourceforge.tuned.ui.DashedSeparator;
class LanguageComboBoxCellRenderer implements ListCellRenderer {
public class LanguageComboBoxCellRenderer implements ListCellRenderer {
private Border padding = new EmptyBorder(2, 2, 2, 2);

View File

@ -1,5 +1,5 @@
package net.sourceforge.filebot.ui.subtitle;
package net.sourceforge.filebot.ui;
import static net.sourceforge.filebot.ui.Language.*;
@ -12,29 +12,35 @@ import java.util.Set;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import net.sourceforge.filebot.ui.Language;
class LanguageComboBoxModel extends AbstractListModel implements ComboBoxModel {
public class LanguageComboBoxModel extends AbstractListModel implements ComboBoxModel {
public static final Language ALL_LANGUAGES = new Language("undefined", "All Languages");
private Language selection = ALL_LANGUAGES;
private boolean requireSpecificLanguage;
private Language selection;
private List<Language> favorites = new Favorites(2);
private List<Language> values = availableLanguages();
public LanguageComboBoxModel(boolean requireSpecificLanguage, Language initialSelection) {
this.requireSpecificLanguage = requireSpecificLanguage;
this.selection = initialSelection;
}
@Override
public Language getElementAt(int index) {
// "All Languages"
if (index == 0) {
if (!requireSpecificLanguage) {
if (index == 0)
return ALL_LANGUAGES;
}
// "All Languages" offset
index -= 1;
}
if (index < favorites.size()) {
return favorites.get(index);
@ -50,7 +56,7 @@ class LanguageComboBoxModel extends AbstractListModel implements ComboBoxModel {
@Override
public int getSize() {
// "All Languages" : favorites[] : values[]
return 1 + favorites.size() + values.size();
return (requireSpecificLanguage ? 0 : 1) + favorites.size() + values.size();
}
@ -74,7 +80,7 @@ class LanguageComboBoxModel extends AbstractListModel implements ComboBoxModel {
protected int convertFavoriteIndexToModel(int favoriteIndex) {
return 1 + favoriteIndex;
return (requireSpecificLanguage ? 0 : 1) + favoriteIndex;
}

View File

@ -15,6 +15,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import javax.swing.AbstractAction;
import javax.swing.Icon;
@ -29,6 +30,8 @@ import net.sourceforge.filebot.ui.AbstractSearchPanel;
import net.sourceforge.filebot.ui.FileBotList;
import net.sourceforge.filebot.ui.FileBotListExportHandler;
import net.sourceforge.filebot.ui.FileBotTab;
import net.sourceforge.filebot.ui.Language;
import net.sourceforge.filebot.ui.LanguageComboBox;
import net.sourceforge.filebot.ui.SelectDialog;
import net.sourceforge.filebot.ui.transfer.ArrayTransferable;
import net.sourceforge.filebot.ui.transfer.ClipboardHandler;
@ -48,6 +51,7 @@ import net.sourceforge.tuned.ui.TunedUtilities;
public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, Episode> {
private SeasonSpinnerModel seasonSpinnerModel = new SeasonSpinnerModel();
private LanguageComboBox languageComboBox = new LanguageComboBox(this, Language.getLanguage("en"));
public EpisodeListPanel() {
@ -61,7 +65,8 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, E
seasonSpinner.setMinimumSize(seasonSpinner.getPreferredSize());
// add after text field
add(seasonSpinner, 1);
add(seasonSpinner, "gap indent", 1);
add(languageComboBox, "gap indent+5", 2);
// add after tabbed pane
tabbedPaneGroup.add(new JButton(new SaveAction(new SelectedTabExportHandler())));
@ -96,8 +101,9 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, E
EpisodeListProvider provider = searchTextField.getSelectButton().getSelectedValue();
String text = searchTextField.getText().trim();
int season = seasonSpinnerModel.getSeason();
Locale language = languageComboBox.getModel().getSelectedItem().toLocale();
return new EpisodeListRequestProcessor(new EpisodeListRequest(provider, text, season));
return new EpisodeListRequestProcessor(new EpisodeListRequest(provider, text, season, language));
};
@ -176,12 +182,14 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, E
private final EpisodeListProvider provider;
private final int season;
private final Locale language;
public EpisodeListRequest(EpisodeListProvider provider, String searchText, int season) {
public EpisodeListRequest(EpisodeListProvider provider, String searchText, int season, Locale language) {
super(searchText);
this.provider = provider;
this.season = season;
this.language = language;
}
@ -194,6 +202,10 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, E
return season;
}
public Locale getLanguage() {
return language;
}
}
@ -206,16 +218,16 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, E
@Override
public Collection<SearchResult> search() throws Exception {
return request.getProvider().search(request.getSearchText());
return request.getProvider().search(request.getSearchText(), request.getLanguage());
}
@Override
public Collection<Episode> fetch() throws Exception {
if (request.getSeason() != ALL_SEASONS)
return request.getProvider().getEpisodeList(getSearchResult(), request.getSeason());
return request.getProvider().getEpisodeList(getSearchResult(), request.getSeason(), request.getLanguage());
else
return request.getProvider().getEpisodeList(getSearchResult());
return request.getProvider().getEpisodeList(getSearchResult(), request.getLanguage());
}

View File

@ -1,52 +0,0 @@
package net.sourceforge.filebot.ui.subtitle;
import static java.awt.event.ItemEvent.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
class PopupSelectionListener implements PopupMenuListener, ItemListener {
private Object selected = null;
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
// selected item before popup
selected = comboBox.getSelectedItem();
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
// check selected item after popup
if (selected != comboBox.getSelectedItem()) {
itemStateChanged(new ItemEvent(comboBox, ITEM_STATE_CHANGED, comboBox.getSelectedItem(), SELECTED));
}
selected = null;
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
selected = null;
}
@Override
public void itemStateChanged(ItemEvent e) {
}
}

View File

@ -16,6 +16,7 @@ import net.miginfocom.swing.MigLayout;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.vfs.ArchiveType;
import net.sourceforge.tuned.ui.AbstractFancyListCellRenderer;
import net.sourceforge.tuned.ui.DashedSeparator;
class SubtitlePackageCellRenderer extends AbstractFancyListCellRenderer {

View File

@ -2,101 +2,46 @@
package net.sourceforge.filebot.ui.subtitle;
import static net.sourceforge.filebot.ui.Language.*;
import static net.sourceforge.filebot.ui.subtitle.LanguageComboBoxModel.*;
import static net.sourceforge.filebot.ui.LanguageComboBoxModel.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ItemEvent;
import java.awt.geom.Path2D;
import java.net.URI;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import javax.swing.Icon;
import javax.swing.JComboBox;
import net.sourceforge.filebot.Analytics;
import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.WebServices;
import net.sourceforge.filebot.ui.AbstractSearchPanel;
import net.sourceforge.filebot.ui.Language;
import net.sourceforge.filebot.ui.LanguageComboBox;
import net.sourceforge.filebot.ui.SelectDialog;
import net.sourceforge.filebot.web.SearchResult;
import net.sourceforge.filebot.web.SubtitleDescriptor;
import net.sourceforge.filebot.web.SubtitleProvider;
import net.sourceforge.filebot.web.VideoHashSubtitleService;
import net.sourceforge.tuned.PreferencesList;
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
import net.sourceforge.tuned.ui.LabelProvider;
import net.sourceforge.tuned.ui.SimpleLabelProvider;
public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, SubtitlePackage> {
private final LanguageComboBoxModel languageModel = new LanguageComboBoxModel();
private static final PreferencesEntry<String> persistentSelectedLanguage = Settings.forPackage(SubtitlePanel.class).entry("language.selected");
private static final PreferencesList<String> persistentFavoriteLanguages = Settings.forPackage(SubtitlePanel.class).node("language.favorites").asList();
private LanguageComboBox languageComboBox = new LanguageComboBox(this, ALL_LANGUAGES);
public SubtitlePanel() {
historyPanel.setColumnHeader(0, "Show / Movie");
historyPanel.setColumnHeader(1, "Number of Subtitles");
JComboBox languageComboBox = new JComboBox(languageModel);
languageComboBox.setRenderer(new LanguageComboBoxCellRenderer(languageComboBox.getRenderer()));
// restore selected language
languageModel.setSelectedItem(Language.getLanguage(persistentSelectedLanguage.getValue()));
// restore favorite languages
for (String favoriteLanguage : persistentFavoriteLanguages) {
languageModel.favorites().add(languageModel.favorites().size(), getLanguage(favoriteLanguage));
}
// guess favorite languages
if (languageModel.favorites().isEmpty()) {
for (Locale locale : new Locale[] { Locale.getDefault(), Locale.ENGLISH }) {
languageModel.favorites().add(getLanguage(locale.getLanguage()));
}
}
// update favorites on change
languageComboBox.addPopupMenuListener(new PopupSelectionListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Language language = (Language) e.getItem();
if (languageModel.favorites().add(language)) {
persistentFavoriteLanguages.set(new AbstractList<String>() {
@Override
public String get(int index) {
return languageModel.favorites().get(index).getCode();
}
@Override
public int size() {
return languageModel.favorites().size();
}
});
}
persistentSelectedLanguage.setValue(language.getCode());
}
});
// add after text field
add(languageComboBox, 1);
add(languageComboBox, "gap indent", 1);
// add at the top right corner
add(dropTarget, "width 1.6cm!, height 1.2cm!, pos n 0% 100% n", 0);
@ -114,7 +59,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
@Override
public String getQueryLanguage() {
// use currently selected language for drop target
return languageModel.getSelectedItem() == ALL_LANGUAGES ? null : languageModel.getSelectedItem().getName();
return languageComboBox.getModel().getSelectedItem() == ALL_LANGUAGES ? null : languageComboBox.getModel().getSelectedItem().getName();
}
@ -165,7 +110,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
protected SubtitleRequestProcessor createRequestProcessor() {
SubtitleProvider provider = searchTextField.getSelectButton().getSelectedValue();
String text = searchTextField.getText().trim();
Language language = languageModel.getSelectedItem();
Language language = languageComboBox.getModel().getSelectedItem();
return new SubtitleRequestProcessor(new SubtitleRequest(provider, text, language));
}

View File

@ -22,19 +22,16 @@ public abstract class AbstractEpisodeListProvider implements EpisodeListProvider
}
@Override
public List<SearchResult> search(String query) throws Exception {
return search(query, Locale.ENGLISH);
}
@Override
public List<Episode> getEpisodeList(SearchResult searchResult) throws Exception {
return getEpisodeList(searchResult, Locale.ENGLISH);
}
@Override
public List<Episode> getEpisodeList(SearchResult searchResult, int season) throws Exception {
return getEpisodeList(searchResult, season, Locale.ENGLISH);
}

View File

@ -23,18 +23,9 @@ public interface EpisodeListProvider {
public boolean hasLocaleSupport();
public List<SearchResult> search(String query) throws Exception;
public List<SearchResult> search(String query, Locale locale) throws Exception;
public List<Episode> getEpisodeList(SearchResult searchResult) throws Exception;
public List<Episode> getEpisodeList(SearchResult searchResult, int season) throws Exception;
public List<Episode> getEpisodeList(SearchResult searchResult, Locale locale) throws Exception;

View File

@ -1,5 +1,5 @@
package net.sourceforge.filebot.ui.subtitle;
package net.sourceforge.tuned.ui;
import static java.awt.BasicStroke.*;
@ -15,7 +15,7 @@ import java.awt.Insets;
import javax.swing.border.Border;
class DashedSeparator implements Border {
public class DashedSeparator implements Border {
private final int height;
private final int dash;