source: josm/trunk/src/org/openstreetmap/josm/gui/io/BasicUploadSettingsPanel.java@ 12719

Last change on this file since 12719 was 12719, checked in by Don-vip, 7 years ago

fix #15232 - Allow user to request feedback when uploading by adding the review_requested=yes changeset tag.
Inspired by:

  • Property svn:eol-style set to native
File size: 11.8 KB
RevLine 
[2711]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.awt.event.FocusAdapter;
11import java.awt.event.FocusEvent;
[12719]12import java.awt.event.ItemEvent;
[10173]13import java.awt.event.KeyAdapter;
[2711]14import java.awt.event.KeyEvent;
[6565]15import java.util.Arrays;
[2711]16import java.util.Collections;
17import java.util.LinkedList;
18import java.util.List;
[11288]19import java.util.concurrent.TimeUnit;
[2711]20
21import javax.swing.Action;
22import javax.swing.BorderFactory;
[12719]23import javax.swing.JCheckBox;
[6654]24import javax.swing.JEditorPane;
[2711]25import javax.swing.JPanel;
[10210]26import javax.swing.event.ChangeEvent;
27import javax.swing.event.ChangeListener;
[6654]28import javax.swing.event.HyperlinkEvent;
[2711]29
30import org.openstreetmap.josm.Main;
[4508]31import org.openstreetmap.josm.data.osm.Changeset;
[12630]32import org.openstreetmap.josm.gui.MainApplication;
[2711]33import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
[6901]34import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
[3133]35import org.openstreetmap.josm.tools.CheckParameterUtil;
[2711]36import org.openstreetmap.josm.tools.GBC;
[9198]37import org.openstreetmap.josm.tools.Utils;
[2711]38
39/**
[7995]40 * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading data.
41 * @since 2599
[2711]42 */
[3133]43public class BasicUploadSettingsPanel extends JPanel {
[12370]44 /**
45 * Preference name for history collection
46 */
[2711]47 public static final String HISTORY_KEY = "upload.comment.history";
[12370]48 /**
49 * Preference name for last used upload comment
50 */
[3399]51 public static final String HISTORY_LAST_USED_KEY = "upload.comment.last-used";
[12370]52 /**
53 * Preference name for the max age search comments may have
54 */
[6587]55 public static final String HISTORY_MAX_AGE_KEY = "upload.comment.max-age";
[12370]56 /**
57 * Preference name for the history of source values
58 */
[6408]59 public static final String SOURCE_HISTORY_KEY = "upload.source.history";
[2711]60
61 /** the history combo box for the upload comment */
[6401]62 private final HistoryComboBox hcbUploadComment = new HistoryComboBox();
63 private final HistoryComboBox hcbUploadSource = new HistoryComboBox();
[2711]64 /** the panel with a summary of the upload parameters */
[6401]65 private final UploadParameterSummaryPanel pnlUploadParameterSummary = new UploadParameterSummaryPanel();
[12719]66 /** the checkbox to request feedback from other users */
67 private final JCheckBox cbRequestReview = new JCheckBox(tr("I would like someone to review my edits."));
[6401]68 /** the changeset comment model */
[8308]69 private final transient ChangesetCommentModel changesetCommentModel;
70 private final transient ChangesetCommentModel changesetSourceModel;
[12719]71 private final transient ChangesetReviewModel changesetReviewModel;
[2711]72
73 protected JPanel buildUploadCommentPanel() {
[6660]74 JPanel pnl = new JPanel(new GridBagLayout());
[6401]75
[7995]76 JEditorPane commentLabel = new JMultilineLabel("<html><b>" + tr("Provide a brief comment for the changes you are uploading:"));
[6660]77 pnl.add(commentLabel, GBC.eol().insets(0, 5, 10, 3).fill(GBC.HORIZONTAL));
[3399]78 hcbUploadComment.setToolTipText(tr("Enter an upload comment"));
[7995]79 hcbUploadComment.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
[7005]80 List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
[6401]81 Collections.reverse(cmtHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
[2711]82 hcbUploadComment.setPossibleItems(cmtHistory);
[7995]83 CommentModelListener commentModelListener = new CommentModelListener(hcbUploadComment, changesetCommentModel);
[6401]84 hcbUploadComment.getEditor().addActionListener(commentModelListener);
[9484]85 hcbUploadComment.getEditorComponent().addFocusListener(commentModelListener);
[2711]86 pnl.add(hcbUploadComment, GBC.eol().fill(GBC.HORIZONTAL));
[6401]87
[7995]88 JEditorPane sourceLabel = new JMultilineLabel("<html><b>" + tr("Specify the data source for the changes")
[6654]89 + "</b> (<a href=\"urn:changeset-source\">" + tr("obtain from current layers") + "</a>)<b>:</b>");
[10611]90 sourceLabel.addHyperlinkListener(e -> {
91 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
[12630]92 final String source = MainApplication.getMap().mapView.getLayerInformationForSourceTag();
[10611]93 hcbUploadSource.setText(Utils.shortenString(source, Changeset.MAX_CHANGESET_TAG_LENGTH));
94 // Fix #9965
95 changesetSourceModel.setComment(hcbUploadSource.getText());
[6654]96 }
97 });
[6660]98 pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 3).fill(GBC.HORIZONTAL));
[6654]99
[6401]100 hcbUploadSource.setToolTipText(tr("Enter a source"));
[7995]101 hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
[8177]102 List<String> sourceHistory = new LinkedList<>(Main.pref.getCollection(SOURCE_HISTORY_KEY, getDefaultSources()));
[6401]103 Collections.reverse(sourceHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
[6408]104 hcbUploadSource.setPossibleItems(sourceHistory);
[7995]105 CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel);
[6401]106 hcbUploadSource.getEditor().addActionListener(sourceModelListener);
[9484]107 hcbUploadSource.getEditorComponent().addFocusListener(sourceModelListener);
[6401]108 pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL));
[2711]109 return pnl;
110 }
111
[9685]112 /**
113 * Returns the default list of sources.
114 * @return the default list of sources
115 */
[8318]116 public static List<String> getDefaultSources() {
[8177]117 return Arrays.asList("knowledge", "survey", "Bing");
118 }
119
[2711]120 protected void build() {
121 setLayout(new BorderLayout());
[8510]122 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
[2711]123 add(buildUploadCommentPanel(), BorderLayout.NORTH);
[6401]124 add(pnlUploadParameterSummary, BorderLayout.CENTER);
[12719]125 add(cbRequestReview, BorderLayout.SOUTH);
126 cbRequestReview.addItemListener(e -> changesetReviewModel.setReviewRequested(e.getStateChange() == ItemEvent.SELECTED));
[2711]127 }
128
[3133]129 /**
130 * Creates the panel
[3530]131 *
[3133]132 * @param changesetCommentModel the model for the changeset comment. Must not be null
[6401]133 * @param changesetSourceModel the model for the changeset source. Must not be null.
[12719]134 * @param changesetReviewModel the model for the changeset review. Must not be null.
[8291]135 * @throws IllegalArgumentException if {@code changesetCommentModel} is null
[12719]136 * @since 12719 (signature)
[3133]137 */
[12719]138 public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel,
139 ChangesetReviewModel changesetReviewModel) {
[3133]140 CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
[6401]141 CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
[12719]142 CheckParameterUtil.ensureParameterNotNull(changesetReviewModel, "changesetReviewModel");
[3133]143 this.changesetCommentModel = changesetCommentModel;
[6401]144 this.changesetSourceModel = changesetSourceModel;
[12719]145 this.changesetReviewModel = changesetReviewModel;
[10210]146 changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadComment));
147 changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadSource));
[12719]148 changesetReviewModel.addChangeListener(new ChangesetReviewChangeListener());
[2711]149 build();
150 }
151
[6424]152 public void setUploadTagDownFocusTraversalHandlers(final Action handler) {
153 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadComment);
154 setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadSource);
155 }
156
157 public void setHistoryComboBoxDownFocusTraversalHandler(final Action handler, final HistoryComboBox hcb) {
158 hcb.getEditor().addActionListener(handler);
[11366]159 hcb.getEditorComponent().addKeyListener(new HistoryComboBoxKeyAdapter(hcb, handler));
[2711]160 }
161
162 /**
163 * Remembers the user input in the preference settings
164 */
165 public void rememberUserInput() {
166 // store the history of comments
167 hcbUploadComment.addCurrentItemToHistory();
168 Main.pref.putCollection(HISTORY_KEY, hcbUploadComment.getHistory());
[11288]169 Main.pref.putInteger(HISTORY_LAST_USED_KEY, (int) (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
[6401]170 // store the history of sources
171 hcbUploadSource.addCurrentItemToHistory();
172 Main.pref.putCollection(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory());
[2711]173 }
174
175 /**
176 * Initializes the panel for user input
177 */
178 public void startUserInput() {
179 hcbUploadComment.requestFocusInWindow();
[9484]180 hcbUploadComment.getEditorComponent().requestFocusInWindow();
[2711]181 }
182
[9685]183 /**
184 * Initializes editing of upload comment.
185 */
[3133]186 public void initEditingOfUploadComment() {
[2711]187 hcbUploadComment.getEditor().selectAll();
188 hcbUploadComment.requestFocusInWindow();
189 }
190
[9685]191 /**
192 * Initializes editing of upload source.
193 */
194 public void initEditingOfUploadSource() {
195 hcbUploadSource.getEditor().selectAll();
196 hcbUploadSource.requestFocusInWindow();
197 }
198
[2711]199 public UploadParameterSummaryPanel getUploadParameterSummaryPanel() {
200 return pnlUploadParameterSummary;
201 }
202
[11366]203 static final class HistoryComboBoxKeyAdapter extends KeyAdapter {
204 private final HistoryComboBox hcb;
205 private final Action handler;
206
207 HistoryComboBoxKeyAdapter(HistoryComboBox hcb, Action handler) {
208 this.hcb = hcb;
209 this.handler = handler;
210 }
211
212 @Override
213 public void keyTyped(KeyEvent e) {
214 if (e.getKeyCode() == KeyEvent.VK_TAB) {
215 handler.actionPerformed(new ActionEvent(hcb, 0, "focusDown"));
216 }
217 }
218 }
219
[3133]220 /**
[6401]221 * Updates the changeset comment model upon changes in the input field.
222 */
[6450]223 static class CommentModelListener extends FocusAdapter implements ActionListener {
[6401]224
[8285]225 private final HistoryComboBox source;
226 private final ChangesetCommentModel destination;
[6401]227
228 CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) {
229 this.source = source;
230 this.destination = destination;
231 }
232
233 @Override
234 public void actionPerformed(ActionEvent e) {
235 destination.setComment(source.getText());
236 }
[8510]237
[6401]238 @Override
239 public void focusLost(FocusEvent e) {
240 destination.setComment(source.getText());
241 }
242 }
243
244 /**
[3133]245 * Observes the changeset comment model and keeps the comment input field
246 * in sync with the current changeset comment
247 */
[10210]248 static class ChangesetCommentChangeListener implements ChangeListener {
[6401]249
250 private final HistoryComboBox destination;
251
[10210]252 ChangesetCommentChangeListener(HistoryComboBox destination) {
[6401]253 this.destination = destination;
254 }
255
[6084]256 @Override
[10210]257 public void stateChanged(ChangeEvent e) {
258 if (!(e.getSource() instanceof ChangesetCommentModel)) return;
259 String newComment = ((ChangesetCommentModel) e.getSource()).getComment();
[6401]260 if (!destination.getText().equals(newComment)) {
261 destination.setText(newComment);
[2711]262 }
263 }
264 }
[12719]265
266 /**
267 * Observes the changeset review model and keeps the review checkbox
268 * in sync with the current changeset review request
269 */
270 class ChangesetReviewChangeListener implements ChangeListener {
271 @Override
272 public void stateChanged(ChangeEvent e) {
273 if (!(e.getSource() instanceof ChangesetReviewModel)) return;
274 boolean newState = ((ChangesetReviewModel) e.getSource()).isReviewRequested();
275 if (cbRequestReview.isSelected() != newState) {
276 cbRequestReview.setSelected(newState);
277 }
278 }
279 }
[2711]280}
Note: See TracBrowser for help on using the repository browser.