1 | package org.openstreetmap.josm.gui.io;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.WindowAdapter;
|
---|
12 | import java.awt.event.WindowEvent;
|
---|
13 | import java.beans.PropertyChangeEvent;
|
---|
14 | import java.beans.PropertyChangeListener;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.logging.Logger;
|
---|
18 |
|
---|
19 | import javax.swing.AbstractAction;
|
---|
20 | import javax.swing.BorderFactory;
|
---|
21 | import javax.swing.InputMap;
|
---|
22 | import javax.swing.JButton;
|
---|
23 | import javax.swing.JComponent;
|
---|
24 | import javax.swing.JDialog;
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.JTabbedPane;
|
---|
28 | import javax.swing.KeyStroke;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
32 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
33 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
34 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
35 | import org.openstreetmap.josm.gui.SideButton;
|
---|
36 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
37 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
38 | import org.openstreetmap.josm.io.OsmApi;
|
---|
39 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
40 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * This is a dialog for entering upload options like the parameters for
|
---|
44 | * the upload changeset and the strategy for opening/closing a changeset.
|
---|
45 | *
|
---|
46 | */
|
---|
47 | public class UploadDialog extends JDialog implements PropertyChangeListener{
|
---|
48 | protected static final Logger logger = Logger.getLogger(UploadDialog.class.getName());
|
---|
49 |
|
---|
50 | /** the unique instance of the upload dialog */
|
---|
51 | static private UploadDialog uploadDialog;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Replies the unique instance of the upload dialog
|
---|
55 | *
|
---|
56 | * @return the unique instance of the upload dialog
|
---|
57 | */
|
---|
58 | static public UploadDialog getUploadDialog() {
|
---|
59 | if (uploadDialog == null) {
|
---|
60 | uploadDialog = new UploadDialog();
|
---|
61 | }
|
---|
62 | return uploadDialog;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** the panel with the objects to upload */
|
---|
66 | private UploadedObjectsSummaryPanel pnlUploadedObjects;
|
---|
67 | /** the panel to select the changeset used */
|
---|
68 | private ChangesetManagementPanel pnlChangesetManagement;
|
---|
69 |
|
---|
70 | private BasicUploadSettingsPanel pnlBasicUploadSettings;
|
---|
71 |
|
---|
72 | private UploadStrategySelectionPanel pnlUploadStrategySelectionPanel;
|
---|
73 |
|
---|
74 | /** checkbox for selecting whether an atomic upload is to be used */
|
---|
75 | private TagSettingsPanel pnlTagSettings;
|
---|
76 | /** the tabbed pane used below of the list of primitives */
|
---|
77 | private JTabbedPane tpConfigPanels;
|
---|
78 | /** the upload button */
|
---|
79 | private JButton btnUpload;
|
---|
80 |
|
---|
81 | private boolean canceled = false;
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * builds the content panel for the upload dialog
|
---|
86 | *
|
---|
87 | * @return the content panel
|
---|
88 | */
|
---|
89 | protected JPanel buildContentPanel() {
|
---|
90 | JPanel pnl = new JPanel();
|
---|
91 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
92 | pnl.setLayout(new BorderLayout());
|
---|
93 |
|
---|
94 | // the panel with the list of uploaded objects
|
---|
95 | //
|
---|
96 | pnl.add(pnlUploadedObjects = new UploadedObjectsSummaryPanel(), BorderLayout.CENTER);
|
---|
97 |
|
---|
98 | // a tabbed pane with two configuration panels in the
|
---|
99 | // lower half
|
---|
100 | //
|
---|
101 | tpConfigPanels = new JTabbedPane() {
|
---|
102 | @Override
|
---|
103 | public Dimension getPreferredSize() {
|
---|
104 | // make sure the tabbed pane never grabs more space than necessary
|
---|
105 | //
|
---|
106 | return super.getMinimumSize();
|
---|
107 | }
|
---|
108 | };
|
---|
109 | tpConfigPanels.add(new JPanel());
|
---|
110 | tpConfigPanels.add(new JPanel());
|
---|
111 | tpConfigPanels.add(new JPanel());
|
---|
112 | tpConfigPanels.add(new JPanel());
|
---|
113 |
|
---|
114 | tpConfigPanels.setComponentAt(0, pnlBasicUploadSettings = new BasicUploadSettingsPanel());
|
---|
115 | tpConfigPanels.setTitleAt(0, tr("Settings"));
|
---|
116 | tpConfigPanels.setToolTipTextAt(0, tr("Decide how to upload the data and which changeset to use"));
|
---|
117 |
|
---|
118 | tpConfigPanels.setComponentAt(1,pnlTagSettings = new TagSettingsPanel());
|
---|
119 | tpConfigPanels.setTitleAt(1, tr("Tags of new changeset"));
|
---|
120 | tpConfigPanels.setToolTipTextAt(1, tr("Apply tags to the changeset data is uploaded to"));
|
---|
121 |
|
---|
122 | tpConfigPanels.setComponentAt(2,pnlChangesetManagement = new ChangesetManagementPanel());
|
---|
123 | tpConfigPanels.setTitleAt(2, tr("Changesets"));
|
---|
124 | tpConfigPanels.setToolTipTextAt(2, tr("Manage open changesets and select a changeset to upload to"));
|
---|
125 |
|
---|
126 | tpConfigPanels.setComponentAt(3, pnlUploadStrategySelectionPanel = new UploadStrategySelectionPanel());
|
---|
127 | tpConfigPanels.setTitleAt(3, tr("Advanced"));
|
---|
128 | tpConfigPanels.setToolTipTextAt(3, tr("Configure advanced settings"));
|
---|
129 |
|
---|
130 | pnl.add(tpConfigPanels, BorderLayout.SOUTH);
|
---|
131 | return pnl;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * builds the panel with the OK and CANCEL buttons
|
---|
136 | *
|
---|
137 | * @return
|
---|
138 | */
|
---|
139 | protected JPanel buildActionPanel() {
|
---|
140 | JPanel pnl = new JPanel();
|
---|
141 | pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
|
---|
142 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
143 |
|
---|
144 | // -- upload button
|
---|
145 | UploadAction uploadAction = new UploadAction();
|
---|
146 | pnl.add(btnUpload = new SideButton(uploadAction));
|
---|
147 | btnUpload.setFocusable(true);
|
---|
148 | InputMap inputMap = btnUpload.getInputMap();
|
---|
149 | inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "doUpload");
|
---|
150 | btnUpload.getActionMap().put("doUpload", uploadAction);
|
---|
151 |
|
---|
152 | // -- cancel button
|
---|
153 | CancelAction cancelAction = new CancelAction();
|
---|
154 | pnl.add(new SideButton(cancelAction));
|
---|
155 | getRootPane().registerKeyboardAction(
|
---|
156 | cancelAction,
|
---|
157 | KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0),
|
---|
158 | JComponent.WHEN_IN_FOCUSED_WINDOW
|
---|
159 | );
|
---|
160 | pnl.add(new SideButton(new ContextSensitiveHelpAction(ht("/Dialog/UploadDialog"))));
|
---|
161 | HelpUtil.setHelpContext(getRootPane(),ht("/Dialog/UploadDialog"));
|
---|
162 | return pnl;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * builds the gui
|
---|
167 | */
|
---|
168 | protected void build() {
|
---|
169 | setTitle(tr("Upload to ''{0}''", OsmApi.getOsmApi().getBaseUrl()));
|
---|
170 | getContentPane().setLayout(new BorderLayout());
|
---|
171 | getContentPane().add(buildContentPanel(), BorderLayout.CENTER);
|
---|
172 | getContentPane().add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
173 |
|
---|
174 | addWindowListener(new WindowEventHandler());
|
---|
175 |
|
---|
176 | // synchronized input of upload comments
|
---|
177 | //
|
---|
178 | //UploadCommentSynchronizer synchronizer = new UploadCommentSynchronizer();
|
---|
179 | //pnlTagSettings.getModeaddTableModelListener(synchronizer);
|
---|
180 | pnlTagSettings.addPropertyChangeListener(pnlBasicUploadSettings);
|
---|
181 | pnlBasicUploadSettings.addPropertyChangeListener(pnlTagSettings);
|
---|
182 |
|
---|
183 |
|
---|
184 | // make sure the the configuration panels listen to each other
|
---|
185 | // changes
|
---|
186 | //
|
---|
187 | pnlChangesetManagement.addPropertyChangeListener(
|
---|
188 | pnlBasicUploadSettings.getUploadParameterSummaryPanel()
|
---|
189 | );
|
---|
190 | pnlChangesetManagement.addPropertyChangeListener(pnlTagSettings);
|
---|
191 | pnlChangesetManagement.addPropertyChangeListener(this);
|
---|
192 | pnlUploadedObjects.addPropertyChangeListener(
|
---|
193 | pnlBasicUploadSettings.getUploadParameterSummaryPanel()
|
---|
194 | );
|
---|
195 | pnlUploadedObjects.addPropertyChangeListener(pnlUploadStrategySelectionPanel);
|
---|
196 | pnlUploadStrategySelectionPanel.addPropertyChangeListener(
|
---|
197 | pnlBasicUploadSettings.getUploadParameterSummaryPanel()
|
---|
198 | );
|
---|
199 |
|
---|
200 | // users can click on either of two links in the upload parameter
|
---|
201 | // summary handler. This installs the handler for these two events.
|
---|
202 | // We simply select the appropriate tab in the tabbed pane with the
|
---|
203 | // configuration dialogs.
|
---|
204 | //
|
---|
205 | pnlBasicUploadSettings.getUploadParameterSummaryPanel().setConfigurationParameterRequestListener(
|
---|
206 | new ConfigurationParameterRequestHandler() {
|
---|
207 | public void handleUploadStrategyConfigurationRequest() {
|
---|
208 | tpConfigPanels.setSelectedIndex(3);
|
---|
209 | }
|
---|
210 | public void handleChangesetConfigurationRequest() {
|
---|
211 | tpConfigPanels.setSelectedIndex(2);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | );
|
---|
215 |
|
---|
216 | pnlBasicUploadSettings.setUploadCommentDownFocusTraversalHandler(
|
---|
217 | new AbstractAction() {
|
---|
218 | public void actionPerformed(ActionEvent e) {
|
---|
219 | btnUpload.requestFocusInWindow();
|
---|
220 | }
|
---|
221 | }
|
---|
222 | );
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * constructor
|
---|
227 | */
|
---|
228 | public UploadDialog() {
|
---|
229 | super(JOptionPane.getFrameForComponent(Main.parent), true /* modal */);
|
---|
230 | build();
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Sets the collection of primitives to upload
|
---|
235 | *
|
---|
236 | * @param toUpload the dataset with the objects to upload. If null, assumes the empty
|
---|
237 | * set of objects to upload
|
---|
238 | *
|
---|
239 | */
|
---|
240 | public void setUploadedPrimitives(APIDataSet toUpload) {
|
---|
241 | if (toUpload == null) {
|
---|
242 | List<OsmPrimitive> emptyList = Collections.emptyList();
|
---|
243 | pnlUploadedObjects.setUploadedPrimitives(emptyList, emptyList, emptyList);
|
---|
244 | return;
|
---|
245 | }
|
---|
246 | pnlUploadedObjects.setUploadedPrimitives(
|
---|
247 | toUpload.getPrimitivesToAdd(),
|
---|
248 | toUpload.getPrimitivesToUpdate(),
|
---|
249 | toUpload.getPrimitivesToDelete()
|
---|
250 | );
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Remembers the user input in the preference settings
|
---|
255 | */
|
---|
256 | public void rememberUserInput() {
|
---|
257 | pnlBasicUploadSettings.rememberUserInput();
|
---|
258 | pnlUploadStrategySelectionPanel.rememberUserInput();
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Initializes the panel for user input
|
---|
263 | */
|
---|
264 | public void startUserInput() {
|
---|
265 | tpConfigPanels.setSelectedIndex(0);
|
---|
266 | pnlBasicUploadSettings.startUserInput();
|
---|
267 | pnlTagSettings.startUserInput();
|
---|
268 | pnlTagSettings.setUploadComment(getUploadComment());
|
---|
269 | pnlTagSettings.initFromChangeset(pnlChangesetManagement.getSelectedChangeset());
|
---|
270 | pnlUploadStrategySelectionPanel.initFromPreferences();
|
---|
271 | UploadParameterSummaryPanel pnl = pnlBasicUploadSettings.getUploadParameterSummaryPanel();
|
---|
272 | pnl.setUploadStrategySpecification(pnlUploadStrategySelectionPanel.getUploadStrategySpecification());
|
---|
273 | pnl.setCloseChangesetAfterNextUpload(pnlChangesetManagement.isCloseChangesetAfterUpload());
|
---|
274 | pnl.setNumObjects(pnlUploadedObjects.getNumObjectsToUpload());
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Replies the current changeset
|
---|
279 | *
|
---|
280 | * @return the current changeset
|
---|
281 | */
|
---|
282 | public Changeset getChangeset() {
|
---|
283 | Changeset cs = pnlChangesetManagement.getSelectedChangeset();
|
---|
284 | if (cs == null) {
|
---|
285 | cs = new Changeset();
|
---|
286 | }
|
---|
287 | cs.setKeys(pnlTagSettings.getTags());
|
---|
288 | return cs;
|
---|
289 | }
|
---|
290 |
|
---|
291 | public void setSelectedChangesetForNextUpload(Changeset cs) {
|
---|
292 | pnlChangesetManagement.setSelectedChangesetForNextUpload(cs);
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Replies the {@see UploadStrategySpecification} the user entered in the dialog.
|
---|
297 | *
|
---|
298 | * @return the {@see UploadStrategySpecification} the user entered in the dialog.
|
---|
299 | */
|
---|
300 | public UploadStrategySpecification getUploadStrategySpecification() {
|
---|
301 | UploadStrategySpecification spec = pnlUploadStrategySelectionPanel.getUploadStrategySpecification();
|
---|
302 | spec.setCloseChangesetAfterUpload(pnlChangesetManagement.isCloseChangesetAfterUpload());
|
---|
303 | return spec;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Replies the current value for the upload comment
|
---|
308 | *
|
---|
309 | * @return the current value for the upload comment
|
---|
310 | */
|
---|
311 | protected String getUploadComment() {
|
---|
312 | return pnlBasicUploadSettings.getUploadComment();
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Replies true, if the dialog was canceled
|
---|
317 | *
|
---|
318 | * @return true, if the dialog was canceled
|
---|
319 | */
|
---|
320 | public boolean isCanceled() {
|
---|
321 | return canceled;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Sets whether the dialog was canceled
|
---|
326 | *
|
---|
327 | * @param canceled true, if the dialog is canceled
|
---|
328 | */
|
---|
329 | protected void setCanceled(boolean canceled) {
|
---|
330 | this.canceled = canceled;
|
---|
331 | }
|
---|
332 |
|
---|
333 | @Override
|
---|
334 | public void setVisible(boolean visible) {
|
---|
335 | if (visible) {
|
---|
336 | new WindowGeometry(
|
---|
337 | getClass().getName() + ".geometry",
|
---|
338 | WindowGeometry.centerInWindow(
|
---|
339 | Main.parent,
|
---|
340 | new Dimension(400,600)
|
---|
341 | )
|
---|
342 | ).apply(this);
|
---|
343 | startUserInput();
|
---|
344 | } else if (!visible && isShowing()){
|
---|
345 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
346 | }
|
---|
347 | super.setVisible(visible);
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Handles an upload
|
---|
352 | *
|
---|
353 | */
|
---|
354 | class UploadAction extends AbstractAction {
|
---|
355 | public UploadAction() {
|
---|
356 | putValue(NAME, tr("Upload Changes"));
|
---|
357 | putValue(SMALL_ICON, ImageProvider.get("upload"));
|
---|
358 | putValue(SHORT_DESCRIPTION, tr("Upload the changed primitives"));
|
---|
359 | }
|
---|
360 |
|
---|
361 | protected void warnIllegalUploadComment() {
|
---|
362 | HelpAwareOptionPane.showOptionDialog(
|
---|
363 | UploadDialog.this,
|
---|
364 | tr("Please enter a comment for this upload changeset (min. 3 characters)"),
|
---|
365 | tr("Illegal upload comment"),
|
---|
366 | JOptionPane.ERROR_MESSAGE,
|
---|
367 | ht("/Dialog/UploadDialog#IllegalUploadComment")
|
---|
368 | );
|
---|
369 | }
|
---|
370 |
|
---|
371 | protected void warnIllegalChunkSize() {
|
---|
372 | HelpAwareOptionPane.showOptionDialog(
|
---|
373 | UploadDialog.this,
|
---|
374 | tr("Please enter a valid chunk size first"),
|
---|
375 | tr("Illegal chunk size"),
|
---|
376 | JOptionPane.ERROR_MESSAGE,
|
---|
377 | ht("/Dialog/UploadDialog#IllegalChunkSize")
|
---|
378 | );
|
---|
379 | }
|
---|
380 |
|
---|
381 | public void actionPerformed(ActionEvent e) {
|
---|
382 | if (getUploadComment().trim().length() < 3) {
|
---|
383 | warnIllegalUploadComment();
|
---|
384 | tpConfigPanels.setSelectedIndex(0);
|
---|
385 | pnlBasicUploadSettings.initEditingOfUploadComment(getUploadComment());
|
---|
386 | return;
|
---|
387 | }
|
---|
388 | UploadStrategySpecification strategy = getUploadStrategySpecification();
|
---|
389 | if (strategy.getStrategy().equals(UploadStrategy.CHUNKED_DATASET_STRATEGY)) {
|
---|
390 | if (strategy.getChunkSize() == UploadStrategySpecification.UNSPECIFIED_CHUNK_SIZE) {
|
---|
391 | warnIllegalChunkSize();
|
---|
392 | tpConfigPanels.setSelectedIndex(0);
|
---|
393 | return;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | setCanceled(false);
|
---|
397 | setVisible(false);
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Action for canceling the dialog
|
---|
403 | *
|
---|
404 | */
|
---|
405 | class CancelAction extends AbstractAction {
|
---|
406 | public CancelAction() {
|
---|
407 | putValue(NAME, tr("Cancel"));
|
---|
408 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
409 | putValue(SHORT_DESCRIPTION, tr("Cancel the upload and resume editing"));
|
---|
410 | }
|
---|
411 |
|
---|
412 | public void actionPerformed(ActionEvent e) {
|
---|
413 | setCanceled(true);
|
---|
414 | setVisible(false);
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Listens to window closing events and processes them as cancel events.
|
---|
420 | * Listens to window open events and initializes user input
|
---|
421 | *
|
---|
422 | */
|
---|
423 | class WindowEventHandler extends WindowAdapter {
|
---|
424 | @Override
|
---|
425 | public void windowClosing(WindowEvent e) {
|
---|
426 | setCanceled(true);
|
---|
427 | }
|
---|
428 |
|
---|
429 | @Override
|
---|
430 | public void windowOpened(WindowEvent e) {
|
---|
431 | //startUserInput();
|
---|
432 | }
|
---|
433 |
|
---|
434 | @Override
|
---|
435 | public void windowActivated(WindowEvent arg0) {
|
---|
436 | if (tpConfigPanels.getSelectedIndex() == 0) {
|
---|
437 | pnlBasicUploadSettings.initEditingOfUploadComment(getUploadComment());
|
---|
438 | }
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | /* -------------------------------------------------------------------------- */
|
---|
443 | /* Interface PropertyChangeListener */
|
---|
444 | /* -------------------------------------------------------------------------- */
|
---|
445 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
446 | if (evt.getPropertyName().equals(ChangesetManagementPanel.SELECTED_CHANGESET_PROP)) {
|
---|
447 | Changeset cs = (Changeset)evt.getNewValue();
|
---|
448 | if (cs == null) {
|
---|
449 | tpConfigPanels.setTitleAt(1, tr("Tags of new changeset"));
|
---|
450 | } else {
|
---|
451 | tpConfigPanels.setTitleAt(1, tr("Tags of changeset {0}", cs.getId()));
|
---|
452 | }
|
---|
453 | }
|
---|
454 | }
|
---|
455 | }
|
---|