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

Last change on this file since 12630 was 12630, checked in by Don-vip, 3 months ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

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