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.KeyEvent; 013import java.awt.event.KeyListener; 014import java.util.Arrays; 015import java.util.Collections; 016import java.util.LinkedList; 017import java.util.List; 018import java.util.Observable; 019import java.util.Observer; 020 021import javax.swing.Action; 022import javax.swing.BorderFactory; 023import javax.swing.JEditorPane; 024import javax.swing.JPanel; 025import javax.swing.event.HyperlinkEvent; 026import javax.swing.event.HyperlinkListener; 027 028import org.openstreetmap.josm.Main; 029import org.openstreetmap.josm.data.osm.Changeset; 030import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 031import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 032import org.openstreetmap.josm.tools.CheckParameterUtil; 033import org.openstreetmap.josm.tools.GBC; 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.getEditor().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(new HyperlinkListener() { 072 @Override 073 public void hyperlinkUpdate(HyperlinkEvent e) { 074 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) { 075 hcbUploadSource.setText(Main.map.mapView.getLayerInformationForSourceTag()); 076 // Fix #9965 077 changesetSourceModel.setComment(hcbUploadSource.getText()); 078 } 079 } 080 }); 081 pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 3).fill(GBC.HORIZONTAL)); 082 083 hcbUploadSource.setToolTipText(tr("Enter a source")); 084 hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH); 085 List<String> sourceHistory = new LinkedList<>(Main.pref.getCollection(SOURCE_HISTORY_KEY, getDefaultSources())); 086 Collections.reverse(sourceHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement() 087 hcbUploadSource.setPossibleItems(sourceHistory); 088 CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel); 089 hcbUploadSource.getEditor().addActionListener(sourceModelListener); 090 hcbUploadSource.getEditor().getEditorComponent().addFocusListener(sourceModelListener); 091 pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL)); 092 return pnl; 093 } 094 095 public static List<String> getDefaultSources() { 096 return Arrays.asList("knowledge", "survey", "Bing"); 097 } 098 099 protected void build() { 100 setLayout(new BorderLayout()); 101 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); 102 add(buildUploadCommentPanel(), BorderLayout.NORTH); 103 add(pnlUploadParameterSummary, BorderLayout.CENTER); 104 } 105 106 /** 107 * Creates the panel 108 * 109 * @param changesetCommentModel the model for the changeset comment. Must not be null 110 * @param changesetSourceModel the model for the changeset source. Must not be null. 111 * @throws IllegalArgumentException if {@code changesetCommentModel} is null 112 */ 113 public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) { 114 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel"); 115 CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel"); 116 this.changesetCommentModel = changesetCommentModel; 117 this.changesetSourceModel = changesetSourceModel; 118 changesetCommentModel.addObserver(new ChangesetCommentObserver(hcbUploadComment)); 119 changesetSourceModel.addObserver(new ChangesetCommentObserver(hcbUploadSource)); 120 build(); 121 } 122 123 public void setUploadTagDownFocusTraversalHandlers(final Action handler) { 124 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadComment); 125 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadSource); 126 } 127 128 public void setHistoryComboBoxDownFocusTraversalHandler(final Action handler, final HistoryComboBox hcb) { 129 hcb.getEditor().addActionListener(handler); 130 hcb.getEditor().getEditorComponent().addKeyListener( 131 new KeyListener() { 132 @Override 133 public void keyTyped(KeyEvent e) { 134 if (e.getKeyCode() == KeyEvent.VK_TAB) { 135 handler.actionPerformed(new ActionEvent(hcb, 0, "focusDown")); 136 } 137 } 138 139 @Override 140 public void keyReleased(KeyEvent e) {} 141 142 @Override 143 public void keyPressed(KeyEvent e) {} 144 } 145 ); 146 } 147 148 /** 149 * Remembers the user input in the preference settings 150 */ 151 public void rememberUserInput() { 152 // store the history of comments 153 hcbUploadComment.addCurrentItemToHistory(); 154 Main.pref.putCollection(HISTORY_KEY, hcbUploadComment.getHistory()); 155 Main.pref.putInteger(HISTORY_LAST_USED_KEY, (int) (System.currentTimeMillis() / 1000)); 156 // store the history of sources 157 hcbUploadSource.addCurrentItemToHistory(); 158 Main.pref.putCollection(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory()); 159 } 160 161 /** 162 * Initializes the panel for user input 163 */ 164 public void startUserInput() { 165 hcbUploadComment.requestFocusInWindow(); 166 hcbUploadComment.getEditor().getEditorComponent().requestFocusInWindow(); 167 } 168 169 public void initEditingOfUploadComment() { 170 hcbUploadComment.getEditor().selectAll(); 171 hcbUploadComment.requestFocusInWindow(); 172 } 173 174 public UploadParameterSummaryPanel getUploadParameterSummaryPanel() { 175 return pnlUploadParameterSummary; 176 } 177 178 /** 179 * Updates the changeset comment model upon changes in the input field. 180 */ 181 static class CommentModelListener extends FocusAdapter implements ActionListener { 182 183 private final HistoryComboBox source; 184 private final ChangesetCommentModel destination; 185 186 CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) { 187 this.source = source; 188 this.destination = destination; 189 } 190 191 @Override 192 public void actionPerformed(ActionEvent e) { 193 destination.setComment(source.getText()); 194 } 195 196 @Override 197 public void focusLost(FocusEvent e) { 198 destination.setComment(source.getText()); 199 } 200 } 201 202 /** 203 * Observes the changeset comment model and keeps the comment input field 204 * in sync with the current changeset comment 205 */ 206 static class ChangesetCommentObserver implements Observer { 207 208 private final HistoryComboBox destination; 209 210 ChangesetCommentObserver(HistoryComboBox destination) { 211 this.destination = destination; 212 } 213 214 @Override 215 public void update(Observable o, Object arg) { 216 if (!(o instanceof ChangesetCommentModel)) return; 217 String newComment = (String) arg; 218 if (!destination.getText().equals(newComment)) { 219 destination.setText(newComment); 220 } 221 } 222 } 223}