Commit 268dee75 authored by Florian Schäfer's avatar Florian Schäfer

Populate tab for sitelinks in the WikidataInfoToggleDialog

parent 9bb3e5a2
package org.wikipedia.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Comparator;
import java.util.Optional;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.OpenBrowser;
import org.wikipedia.WikipediaPlugin;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.WikidataActionApiQuery;
import org.wikipedia.api.wikidata_action.json.SitematrixResult;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
import org.wikipedia.data.WikipediaSite;
public class WikidataInfoSitelinkPanel extends ProgressJPanel {
private final JPanel mainPanel;
private String qIdBeingDownloaded;
WikidataInfoSitelinkPanel() {
this.mainPanel = new JPanel();
this.mainPanel.setLayout(new GridBagLayout());
add(new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
}
void downloadSitelinksFor(final String qId) {
this.qIdBeingDownloaded = qId;
new Thread(() -> {
try {
mainPanel.removeAll();
showProgress(I18n.tr("Download sitelinks for {0}…", qId));
final Collection<WbgetentitiesResult.Entity.Sitelink> sitelinks =
ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesSitelinks(qId));
final SitematrixResult.Sitematrix sitematrix = ApiQueryClient.query(WikidataActionApiQuery.sitematrix());
if (qIdBeingDownloaded != null && qIdBeingDownloaded.equals(qId)) {
synchronized (mainPanel) {
mainPanel.removeAll();
final GBC gbc = GBC.std().fill().grid(0, 0);
sitelinks.stream()
.sorted(Comparator.comparing(WbgetentitiesResult.Entity.Sitelink::getSite))
.forEach(sitelink -> {
final Optional<SitematrixResult.Sitematrix.Site> site = sitematrix.getSiteForDbname(sitelink.getSite());
if (!site.isPresent()) {
Logging.warn("Could not find site {0} for sitelink {1}!", sitelink.getSite(), sitelink.getTitle());
}
site.ifPresent(s -> {
final JButton linkButton = new JButton();
linkButton.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
final String uri = s.getUrl() + "/w/index.php?title=" + URLEncoder.encode(sitelink.getTitle());
final String error = OpenBrowser.displayUrl(uri);
if (error != null) {
new Notification(I18n.tr("Can't open website {0} in browser! Error message: {1}", uri, error))
.setIcon(WikipediaPlugin.W_IMAGE.get())
.show();
}
}
});
linkButton.setText(
s.getDbName() + ": " + s.getSiteName() + (s.getLanguage() == null ? "" : " " + s.getLanguage().getName())
);
mainPanel.add(linkButton, gbc);
gbc.gridy++;
});
});
hideProgress();
}
}
} catch (IOException e) {
new Notification(I18n.tr("Failed to download sitelinks for Wikidata item {0}!", qId))
.setIcon(WikipediaPlugin.W_IMAGE.get())
.show();
}
hideProgress();
}).start();
}
}
......@@ -55,8 +55,7 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
private final JTabbedPane tabs = new JTabbedPane();
private final WikidataInfoLabelPanel labelTab = new WikidataInfoLabelPanel();
private final WikidataInfoClaimPanel statementTab = new WikidataInfoClaimPanel();
private final JPanel linkTab = new JPanel();
private final JButton webLinkButton = new JButton();
private final WikidataInfoSitelinkPanel linkTab = new WikidataInfoSitelinkPanel();
private final DataSelectionListener selectionListener = it -> updateDisplayedItem();
private final DataSetListener datasetListener = new DataSetListenerAdapter(it -> {
......@@ -96,8 +95,6 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
infoPanel.add(basicInfoPanel, BorderLayout.NORTH);
infoPanel.add(tabs, BorderLayout.CENTER);
linkTab.add(webLinkButton);
tabs.add(I18n.tr("Statements"), statementTab);
tabs.add(I18n.tr("Labels"), labelTab);
tabs.add(I18n.tr("Links"), linkTab);
......@@ -132,6 +129,9 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
* It checks for the currently selected items in the active dataset and in the Wikidata list. The panel is updated.
*/
private void updateDisplayedItem() {
if (!isShowing()) {
return;
}
final DataSet dataset = MainApplication.getLayerManager().getActiveDataSet();
final Map<String, Integer> wdTagsInDataset =
dataset == null
......@@ -171,22 +171,8 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
setDisplayedItem(qId);
labelTab.downloadLabelsFor(qId);
statementTab.downloadStatementsFor(qId);
webLinkButton.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
final String uri = "https://www.wikidata.org/wiki/" + qId;
final String error = OpenBrowser.displayUrl(uri);
if (error != null) {
new Notification(I18n.tr("Can't open website {0} in browser! Error message: {1}", uri, error))
.setIcon(WikipediaPlugin.W_IMAGE.get())
.show();
}
}
});
webLinkButton.setText(I18n.tr("Open item {0} in browser", qId));
linkTab.downloadSitelinksFor(qId);
mainPanel.add(infoPanel);
mainPanel.revalidate();
......@@ -213,4 +199,10 @@ public class WikidataInfoToggleDialog extends ToggleDialog {
private String getDisplayedItem() {
return displayedItem;
}
@Override
protected void stateChanged() {
super.stateChanged();
updateDisplayedItem();
}
}
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