1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 |
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.JComboBox;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 | import javax.swing.JSeparator;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.Bounds;
|
---|
22 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
23 | import org.openstreetmap.josm.data.preferences.CollectionProperty;
|
---|
24 | import org.openstreetmap.josm.data.preferences.ParametrizedCollectionProperty;
|
---|
25 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
26 | import org.openstreetmap.josm.data.projection.Mercator;
|
---|
27 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
28 | import org.openstreetmap.josm.data.projection.ProjectionSubPrefs;
|
---|
29 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
30 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
31 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
32 | import org.openstreetmap.josm.tools.GBC;
|
---|
33 |
|
---|
34 | public class ProjectionPreference implements PreferenceSetting {
|
---|
35 |
|
---|
36 | public static class Factory implements PreferenceSettingFactory {
|
---|
37 | public PreferenceSetting createPreferenceSetting() {
|
---|
38 | return new ProjectionPreference();
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private static final StringProperty PROP_PROJECTION = new StringProperty("projection", Mercator.class.getName());
|
---|
43 | private static final StringProperty PROP_COORDINATES = new StringProperty("coordinates", null);
|
---|
44 | private static final CollectionProperty PROP_SUB_PROJECTION = new CollectionProperty("projection.sub", null);
|
---|
45 | private static final ParametrizedCollectionProperty PROP_PROJECTION_SUBPROJECTION = new ParametrizedCollectionProperty(null) {
|
---|
46 | @Override
|
---|
47 | protected String getKey(String... params) {
|
---|
48 | String name = params[0];
|
---|
49 | String sname = name.substring(name.lastIndexOf(".")+1);
|
---|
50 | return "projection.sub."+sname;
|
---|
51 | }
|
---|
52 | };
|
---|
53 | public static final StringProperty PROP_SYSTEM_OF_MEASUREMENT = new StringProperty("system_of_measurement", "Metric");
|
---|
54 | private static final String[] unitsValues = (new ArrayList<String>(NavigatableComponent.SYSTEMS_OF_MEASUREMENT.keySet())).toArray(new String[0]);
|
---|
55 | private static final String[] unitsValuesTr = new String[unitsValues.length];
|
---|
56 | static {
|
---|
57 | for (int i=0; i<unitsValues.length; ++i) {
|
---|
58 | unitsValuesTr[i] = tr(unitsValues[i]);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Combobox with all projections available
|
---|
64 | */
|
---|
65 | private JComboBox projectionCombo = new JComboBox(Projections.getProjections().toArray());
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Combobox with all coordinate display possibilities
|
---|
69 | */
|
---|
70 | private JComboBox coordinatesCombo = new JComboBox(CoordinateFormat.values());
|
---|
71 |
|
---|
72 | private JComboBox unitsCombo = new JComboBox(unitsValuesTr);
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * This variable holds the JPanel with the projection's preferences. If the
|
---|
76 | * selected projection does not implement this, it will be set to an empty
|
---|
77 | * Panel.
|
---|
78 | */
|
---|
79 | private JPanel projSubPrefPanel;
|
---|
80 | private JPanel projSubPrefPanelWrapper = new JPanel(new GridBagLayout());
|
---|
81 |
|
---|
82 | private JLabel projectionCode = new JLabel();
|
---|
83 | private JLabel bounds = new JLabel();
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * This is the panel holding all projection preferences
|
---|
87 | */
|
---|
88 | private JPanel projPanel = new JPanel(new GridBagLayout());
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * The GridBagConstraints for the Panel containing the ProjectionSubPrefs.
|
---|
92 | * This is required twice in the code, creating it here keeps both occurrences
|
---|
93 | * in sync
|
---|
94 | */
|
---|
95 | static private GBC projSubPrefPanelGBC = GBC.std().fill(GBC.BOTH).weight(1.0, 1.0);
|
---|
96 |
|
---|
97 | public void addGui(PreferenceTabbedPane gui) {
|
---|
98 | setupProjectionCombo();
|
---|
99 |
|
---|
100 | for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
|
---|
101 | if (((CoordinateFormat)coordinatesCombo.getItemAt(i)).name().equals(PROP_COORDINATES.get())) {
|
---|
102 | coordinatesCombo.setSelectedIndex(i);
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (int i = 0; i < unitsValues.length; ++i) {
|
---|
108 | if (unitsValues[i].equals(PROP_SYSTEM_OF_MEASUREMENT.get())) {
|
---|
109 | unitsCombo.setSelectedIndex(i);
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | projPanel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
---|
115 | projPanel.setLayout(new GridBagLayout());
|
---|
116 | projPanel.add(new JLabel(tr("Projection method")), GBC.std().insets(5,5,0,5));
|
---|
117 | projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
118 | projPanel.add(projectionCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
|
---|
119 | projPanel.add(new JLabel(tr("Projection code")), GBC.std().insets(25,5,0,5));
|
---|
120 | projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
121 | projPanel.add(projectionCode, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
|
---|
122 | projPanel.add(new JLabel(tr("Bounds")), GBC.std().insets(25,5,0,5));
|
---|
123 | projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
124 | projPanel.add(bounds, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
|
---|
125 | projSubPrefPanelWrapper.add(projSubPrefPanel, projSubPrefPanelGBC);
|
---|
126 | projPanel.add(projSubPrefPanelWrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(20,5,5,5));
|
---|
127 |
|
---|
128 | projPanel.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0,5,0,10));
|
---|
129 | projPanel.add(new JLabel(tr("Display coordinates as")), GBC.std().insets(5,5,0,5));
|
---|
130 | projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
131 | projPanel.add(coordinatesCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
|
---|
132 | projPanel.add(new JLabel(tr("System of measurement")), GBC.std().insets(5,5,0,5));
|
---|
133 | projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
134 | projPanel.add(unitsCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
|
---|
135 | projPanel.add(GBC.glue(1,1), GBC.std().fill(GBC.HORIZONTAL).weight(1.0, 1.0));
|
---|
136 |
|
---|
137 | JScrollPane scrollpane = new JScrollPane(projPanel);
|
---|
138 | gui.mapcontent.addTab(tr("Map Projection"), scrollpane);
|
---|
139 |
|
---|
140 | updateMeta(Main.getProjection());
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void updateMeta(Projection proj)
|
---|
144 | {
|
---|
145 | projectionCode.setText(proj.toCode());
|
---|
146 | Bounds b = proj.getWorldBoundsLatLon();
|
---|
147 | CoordinateFormat cf = CoordinateFormat.getDefaultFormat();
|
---|
148 | bounds.setText(b.getMin().latToString(cf)+"; "+b.getMin().lonToString(cf)+" : "+b.getMax().latToString(cf)+"; "+b.getMax().lonToString(cf));
|
---|
149 | }
|
---|
150 |
|
---|
151 | public boolean ok() {
|
---|
152 | Projection proj = (Projection) projectionCombo.getSelectedItem();
|
---|
153 |
|
---|
154 | String projname = proj.getClass().getName();
|
---|
155 | Collection<String> prefs = null;
|
---|
156 | if(proj instanceof ProjectionSubPrefs) {
|
---|
157 | prefs = ((ProjectionSubPrefs) proj).getPreferences(projSubPrefPanel);
|
---|
158 | }
|
---|
159 |
|
---|
160 | PROP_PROJECTION.put(projname);
|
---|
161 | setProjection(projname, prefs);
|
---|
162 |
|
---|
163 | if(PROP_COORDINATES.put(((CoordinateFormat)coordinatesCombo.getSelectedItem()).name())) {
|
---|
164 | CoordinateFormat.setCoordinateFormat((CoordinateFormat)coordinatesCombo.getSelectedItem());
|
---|
165 | }
|
---|
166 |
|
---|
167 | int i = unitsCombo.getSelectedIndex();
|
---|
168 | PROP_SYSTEM_OF_MEASUREMENT.put(unitsValues[i]);
|
---|
169 |
|
---|
170 | return false;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static public void setProjection()
|
---|
174 | {
|
---|
175 | setProjection(PROP_PROJECTION.get(), PROP_SUB_PROJECTION.get());
|
---|
176 | }
|
---|
177 |
|
---|
178 | static public void setProjection(String name, Collection<String> coll)
|
---|
179 | {
|
---|
180 | Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
|
---|
181 |
|
---|
182 | Projection proj = null;
|
---|
183 | for (ClassLoader cl : PluginHandler.getResourceClassLoaders()) {
|
---|
184 | try {
|
---|
185 | proj = (Projection) Class.forName(name, true, cl).newInstance();
|
---|
186 | } catch (final Exception e) {
|
---|
187 | }
|
---|
188 | if (proj != null) {
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | if (proj == null) {
|
---|
193 | JOptionPane.showMessageDialog(
|
---|
194 | Main.parent,
|
---|
195 | tr("The projection {0} could not be activated. Using Mercator", name),
|
---|
196 | tr("Error"),
|
---|
197 | JOptionPane.ERROR_MESSAGE
|
---|
198 | );
|
---|
199 | coll = null;
|
---|
200 | proj = new Mercator();
|
---|
201 | name = Main.getProjection().getClass().getName();
|
---|
202 | }
|
---|
203 | PROP_SUB_PROJECTION.put(coll);
|
---|
204 | PROP_PROJECTION_SUBPROJECTION.put(coll, name);
|
---|
205 | if(proj instanceof ProjectionSubPrefs) {
|
---|
206 | ((ProjectionSubPrefs) proj).setPreferences(coll);
|
---|
207 | }
|
---|
208 | Projection oldProj = Main.getProjection();
|
---|
209 | Main.setProjection(proj);
|
---|
210 | if(b != null && (!proj.getClass().getName().equals(oldProj.getClass().getName()) || proj.hashCode() != oldProj.hashCode()))
|
---|
211 | {
|
---|
212 | Main.map.mapView.zoomTo(b);
|
---|
213 | /* TODO - remove layers with fixed projection */
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | private class SBPanel extends JPanel implements ActionListener
|
---|
218 | {
|
---|
219 | private ProjectionSubPrefs p;
|
---|
220 | public SBPanel(ProjectionSubPrefs pr)
|
---|
221 | {
|
---|
222 | super();
|
---|
223 | p = pr;
|
---|
224 | }
|
---|
225 |
|
---|
226 | @Override
|
---|
227 | public void actionPerformed(ActionEvent e) {
|
---|
228 | p.setPreferences(p.getPreferences(this));
|
---|
229 | updateMeta(p);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Handles all the work related to update the projection-specific
|
---|
235 | * preferences
|
---|
236 | * @param proj
|
---|
237 | */
|
---|
238 | private void selectedProjectionChanged(Projection proj) {
|
---|
239 | if(!(proj instanceof ProjectionSubPrefs)) {
|
---|
240 | projSubPrefPanel = new JPanel();
|
---|
241 | } else {
|
---|
242 | ProjectionSubPrefs projPref = (ProjectionSubPrefs) proj;
|
---|
243 | projSubPrefPanel = new SBPanel(projPref);
|
---|
244 | projPref.setupPreferencePanel(projSubPrefPanel, (SBPanel)projSubPrefPanel);
|
---|
245 | }
|
---|
246 |
|
---|
247 | // Don't try to update if we're still starting up
|
---|
248 | int size = projPanel.getComponentCount();
|
---|
249 | if(size < 1)
|
---|
250 | return;
|
---|
251 |
|
---|
252 | // Replace old panel with new one
|
---|
253 | projSubPrefPanelWrapper.removeAll();
|
---|
254 | projSubPrefPanelWrapper.add(projSubPrefPanel, projSubPrefPanelGBC);
|
---|
255 | projPanel.revalidate();
|
---|
256 | projSubPrefPanel.repaint();
|
---|
257 | updateMeta(proj);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Sets up projection combobox with default values and action listener
|
---|
262 | */
|
---|
263 | private void setupProjectionCombo() {
|
---|
264 | boolean found = false;
|
---|
265 | for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
|
---|
266 | Projection proj = (Projection)projectionCombo.getItemAt(i);
|
---|
267 | String name = proj.getClass().getName();
|
---|
268 | if(proj instanceof ProjectionSubPrefs) {
|
---|
269 | ((ProjectionSubPrefs) proj).setPreferences(PROP_PROJECTION_SUBPROJECTION.get(name));
|
---|
270 | }
|
---|
271 | if (name.equals(PROP_PROJECTION.get())) {
|
---|
272 | projectionCombo.setSelectedIndex(i);
|
---|
273 | selectedProjectionChanged(proj);
|
---|
274 | found = true;
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | if (!found)
|
---|
279 | throw new RuntimeException("Couldn't find the current projection in the list of available projections!");
|
---|
280 |
|
---|
281 | projectionCombo.addActionListener(new ActionListener() {
|
---|
282 | public void actionPerformed(ActionEvent e) {
|
---|
283 | JComboBox cb = (JComboBox)e.getSource();
|
---|
284 | Projection proj = (Projection)cb.getSelectedItem();
|
---|
285 | selectedProjectionChanged(proj);
|
---|
286 | }
|
---|
287 | });
|
---|
288 | }
|
---|
289 | }
|
---|