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

Last change on this file since 6450 was 6450, checked in by Don-vip, 10 years ago

Sonar/Findbugs: fix recent issues

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