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

Last change on this file since 18 was 18, checked in by imi, 19 years ago

added Server connection to osm server.

File size: 11.4 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import java.awt.Component;
4import java.awt.Dimension;
5import java.awt.Font;
6import java.awt.GridBagLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9
10import javax.swing.AbstractAction;
11import javax.swing.BorderFactory;
12import javax.swing.Box;
13import javax.swing.DefaultListCellRenderer;
14import javax.swing.JButton;
15import javax.swing.JCheckBox;
16import javax.swing.JComboBox;
17import javax.swing.JComponent;
18import javax.swing.JDialog;
19import javax.swing.JLabel;
20import javax.swing.JList;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.JPasswordField;
24import javax.swing.JTabbedPane;
25import javax.swing.JTextField;
26import javax.swing.ListCellRenderer;
27import javax.swing.UIManager;
28import javax.swing.UIManager.LookAndFeelInfo;
29
30import org.openstreetmap.josm.data.Preferences;
31import org.openstreetmap.josm.data.Preferences.PreferencesException;
32import org.openstreetmap.josm.data.projection.Projection;
33
34/**
35 * The preference settings.
36 *
37 * @author imi
38 */
39public class PreferenceDialog extends JDialog {
40
41 /**
42 * Action to take place when user pressed the ok button.
43 */
44 class OkAction extends AbstractAction {
45 public OkAction() {
46 super(UIManager.getString("OptionPane.okButtonText"),
47 UIManager.getIcon("OptionPane.okIcon"));
48 putValue(MNEMONIC_KEY, new Integer((String)UIManager.get("OptionPane.okButtonMnemonic")));
49 }
50 public void actionPerformed(ActionEvent e) {
51 Main.pref.laf = (LookAndFeelInfo)lafCombo.getSelectedItem();
52 Projection projection = (Projection)projectionCombo.getSelectedItem();
53 projection.commitConfigurationPanel();
54 Main.pref.setProjection(projection);
55 Main.pref.mergeNodes = mergeNodes.isSelected();
56 Main.pref.osmDataServer = osmDataServer.getText();
57 Main.pref.osmDataUsername = osmDataUsername.getText();
58 Main.pref.osmDataPassword = String.valueOf(osmDataPassword.getPassword());
59 if (Main.pref.osmDataPassword == "")
60 Main.pref.osmDataPassword = null;
61 Main.pref.setDrawRawGpsLines(drawRawGpsLines.isSelected());
62 Main.pref.setForceRawGpsLines(forceRawGpsLines.isSelected());
63 try {
64 Main.pref.save();
65 } catch (PreferencesException x) {
66 x.printStackTrace();
67 JOptionPane.showMessageDialog(PreferenceDialog.this, "Could not save preferences:\n"+x.getMessage());
68 }
69 if (requiresRestart)
70 JOptionPane.showMessageDialog(PreferenceDialog.this, "You have to restart JOSM for some settings to take effect.");
71 setVisible(false);
72 }
73 }
74
75 /**
76 * Action to take place when user pressed the cancel button.
77 */
78 class CancelAction extends AbstractAction {
79 public CancelAction() {
80 super(UIManager.getString("OptionPane.cancelButtonText"),
81 UIManager.getIcon("OptionPane.cancelIcon"));
82 putValue(MNEMONIC_KEY, new Integer((String)UIManager.get("OptionPane.cancelButtonMnemonic")));
83 }
84 public void actionPerformed(ActionEvent e) {
85 setVisible(false);
86 }
87 }
88
89 /**
90 * Indicate, that the application has to be restarted for the settings to take effect.
91 */
92 private boolean requiresRestart = false;
93 /**
94 * ComboBox with all look and feels.
95 */
96 private JComboBox lafCombo = new JComboBox(UIManager.getInstalledLookAndFeels());
97 /**
98 * Combobox with all projections available
99 */
100 private JComboBox projectionCombo = new JComboBox(Preferences.allProjections.clone());
101 /**
102 * The main tab panel.
103 */
104 private JTabbedPane tabPane = new JTabbedPane(JTabbedPane.LEFT);
105
106 /**
107 * Editfield for the Base url to the REST API from OSM.
108 */
109 private JTextField osmDataServer = new JTextField(20);
110 /**
111 * Editfield for the username to the OSM account.
112 */
113 private JTextField osmDataUsername = new JTextField(20);
114 /**
115 * Passwordfield for the userpassword of the REST API.
116 */
117 private JPasswordField osmDataPassword = new JPasswordField(20);
118 /**
119 * The checkbox stating whether nodes should be merged together.
120 */
121 private JCheckBox drawRawGpsLines = new JCheckBox("Draw lines between raw gps points.");
122 /**
123 * The checkbox stating whether raw gps lines should be forced.
124 */
125 private JCheckBox forceRawGpsLines = new JCheckBox("Force lines if no line segments imported.");
126 /**
127 * The checkbox stating whether nodes should be merged together.
128 */
129 private JCheckBox mergeNodes = new JCheckBox("Merge nodes with equal latitude/longitude.");
130
131 /**
132 * Create a preference setting dialog from an preferences-file. If the file does not
133 * exist, it will be created.
134 * If the dialog is closed with Ok, the preferences will be stored to the preferences-
135 * file, otherwise no change of the file happens.
136 */
137 public PreferenceDialog() {
138 super(Main.main, "Preferences");
139
140 // look and feel combo box
141 final ListCellRenderer oldRenderer = lafCombo.getRenderer();
142 lafCombo.setRenderer(new DefaultListCellRenderer(){
143 @Override
144 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
145 return oldRenderer.getListCellRendererComponent(list, ((LookAndFeelInfo)value).getName(), index, isSelected, cellHasFocus);
146 }});
147 lafCombo.setSelectedItem(Main.pref.laf);
148 lafCombo.addActionListener(new ActionListener(){
149 public void actionPerformed(ActionEvent e) {
150 setRequiresRestart();
151 }});
152
153 // projection combo box
154 for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
155 if (projectionCombo.getItemAt(i).getClass().equals(Main.pref.getProjection().getClass())) {
156 projectionCombo.setSelectedIndex(i);
157 break;
158 }
159 }
160 JButton projectionDetail = new JButton("Configure");
161 projectionDetail.addActionListener(new ActionListener(){
162 public void actionPerformed(ActionEvent e) {
163 Projection p = (Projection)projectionCombo.getSelectedItem();
164 JComponent configurationPanel = p.getConfigurationPanel();
165 if (configurationPanel == null) {
166 JOptionPane.showMessageDialog(PreferenceDialog.this,
167 "This projection does not need any configuration.");
168 return;
169 }
170 JPanel detail = new JPanel(new GridBagLayout());
171 detail.setLayout(new GridBagLayout());
172 detail.add(configurationPanel, GBC.eop().fill());
173 int result = JOptionPane.showConfirmDialog(
174 PreferenceDialog.this, detail, "Configuration of "+p,
175 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
176 if (result != JOptionPane.OK_OPTION)
177 p.getConfigurationPanel(); // rollback
178 }
179 });
180
181 // drawRawGpsLines
182 drawRawGpsLines.addActionListener(new ActionListener(){
183 public void actionPerformed(ActionEvent e) {
184 if (!drawRawGpsLines.isSelected())
185 forceRawGpsLines.setSelected(false);
186 forceRawGpsLines.setEnabled(drawRawGpsLines.isSelected());
187 }
188 });
189
190
191 // tooltips
192 osmDataServer.setToolTipText("The base URL to the OSM server (REST API)");
193 osmDataUsername.setToolTipText("Login name (email) to the OSM account.");
194 osmDataPassword.setToolTipText("Login password to the OSM account. Leave blank to not store any password.");
195 drawRawGpsLines.setToolTipText("If your gps device draw to few lines, select this to draw lines along your track.");
196 drawRawGpsLines.setSelected(Main.pref.isDrawRawGpsLines());
197 forceRawGpsLines.setToolTipText("Force drawing of lines if the imported data contain no line information.");
198 forceRawGpsLines.setSelected(Main.pref.isForceRawGpsLines());
199 mergeNodes.setToolTipText("When importing GPX data, all nodes with exact the same lat/lon are merged.");
200 mergeNodes.setSelected(Main.pref.mergeNodes);
201
202 osmDataServer.setText(Main.pref.osmDataServer);
203 osmDataUsername.setText(Main.pref.osmDataUsername);
204 osmDataPassword.setText(Main.pref.osmDataPassword);
205
206 // Display tab
207 JPanel display = createPreferenceTab("display", "Display Settings", "Various settings that influence the visual representation of the whole program.");
208 display.add(new JLabel("Look and Feel"), GBC.std());
209 display.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
210 display.add(lafCombo, GBC.eol().fill(GBC.HORIZONTAL));
211 display.add(drawRawGpsLines, GBC.eol().insets(20,0,0,0));
212 display.add(forceRawGpsLines, GBC.eol().insets(40,0,0,0));
213 display.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
214
215 // Connection tab
216 JPanel con = createPreferenceTab("connection", "Connection Settings", "Connection Settings to the OSM server.");
217 con.add(new JLabel("Base Server URL"), GBC.std());
218 con.add(osmDataServer, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
219 con.add(new JLabel("OSM username (email)"), GBC.std());
220 con.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
221 con.add(new JLabel("OSM password"), GBC.std());
222 con.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0));
223 JLabel warning = new JLabel("<html>" +
224 "WARNING: The password is stored in plain text in the preferences file.<br>" +
225 "The password is transfered in plain text to the server, encoded in the url.<br>" +
226 "<b>Do not use a valuable Password.</b></html>");
227 warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
228 con.add(warning, GBC.eop().fill(GBC.HORIZONTAL));
229
230
231 // Map tab
232 JPanel map = createPreferenceTab("map", "Map Settings", "Settings for the map projection and data interpretation.");
233 map.add(new JLabel("Projection method"), GBC.std());
234 map.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
235 map.add(projectionCombo, GBC.eol().fill(GBC.HORIZONTAL).insets(0,0,0,5));
236 map.add(new JLabel("Projection details:"), GBC.std());
237 map.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
238 map.add(projectionDetail, GBC.eop());
239
240 map.add(new JLabel("GPX import / export"), GBC.eol());
241 map.add(mergeNodes, GBC.eol().insets(20,0,0,0));
242 map.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
243
244
245 tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
246
247 // OK/Cancel panel at bottom
248 JPanel okPanel = new JPanel(new GridBagLayout());
249 okPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
250 okPanel.add(new JButton(new OkAction()), GBC.std());
251 okPanel.add(new JButton(new CancelAction()), GBC.std());
252 okPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
253
254 // merging all in the content pane
255 getContentPane().setLayout(new GridBagLayout());
256 getContentPane().add(tabPane, GBC.eol().fill());
257 getContentPane().add(okPanel, GBC.eol().fill(GBC.HORIZONTAL));
258
259 setModal(true);
260 pack();
261 Dimension s = Main.main.getSize();
262 setLocation(s.width/2-getWidth()/2, s.height/2-getHeight()/2);
263 }
264
265 /**
266 * Construct a JPanel for the preference settings. Layout is GridBagLayout
267 * and a centered title label and the description are added.
268 * @param icon The name of the icon.
269 * @param title The title of this preference tab.
270 * @param desc A description in one sentence for this tab. Will be displayed
271 * italic under the title.
272 * @return The created panel ready to add other controls.
273 */
274 private JPanel createPreferenceTab(String icon, String title, String desc) {
275 JPanel p = new JPanel(new GridBagLayout());
276 p.add(new JLabel(title), GBC.eol().anchor(GBC.CENTER).insets(0,5,0,10));
277
278 JLabel descLabel = new JLabel("<html>"+desc+"</html>");
279 descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
280 p.add(descLabel, GBC.eol().insets(5,0,5,20).fill(GBC.HORIZONTAL));
281
282 tabPane.addTab(null, ImageProvider.get("preferences", icon), p);
283 tabPane.setToolTipTextAt(tabPane.getTabCount()-1, desc);
284 return p;
285 }
286
287 /**
288 * Remember, that the settings made requires a restart of the application.
289 * Called from various actionListener - classes
290 */
291 protected void setRequiresRestart() {
292 requiresRestart = true;
293 }
294}
Note: See TracBrowser for help on using the repository browser.