001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.GridBagLayout; 008import java.awt.event.ActionEvent; 009import java.awt.event.ActionListener; 010import java.awt.event.FocusAdapter; 011import java.awt.event.FocusEvent; 012import java.awt.event.KeyAdapter; 013import java.awt.event.KeyEvent; 014import java.util.Arrays; 015import java.util.Collections; 016import java.util.LinkedList; 017import java.util.List; 018 019import javax.swing.Action; 020import javax.swing.BorderFactory; 021import javax.swing.JEditorPane; 022import javax.swing.JPanel; 023import javax.swing.event.ChangeEvent; 024import javax.swing.event.ChangeListener; 025import javax.swing.event.HyperlinkEvent; 026 027import org.openstreetmap.josm.Main; 028import org.openstreetmap.josm.data.osm.Changeset; 029import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 030import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 031import org.openstreetmap.josm.tools.CheckParameterUtil; 032import org.openstreetmap.josm.tools.GBC; 033import org.openstreetmap.josm.tools.Utils; 034 035/** 036 * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading data. 037 * @since 2599 038 */ 039public class BasicUploadSettingsPanel extends JPanel { 040 public static final String HISTORY_KEY = "upload.comment.history"; 041 public static final String HISTORY_LAST_USED_KEY = "upload.comment.last-used"; 042 public static final String HISTORY_MAX_AGE_KEY = "upload.comment.max-age"; 043 public static final String SOURCE_HISTORY_KEY = "upload.source.history"; 044 045 /** the history combo box for the upload comment */ 046 private final HistoryComboBox hcbUploadComment = new HistoryComboBox(); 047 private final HistoryComboBox hcbUploadSource = new HistoryComboBox(); 048 /** the panel with a summary of the upload parameters */ 049 private final UploadParameterSummaryPanel pnlUploadParameterSummary = new UploadParameterSummaryPanel(); 050 /** the changeset comment model */ 051 private final transient ChangesetCommentModel changesetCommentModel; 052 private final transient ChangesetCommentModel changesetSourceModel; 053 054 protected JPanel buildUploadCommentPanel() { 055 JPanel pnl = new JPanel(new GridBagLayout()); 056 057 JEditorPane commentLabel = new JMultilineLabel("<html><b>" + tr("Provide a brief comment for the changes you are uploading:")); 058 pnl.add(commentLabel, GBC.eol().insets(0, 5, 10, 3).fill(GBC.HORIZONTAL)); 059 hcbUploadComment.setToolTipText(tr("Enter an upload comment")); 060 hcbUploadComment.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH); 061 List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>())); 062 Collections.reverse(cmtHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement() 063 hcbUploadComment.setPossibleItems(cmtHistory); 064 CommentModelListener commentModelListener = new CommentModelListener(hcbUploadComment, changesetCommentModel); 065 hcbUploadComment.getEditor().addActionListener(commentModelListener); 066 hcbUploadComment.getEditorComponent().addFocusListener(commentModelListener); 067 pnl.add(hcbUploadComment, GBC.eol().fill(GBC.HORIZONTAL)); 068 069 JEditorPane sourceLabel = new JMultilineLabel("<html><b>" + tr("Specify the data source for the changes") 070 + "</b> (<a href=\"urn:changeset-source\">" + tr("obtain from current layers") + "</a>)<b>:</b>"); 071 sourceLabel.addHyperlinkListener(e -> { 072 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) { 073 final String source = Main.map.mapView.getLayerInformationForSourceTag(); 074 hcbUploadSource.setText(Utils.shortenString(source, Changeset.MAX_CHANGESET_TAG_LENGTH)); 075 // Fix #9965 076 changesetSourceModel.setComment(hcbUploadSource.getText()); 077 } 078 }); 079 pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 3).fill(GBC.HORIZONTAL)); 080 081 hcbUploadSource.setToolTipText(tr("Enter a source")); 082 hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH); 083 List<String> sourceHistory = new LinkedList<>(Main.pref.getCollection(SOURCE_HISTORY_KEY, getDefaultSources())); 084 Collections.reverse(sourceHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement() 085 hcbUploadSource.setPossibleItems(sourceHistory); 086 CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel); 087 hcbUploadSource.getEditor().addActionListener(sourceModelListener); 088 hcbUploadSource.getEditorComponent().addFocusListener(sourceModelListener); 089 pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL)); 090 return pnl; 091 } 092 093 /** 094 * Returns the default list of sources. 095 * @return the default list of sources 096 */ 097 public static List<String> getDefaultSources() { 098 return Arrays.asList("knowledge", "survey", "Bing"); 099 } 100 101 protected void build() { 102 setLayout(new BorderLayout()); 103 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); 104 add(buildUploadCommentPanel(), BorderLayout.NORTH); 105 add(pnlUploadParameterSummary, BorderLayout.CENTER); 106 } 107 108 /** 109 * Creates the panel 110 * 111 * @param changesetCommentModel the model for the changeset comment. Must not be null 112 * @param changesetSourceModel the model for the changeset source. Must not be null. 113 * @throws IllegalArgumentException if {@code changesetCommentModel} is null 114 */ 115 public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) { 116 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel"); 117 CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel"); 118 this.changesetCommentModel = changesetCommentModel; 119 this.changesetSourceModel = changesetSourceModel; 120 changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadComment)); 121 changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadSource)); 122 build(); 123 } 124 125 public void setUploadTagDownFocusTraversalHandlers(final Action handler) { 126 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadComment); 127 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadSource); 128 } 129 130 public void setHistoryComboBoxDownFocusTraversalHandler(final Action handler, final HistoryComboBox hcb) { 131 hcb.getEditor().addActionListener(handler); 132 hcb.getEditorComponent().addKeyListener( 133 new KeyAdapter() { 134 @Override 135 public void keyTyped(KeyEvent e) { 136 if (e.getKeyCode() == KeyEvent.VK_TAB) { 137 handler.actionPerformed(new ActionEvent(hcb, 0, "focusDown")); 138 } 139 } 140 } 141 ); 142 } 143 144 /** 145 * Remembers the user input in the preference settings 146 */ 147 public void rememberUserInput() { 148 // store the history of comments 149 hcbUploadComment.addCurrentItemToHistory(); 150 Main.pref.putCollection(HISTORY_KEY, hcbUploadComment.getHistory()); 151 Main.pref.putInteger(HISTORY_LAST_USED_KEY, (int) (System.currentTimeMillis() / 1000)); 152 // store the history of sources 153 hcbUploadSource.addCurrentItemToHistory(); 154 Main.pref.putCollection(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory()); 155 } 156 157 /** 158 * Initializes the panel for user input 159 */ 160 public void startUserInput() { 161 hcbUploadComment.requestFocusInWindow(); 162 hcbUploadComment.getEditorComponent().requestFocusInWindow(); 163 } 164 165 /** 166 * Initializes editing of upload comment. 167 */ 168 public void initEditingOfUploadComment() { 169 hcbUploadComment.getEditor().selectAll(); 170 hcbUploadComment.requestFocusInWindow(); 171 } 172 173 /** 174 * Initializes editing of upload source. 175 */ 176 public void initEditingOfUploadSource() { 177 hcbUploadSource.getEditor().selectAll(); 178 hcbUploadSource.requestFocusInWindow(); 179 } 180 181 public UploadParameterSummaryPanel getUploadParameterSummaryPanel() { 182 return pnlUploadParameterSummary; 183 } 184 185 /** 186 * Updates the changeset comment model upon changes in the input field. 187 */ 188 static class CommentModelListener extends FocusAdapter implements ActionListener { 189 190 private final HistoryComboBox source; 191 private final ChangesetCommentModel destination; 192 193 CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) { 194 this.source = source; 195 this.destination = destination; 196 } 197 198 @Override 199 public void actionPerformed(ActionEvent e) { 200 destination.setComment(source.getText()); 201 } 202 203 @Override 204 public void focusLost(FocusEvent e) { 205 destination.setComment(source.getText()); 206 } 207 } 208 209 /** 210 * Observes the changeset comment model and keeps the comment input field 211 * in sync with the current changeset comment 212 */ 213 static class ChangesetCommentChangeListener implements ChangeListener { 214 215 private final HistoryComboBox destination; 216 217 ChangesetCommentChangeListener(HistoryComboBox destination) { 218 this.destination = destination; 219 } 220 221 @Override 222 public void stateChanged(ChangeEvent e) { 223 if (!(e.getSource() instanceof ChangesetCommentModel)) return; 224 String newComment = ((ChangesetCommentModel) e.getSource()).getComment(); 225 if (!destination.getText().equals(newComment)) { 226 destination.setText(newComment); 227 } 228 } 229 } 230}