source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/ProjectionPreference.java@ 3105

Last change on this file since 3105 was 2990, checked in by jttt, 14 years ago

Fix some eclipse warnings

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