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