1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.FocusAdapter;
|
---|
11 | import java.awt.event.FocusEvent;
|
---|
12 | import java.awt.event.KeyEvent;
|
---|
13 | import java.awt.event.KeyListener;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Observable;
|
---|
19 | import java.util.Observer;
|
---|
20 |
|
---|
21 | import javax.swing.Action;
|
---|
22 | import javax.swing.BorderFactory;
|
---|
23 | import javax.swing.JEditorPane;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.event.HyperlinkEvent;
|
---|
26 | import javax.swing.event.HyperlinkListener;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
32 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading data.
|
---|
37 | * @since 2599
|
---|
38 | */
|
---|
39 | public 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.getEditor().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(new HyperlinkListener() {
|
---|
72 | @Override
|
---|
73 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
74 | if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
|
---|
75 | hcbUploadSource.setText(Main.map.mapView.getLayerInformationForSourceTag());
|
---|
76 | // Fix #9965
|
---|
77 | changesetSourceModel.setComment(hcbUploadSource.getText());
|
---|
78 | }
|
---|
79 | }
|
---|
80 | });
|
---|
81 | pnl.add(sourceLabel, GBC.eol().insets(0, 8, 10, 3).fill(GBC.HORIZONTAL));
|
---|
82 |
|
---|
83 | hcbUploadSource.setToolTipText(tr("Enter a source"));
|
---|
84 | hcbUploadSource.setMaxTextLength(Changeset.MAX_CHANGESET_TAG_LENGTH);
|
---|
85 | List<String> sourceHistory = new LinkedList<>(Main.pref.getCollection(SOURCE_HISTORY_KEY, getDefaultSources()));
|
---|
86 | Collections.reverse(sourceHistory); // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
|
---|
87 | hcbUploadSource.setPossibleItems(sourceHistory);
|
---|
88 | CommentModelListener sourceModelListener = new CommentModelListener(hcbUploadSource, changesetSourceModel);
|
---|
89 | hcbUploadSource.getEditor().addActionListener(sourceModelListener);
|
---|
90 | hcbUploadSource.getEditor().getEditorComponent().addFocusListener(sourceModelListener);
|
---|
91 | pnl.add(hcbUploadSource, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
92 | return pnl;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static List<String> getDefaultSources() {
|
---|
96 | return Arrays.asList("knowledge", "survey", "Bing");
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void build() {
|
---|
100 | setLayout(new BorderLayout());
|
---|
101 | setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
|
---|
102 | add(buildUploadCommentPanel(), BorderLayout.NORTH);
|
---|
103 | add(pnlUploadParameterSummary, BorderLayout.CENTER);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Creates the panel
|
---|
108 | *
|
---|
109 | * @param changesetCommentModel the model for the changeset comment. Must not be null
|
---|
110 | * @param changesetSourceModel the model for the changeset source. Must not be null.
|
---|
111 | * @throws IllegalArgumentException if {@code changesetCommentModel} is null
|
---|
112 | */
|
---|
113 | public BasicUploadSettingsPanel(ChangesetCommentModel changesetCommentModel, ChangesetCommentModel changesetSourceModel) {
|
---|
114 | CheckParameterUtil.ensureParameterNotNull(changesetCommentModel, "changesetCommentModel");
|
---|
115 | CheckParameterUtil.ensureParameterNotNull(changesetSourceModel, "changesetSourceModel");
|
---|
116 | this.changesetCommentModel = changesetCommentModel;
|
---|
117 | this.changesetSourceModel = changesetSourceModel;
|
---|
118 | changesetCommentModel.addObserver(new ChangesetCommentObserver(hcbUploadComment));
|
---|
119 | changesetSourceModel.addObserver(new ChangesetCommentObserver(hcbUploadSource));
|
---|
120 | build();
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void setUploadTagDownFocusTraversalHandlers(final Action handler) {
|
---|
124 | setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadComment);
|
---|
125 | setHistoryComboBoxDownFocusTraversalHandler(handler, hcbUploadSource);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public void setHistoryComboBoxDownFocusTraversalHandler(final Action handler, final HistoryComboBox hcb) {
|
---|
129 | hcb.getEditor().addActionListener(handler);
|
---|
130 | hcb.getEditor().getEditorComponent().addKeyListener(
|
---|
131 | new KeyListener() {
|
---|
132 | @Override
|
---|
133 | public void keyTyped(KeyEvent e) {
|
---|
134 | if (e.getKeyCode() == KeyEvent.VK_TAB) {
|
---|
135 | handler.actionPerformed(new ActionEvent(hcb, 0, "focusDown"));
|
---|
136 | }
|
---|
137 | }
|
---|
138 | @Override
|
---|
139 | public void keyReleased(KeyEvent e) {}
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public void keyPressed(KeyEvent e) {}
|
---|
143 | }
|
---|
144 | );
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Remembers the user input in the preference settings
|
---|
149 | */
|
---|
150 | public void rememberUserInput() {
|
---|
151 | // store the history of comments
|
---|
152 | hcbUploadComment.addCurrentItemToHistory();
|
---|
153 | Main.pref.putCollection(HISTORY_KEY, hcbUploadComment.getHistory());
|
---|
154 | Main.pref.putInteger(HISTORY_LAST_USED_KEY, (int) (System.currentTimeMillis() / 1000));
|
---|
155 | // store the history of sources
|
---|
156 | hcbUploadSource.addCurrentItemToHistory();
|
---|
157 | Main.pref.putCollection(SOURCE_HISTORY_KEY, hcbUploadSource.getHistory());
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Initializes the panel for user input
|
---|
162 | */
|
---|
163 | public void startUserInput() {
|
---|
164 | hcbUploadComment.requestFocusInWindow();
|
---|
165 | hcbUploadComment.getEditor().getEditorComponent().requestFocusInWindow();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void initEditingOfUploadComment() {
|
---|
169 | hcbUploadComment.getEditor().selectAll();
|
---|
170 | hcbUploadComment.requestFocusInWindow();
|
---|
171 | }
|
---|
172 |
|
---|
173 | public UploadParameterSummaryPanel getUploadParameterSummaryPanel() {
|
---|
174 | return pnlUploadParameterSummary;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Updates the changeset comment model upon changes in the input field.
|
---|
179 | */
|
---|
180 | static class CommentModelListener extends FocusAdapter implements ActionListener {
|
---|
181 |
|
---|
182 | private final HistoryComboBox source;
|
---|
183 | private final ChangesetCommentModel destination;
|
---|
184 |
|
---|
185 | CommentModelListener(HistoryComboBox source, ChangesetCommentModel destination) {
|
---|
186 | this.source = source;
|
---|
187 | this.destination = destination;
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public void actionPerformed(ActionEvent e) {
|
---|
192 | destination.setComment(source.getText());
|
---|
193 | }
|
---|
194 | @Override
|
---|
195 | public void focusLost(FocusEvent e) {
|
---|
196 | destination.setComment(source.getText());
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Observes the changeset comment model and keeps the comment input field
|
---|
202 | * in sync with the current changeset comment
|
---|
203 | */
|
---|
204 | static class ChangesetCommentObserver implements Observer {
|
---|
205 |
|
---|
206 | private final HistoryComboBox destination;
|
---|
207 |
|
---|
208 | ChangesetCommentObserver(HistoryComboBox destination) {
|
---|
209 | this.destination = destination;
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | public void update(Observable o, Object arg) {
|
---|
214 | if (!(o instanceof ChangesetCommentModel)) return;
|
---|
215 | String newComment = (String)arg;
|
---|
216 | if (!destination.getText().equals(newComment)) {
|
---|
217 | destination.setText(newComment);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|