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

Last change on this file since 10680 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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