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

Last change on this file since 6565 was 6565, checked in by simon04, 10 years ago

fix #9484 - source in upload dialog: add knowledge, survey to selection, compute value from layers

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