Changeset 16504 in josm


Ignore:
Timestamp:
2020-05-27T08:03:42+02:00 (4 years ago)
Author:
simon04
Message:

see #18340 - OSMDownloadSource: refactoring, improve i18n

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/download/OSMDownloadSource.java

    r16503 r16504  
    1111import java.lang.reflect.InvocationTargetException;
    1212import java.util.ArrayList;
     13import java.util.Collection;
    1314import java.util.Collections;
    1415import java.util.List;
    1516import java.util.concurrent.ExecutionException;
    1617import java.util.concurrent.Future;
     18import java.util.stream.Collectors;
    1719
    1820import javax.swing.Icon;
     
    131133
    132134    /**
    133      * @return The possible downloads that JOSM can make in the default Download
    134      *         screen
     135     * @return The possible downloads that JOSM can make in the default Download screen
    135136     * @since 16503
    136137     */
     
    153154    /**
    154155     * @param type The IDownloadSourceType object to remove
    155      * @return true See {@link List#remove}, but it also returns false if the
    156      * parameter is a class from JOSM core.
     156     * @return {@code true} if this download types contained the specified object
    157157     * @since 16503
    158158     */
    159159    public static boolean removeDownloadType(IDownloadSourceType type) {
    160         boolean modified = false;
    161         if (!(type instanceof OsmDataDownloadType) && !(type instanceof GpsDataDownloadType)
    162                 && !(type instanceof NotesDataDownloadType)) {
    163             modified = DOWNLOAD_SOURCES.remove(type);
    164         }
    165         return modified;
     160        if (type instanceof OsmDataDownloadType || type instanceof GpsDataDownloadType || type instanceof NotesDataDownloadType) {
     161            throw new IllegalArgumentException(type.getClass().getName());
     162        }
     163        return DOWNLOAD_SOURCES.remove(type);
    166164    }
    167165
     
    170168     *
    171169     * @param type The initialized type to download
    172      * @return See {@link List#add}, but it also returns false if the class
    173      * already has an instance in the list or it is a class from JOSM core.
     170     * @return {@code true} (as specified by {@link Collection#add}), but it also returns false if the class already has an instance in the list
    174171     * @since 16503
    175172     */
    176173    public static boolean addDownloadType(IDownloadSourceType type) {
    177         boolean modified = false;
    178         if (!(type instanceof OsmDataDownloadType) && !(type instanceof GpsDataDownloadType)
    179                 && !(type instanceof NotesDataDownloadType)
    180                 || DOWNLOAD_SOURCES.stream()
    181                         .noneMatch(possibility -> type.getClass().isInstance(possibility))) {
    182             modified = DOWNLOAD_SOURCES.add(type);
    183         } else {
    184             throw new IllegalArgumentException("There can only be one instance of a class added, and it cannot be a built-in class.");
    185         }
    186         return modified;
     174        if (type instanceof OsmDataDownloadType || type instanceof GpsDataDownloadType || type instanceof NotesDataDownloadType) {
     175            throw new IllegalArgumentException(type.getClass().getName());
     176        } else if (getDownloadType(type.getClass()) != null) {
     177            return false;
     178        }
     179        return DOWNLOAD_SOURCES.add(type);
    187180    }
    188181
     
    195188
    196189        /** This is used to keep track of the components for download sources, and to dynamically update/remove them */
    197         private JPanel downloadSourcesPanel;
    198 
    199         private ChangeListener checkboxChangeListener;
     190        private final JPanel downloadSourcesPanel;
     191
     192        private final ChangeListener checkboxChangeListener;
    200193
    201194        /**
     
    285278             */
    286279            if (DOWNLOAD_SOURCES.stream().noneMatch(IDownloadSourceType::isEnabled)) {
    287                 StringBuilder line1 = new StringBuilder("<html>").append(tr("None of"));
    288                 StringBuilder line2 = new StringBuilder(tr("Please choose to either download"));
    289 
    290                 DOWNLOAD_SOURCES.forEach(type -> {
    291                     line1.append(" <strong>").append(type.getCheckBox().getText()).append("</strong> ");
    292                     line2.append(' ').append(type.getCheckBox().getText()).append(tr(", or"));
    293                 });
    294                 line1.append(tr("is enabled.")).append("<br>");
    295                 line2.append(tr(" all.")).append("</html>");
     280                String sources = DOWNLOAD_SOURCES.stream()
     281                        .map(type -> type.getCheckBox().getText())
     282                        .collect(Collectors.joining(", "));
     283                String message = "<html>"
     284                        + tr("None of {0} is enabled!", sources)
     285                        + "<br>"
     286                        + tr("Please select at least one of {0}.", sources);
    296287                JOptionPane.showMessageDialog(
    297288                        this.getParent(),
    298                         line1.append(line2).toString(),
     289                        message,
    299290                        tr("Error"),
    300291                        JOptionPane.ERROR_MESSAGE
     
    313304         *
    314305         * @return true if the user selected to download OSM data
    315          * @deprecated since xxx -- use {@link OSMDownloadSource#getDownloadTypes} with
    316          *             {@code get(0).getCheckBox().isSelected()}
     306         * @deprecated since xxx -- use {@code getDownloadType(OsmDataDownloadType.class).getCheckBox().isSelected()}
    317307         */
    318308        @Deprecated
    319309        public boolean isDownloadOsmData() {
    320             return DOWNLOAD_SOURCES.get(0).getCheckBox().isSelected();
     310            return getDownloadType(OsmDataDownloadType.class).getCheckBox().isSelected();
    321311        }
    322312
     
    325315         *
    326316         * @return true if the user selected to download GPX data
    327          * @deprecated since xxx -- use {@link OSMDownloadSource#getDownloadTypes} with
    328          *             {@code get(1).getCheckBox().isSelected()}
     317         * @deprecated since xxx -- use {@code getDownloadType(GpsDataDownloadType.class).getCheckBox().isSelected()}
    329318         */
    330319        @Deprecated
    331320        public boolean isDownloadGpxData() {
    332             return DOWNLOAD_SOURCES.get(1).getCheckBox().isSelected();
     321            return getDownloadType(GpsDataDownloadType.class).getCheckBox().isSelected();
    333322        }
    334323
     
    337326         *
    338327         * @return true if user selected to download notes
    339          * @deprecated since xxx -- use {@link OSMDownloadSource#getDownloadTypes} with
    340          *             {@code get(2).getCheckBox().isSelected()}
     328         * @deprecated since xxx -- use {@code getDownloadType(NotesDataDownloadType.class).getCheckBox().isSelected()}
    341329         */
    342330        @Deprecated
    343331        public boolean isDownloadNotes() {
    344             return DOWNLOAD_SOURCES.get(2).getCheckBox().isSelected();
     332            return getDownloadType(NotesDataDownloadType.class).getCheckBox().isSelected();
    345333        }
    346334
     
    388376    static class OSMDownloadData {
    389377
    390         private List<IDownloadSourceType> downloadPossibilities;
     378        private final List<IDownloadSourceType> downloadPossibilities;
    391379
    392380        /**
Note: See TracChangeset for help on using the changeset viewer.