Changeset 9685 in josm for trunk


Ignore:
Timestamp:
2016-01-30T01:43:42+01:00 (8 years ago)
Author:
Don-vip
Message:

UploadDialog: code refactoring, add javadoc, basic unit test, remove deprecated code

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/AbstractUploadDialog.java

    r8840 r9685  
    1313 * @since 7358
    1414 */
    15 public abstract class AbstractUploadDialog extends JDialog {
     15public abstract class AbstractUploadDialog extends JDialog implements IUploadDialog {
    1616
    1717    private boolean canceled;
     
    181181    }
    182182
    183     /**
    184      * Returns true if the dialog was canceled
    185      *
    186      * @return true if the dialog was canceled
    187      */
     183    @Override
    188184    public final boolean isCanceled() {
    189185        return canceled;
     
    199195    }
    200196
    201     /**
    202      * Remembers the user input in the preference settings
    203      */
     197    @Override
    204198    public void rememberUserInput() {
    205199        // Override if needed
  • trunk/src/org/openstreetmap/josm/gui/io/BasicUploadSettingsPanel.java

    r9484 r9685  
    9595    }
    9696
     97    /**
     98     * Returns the default list of sources.
     99     * @return the default list of sources
     100     */
    97101    public static List<String> getDefaultSources() {
    98102        return Arrays.asList("knowledge", "survey", "Bing");
     
    140144
    141145                    @Override
    142                     public void keyReleased(KeyEvent e) {}
     146                    public void keyReleased(KeyEvent e) {
     147                        // Do nothing
     148                    }
    143149
    144150                    @Override
    145                     public void keyPressed(KeyEvent e) {}
     151                    public void keyPressed(KeyEvent e) {
     152                        // Do nothing
     153                    }
    146154                }
    147155        );
     
    169177    }
    170178
     179    /**
     180     * Initializes editing of upload comment.
     181     */
    171182    public void initEditingOfUploadComment() {
    172183        hcbUploadComment.getEditor().selectAll();
    173184        hcbUploadComment.requestFocusInWindow();
     185    }
     186
     187    /**
     188     * Initializes editing of upload source.
     189     */
     190    public void initEditingOfUploadSource() {
     191        hcbUploadSource.getEditor().selectAll();
     192        hcbUploadSource.requestFocusInWindow();
    174193    }
    175194
  • trunk/src/org/openstreetmap/josm/gui/io/TagSettingsPanel.java

    r9530 r9685  
    33
    44import java.awt.BorderLayout;
    5 import java.util.HashMap;
    65import java.util.Map;
    76import java.util.Observable;
     
    1413import org.openstreetmap.josm.Main;
    1514import org.openstreetmap.josm.data.osm.Changeset;
    16 import org.openstreetmap.josm.data.osm.DataSet;
    1715import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
    1816import org.openstreetmap.josm.gui.tagging.TagModel;
     
    9694
    9795    /**
    98      * @return an empty map
    99      * @deprecated No longer supported, returns an empty map
    100      */
    101     @Deprecated
    102     public Map<String, String> getDefaultTags() {
    103         return new HashMap<>();
    104     }
    105 
    106     /**
    107      * @param tags ignored
    108      * @deprecated No longer supported, does nothing; use {@link UploadDialog#setChangesetTags(DataSet)} instead!
    109      */
    110     @Deprecated
    111     public void setDefaultTags(Map<String, String> tags) {
    112         // Deprecated
    113     }
    114 
    115     /**
    11696     * Initializes the panel for user input
    11797     */
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java

    r9543 r9685  
    1010import java.awt.Dimension;
    1111import java.awt.FlowLayout;
     12import java.awt.GraphicsEnvironment;
    1213import java.awt.GridBagLayout;
    1314import java.awt.event.ActionEvent;
     
    177178
    178179        // -- upload button
    179         btnUpload = new SideButton(new UploadAction());
     180        btnUpload = new SideButton(new UploadAction(this));
    180181        pnl.add(btnUpload);
    181182        btnUpload.setFocusable(true);
     
    183184
    184185        // -- cancel button
    185         CancelAction cancelAction = new CancelAction();
     186        CancelAction cancelAction = new CancelAction(this);
    186187        pnl.add(new SideButton(cancelAction));
    187188        getRootPane().registerKeyboardAction(
     
    370371    }
    371372
    372     /**
    373      * @deprecated No longer supported, does nothing;
    374      * @return empty map
    375      */
    376     @Deprecated
    377     public Map<String, String> getDefaultChangesetTags() {
    378         return pnlTagSettings.getDefaultTags();
    379     }
    380 
    381     /**
    382      * @param tags ignored
    383      * @deprecated No longer supported, does nothing; use {@link #setChangesetTags(DataSet)} instead!
    384      */
    385     @Deprecated
    386     public void setDefaultChangesetTags(Map<String, String> tags) {
    387         // Deprecated
    388     }
    389 
    390     /**
    391      * Replies the {@link UploadStrategySpecification} the user entered in the dialog.
    392      *
    393      * @return the {@link UploadStrategySpecification} the user entered in the dialog.
    394      */
     373    @Override
    395374    public UploadStrategySpecification getUploadStrategySpecification() {
    396375        UploadStrategySpecification spec = pnlUploadStrategySelectionPanel.getUploadStrategySpecification();
     
    399378    }
    400379
    401     /**
    402      * Returns the current value for the upload comment
    403      *
    404      * @return the current value for the upload comment
    405      */
    406     protected String getUploadComment() {
     380    @Override
     381    public String getUploadComment() {
    407382        return changesetCommentModel.getComment();
    408383    }
    409384
    410     /**
    411      * Returns the current value for the changeset source
    412      *
    413      * @return the current value for the changeset source
    414      */
    415     protected String getUploadSource() {
     385    @Override
     386    public String getUploadSource() {
    416387        return changesetSourceModel.getComment();
    417388    }
     
    449420
    450421    /**
    451      * Handles an upload
    452      *
    453      */
    454     class UploadAction extends AbstractAction {
    455         UploadAction() {
     422     * Handles an upload.
     423     */
     424    static class UploadAction extends AbstractAction {
     425
     426        private final transient IUploadDialog dialog;
     427
     428        UploadAction(IUploadDialog dialog) {
     429            this.dialog = dialog;
    456430            putValue(NAME, tr("Upload Changes"));
    457431            putValue(SMALL_ICON, ImageProvider.get("upload"));
     
    493467
    494468        protected boolean warnUploadTag(final String title, final String message, final String togglePref) {
    495             ExtendedDialog dlg = new ExtendedDialog(UploadDialog.this,
    496                     title,
    497                     new String[] {tr("Revise"), tr("Cancel"), tr("Continue as is")});
    498             dlg.setContent("<html>" + message + "</html>");
    499             dlg.setButtonIcons(new Icon[] {
     469            String[] buttonTexts = new String[] {tr("Revise"), tr("Cancel"), tr("Continue as is")};
     470            Icon[] buttonIcons = new Icon[] {
    500471                    new ImageProvider("ok").setMaxSize(ImageSizes.LARGEICON).get(),
    501472                    new ImageProvider("cancel").setMaxSize(ImageSizes.LARGEICON).get(),
    502473                    new ImageProvider("upload").setMaxSize(ImageSizes.LARGEICON).addOverlay(
    503                             new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0)).get()});
    504             dlg.setToolTipTexts(new String[] {
     474                            new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0)).get()};
     475            String[] tooltips = new String[] {
    505476                    tr("Return to the previous dialog to enter a more descriptive comment"),
    506477                    tr("Cancel and return to the previous dialog"),
    507                     tr("Ignore this hint and upload anyway")});
     478                    tr("Ignore this hint and upload anyway")};
     479
     480            if (GraphicsEnvironment.isHeadless()) {
     481                return false;
     482            }
     483
     484            ExtendedDialog dlg = new ExtendedDialog((Component) dialog, title, buttonTexts);
     485            dlg.setContent("<html>" + message + "</html>");
     486            dlg.setButtonIcons(buttonIcons);
     487            dlg.setToolTipTexts(tooltips);
    508488            dlg.setIcon(JOptionPane.WARNING_MESSAGE);
    509489            dlg.toggleEnable(togglePref);
     
    514494        protected void warnIllegalChunkSize() {
    515495            HelpAwareOptionPane.showOptionDialog(
    516                     UploadDialog.this,
     496                    (Component) dialog,
    517497                    tr("Please enter a valid chunk size first"),
    518498                    tr("Illegal chunk size"),
     
    524504        @Override
    525505        public void actionPerformed(ActionEvent e) {
    526             if ((getUploadComment().trim().length() < 10 && warnUploadComment()) /* abort for missing comment */
    527                     || (getUploadSource().trim().isEmpty() && warnUploadSource()) /* abort for missing changeset source */
    528                     ) {
    529                 tpConfigPanels.setSelectedIndex(0);
    530                 pnlBasicUploadSettings.initEditingOfUploadComment();
     506            if (dialog.getUploadComment().trim().length() < 10 && warnUploadComment()) {
     507                // abort for missing comment
     508                dialog.handleMissingComment();
     509                return;
     510            }
     511            if (dialog.getUploadSource().trim().isEmpty() && warnUploadSource()) {
     512                // abort for missing changeset source
     513                dialog.handleMissingSource();
    531514                return;
    532515            }
     
    535518             * though, accept if key and value are empty (cf. xor). */
    536519            List<String> emptyChangesetTags = new ArrayList<>();
    537             for (final Entry<String, String> i : pnlTagSettings.getTags(true).entrySet()) {
     520            for (final Entry<String, String> i : dialog.getTags(true).entrySet()) {
    538521                final boolean isKeyEmpty = i.getKey() == null || i.getKey().trim().isEmpty();
    539522                final boolean isValueEmpty = i.getValue() == null || i.getValue().trim().isEmpty();
     
    553536                    JOptionPane.WARNING_MESSAGE
    554537            )) {
    555                 tpConfigPanels.setSelectedIndex(0);
    556                 pnlBasicUploadSettings.initEditingOfUploadComment();
     538                dialog.handleMissingComment();
    557539                return;
    558540            }
    559541
    560             UploadStrategySpecification strategy = getUploadStrategySpecification();
     542            UploadStrategySpecification strategy = dialog.getUploadStrategySpecification();
    561543            if (strategy.getStrategy().equals(UploadStrategy.CHUNKED_DATASET_STRATEGY)
    562544                    && strategy.getChunkSize() == UploadStrategySpecification.UNSPECIFIED_CHUNK_SIZE) {
    563545                warnIllegalChunkSize();
    564                 tpConfigPanels.setSelectedIndex(0);
     546                dialog.handleIllegalChunkSize();
    565547                return;
    566548            }
    567             setCanceled(false);
    568             setVisible(false);
    569         }
    570     }
    571 
    572     /**
    573      * Action for canceling the dialog
    574      *
    575      */
    576     class CancelAction extends AbstractAction {
    577         CancelAction() {
     549            if (dialog instanceof AbstractUploadDialog) {
     550                ((AbstractUploadDialog) dialog).setCanceled(false);
     551                ((AbstractUploadDialog) dialog).setVisible(false);
     552            }
     553        }
     554    }
     555
     556    /**
     557     * Action for canceling the dialog.
     558     */
     559    static class CancelAction extends AbstractAction {
     560
     561        private final transient IUploadDialog dialog;
     562
     563        CancelAction(IUploadDialog dialog) {
     564            this.dialog = dialog;
    578565            putValue(NAME, tr("Cancel"));
    579566            putValue(SMALL_ICON, ImageProvider.get("cancel"));
     
    583570        @Override
    584571        public void actionPerformed(ActionEvent e) {
    585             setCanceled(true);
    586             setVisible(false);
     572            if (dialog instanceof AbstractUploadDialog) {
     573                ((AbstractUploadDialog) dialog).setCanceled(true);
     574                ((AbstractUploadDialog) dialog).setVisible(false);
     575            }
    587576        }
    588577    }
     
    665654        return getLastChangesetTagFromHistory(BasicUploadSettingsPanel.SOURCE_HISTORY_KEY, BasicUploadSettingsPanel.getDefaultSources());
    666655    }
     656
     657    @Override
     658    public Map<String, String> getTags(boolean keepEmpty) {
     659        return pnlTagSettings.getTags(keepEmpty);
     660    }
     661
     662    @Override
     663    public void handleMissingComment() {
     664        tpConfigPanels.setSelectedIndex(0);
     665        pnlBasicUploadSettings.initEditingOfUploadComment();
     666    }
     667
     668    @Override
     669    public void handleMissingSource() {
     670        tpConfigPanels.setSelectedIndex(0);
     671        pnlBasicUploadSettings.initEditingOfUploadSource();
     672    }
     673
     674    @Override
     675    public void handleIllegalChunkSize() {
     676        tpConfigPanels.setSelectedIndex(0);
     677    }
    667678}
Note: See TracChangeset for help on using the changeset viewer.