source: josm/src/org/openstreetmap/josm/gui/PreferenceDialog.java@ 79

Last change on this file since 79 was 79, checked in by imi, 18 years ago
  • fixed bug in osm import (now reject id=0)
  • added WMS server layer (not finished)
  • added save state for dialogs
File size: 14.5 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.Font;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.util.Map;
11import java.util.TreeMap;
12import java.util.Vector;
13import java.util.Map.Entry;
14
15import javax.swing.AbstractAction;
16import javax.swing.BorderFactory;
17import javax.swing.Box;
18import javax.swing.DefaultListCellRenderer;
19import javax.swing.JButton;
20import javax.swing.JCheckBox;
21import javax.swing.JColorChooser;
22import javax.swing.JComboBox;
23import javax.swing.JDialog;
24import javax.swing.JLabel;
25import javax.swing.JList;
26import javax.swing.JOptionPane;
27import javax.swing.JPanel;
28import javax.swing.JPasswordField;
29import javax.swing.JScrollPane;
30import javax.swing.JTabbedPane;
31import javax.swing.JTable;
32import javax.swing.JTextField;
33import javax.swing.ListCellRenderer;
34import javax.swing.ListSelectionModel;
35import javax.swing.UIManager;
36import javax.swing.UIManager.LookAndFeelInfo;
37import javax.swing.table.TableCellRenderer;
38
39import org.openstreetmap.josm.Main;
40import org.openstreetmap.josm.data.projection.Projection;
41import org.openstreetmap.josm.tools.ColorHelper;
42import org.openstreetmap.josm.tools.GBC;
43import org.openstreetmap.josm.tools.ImageProvider;
44
45/**
46 * The preference settings.
47 *
48 * @author imi
49 */
50public class PreferenceDialog extends JDialog {
51
52 /**
53 * Action to take place when user pressed the ok button.
54 */
55 class OkAction extends AbstractAction {
56 public OkAction() {
57 super(UIManager.getString("OptionPane.okButtonText"),
58 UIManager.getIcon("OptionPane.okIcon"));
59 putValue(MNEMONIC_KEY, new Integer((String)UIManager.get("OptionPane.okButtonMnemonic")));
60 }
61 public void actionPerformed(ActionEvent e) {
62 Main.pref.put("laf", ((LookAndFeelInfo)lafCombo.getSelectedItem()).getClassName());
63 Main.pref.put("projection", projectionCombo.getSelectedItem().getClass().getName());
64 Main.pref.put("osm-server.url", osmDataServer.getText());
65 Main.pref.put("osm-server.username", osmDataUsername.getText());
66 String pwd = String.valueOf(osmDataPassword.getPassword());
67 if (pwd.equals(""))
68 pwd = null;
69 Main.pref.put("osm-server.password", pwd);
70 Main.pref.put("wmsServerBaseUrl", wmsServerBaseUrl.getText());
71 Main.pref.put("csvImportString", csvImportString.getText());
72 Main.pref.put("drawRawGpsLines", drawRawGpsLines.isSelected());
73 Main.pref.put("forceRawGpsLines", forceRawGpsLines.isSelected());
74
75 for (int i = 0; i < colors.getRowCount(); ++i) {
76 String name = (String)colors.getValueAt(i, 0);
77 Color col = (Color)colors.getValueAt(i, 1);
78 Main.pref.put("color."+name, ColorHelper.color2html(col));
79 }
80
81 if (requiresRestart)
82 JOptionPane.showMessageDialog(PreferenceDialog.this, "You have to restart JOSM for some settings to take effect.");
83 Main.main.repaint();
84 setVisible(false);
85 }
86 }
87
88 /**
89 * Action to take place when user pressed the cancel button.
90 */
91 class CancelAction extends AbstractAction {
92 public CancelAction() {
93 super(UIManager.getString("OptionPane.cancelButtonText"),
94 UIManager.getIcon("OptionPane.cancelIcon"));
95 putValue(MNEMONIC_KEY, new Integer((String)UIManager.get("OptionPane.cancelButtonMnemonic")));
96 }
97 public void actionPerformed(ActionEvent e) {
98 setVisible(false);
99 }
100 }
101
102 /**
103 * Indicate, that the application has to be restarted for the settings to take effect.
104 */
105 private boolean requiresRestart = false;
106 /**
107 * ComboBox with all look and feels.
108 */
109 private JComboBox lafCombo = new JComboBox(UIManager.getInstalledLookAndFeels());
110 /**
111 * Combobox with all projections available
112 */
113 private JComboBox projectionCombo = new JComboBox(Projection.allProjections);
114 /**
115 * The main tab panel.
116 */
117 private JTabbedPane tabPane = new JTabbedPane(JTabbedPane.LEFT);
118
119 /**
120 * Editfield for the Base url to the REST API from OSM.
121 */
122 private JTextField osmDataServer = new JTextField(20);
123 /**
124 * Editfield for the username to the OSM account.
125 */
126 private JTextField osmDataUsername = new JTextField(20);
127 /**
128 * Passwordfield for the userpassword of the REST API.
129 */
130 private JPasswordField osmDataPassword = new JPasswordField(20);
131 /**
132 * Base url of the WMS server. Holds everything except the bbox= argument.
133 */
134 private JTextField wmsServerBaseUrl = new JTextField(20);
135 /**
136 * Comma seperated import string specifier or <code>null</code> if the first
137 * data line should be interpreted as one.
138 */
139 private JTextField csvImportString = new JTextField(20);
140 /**
141 * The checkbox stating whether nodes should be merged together.
142 */
143 private JCheckBox drawRawGpsLines = new JCheckBox("Draw lines between raw gps points.");
144 /**
145 * The checkbox stating whether raw gps lines should be forced.
146 */
147 private JCheckBox forceRawGpsLines = new JCheckBox("Force lines if no line segments imported.");
148
149 private JTable colors;
150
151
152 /**
153 * Create a preference setting dialog from an preferences-file. If the file does not
154 * exist, it will be created.
155 * If the dialog is closed with Ok, the preferences will be stored to the preferences-
156 * file, otherwise no change of the file happens.
157 */
158 public PreferenceDialog() {
159 super(Main.main, "Preferences");
160
161 // look and feel combo box
162 String laf = Main.pref.get("laf");
163 for (int i = 0; i < lafCombo.getItemCount(); ++i) {
164 if (((LookAndFeelInfo)lafCombo.getItemAt(i)).getClassName().equals(laf)) {
165 lafCombo.setSelectedIndex(i);
166 break;
167 }
168 }
169 final ListCellRenderer oldRenderer = lafCombo.getRenderer();
170 lafCombo.setRenderer(new DefaultListCellRenderer(){
171 @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
172 return oldRenderer.getListCellRendererComponent(list, ((LookAndFeelInfo)value).getName(), index, isSelected, cellHasFocus);
173 }
174 });
175 lafCombo.addActionListener(new ActionListener(){
176 public void actionPerformed(ActionEvent e) {
177 requiresRestart = true;
178 }
179 });
180
181 // projection combo box
182 for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
183 if (projectionCombo.getItemAt(i).getClass().getName().equals(Main.pref.get("projection"))) {
184 projectionCombo.setSelectedIndex(i);
185 break;
186 }
187 }
188 projectionCombo.addActionListener(new ActionListener(){
189 public void actionPerformed(ActionEvent e) {
190 requiresRestart = true;
191 }
192 });
193
194 // drawRawGpsLines
195 drawRawGpsLines.addActionListener(new ActionListener(){
196 public void actionPerformed(ActionEvent e) {
197 if (!drawRawGpsLines.isSelected())
198 forceRawGpsLines.setSelected(false);
199 forceRawGpsLines.setEnabled(drawRawGpsLines.isSelected());
200 }
201 });
202
203 osmDataServer.setText(Main.pref.get("osm-server.url"));
204 osmDataUsername.setText(Main.pref.get("osm-server.username"));
205 osmDataPassword.setText(Main.pref.get("osm-server.password"));
206 wmsServerBaseUrl.setText(Main.pref.get("wmsServerBaseUrl", "http://wms.jpl.nasa.gov/wms.cgi?request=GetMap&width=512&height=512&layers=global_mosaic&styles=&srs=EPSG:4326&format=image/jpeg&"));
207 csvImportString.setText(Main.pref.get("csvImportString"));
208 drawRawGpsLines.setSelected(Main.pref.getBoolean("drawRawGpsLines"));
209 forceRawGpsLines.setToolTipText("Force drawing of lines if the imported data contain no line information.");
210 forceRawGpsLines.setSelected(Main.pref.getBoolean("forceRawGpsLines"));
211 forceRawGpsLines.setEnabled(drawRawGpsLines.isSelected());
212
213
214
215 Map<String,String> allColors = new TreeMap<String, String>(Main.pref.getAllPrefix("color."));
216
217 Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
218 for (Entry<String,String> e : allColors.entrySet()) {
219 Vector<Object> row = new Vector<Object>(2);
220 row.add(e.getKey().substring("color.".length()));
221 row.add(ColorHelper.html2color(e.getValue()));
222 rows.add(row);
223 }
224 Vector<Object> cols = new Vector<Object>(2);
225 cols.add("Color");
226 cols.add("Name");
227 colors = new JTable(rows, cols){
228 @Override public boolean isCellEditable(int row, int column) {
229 return false;
230 }
231 };
232 colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
233 final TableCellRenderer oldColorsRenderer = colors.getDefaultRenderer(Object.class);
234 colors.setDefaultRenderer(Object.class, new TableCellRenderer(){
235 public Component getTableCellRendererComponent(JTable t, Object o, boolean selected, boolean focus, int row, int column) {
236 if (column == 1) {
237 JLabel l = new JLabel(ColorHelper.color2html((Color)o));
238 l.setBackground((Color)o);
239 l.setOpaque(true);
240 return l;
241 }
242 return oldColorsRenderer.getTableCellRendererComponent(t,o,selected,focus,row,column);
243 }
244 });
245 colors.getColumnModel().getColumn(1).setWidth(100);
246
247 JButton colorEdit = new JButton("Choose");
248 colorEdit.addActionListener(new ActionListener(){
249 public void actionPerformed(ActionEvent e) {
250 if (colors.getSelectedRowCount() == 0) {
251 JOptionPane.showMessageDialog(PreferenceDialog.this, "Please select a color.");
252 return;
253 }
254 int sel = colors.getSelectedRow();
255 JColorChooser chooser = new JColorChooser((Color)colors.getValueAt(sel, 1));
256 int answer = JOptionPane.showConfirmDialog(PreferenceDialog.this, chooser, "Choose a color for "+colors.getValueAt(sel, 0), JOptionPane.OK_CANCEL_OPTION);
257 if (answer == JOptionPane.OK_OPTION)
258 colors.setValueAt(chooser.getColor(), sel, 1);
259 }
260 });
261
262 // setting tooltips
263 osmDataServer.setToolTipText("The base URL to the OSM server (REST API)");
264 osmDataUsername.setToolTipText("Login name (email) to the OSM account.");
265 osmDataPassword.setToolTipText("Login password to the OSM account. Leave blank to not store any password.");
266 wmsServerBaseUrl.setToolTipText("The base URL to the server retrieving WMS background pictures from.");
267 csvImportString.setToolTipText("<html>Import string specification. lat/lon and time are imported.<br>" +
268 "<b>lat</b>: The latitude coordinate<br>" +
269 "<b>lon</b>: The longitude coordinate<br>" +
270 "<b>time</b>: The measured time as string<br>" +
271 "<b>ignore</b>: Skip this field<br>" +
272 "An example: \"ignore ignore lat lon\" will use ' ' as delimiter, skip the first two values and read then lat/lon.<br>" +
273 "Other example: \"lat,lon\" will just read lat/lon values comma seperated.</html>");
274 drawRawGpsLines.setToolTipText("If your gps device draw to few lines, select this to draw lines along your way.");
275 colors.setToolTipText("Colors used by different objects in JOSM.");
276
277 // creating the gui
278
279 // Display tab
280 JPanel display = createPreferenceTab("display", "Display Settings", "Various settings that influence the visual representation of the whole program.");
281 display.add(new JLabel("Look and Feel"), GBC.std());
282 display.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
283 display.add(lafCombo, GBC.eol().fill(GBC.HORIZONTAL));
284 display.add(drawRawGpsLines, GBC.eol().insets(20,0,0,0));
285 display.add(forceRawGpsLines, GBC.eop().insets(40,0,0,0));
286 display.add(new JLabel("Colors"), GBC.eol());
287 colors.setPreferredScrollableViewportSize(new Dimension(100,112));
288 display.add(new JScrollPane(colors), GBC.eol().fill(GBC.BOTH));
289 display.add(colorEdit, GBC.eol().anchor(GBC.EAST));
290 //display.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
291
292 // Connection tab
293 JPanel con = createPreferenceTab("connection", "Connection Settings", "Connection Settings to the OSM server.");
294 con.add(new JLabel("Base Server URL"), GBC.std());
295 con.add(osmDataServer, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
296 con.add(new JLabel("OSM username (email)"), GBC.std());
297 con.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
298 con.add(new JLabel("OSM password"), GBC.std());
299 con.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0));
300 JLabel warning = new JLabel("<html>" +
301 "WARNING: The password is stored in plain text in the preferences file.<br>" +
302 "The password is transfered in plain text to the server, encoded in the url.<br>" +
303 "<b>Do not use a valuable Password.</b></html>");
304 warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
305 con.add(warning, GBC.eop().fill(GBC.HORIZONTAL));
306 con.add(new JLabel("WMS server base url (everything except bbox-parameter)"), GBC.eol());
307 con.add(wmsServerBaseUrl, GBC.eop().fill(GBC.HORIZONTAL));
308 con.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
309 con.add(new JLabel("CSV import specification (empty: read from first line in data)"), GBC.eol());
310 con.add(csvImportString, GBC.eop().fill(GBC.HORIZONTAL));
311 con.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
312
313 // Map tab
314 JPanel map = createPreferenceTab("map", "Map Settings", "Settings for the map projection and data interpretation.");
315 map.add(new JLabel("Projection method"), GBC.std());
316 map.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
317 map.add(projectionCombo, GBC.eol().fill(GBC.HORIZONTAL).insets(0,0,0,5));
318 map.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
319
320
321 tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
322
323 // OK/Cancel panel at bottom
324 JPanel okPanel = new JPanel(new GridBagLayout());
325 okPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
326 okPanel.add(new JButton(new OkAction()), GBC.std());
327 okPanel.add(new JButton(new CancelAction()), GBC.std());
328 okPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
329
330 // merging all in the content pane
331 getContentPane().setLayout(new GridBagLayout());
332 getContentPane().add(tabPane, GBC.eol().fill());
333 getContentPane().add(okPanel, GBC.eol().fill(GBC.HORIZONTAL));
334
335 setModal(true);
336 pack();
337 Dimension s = Main.main.getSize();
338 setLocation(Main.main.getX()+s.width/2-getWidth()/2, Main.main.getY()+s.height/2-getHeight()/2);
339 }
340
341 /**
342 * Construct a JPanel for the preference settings. Layout is GridBagLayout
343 * and a centered title label and the description are added.
344 * @param icon The name of the icon.
345 * @param title The title of this preference tab.
346 * @param desc A description in one sentence for this tab. Will be displayed
347 * italic under the title.
348 * @return The created panel ready to add other controls.
349 */
350 private JPanel createPreferenceTab(String icon, String title, String desc) {
351 JPanel p = new JPanel(new GridBagLayout());
352 p.add(new JLabel(title), GBC.eol().anchor(GBC.CENTER).insets(0,5,0,10));
353
354 JLabel descLabel = new JLabel("<html>"+desc+"</html>");
355 descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
356 p.add(descLabel, GBC.eol().insets(5,0,5,20).fill(GBC.HORIZONTAL));
357
358 tabPane.addTab(null, ImageProvider.get("preferences", icon), p);
359 tabPane.setToolTipTextAt(tabPane.getTabCount()-1, desc);
360 return p;
361 }
362}
Note: See TracBrowser for help on using the repository browser.