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.beans.PropertyChangeEvent;
|
---|
15 | import java.beans.PropertyChangeListener;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
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.gui.widgets.HistoryComboBox;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * BasicUploadSettingsPanel allows to enter the basic parameters required for uploading
|
---|
31 | * data.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class BasicUploadSettingsPanel extends JPanel implements PropertyChangeListener{
|
---|
35 | static public final String UPLOAD_COMMENT_PROP = BasicUploadSettingsPanel.class.getName() + ".uploadComment";
|
---|
36 | public static final String HISTORY_KEY = "upload.comment.history";
|
---|
37 |
|
---|
38 | /** the history combo box for the upload comment */
|
---|
39 | private HistoryComboBox hcbUploadComment;
|
---|
40 | /** the panel with a summary of the upload parameters */
|
---|
41 | private UploadParameterSummaryPanel pnlUploadParameterSummary;
|
---|
42 |
|
---|
43 | protected JPanel buildUploadCommentPanel() {
|
---|
44 | JPanel pnl = new JPanel();
|
---|
45 | pnl.setLayout(new GridBagLayout());
|
---|
46 | pnl.add(new JLabel(tr("Provide a brief comment for the changes you are uploading:")), GBC.eol().insets(0, 5, 10, 3));
|
---|
47 | hcbUploadComment = new HistoryComboBox();
|
---|
48 | hcbUploadComment.setToolTipText(tr("Enter an upload comment (min. 3 characters)"));
|
---|
49 | List<String> cmtHistory = new LinkedList<String>(Main.pref.getCollection(HISTORY_KEY, new LinkedList<String>()));
|
---|
50 | // we have to reverse the history, because ComboBoxHistory will reverse it again
|
---|
51 | // in addElement()
|
---|
52 | //
|
---|
53 | Collections.reverse(cmtHistory);
|
---|
54 | hcbUploadComment.setPossibleItems(cmtHistory);
|
---|
55 | hcbUploadComment.getEditor().addActionListener(
|
---|
56 | new ActionListener() {
|
---|
57 | public void actionPerformed(ActionEvent e) {
|
---|
58 | firePropertyChange(UPLOAD_COMMENT_PROP, null, hcbUploadComment.getText());
|
---|
59 | }
|
---|
60 | }
|
---|
61 | );
|
---|
62 | hcbUploadComment.getEditor().getEditorComponent().addFocusListener(
|
---|
63 | new FocusAdapter() {
|
---|
64 | @Override
|
---|
65 | public void focusLost(FocusEvent e) {
|
---|
66 | firePropertyChange(UPLOAD_COMMENT_PROP, null, hcbUploadComment.getText());
|
---|
67 | }
|
---|
68 | }
|
---|
69 | );
|
---|
70 | pnl.add(hcbUploadComment, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
71 | return pnl;
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void build() {
|
---|
75 | setLayout(new BorderLayout());
|
---|
76 | setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
|
---|
77 | add(buildUploadCommentPanel(), BorderLayout.NORTH);
|
---|
78 | add(pnlUploadParameterSummary = new UploadParameterSummaryPanel(), BorderLayout.CENTER);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public BasicUploadSettingsPanel() {
|
---|
82 | build();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void setUploadCommentDownFocusTraversalHandler(final Action handler) {
|
---|
86 | hcbUploadComment.getEditor().addActionListener(handler);
|
---|
87 | hcbUploadComment.getEditor().getEditorComponent().addKeyListener(
|
---|
88 | new KeyListener() {
|
---|
89 | public void keyTyped(KeyEvent e) {
|
---|
90 | if (e.getKeyCode() == KeyEvent.VK_TAB) {
|
---|
91 | handler.actionPerformed(new ActionEvent(hcbUploadComment,0, "focusDown"));
|
---|
92 | }
|
---|
93 | }
|
---|
94 | public void keyReleased(KeyEvent e) {}
|
---|
95 |
|
---|
96 | public void keyPressed(KeyEvent e) {}
|
---|
97 | }
|
---|
98 | );
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Remembers the user input in the preference settings
|
---|
103 | */
|
---|
104 | public void rememberUserInput() {
|
---|
105 | // store the history of comments
|
---|
106 | hcbUploadComment.addCurrentItemToHistory();
|
---|
107 | Main.pref.putCollection(HISTORY_KEY, hcbUploadComment.getHistory());
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Initializes the panel for user input
|
---|
112 | */
|
---|
113 | public void startUserInput() {
|
---|
114 | List<String> history = hcbUploadComment.getHistory();
|
---|
115 | if (history != null && !history.isEmpty()) {
|
---|
116 | hcbUploadComment.setText(history.get(0));
|
---|
117 | }
|
---|
118 | hcbUploadComment.requestFocusInWindow();
|
---|
119 | hcbUploadComment.getEditor().getEditorComponent().requestFocusInWindow();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Replies the current upload comment
|
---|
124 | *
|
---|
125 | * @return
|
---|
126 | */
|
---|
127 | public String getUploadComment() {
|
---|
128 | return hcbUploadComment.getText();
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Sets the current upload comment
|
---|
133 | *
|
---|
134 | * @return
|
---|
135 | */
|
---|
136 | public void setUploadComment(String uploadComment) {
|
---|
137 | if (uploadComment == null) {
|
---|
138 | uploadComment = "";
|
---|
139 | }
|
---|
140 | if (!uploadComment.equals(hcbUploadComment.getText())) {
|
---|
141 | hcbUploadComment.setText(uploadComment);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void initEditingOfUploadComment(String comment) {
|
---|
146 | setUploadComment(comment);
|
---|
147 | hcbUploadComment.getEditor().selectAll();
|
---|
148 | hcbUploadComment.requestFocusInWindow();
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | public UploadParameterSummaryPanel getUploadParameterSummaryPanel() {
|
---|
153 | return pnlUploadParameterSummary;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* -------------------------------------------------------------------------- */
|
---|
157 | /* Interface PropertyChangeListener */
|
---|
158 | /* -------------------------------------------------------------------------- */
|
---|
159 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
160 | if (evt.getPropertyName().equals(TagSettingsPanel.UPLOAD_COMMENT_PROP)) {
|
---|
161 | String comment = (String)evt.getNewValue();
|
---|
162 | if (comment == null) {
|
---|
163 | comment = "";
|
---|
164 | }
|
---|
165 | if (comment.equals(hcbUploadComment.getText()))
|
---|
166 | // nothing to change - return
|
---|
167 | return;
|
---|
168 | hcbUploadComment.setText(comment);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|