Commit adeaa86a authored by Florian Schäfer's avatar Florian Schäfer

Make the label tab of WikidataInfoToggleDialog work

parent e4d84de7
Pipeline #27299554 passed with stages
in 7 minutes and 52 seconds
......@@ -3,10 +3,6 @@ package org.wikipedia.api;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openstreetmap.josm.tools.Logging;
public class ApiUrl {
......
package org.wikipedia.gui;
import java.awt.BorderLayout;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.Pair;
import org.wikipedia.WikipediaPlugin;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.WikidataActionApiQuery;
/**
* Panel displaying the labels for a Wikidata item
*/
class WikidataInfoLabelPanel extends JPanel {
private static final JProgressBar downloadProgress = new JProgressBar();
private final LabelTableModel tableModel = new LabelTableModel(this);
static {
downloadProgress.setStringPainted(true);
}
WikidataInfoLabelPanel() {
setLayout(new BorderLayout());
add(new JTable(tableModel), BorderLayout.CENTER);
}
void clear() {
tableModel.clear();
}
void downloadLabelsFor(final String qId) {
tableModel.downloadLabelsFor(qId);
}
private synchronized void showDownloadProgress(final String qId) {
if (!isAncestorOf(downloadProgress)) {
add(downloadProgress, BorderLayout.NORTH);
}
downloadProgress.setIndeterminate(true);
downloadProgress.setString(I18n.tr("Download labels for {0}…", qId));
revalidate();
}
private synchronized void hideDownloadProgress() {
remove(downloadProgress);
revalidate();
}
private static class LabelTableModel extends AbstractTableModel {
private final WikidataInfoLabelPanel parent;
private String qIdBeingDownloaded;
private final List<Pair<String, String>> valueMap = new ArrayList<>();
LabelTableModel(final WikidataInfoLabelPanel parent) {
this.parent = parent;
}
void clear() {
this.valueMap.clear();
}
void downloadLabelsFor(final String qId) {
synchronized (valueMap) {
qIdBeingDownloaded = qId;
parent.showDownloadProgress(qId);
}
new Thread(() -> {
try {
final Map<String, String> newValues = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels(qId));
synchronized (valueMap) {
if (qIdBeingDownloaded != null && qIdBeingDownloaded.equals(qId)) {
valueMap.clear();
valueMap.addAll(
newValues.entrySet().stream()
.map(it -> Pair.create(it.getKey(), it.getValue()))
.sorted((x, y) -> x.a.compareTo(y.a))
.collect(Collectors.toList())
);
}
}
} catch (IOException e) {
new Notification(I18n.tr("Failed to download labels for {0}!", qId)).setIcon(WikipediaPlugin.W_IMAGE.get()).show();
}
parent.hideDownloadProgress();
}).start();
}
@Override
public int getRowCount() {
return valueMap.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return columnIndex == 0 ? valueMap.get(rowIndex).a : valueMap.get(rowIndex).b;
}
@Override
public String getColumnName(int column) {
return column == 0 ? "language" : "label";
}
}
}
......@@ -55,7 +55,7 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
private final JPanel infoPanel = new JPanel(new BorderLayout());
private final JTabbedPane tabs = new JTabbedPane();
private final JPanel labelTab = new JPanel();
private final WikidataInfoLabelPanel labelTab = new WikidataInfoLabelPanel();
private final JPanel statementTab = new JPanel();
private final JPanel linkTab = new JPanel();
private final JButton webLinkButton = new JButton();
......@@ -190,6 +190,9 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
descriptionLabel.setText(description);
setDisplayedItem(qId);
labelTab.clear();
labelTab.downloadLabelsFor(qId);
webLinkButton.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
......
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