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