Commit 07bda9d1 authored by Florian Schäfer's avatar Florian Schäfer

Move the language preference and the Sophox API URL preference to new tab in JOSM preferences

parent 4b0750fa
......@@ -17,7 +17,7 @@ import org.wikipedia.actions.FetchWikidataAction;
import org.wikipedia.actions.WikipediaAddNamesAction;
import org.wikipedia.actions.WikipediaCopyTemplate;
import org.wikipedia.gui.SophoxDownloadReader;
import org.wikipedia.gui.SophoxServerPreference;
import org.wikipedia.gui.WikiPreferences;
import org.wikipedia.gui.WikidataItemSearchDialog;
import org.wikipedia.gui.WikidataTagCellRenderer;
import org.wikipedia.gui.WikipediaToggleDialog;
......@@ -32,7 +32,7 @@ public final class WikipediaPlugin extends Plugin {
private static String name;
private static String versionInfo;
private PreferenceSetting preferences;
private final PreferenceSetting preferences = new WikiPreferences();
public WikipediaPlugin(PluginInformation info) {
super(info);
......@@ -69,9 +69,6 @@ public final class WikipediaPlugin extends Plugin {
@Override
public PreferenceSetting getPreferenceSetting() {
if (preferences == null) {
preferences = (new SophoxServerPreference.Factory()).createPreferenceSetting();
}
return preferences;
}
}
// License: GPL. For details, see LICENSE file.
package org.wikipedia.gui;
import static org.openstreetmap.josm.tools.I18n.tr;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
import org.openstreetmap.josm.tools.GBC;
import org.wikipedia.io.SophoxDownloadReader;
/**
* Preferences related to Sophox API servers.
*/
public class SophoxServerPreference implements SubPreferenceSetting {
private final HistoryComboBox SophoxServer = new HistoryComboBox();
/**
* Factory used to create a new {@link SophoxServerPreference}.
*/
public static class Factory implements PreferenceSettingFactory {
@Override
public SophoxServerPreference createPreferenceSetting() {
return new SophoxServerPreference();
}
}
@Override
public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
return gui.getServerPreference();
}
@Override
public void addGui(PreferenceTabbedPane gui) {
final JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.add(new JLabel(tr("Server: ")), GBC.std().insets(5, 5, 5, 5));
panel.add(SophoxServer, GBC.eop().fill(GBC.HORIZONTAL));
SophoxServer.setPossibleItems(SophoxDownloadReader.SOPHOX_SERVER_HISTORY.get());
SophoxServer.setText(SophoxDownloadReader.SOPHOX_SERVER.get());
panel.add(Box.createVerticalGlue(), GBC.eol().fill());
getTabPreferenceSetting(gui).addSubTab(this, tr("Wikidata+OSM server"), panel);
}
@Override
public boolean ok() {
SophoxDownloadReader.SOPHOX_SERVER.put(SophoxServer.getText());
SophoxDownloadReader.SOPHOX_SERVER_HISTORY.put(SophoxServer.getHistory());
return false;
}
@Override
public boolean isExpert() {
return true;
}
}
// License: GPL. For details, see LICENSE file.
package org.wikipedia.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.openstreetmap.josm.actions.ExpertToggleAction;
import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
import org.openstreetmap.josm.tools.I18n;
import org.wikipedia.io.SophoxDownloadReader;
import org.wikipedia.tools.WikiProperties;
public class WikiPreferences implements SubPreferenceSetting {
private final JLabel languageLabel = new JLabel(I18n.tr("Wikipedia language"), JLabel.TRAILING);
private final JTextField languageField = new JTextField();
private final JLabel sophoxServerLabel = new JLabel(I18n.tr("Sophox server"), JLabel.TRAILING);
private final HistoryComboBox sophoxServerField = new HistoryComboBox();
public WikiPreferences() {
super();
languageLabel.setToolTipText(I18n.tr("Sets the default language for the Wikipedia articles"));
ExpertToggleAction.addExpertModeChangeListener(isExpert -> {
sophoxServerLabel.setVisible(isExpert);
sophoxServerField.setVisible(isExpert);
}, true);
}
@Override
public void addGui(PreferenceTabbedPane gui) {
final JPanel container = new JPanel(new GridBagLayout());
container.setAlignmentY(JPanel.TOP_ALIGNMENT);
final GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = .1;
constraints.weighty = 0;
constraints.insets = new Insets(5, 10, 5, 10);
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.HORIZONTAL;
container.add(languageLabel, constraints);
constraints.gridx++;
constraints.weightx = 1;
container.add(languageField, constraints);
constraints.gridy++;
constraints.gridx = 0;
constraints.weightx = .1;
container.add(sophoxServerLabel, constraints);
constraints.gridx++;
constraints.weightx = 1;
container.add(sophoxServerField, constraints);
constraints.gridy++;
constraints.weighty = 1;
container.add(Box.createVerticalGlue(), constraints);
languageField.setText(WikiProperties.WIKIPEDIA_LANGUAGE.get());
sophoxServerField.setPossibleItems(SophoxDownloadReader.SOPHOX_SERVER_HISTORY.get());
sophoxServerField.setText(SophoxDownloadReader.SOPHOX_SERVER.get());
getTabPreferenceSetting(gui).addSubTab(this, "Wikipedia", container);
}
@Override
public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
return gui.getPluginPreference();
}
@Override
public boolean isExpert() {
return false;
}
@Override
public boolean ok() {
WikiProperties.WIKIPEDIA_LANGUAGE.put(languageField.getText());
SophoxDownloadReader.SOPHOX_SERVER_HISTORY.put(sophoxServerField.getHistory());
SophoxDownloadReader.SOPHOX_SERVER.put(sophoxServerField.getText());
return false;
}
}
......@@ -31,6 +31,7 @@ import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.WikipediaApp;
import org.wikipedia.data.WikidataEntry;
import org.wikipedia.tools.WikiProperties;
public final class WikidataItemSearchDialog extends ExtendedDialog {
......@@ -128,7 +129,7 @@ public final class WikidataItemSearchDialog extends ExtendedDialog {
debouncer.debounce(getClass(), () -> {
final List<WikidataEntry> entries = query == null || query.isEmpty()
? Collections.emptyList()
: WikipediaApp.getWikidataEntriesForQuery(WikipediaToggleDialog.wikipediaLang.get(), query, Locale.getDefault());
: WikipediaApp.getWikidataEntriesForQuery(WikiProperties.WIKIPEDIA_LANGUAGE.get(), query, Locale.getDefault());
GuiHelper.runInEDT(() -> lsResultModel.setItems(entries));
}, 200, TimeUnit.MILLISECONDS);
}
......
......@@ -11,6 +11,7 @@ import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.gui.ExtendedDialog;
import org.openstreetmap.josm.gui.util.GuiHelper;
import org.wikipedia.WikipediaApp;
import org.wikipedia.tools.WikiProperties;
final class WikipediaCategorySearchDialog extends ExtendedDialog {
......@@ -56,7 +57,7 @@ final class WikipediaCategorySearchDialog extends ExtendedDialog {
debouncer.debounce(getClass(), () -> {
final List<String> entries = query == null || query.isEmpty()
? Collections.emptyList()
: WikipediaApp.forLanguage(WikipediaToggleDialog.wikipediaLang.get()).getCategoriesForPrefix(query);
: WikipediaApp.forLanguage(WikiProperties.WIKIPEDIA_LANGUAGE.get()).getCategoriesForPrefix(query);
GuiHelper.runInEDT(() -> lsResultModel.setItems(entries));
}, 200, TimeUnit.MILLISECONDS);
}
......
......@@ -20,7 +20,6 @@ import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingWorker;
import org.openstreetmap.josm.Main;
......@@ -36,7 +35,6 @@ import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
import org.openstreetmap.josm.data.osm.search.SearchMode;
import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
import org.openstreetmap.josm.data.preferences.StringProperty;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MapView;
import org.openstreetmap.josm.gui.SideButton;
......@@ -45,7 +43,6 @@ import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.LanguageInfo;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.OpenBrowser;
import org.wikipedia.WikipediaApp;
......@@ -54,11 +51,20 @@ import org.wikipedia.actions.MultiAction;
import org.wikipedia.actions.ToggleWikiLayerAction;
import org.wikipedia.data.WikipediaEntry;
import org.wikipedia.tools.ListUtil;
import org.wikipedia.tools.WikiProperties;
public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerChangeListener, DataSetListenerAdapter.Listener {
public WikipediaToggleDialog() {
super(tr("Wikipedia"), "wikipedia", tr("Fetch Wikipedia articles with coordinates"), null, 150);
super(
tr("Wikipedia"),
"wikipedia",
tr("Fetch Wikipedia articles with coordinates"),
null,
150,
true,
WikiPreferences.class
);
final Action[] downloadActions = {
new WikipediaLoadCoordinatesAction(false),
......@@ -70,19 +76,17 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
MultiAction.createButton(
I18n.tr("Download elements"),
"download",
I18n.tr("Download all elements in the current viewport from one of {0} sources", downloadActions.length),
I18n.tr("Download all elements from one of {0} sources", downloadActions.length),
downloadActions
),
new SideButton(new PasteWikipediaArticlesAction()),
new SideButton(new AddWikipediaTagAction(list)),
new SideButton(new WikipediaSettingsAction(), false)
new SideButton(new AddWikipediaTagAction(list))
));
updateTitle();
}
/** A string describing the context (use-case) for determining the dialog title */
String titleContext = null;
static final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2));
final Set<String> articles = new HashSet<>();
final DefaultListModel<WikipediaEntry> model = new DefaultListModel<>();
final JList<WikipediaEntry> list = new JList<WikipediaEntry>(model) {
......@@ -152,7 +156,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
try {
return list.getModel().getElementAt(0).lang;
} catch (ArrayIndexOutOfBoundsException ignore) {
return wikipediaLang.get();
return WikiProperties.WIKIPEDIA_LANGUAGE.get();
}
}
......@@ -183,7 +187,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
@Override
List<WikipediaEntry> getEntries() {
return WikipediaApp.forLanguage(wikidata ? "wikidata" : wikipediaLang.get())
return WikipediaApp.forLanguage(wikidata ? "wikidata" : WikiProperties.WIKIPEDIA_LANGUAGE.get())
.getEntriesFromCoordinates(min, max);
}
}.execute();
......@@ -245,7 +249,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
new UpdateWikipediaArticlesSwingWorker() {
@Override
List<WikipediaEntry> getEntries() {
return WikipediaApp.forLanguage(wikipediaLang.get())
return WikipediaApp.forLanguage(WikiProperties.WIKIPEDIA_LANGUAGE.get())
.getEntriesFromCategory(category, Main.pref.getInt("wikipedia.depth", 3));
}
}.execute();
......@@ -268,7 +272,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
@Override
List<WikipediaEntry> getEntries() {
return WikipediaApp.getEntriesFromClipboard(wikipediaLang.get());
return WikipediaApp.getEntriesFromClipboard(WikiProperties.WIKIPEDIA_LANGUAGE.get());
}
}.execute();
}
......@@ -292,28 +296,6 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
}
}
class WikipediaSettingsAction extends AbstractAction {
WikipediaSettingsAction() {
super(tr("Language"));
new ImageProvider("dialogs/settings").getResource().attachImageIcon(this, true);
putValue(SHORT_DESCRIPTION, tr("Sets the default language for the Wikipedia articles"));
}
@Override
public void actionPerformed(ActionEvent e) {
String lang = JOptionPane.showInputDialog(
Main.parent,
tr("Enter the Wikipedia language"),
wikipediaLang.get());
if (lang != null) {
wikipediaLang.put(lang);
updateTitle();
updateWikipediaArticles();
}
}
}
static class AddWikipediaTagAction extends AbstractAction {
private final JList<WikipediaEntry> list;
......
......@@ -3,11 +3,15 @@ package org.wikipedia.tools;
import org.openstreetmap.josm.data.preferences.DoubleProperty;
import org.openstreetmap.josm.data.preferences.StringProperty;
import org.openstreetmap.josm.tools.LanguageInfo;
public final class WikiProperties {
public static final DoubleProperty WIKI_LAYER_MARKER_HEIGHT = new DoubleProperty("wikipedia.layer.marker_height", 30.0);
public static final StringProperty WIKIPEDIA_LANGUAGE = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2));
private WikiProperties() {
// Private constructor to avoid instantiation
}
......
......@@ -11,39 +11,21 @@ import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
import org.openstreetmap.josm.gui.preferences.server.ServerAccessPreference;
import org.openstreetmap.josm.testutils.JOSMTestRules;
/**
* Unit tests of {@link SophoxServerPreference} class.
*/
public class SophoxServerPreferenceTest {
public class WikiPreferencesTest {
/**
* Setup tests
*/
@Rule
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public JOSMTestRules test = new JOSMTestRules().preferences().platform();
public JOSMTestRules test = new JOSMTestRules().platform();
/**
* Unit test of {@link SophoxServerPreference}.
*/
@Test
public void testSophoxServerPreference() {
assertNotNull(new SophoxServerPreference.Factory().createPreferenceSetting());
}
/**
* Unit test of {@link SophoxServerPreference#addGui}.
*/
@Test
public void testAddGui() {
final SophoxServerPreference setting = new SophoxServerPreference.Factory().createPreferenceSetting();
final WikiPreferences setting = new WikiPreferences();
final PreferenceTabbedPane tabPane = new PreferenceTabbedPane();
tabPane.buildGui();
int tabs = tabPane.getSetting(ServerAccessPreference.class).getTabPane().getTabCount();
int tabs = tabPane.getPluginPreference().getTabPane().getTabCount();
setting.addGui(tabPane);
assertEquals(tabs + 1, tabPane.getSetting(ServerAccessPreference.class).getTabPane().getTabCount());
assertEquals(tabPane.getSetting(ServerAccessPreference.class), setting.getTabPreferenceSetting(tabPane));
assertEquals(tabs + 1, tabPane.getPluginPreference().getTabPane().getTabCount());
assertEquals(tabPane.getPluginPreference(), setting.getTabPreferenceSetting(tabPane));
setting.ok();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment