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

Last change on this file since 12531 was 12370, checked in by michael2402, 7 years ago

Document upload dialog classes

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