Changeset 7092 in josm for trunk/src


Ignore:
Timestamp:
2014-05-10T02:51:06+02:00 (6 years ago)
Author:
Don-vip
Message:

refactor duplicated code

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/ChildRelationBrowser.java

    r7005 r7092  
    5050/**
    5151 * ChildRelationBrowser is a UI component which provides a tree-like view on the hierarchical
    52  * structure of relations
     52 * structure of relations.
    5353 *
    54  *
     54 * @since 1828
    5555 */
    5656public class ChildRelationBrowser extends JPanel {
     
    278278    }
    279279
    280     /**
    281      * The asynchronous task for downloading relation members.
    282      *
    283      *
    284      */
    285     class DownloadAllChildrenTask extends PleaseWaitRunnable {
    286         private boolean canceled;
    287         private int conflictsCount;
    288         private Exception lastException;
    289         private Relation relation;
    290         private Stack<Relation> relationsToDownload;
    291         private Set<Long> downloadedRelationIds;
    292 
    293         public DownloadAllChildrenTask(Dialog parent, Relation r) {
    294             super(tr("Download relation members"), new PleaseWaitProgressMonitor(parent), false /*
    295              * don't
    296              * ignore
    297              * exception
    298              */);
    299             this.relation = r;
    300             relationsToDownload = new Stack<>();
    301             downloadedRelationIds = new HashSet<>();
    302             relationsToDownload.push(this.relation);
     280    abstract class DownloadTask extends PleaseWaitRunnable {
     281        protected boolean canceled;
     282        protected int conflictsCount;
     283        protected Exception lastException;
     284
     285        public DownloadTask(String title, Dialog parent) {
     286            super(title, new PleaseWaitProgressMonitor(parent), false);
    303287        }
    304288
     
    337321                );
    338322            }
     323        }
     324    }
     325
     326    /**
     327     * The asynchronous task for downloading relation members.
     328     */
     329    class DownloadAllChildrenTask extends DownloadTask {
     330        private final Relation relation;
     331        private final Stack<Relation> relationsToDownload;
     332        private final Set<Long> downloadedRelationIds;
     333
     334        public DownloadAllChildrenTask(Dialog parent, Relation r) {
     335            super(tr("Download relation members"), parent);
     336            this.relation = r;
     337            relationsToDownload = new Stack<>();
     338            downloadedRelationIds = new HashSet<>();
     339            relationsToDownload.push(this.relation);
    339340        }
    340341
     
    439440     * The asynchronous task for downloading a set of relations
    440441     */
    441     class DownloadRelationSetTask extends PleaseWaitRunnable {
    442         private boolean canceled;
    443         private int conflictsCount;
    444         private Exception lastException;
    445         private Set<Relation> relations;
     442    class DownloadRelationSetTask extends DownloadTask {
     443        private final Set<Relation> relations;
    446444
    447445        public DownloadRelationSetTask(Dialog parent, Set<Relation> relations) {
    448             super(tr("Download relation members"), new PleaseWaitProgressMonitor(parent), false /*
    449              * don't
    450              * ignore
    451              * exception
    452              */);
     446            super(tr("Download relation members"), parent);
    453447            this.relations = relations;
    454         }
    455 
    456         @Override
    457         protected void cancel() {
    458             canceled = true;
    459             OsmApi.getOsmApi().cancel();
    460         }
    461 
    462         protected void refreshView(Relation relation){
    463             for (int i=0; i < childTree.getRowCount(); i++) {
    464                 Relation reference = (Relation)childTree.getPathForRow(i).getLastPathComponent();
    465                 if (reference == relation) {
    466                     model.refreshNode(childTree.getPathForRow(i));
    467                 }
    468             }
    469         }
    470 
    471         @Override
    472         protected void finish() {
    473             if (canceled)
    474                 return;
    475             if (lastException != null) {
    476                 ExceptionDialogUtil.explainException(lastException);
    477                 return;
    478             }
    479 
    480             if (conflictsCount > 0) {
    481                 JOptionPane.showMessageDialog(
    482                         Main.parent,
    483                         trn("There was {0} conflict during import.",
    484                                 "There were {0} conflicts during import.",
    485                                 conflictsCount, conflictsCount),
    486                                 trn("Conflict in data", "Conflicts in data", conflictsCount),
    487                                 JOptionPane.WARNING_MESSAGE
    488                 );
    489             }
    490448        }
    491449
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r7083 r7092  
    698698    }
    699699
     700    private static void prepareFileChooser(String url, JFileChooser fc) {
     701        if (url == null || url.trim().length() == 0) return;
     702        URL sourceUrl = null;
     703        try {
     704            sourceUrl = new URL(url);
     705        } catch(MalformedURLException e) {
     706            File f = new File(url);
     707            if (f.isFile()) {
     708                f = f.getParentFile();
     709            }
     710            if (f != null) {
     711                fc.setCurrentDirectory(f);
     712            }
     713            return;
     714        }
     715        if (sourceUrl.getProtocol().startsWith("file")) {
     716            File f = new File(sourceUrl.getPath());
     717            if (f.isFile()) {
     718                f = f.getParentFile();
     719            }
     720            if (f != null) {
     721                fc.setCurrentDirectory(f);
     722            }
     723        }
     724    }
     725
    700726    protected class EditSourceEntryDialog extends ExtendedDialog {
    701727
     
    765791                putValue(SMALL_ICON, ImageProvider.get("open"));
    766792                putValue(SHORT_DESCRIPTION, tr("Launch a file chooser to select a file"));
    767             }
    768 
    769             protected void prepareFileChooser(String url, JFileChooser fc) {
    770                 if (url == null || url.trim().length() == 0) return;
    771                 URL sourceUrl = null;
    772                 try {
    773                     sourceUrl = new URL(url);
    774                 } catch(MalformedURLException e) {
    775                     File f = new File(url);
    776                     if (f.isFile()) {
    777                         f = f.getParentFile();
    778                     }
    779                     if (f != null) {
    780                         fc.setCurrentDirectory(f);
    781                     }
    782                     return;
    783                 }
    784                 if (sourceUrl.getProtocol().startsWith("file")) {
    785                     File f = new File(sourceUrl.getPath());
    786                     if (f.isFile()) {
    787                         f = f.getParentFile();
    788                     }
    789                     if (f != null) {
    790                         fc.setCurrentDirectory(f);
    791                     }
    792                 }
    793793            }
    794794
     
    15011501            }
    15021502
    1503             protected void prepareFileChooser(String url, JFileChooser fc) {
    1504                 if (url == null || url.trim().length() == 0) return;
    1505                 URL sourceUrl = null;
    1506                 try {
    1507                     sourceUrl = new URL(url);
    1508                 } catch(MalformedURLException e) {
    1509                     File f = new File(url);
    1510                     if (f.isFile()) {
    1511                         f = f.getParentFile();
    1512                     }
    1513                     if (f != null) {
    1514                         fc.setCurrentDirectory(f);
    1515                     }
    1516                     return;
    1517                 }
    1518                 if (sourceUrl.getProtocol().startsWith("file")) {
    1519                     File f = new File(sourceUrl.getPath());
    1520                     if (f.isFile()) {
    1521                         f = f.getParentFile();
    1522                     }
    1523                     if (f != null) {
    1524                         fc.setCurrentDirectory(f);
    1525                     }
    1526                 }
    1527             }
    1528 
    15291503            @Override
    15301504            public void actionPerformed(ActionEvent e) {
  • trunk/src/org/openstreetmap/josm/io/OsmServerBackreferenceReader.java

    r7050 r7092  
    2222 * OsmServerBackreferenceReader fetches the primitives from the OSM server which
    2323 * refer to a specific primitive. For a {@link org.openstreetmap.josm.data.osm.Node Node}, ways and relations are retrieved
    24  * which refer to the node. For a {@link Way} or a {@link Relation}, only relations are
    25  * read.
     24 * which refer to the node. For a {@link Way} or a {@link Relation}, only relations are read.
    2625 *
    2726 * OsmServerBackreferenceReader uses the API calls <code>[node|way|relation]/#id/relations</code>
     
    3231 * to complete incomplete primitives.
    3332 *
    34  *
     33 * @since 1806
    3534 */
    3635public class OsmServerBackreferenceReader extends OsmServerReader {
     
    123122    }
    124123
    125     /**
    126      * Reads referring ways from the API server and replies them in a {@link DataSet}
    127      *
    128      * @return the data set
    129      * @throws OsmTransferException
    130      */
    131     protected DataSet getReferringWays(ProgressMonitor progressMonitor) throws OsmTransferException {
     124    private DataSet getReferringPrimitives(ProgressMonitor progressMonitor, String type, String message) throws OsmTransferException {
    132125        progressMonitor.beginTask(null, 2);
    133126        try {
    134             progressMonitor.indeterminateSubTask(tr("Downloading from OSM Server..."));
     127            progressMonitor.subTask(tr("Contacting OSM Server..."));
    135128            StringBuilder sb = new StringBuilder();
    136             sb.append(primitiveType.getAPIName())
    137             .append("/").append(id).append("/ways");
     129            sb.append(primitiveType.getAPIName()).append("/").append(id).append(type);
    138130
    139131            try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
    140132                if (in == null)
    141133                    return null;
    142                 progressMonitor.subTask(tr("Downloading referring ways ..."));
     134                progressMonitor.subTask(message);
    143135                return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
    144136            }
     
    156148
    157149    /**
     150     * Reads referring ways from the API server and replies them in a {@link DataSet}
     151     *
     152     * @return the data set
     153     * @throws OsmTransferException
     154     */
     155    protected DataSet getReferringWays(ProgressMonitor progressMonitor) throws OsmTransferException {
     156        return getReferringPrimitives(progressMonitor, "/ways", tr("Downloading referring ways ..."));
     157    }
     158
     159    /**
    158160     * Reads referring relations from the API server and replies them in a {@link DataSet}
    159161     *
     
    163165     */
    164166    protected DataSet getReferringRelations(ProgressMonitor progressMonitor) throws OsmTransferException {
    165         progressMonitor.beginTask(null, 2);
    166         try {
    167             progressMonitor.subTask(tr("Contacting OSM Server..."));
    168             StringBuilder sb = new StringBuilder();
    169             sb.append(primitiveType.getAPIName())
    170             .append("/").append(id).append("/relations");
    171 
    172             try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
    173                 if (in == null)
    174                     return null;
    175                 progressMonitor.subTask(tr("Downloading referring relations ..."));
    176                 return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
    177             }
    178         } catch(OsmTransferException e) {
    179             throw e;
    180         } catch (Exception e) {
    181             if (cancel)
    182                 return null;
    183             throw new OsmTransferException(e);
    184         } finally {
    185             progressMonitor.finishTask();
    186             activeConnection = null;
    187         }
     167        return getReferringPrimitives(progressMonitor, "/relations", tr("Downloading referring relations ..."));
    188168    }
    189169
Note: See TracChangeset for help on using the changeset viewer.