001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.changeset; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Color; 007import java.awt.FlowLayout; 008import java.awt.event.ActionEvent; 009import java.util.Collections; 010 011import javax.swing.AbstractAction; 012import javax.swing.BorderFactory; 013import javax.swing.JLabel; 014import javax.swing.JPanel; 015import javax.swing.event.DocumentEvent; 016import javax.swing.event.DocumentListener; 017 018import org.openstreetmap.josm.Main; 019import org.openstreetmap.josm.gui.SideButton; 020import org.openstreetmap.josm.gui.widgets.ChangesetIdTextField; 021import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 022import org.openstreetmap.josm.io.OnlineResource; 023 024/** 025 * This panel allows to enter the ID of single changeset and to download 026 * the respective changeset. 027 * 028 */ 029public class SingleChangesetDownloadPanel extends JPanel { 030 031 private ChangesetIdTextField tfChangesetId; 032 private DownloadAction actDownload; 033 034 protected void build() { 035 setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); 036 setBorder( 037 BorderFactory.createCompoundBorder( 038 BorderFactory.createLineBorder(Color.GRAY), 039 BorderFactory.createEmptyBorder(0,3,0,3) 040 ) 041 ); 042 043 add(new JLabel(tr("Changeset ID: "))); 044 add(tfChangesetId = new ChangesetIdTextField()); 045 tfChangesetId.setToolTipText(tr("Enter a changeset id")); 046 SelectAllOnFocusGainedDecorator.decorate(tfChangesetId); 047 048 actDownload = new DownloadAction(); 049 SideButton btn = new SideButton(actDownload); 050 tfChangesetId.addActionListener(actDownload); 051 tfChangesetId.getDocument().addDocumentListener(actDownload); 052 add(btn); 053 054 if (Main.pref.getBoolean("downloadchangeset.autopaste", true)) { 055 tfChangesetId.tryToPasteFromClipboard(); 056 } 057 } 058 059 /** 060 * Constructs a new {@link SingleChangesetDownloadPanel} 061 */ 062 public SingleChangesetDownloadPanel() { 063 build(); 064 } 065 066 /** 067 * Replies the changeset id entered in this panel. 0 if no changeset id 068 * or an invalid changeset id is currently entered. 069 * 070 * @return the changeset id entered in this panel 071 */ 072 public int getChangesetId() { 073 return tfChangesetId.getChangesetId(); 074 } 075 076 /** 077 * Downloads the single changeset from the OSM API 078 */ 079 class DownloadAction extends AbstractAction implements DocumentListener{ 080 081 public DownloadAction() { 082 putValue(SMALL_ICON, ChangesetCacheManager.DOWNLOAD_CONTENT_ICON); 083 putValue(SHORT_DESCRIPTION, tr("Download the changeset with the specified id, including the changeset content")); 084 updateEnabledState(); 085 } 086 087 @Override 088 public void actionPerformed(ActionEvent arg0) { 089 if (!isEnabled()) 090 return; 091 int id = getChangesetId(); 092 if (id == 0) return; 093 ChangesetContentDownloadTask task = new ChangesetContentDownloadTask( 094 SingleChangesetDownloadPanel.this, 095 Collections.singleton(id) 096 ); 097 ChangesetCacheManager.getInstance().runDownloadTask(task); 098 } 099 100 protected void updateEnabledState() { 101 setEnabled(tfChangesetId.readIds() && !Main.isOffline(OnlineResource.OSM_API)); 102 } 103 104 @Override 105 public void changedUpdate(DocumentEvent arg0) { 106 updateEnabledState(); 107 } 108 109 @Override 110 public void insertUpdate(DocumentEvent arg0) { 111 updateEnabledState(); 112 } 113 114 @Override 115 public void removeUpdate(DocumentEvent arg0) { 116 updateEnabledState(); 117 } 118 } 119}