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, 3 weeks 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
Line 
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;
12import java.awt.event.ItemEvent;
13import java.awt.event.KeyAdapter;
14import java.awt.event.KeyEvent;
15import java.util.Arrays;
16import java.util.Collections;
17import java.util.LinkedList;
18import java.util.List;
19import java.util.concurrent.TimeUnit;
20
21import javax.swing.Action;
22import javax.swing.BorderFactory;
23import javax.swing.JCheckBox;
24import javax.swing.JEditorPane;
25import javax.swing.JPanel;
26import javax.swing.event.ChangeEvent;
27import javax.swing.event.ChangeListener;
28import javax.swing.event.HyperlinkEvent;
29
30import org.openstreetmap.josm.Main;
31import org.openstreetmap.josm.data.osm.Changeset;
32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
34import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
35import org.openstreetmap.josm.tools.CheckParameterUtil;
36import org.openstreetmap.josm.tools.GBC;
37import org.openstreetmap.josm.tools.Utils;
38
39/**
40 * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading data.
41 * @since 2599
42 */
43public class BasicUploadSettingsPanel extends JPanel {
44    /**
45     * Preference name for history collection
46     */
47    public static final String HISTORY_KEY = "upload.comment.history";
48    /**
49     * Preference name for last used upload comment
50     */
51    public static final String HISTORY_LAST_USED_KEY = "upload.comment.last-used";
52    /**
53     * Preference name for the max age search comments may have
54     */
55    public static final String HISTORY_MAX_AGE_KEY = "upload.comment.max-age";
56    /**
57     * Preference name for the history of source values
58     */
59    public static final String SOURCE_HISTORY_KEY = "upload.source.history";
60
61    /** the history combo box for the upload comment */
62    private final HistoryComboBox hcbUploadComment = new HistoryComboBox();
63    private final HistoryComboBox hcbUploadSource = new HistoryComboBox();
64    /** the panel with a summary of the upload parameters */
65    private final UploadParameterSummaryPanel pnlUploadParameterSummary = new UploadParameterSummaryPanel();
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."));
68    /** the changeset comment model */
69    private final transient ChangesetCommentModel changesetCommentModel;
70    private final transient ChangesetCommentModel changesetSourceModel;
71    private final transient ChangesetReviewModel changesetReviewModel;
72
73    protected JPanel buildUploadCommentPanel() {
74        JPanel pnl = new JPanel(new GridBagLayout());
75
76        JEditorPane commentLabel = new JMultilineLabel("<html><b>" + tr("Provide a brief comment for the changes you are uploading:"));
77        pnl.add(commentLabel, GBC.eol().insets(0, 5, 10, 3).fill(GBC.HORIZONTAL));
78        hcbUploadComment.setToolTipText(tr("Enter an upload comment"));
79        hcbUploadComment.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
80        List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
81        Collections.reverse(cmtHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
82        hcbUploadComment.setPossibleItems(cmtHistory);
83        CommentModelListener commentModelListener = new CommentModelListener(hcbUploadComment, changesetCommentModel);
84        hcbUploadComment.getEditor().addActionListener(commentModelListener);
85        hcbUploadComment.getEditorComponent().addFocusListener(commentModelListener);
86        pnl.add(hcbUploadComment, GBC.eol().fill(GBC.HORIZONTAL));
87
88        JEditorPane sourceLabel = new JMultilineLabel("<html><b>" + tr("Specify the data source for the changes")
89                + "</b> (<a href=\"urn:changeset-source\">" + tr("obtain from current layers") + "</a>)<b>:</b>");
90        sourceLabel.addHyperlinkListener(e -> {
91            if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
92                final String source = MainApplication.getMap().mapView.getLayerInformationForSourceTag();
93                hcbUploadSource.setText(Utils.shortenString(source, Changeset.MAX_CHANGESET_TAG_LENGTH));
94                // Fix #9965
95                changesetSourceModel.setComment(hcbUploadSource.getText());
96            }
97        });
98        pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 3).fill(GBC.HORIZONTAL));
99
100        hcbUploadSource.setToolTipText(tr("Enter a source"));
101        hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
102        List<String> sourceHistory = new LinkedList<>(Main.pref.getCollection(SOURCE_HISTORY_KEY, getDefaultSources()));
103        Collections.reverse(sourceHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
104        hcbUploadSource.setPossibleItems(sourceHistory);
105        CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel);
106        hcbUploadSource.getEditor().addActionListener(sourceModelListener);
107        hcbUploadSource.getEditorComponent().addFocusListener(sourceModelListener);
108        pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL));
109        return pnl;
110    }
111
112    /**
113     * Returns the default list of sources.
114     * @return the default list of sources
115     */
116    public static List<String> getDefaultSources() {
117        return Arrays.asList("knowledge", "survey", "Bing");
118    }
119
120    protected void build() {
121        setLayout(new BorderLayout());
122        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
123        add(buildUploadCommentPanel(), BorderLayout.NORTH);
124        add(pnlUploadParameterSummary, BorderLayout.CENTER);
125        add(cbRequestReview, BorderLayout.SOUTH);
126        cbRequestReview.addItemListener(e -> changesetReviewModel.setReviewRequested(e.getStateChange() == ItemEvent.SELECTED));
127    }
128
129    /**
130     * Creates the panel
131     *
132     * @param changesetCommentModel the model for the changeset comment. Must not be null
133     * @param changesetSourceModel the model for the changeset source. Must not be null.
134     * @param changesetReviewModel the model for the changeset review. Must not be null.
135     * @throws IllegalArgumentException if {@code changesetCommentModel} is null
136     * @since 12719 (signature)
137     */
138    public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel,
139            ChangesetReviewModel changesetReviewModel) {
140        CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
141        CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
142        CheckParameterUtil.ensureParameterNotNull(changesetReviewModel, "changesetReviewModel");
143        this.changesetCommentModel = changesetCommentModel;
144        this.changesetSourceModel = changesetSourceModel;
145        this.changesetReviewModel = changesetReviewModel;
146        changesetCommentModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadComment));
147        changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener(hcbUploadSource));
148        changesetReviewModel.addChangeListener(new ChangesetReviewChangeListener());
149        build();
150    }
151
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);
159        hcb.getEditorComponent().addKeyListener(new HistoryComboBoxKeyAdapter(hcb, handler));
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());
169        Main.pref.putInteger(HISTORY_LAST_USED_KEY, (int) (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
170        // store the history of sources
171        hcbUploadSource.addCurrentItemToHistory();
172        Main.pref.putCollection(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory());
173    }
174
175    /**
176     * Initializes the panel for user input
177     */
178    public void startUserInput() {
179        hcbUploadComment.requestFocusInWindow();
180        hcbUploadComment.getEditorComponent().requestFocusInWindow();
181    }
182
183    /**
184     * Initializes editing of upload comment.
185     */
186    public void initEditingOfUploadComment() {
187        hcbUploadComment.getEditor().selectAll();
188        hcbUploadComment.requestFocusInWindow();
189    }
190
191    /**
192     * Initializes editing of upload source.
193     */
194    public void initEditingOfUploadSource() {
195        hcbUploadSource.getEditor().selectAll();
196        hcbUploadSource.requestFocusInWindow();
197    }
198
199    public UploadParameterSummaryPanel getUploadParameterSummaryPanel() {
200        return pnlUploadParameterSummary;
201    }
202
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
220    /**
221     * Updates the changeset comment model upon changes in the input field.
222     */
223    static class CommentModelListener extends FocusAdapter implements ActionListener {
224
225        private final HistoryComboBox source;
226        private final ChangesetCommentModel destination;
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        }
237
238        @Override
239        public void focusLost(FocusEvent e) {
240            destination.setComment(source.getText());
241        }
242    }
243
244    /**
245     * Observes the changeset comment model and keeps the comment input field
246     * in sync with the current changeset comment
247     */
248    static class ChangesetCommentChangeListener implements ChangeListener {
249
250        private final HistoryComboBox destination;
251
252        ChangesetCommentChangeListener(HistoryComboBox destination) {
253            this.destination = destination;
254        }
255
256        @Override
257        public void stateChanged(ChangeEvent e) {
258            if (!(e.getSource() instanceof ChangesetCommentModel)) return;
259            String newComment = ((ChangesetCommentModel) e.getSource()).getComment();
260            if (!destination.getText().equals(newComment)) {
261                destination.setText(newComment);
262            }
263        }
264    }
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    }
280}
Note: See TracBrowser for help on using the repository browser.