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

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

see #11924 - Java 9 - replace calls to deprecated classes java.util.Observable / java.util.Observer by a new class ChangeNotifier + swing's ChangeListener

  • Property svn:eol-style set to native
File size: 9.9 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;
26import javax.swing.event.HyperlinkListener;
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 public static final String HISTORY_KEY = "upload.comment.history";
42 public static final String HISTORY_LAST_USED_KEY = "upload.comment.last-used";
43 public static final String HISTORY_MAX_AGE_KEY = "upload.comment.max-age";
44 public static final String SOURCE_HISTORY_KEY = "upload.source.history";
45
46 /** the history combo box for the upload comment */
47 private final HistoryComboBox hcbUploadComment = new HistoryComboBox();
48 private final HistoryComboBox hcbUploadSource = new HistoryComboBox();
49 /** the panel with a summary of the upload parameters */
50 private final UploadParameterSummaryPanel pnlUploadParameterSummary = new UploadParameterSummaryPanel();
51 /** the changeset comment model */
52 private final transient ChangesetCommentModel changesetCommentModel;
53 private final transient ChangesetCommentModel changesetSourceModel;
54
55 protected JPanel buildUploadCommentPanel() {
56 JPanel pnl = new JPanel(new GridBagLayout());
57
58 JEditorPane commentLabel = new JMultilineLabel("<html><b>" + tr("Provide a brief comment for the changes you are uploading:"));
59 pnl.add(commentLabel, GBC.eol().insets(0, 5, 10, 3).fill(GBC.HORIZONTAL));
60 hcbUploadComment.setToolTipText(tr("Enter an upload comment"));
61 hcbUploadComment.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
62 List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
63 Collections.reverse(cmtHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
64 hcbUploadComment.setPossibleItems(cmtHistory);
65 CommentModelListener commentModelListener = new CommentModelListener(hcbUploadComment, changesetCommentModel);
66 hcbUploadComment.getEditor().addActionListener(commentModelListener);
67 hcbUploadComment.getEditorComponent().addFocusListener(commentModelListener);
68 pnl.add(hcbUploadComment, GBC.eol().fill(GBC.HORIZONTAL));
69
70 JEditorPane sourceLabel = new JMultilineLabel("<html><b>" + tr("Specify the data source for the changes")
71 + "</b> (<a href=\"urn:changeset-source\">" + tr("obtain from current layers") + "</a>)<b>:</b>");
72 sourceLabel.addHyperlinkListener(new HyperlinkListener() {
73 @Override
74 public void hyperlinkUpdate(HyperlinkEvent e) {
75 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
76 final String source = Main.map.mapView.getLayerInformationForSourceTag();
77 hcbUploadSource.setText(Utils.shortenString(source, Changeset.MAX_CHANGESET_TAG_LENGTH));
78 // Fix #9965
79 changesetSourceModel.setComment(hcbUploadSource.getText());
80 }
81 }
82 });
83 pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 3).fill(GBC.HORIZONTAL));
84
85 hcbUploadSource.setToolTipText(tr("Enter a source"));
86 hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
87 List<String> sourceHistory = new LinkedList<>(Main.pref.getCollection(SOURCE_HISTORY_KEY, getDefaultSources()));
88 Collections.reverse(sourceHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
89 hcbUploadSource.setPossibleItems(sourceHistory);
90 CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel);
91 hcbUploadSource.getEditor().addActionListener(sourceModelListener);
92 hcbUploadSource.getEditorComponent().addFocusListener(sourceModelListener);
93 pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL));
94 return pnl;
95 }
96
97 /**
98 * Returns the default list of sources.
99 * @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.addChangeListener(new ChangesetCommentChangeListener(hcbUploadComment));
125 changesetSourceModel.addChangeListener(new ChangesetCommentChangeListener(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 KeyAdapter() {
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 );
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) (System.currentTimeMillis() / 1000));
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 /**
190 * Updates the changeset comment model upon changes in the input field.
191 */
192 static class CommentModelListener extends FocusAdapter implements ActionListener {
193
194 private final HistoryComboBox source;
195 private final ChangesetCommentModel destination;
196
197 CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) {
198 this.source = source;
199 this.destination = destination;
200 }
201
202 @Override
203 public void actionPerformed(ActionEvent e) {
204 destination.setComment(source.getText());
205 }
206
207 @Override
208 public void focusLost(FocusEvent e) {
209 destination.setComment(source.getText());
210 }
211 }
212
213 /**
214 * Observes the changeset comment model and keeps the comment input field
215 * in sync with the current changeset comment
216 */
217 static class ChangesetCommentChangeListener implements ChangeListener {
218
219 private final HistoryComboBox destination;
220
221 ChangesetCommentChangeListener(HistoryComboBox destination) {
222 this.destination = destination;
223 }
224
225 @Override
226 public void stateChanged(ChangeEvent e) {
227 if (!(e.getSource() instanceof ChangesetCommentModel)) return;
228 String newComment = ((ChangesetCommentModel) e.getSource()).getComment();
229 if (!destination.getText().equals(newComment)) {
230 destination.setText(newComment);
231 }
232 }
233 }
234}
Note: See TracBrowser for help on using the repository browser.