Ticket #14877: projection-default.patch

File projection-default.patch, 5.9 KB (added by bastiK, 22 months ago)
  • src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java

     
    1515import java.util.Map;
    1616
    1717import javax.swing.BorderFactory;
     18import javax.swing.JButton;
    1819import javax.swing.JLabel;
    1920import javax.swing.JOptionPane;
    2021import javax.swing.JPanel;
     
    6667        }
    6768    }
    6869
    69     private static List<ProjectionChoice> projectionChoices = new ArrayList<>();
    70     private static Map<String, ProjectionChoice> projectionChoicesById = new HashMap<>();
     70    private static final List<ProjectionChoice> projectionChoices = new ArrayList<>();
     71    private static final Map<String, ProjectionChoice> projectionChoicesById = new HashMap<>();
    7172
    7273    /**
    7374     * WGS84: Directly use latitude / longitude values as x/y.
     
    268269        return Collections.unmodifiableList(projectionChoices);
    269270    }
    270271
    271     private static final StringProperty PROP_PROJECTION = new StringProperty("projection", mercator.getId());
     272    private static String projectionChoice;
     273    private static final Map<String, Collection<String>> projectionChoicesSub = new HashMap<>();
     274   
     275    private static final StringProperty PROP_PROJECTION_DEFAULT = new StringProperty("projection.default", mercator.getId());
    272276    private static final StringProperty PROP_COORDINATES = new StringProperty("coordinates", null);
    273     private static final CollectionProperty PROP_SUB_PROJECTION = new CollectionProperty("projection.sub", null);
     277    private static final CollectionProperty PROP_SUB_PROJECTION_DEFAULT = new CollectionProperty("projection.default.sub", null);
    274278    public static final StringProperty PROP_SYSTEM_OF_MEASUREMENT = new StringProperty("system_of_measurement", "Metric");
    275279    private static final String[] unitsValues = ALL_SYSTEMS.keySet().toArray(new String[ALL_SYSTEMS.size()]);
    276280    private static final String[] unitsValuesTr = new String[unitsValues.length];
     
    323327
    324328    @Override
    325329    public void addGui(PreferenceTabbedPane gui) {
    326         ProjectionChoice pc = setupProjectionCombo();
     330        final ProjectionChoice pc = setupProjectionCombo();
    327331
    328332        for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
    329333            if (coordinatesCombo.getItemAt(i).name().equals(PROP_COORDINATES.get())) {
     
    357361        projectionCodeLabel.setLabelFor(projectionCode);
    358362        projectionNameLabel.setLabelFor(projectionName);
    359363
     364        JButton btnSetAsDefault = new JButton(tr("Set as default"));
     365        projPanel.add(btnSetAsDefault, GBC.eol().insets(5, 10, 5, 5));
     366        btnSetAsDefault.addActionListener(e -> {
     367            String id = pc.getId();
     368            Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
     369            setProjection(id, prefs, true);
     370        });
     371
    360372        projPanel.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 10));
    361373        projPanel.add(new JLabel(tr("Display coordinates as")), GBC.std().insets(5, 5, 0, 5));
    362374        projPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     
    401413        String id = pc.getId();
    402414        Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
    403415
    404         setProjection(id, prefs);
     416        setProjection(id, prefs, false);
    405417
    406418        if (PROP_COORDINATES.put(((CoordinateFormat) coordinatesCombo.getSelectedItem()).name())) {
    407419            CoordinateFormat.setCoordinateFormat((CoordinateFormat) coordinatesCombo.getSelectedItem());
     
    414426    }
    415427
    416428    public static void setProjection() {
    417         setProjection(PROP_PROJECTION.get(), PROP_SUB_PROJECTION.get());
     429        setProjection(PROP_PROJECTION_DEFAULT.get(), PROP_SUB_PROJECTION_DEFAULT.get(), false);
    418430    }
    419431
    420     public static void setProjection(String id, Collection<String> pref) {
     432    public static void setProjection(String id, Collection<String> pref, boolean makeDefault) {
    421433        ProjectionChoice pc = projectionChoicesById.get(id);
    422434
    423435        if (pc == null) {
     
    431443            pc = mercator;
    432444        }
    433445        id = pc.getId();
    434         PROP_PROJECTION.put(id);
    435         PROP_SUB_PROJECTION.put(pref);
    436         Main.pref.putCollection("projection.sub."+id, pref);
     446        if (makeDefault) {
     447            PROP_PROJECTION_DEFAULT.put(id);
     448            PROP_SUB_PROJECTION_DEFAULT.put(pref);
     449            Main.pref.putCollection("projection.default.sub."+id, pref);
     450        } else {
     451            projectionChoice = id;
     452            projectionChoicesSub.put(id, pref);
     453        }
    437454        pc.setPreferences(pref);
    438455        Projection proj = pc.getProjection();
    439456        Main.setProjection(proj);
     
    466483     * @return the choice class for user selection
    467484     */
    468485    private ProjectionChoice setupProjectionCombo() {
     486        String pcId = projectionChoice != null ? projectionChoice : PROP_PROJECTION_DEFAULT.get();
    469487        ProjectionChoice pc = null;
    470488        for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
    471489            ProjectionChoice pc1 = projectionCombo.getItemAt(i);
    472490            pc1.setPreferences(getSubprojectionPreference(pc1));
    473             if (pc1.getId().equals(PROP_PROJECTION.get())) {
     491            if (pc1.getId().equals(pcId)) {
    474492                projectionCombo.setSelectedIndex(i);
    475493                selectedProjectionChanged(pc1);
    476494                pc = pc1;
     
    489507    }
    490508
    491509    private static Collection<String> getSubprojectionPreference(ProjectionChoice pc) {
    492         return Main.pref.getCollection("projection.sub."+pc.getId(), null);
     510        Collection<String> sessionValue = projectionChoicesSub.get(pc.getId());
     511        if (sessionValue != null)
     512            return sessionValue;
     513        return Main.pref.getCollection("projection.default.sub."+pc.getId(), null);
    493514    }
    494515
    495516    @Override