Changeset 12786 in josm for trunk/src


Ignore:
Timestamp:
2017-09-08T19:25:12+02:00 (2 years ago)
Author:
bastiK
Message:

see #15182 - remove GUI references from Projections class

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r12778 r12786  
    1414import java.util.Map;
    1515import java.util.Set;
     16import java.util.function.Supplier;
    1617import java.util.regex.Matcher;
    1718import java.util.regex.Pattern;
     
    4243import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
    4344import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
    44 import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
    45 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    4645import org.openstreetmap.josm.io.CachedFile;
    4746import org.openstreetmap.josm.tools.JosmRuntimeException;
     
    7877
    7978    private static final Set<String> allCodes = new HashSet<>();
    80     private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
     79    private static final Map<String, Supplier<Projection>> projectionSuppliersByCode = new HashMap<>();
    8180    private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
    8281
     
    157156            throw new JosmRuntimeException(ex);
    158157        }
    159 
    160         for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
    161             for (String code : pc.allCodes()) {
    162                 allProjectionChoicesByCode.put(code, pc);
    163             }
    164         }
    165158        allCodes.addAll(inits.keySet());
    166         allCodes.addAll(allProjectionChoicesByCode.keySet());
    167159    }
    168160
     
    242234    public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
    243235        registerBaseProjection(id, new ClassProjFactory(projClass), origin);
     236    }
     237
     238    /**
     239     * Register a projection supplier, that is, a factory class for projections.
     240     * @param code the code of the projection that will be returned
     241     * @param supplier a supplier to return a projection with given code
     242     * @since 12786
     243     */
     244    public static void registerProjectionSupplier(String code, Supplier<Projection> supplier) {
     245        projectionSuppliersByCode.put(code, supplier);
     246        allCodes.add(code);
    244247    }
    245248
     
    351354        Projection proj = projectionsByCode_cache.get(code);
    352355        if (proj != null) return proj;
    353         ProjectionChoice pc = allProjectionChoicesByCode.get(code);
    354         if (pc != null) {
    355             Collection<String> pref = pc.getPreferencesFromCode(code);
    356             pc.setPreferences(pref);
    357             try {
    358                 proj = pc.getProjection();
    359             } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
    360                 Logging.log(Logging.LEVEL_WARN, "Unable to get projection "+code+" with "+pc+':', e);
     356
     357        ProjectionDefinition pd = inits.get(code);
     358        if (pd != null) {
     359            proj = new CustomProjection(pd.name, code, pd.definition);
     360        }
     361        if (proj == null) {
     362            Supplier<Projection> ps = projectionSuppliersByCode.get(code);
     363            if (ps != null) {
     364                proj = ps.get();
    361365            }
    362366        }
    363         if (proj == null) {
    364             ProjectionDefinition pd = inits.get(code);
    365             if (pd == null) return null;
    366             proj = new CustomProjection(pd.name, code, pd.definition);
    367         }
    368         projectionsByCode_cache.put(code, proj);
     367        if (proj != null) {
     368            projectionsByCode_cache.put(code, proj);
     369        }
    369370        return proj;
    370371    }
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java

    r12735 r12786  
    3232import org.openstreetmap.josm.data.projection.CustomProjection;
    3333import org.openstreetmap.josm.data.projection.Projection;
     34import org.openstreetmap.josm.data.projection.Projections;
    3435import org.openstreetmap.josm.gui.ExtendedDialog;
    3536import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
     
    4243import org.openstreetmap.josm.tools.GBC;
    4344import org.openstreetmap.josm.tools.JosmRuntimeException;
     45import org.openstreetmap.josm.tools.Logging;
    4446
    4547/**
     
    255257        projectionChoices.add(c);
    256258        projectionChoicesById.put(c.getId(), c);
     259        for (String code : c.allCodes()) {
     260            Projections.registerProjectionSupplier(code, () -> {
     261                Collection<String> pref = c.getPreferencesFromCode(code);
     262                c.setPreferences(pref);
     263                try {
     264                    return c.getProjection();
     265                } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
     266                    Logging.log(Logging.LEVEL_WARN, "Unable to get projection "+code+" with "+c+':', e);
     267                    return null;
     268                }
     269            });
     270        }
    257271    }
    258272
Note: See TracChangeset for help on using the changeset viewer.