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

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

fix #9559 - Rendering of lower part of upload dialog when resized

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