1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.CardLayout;
5 import java.awt.Component;
6 import java.awt.FlowLayout;
7 import java.awt.GridLayout;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.KeyAdapter;
11 import java.awt.event.KeyEvent;
12 import java.net.MalformedURLException;
15 import javax.swing.BorderFactory;
16 import javax.swing.ButtonGroup;
17 import javax.swing.JButton;
18 import javax.swing.JComboBox;
19 import javax.swing.JDialog;
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
22 import javax.swing.JRadioButton;
23 import javax.swing.JTextField;
25 import tim.prune.I18nManager;
26 import tim.prune.gui.map.CloudmadeMapSource;
27 import tim.prune.gui.map.MapSource;
28 import tim.prune.gui.map.MapSourceLibrary;
29 import tim.prune.gui.map.OsmMapSource;
32 * Class to handle the adding of a new map source
34 public class AddMapSourceDialog
36 private SetMapBgFunction _parent = null;
37 private JDialog _addDialog = null;
38 private JRadioButton[] _typeRadios = null;
39 private JPanel _cards = null;
40 // controls for osm panel
41 private JTextField _oNameField = null;
42 private JTextField _baseUrlField = null, _topUrlField = null;
43 private JComboBox _oZoomCombo = null;
44 // controls for cloudmade panel
45 private JTextField _cNameField = null;
46 private JTextField _cStyleField = null;
47 private JComboBox _cZoomCombo = null;
48 private JButton _okButton = null;
53 * @param inParent parent dialog
55 public AddMapSourceDialog(JDialog inParentDialog, SetMapBgFunction inParentFunction)
57 _parent = inParentFunction;
58 _addDialog = new JDialog(inParentDialog, I18nManager.getText("dialog.addmapsource.title"), true);
59 _addDialog.add(makeDialogComponents());
60 _addDialog.setLocationRelativeTo(inParentDialog);
66 * Create dialog components
67 * @return Panel containing all gui elements in dialog
69 private Component makeDialogComponents()
71 JPanel dialogPanel = new JPanel();
72 dialogPanel.setLayout(new BorderLayout());
73 // Top panel with two radio buttons to select source type
74 JPanel radioPanel = new JPanel();
75 ButtonGroup radioGroup = new ButtonGroup();
76 radioPanel.setLayout(new GridLayout(1, 0));
77 _typeRadios = new JRadioButton[2];
78 _typeRadios[0] = new JRadioButton("Openstreetmap");
79 radioGroup.add(_typeRadios[0]);
80 radioPanel.add(_typeRadios[0]);
81 _typeRadios[1] = new JRadioButton("Cloudmade");
82 radioGroup.add(_typeRadios[1]);
83 radioPanel.add(_typeRadios[1]);
84 _typeRadios[0].setSelected(true);
85 // listener for clicks on type radios
86 ActionListener typeListener = new ActionListener() {
87 public void actionPerformed(ActionEvent arg0) {
88 CardLayout cl = (CardLayout) _cards.getLayout();
89 if (_typeRadios[0].isSelected()) {cl.first(_cards);}
90 else {cl.last(_cards);}
94 _typeRadios[0].addActionListener(typeListener);
95 _typeRadios[1].addActionListener(typeListener);
96 radioPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
97 dialogPanel.add(radioPanel, BorderLayout.NORTH);
99 _cards = new JPanel();
100 _cards.setLayout(new CardLayout());
102 KeyAdapter keyListener = new KeyAdapter() {
103 public void keyReleased(KeyEvent e) {
104 super.keyReleased(e);
108 // openstreetmap panel
109 JPanel osmPanel = new JPanel();
110 osmPanel.setLayout(new GridLayout(0, 2, 5, 5));
111 osmPanel.setBorder(BorderFactory.createEmptyBorder(6, 2, 4, 2));
112 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.sourcename")));
113 _oNameField = new JTextField(18);
114 _oNameField.addKeyListener(keyListener);
115 osmPanel.add(_oNameField);
117 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer1url")));
118 _baseUrlField = new JTextField(18);
119 _baseUrlField.addKeyListener(keyListener);
120 osmPanel.add(_baseUrlField);
122 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer2url")));
123 _topUrlField = new JTextField(18);
124 _topUrlField.addKeyListener(keyListener);
125 osmPanel.add(_topUrlField);
127 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.maxzoom")));
128 _oZoomCombo = new JComboBox();
129 for (int i=10; i<=20; i++) {
130 _oZoomCombo.addItem("" + i);
132 osmPanel.add(_oZoomCombo);
133 _cards.add(osmPanel, "card1");
134 // Panel for cloudmade source
135 JPanel cloudPanel = new JPanel();
136 cloudPanel.setLayout(new GridLayout(0, 2, 5, 5));
137 cloudPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.sourcename")));
138 _cNameField = new JTextField(18);
139 _cNameField.addKeyListener(keyListener);
140 cloudPanel.add(_cNameField);
141 cloudPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.cloudstyle")));
142 _cStyleField = new JTextField(18);
143 _cStyleField.addKeyListener(keyListener);
144 cloudPanel.add(_cStyleField);
145 cloudPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.maxzoom")));
146 _cZoomCombo = new JComboBox();
147 for (int i=10; i<=20; i++) {
148 _cZoomCombo.addItem("" + i);
150 cloudPanel.add(_cZoomCombo);
151 cloudPanel.add(new JLabel(" ")); // force four rows to space text boxes properly
152 _cards.add(cloudPanel, "card2");
154 JPanel holderPanel = new JPanel();
155 holderPanel.setLayout(new BorderLayout());
156 holderPanel.add(_cards, BorderLayout.NORTH);
157 dialogPanel.add(holderPanel, BorderLayout.CENTER);
159 // button panel at bottom
160 JPanel buttonPanel = new JPanel();
161 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
162 _okButton = new JButton(I18nManager.getText("button.ok"));
163 ActionListener okListener = new ActionListener() {
164 public void actionPerformed(ActionEvent e)
169 _okButton.addActionListener(okListener);
170 buttonPanel.add(_okButton);
171 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
172 cancelButton.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent e)
175 _addDialog.dispose();
178 buttonPanel.add(cancelButton);
179 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
185 * Init and show the dialog
187 public void showDialog()
189 _oNameField.setText("");
190 _baseUrlField.setText("");
191 _topUrlField.setText("");
192 _oZoomCombo.setSelectedIndex(8);
193 _cNameField.setText("");
194 _cStyleField.setText("");
195 _cZoomCombo.setSelectedIndex(8);
196 _okButton.setEnabled(false);
197 _addDialog.setVisible(true);
202 * Check the currently entered details and enable the OK button if it looks OK
204 private void enableOK()
207 if (_typeRadios[0].isSelected()) {ok = isOsmPanelOk();}
208 if (_typeRadios[1].isSelected()) {ok = isCloudPanelOk();}
209 _okButton.setEnabled(ok);
213 * Check the openstreetmap panel if all details are complete
214 * @return true if details look ok
216 private boolean isOsmPanelOk()
218 boolean ok = _oNameField.getText().trim().length() > 1;
219 URL baseUrl = null, topUrl = null;
221 // Try to parse base url if given
222 String baseText = _baseUrlField.getText().trim();
223 if (baseText.length() > 10) {
224 baseUrl = new URL(baseText);
226 else if (baseText.length() > 0) {ok = false;}
227 // Same again for top url if given
228 String topText = _topUrlField.getText().trim();
229 if (topText.length() > 10) {
230 topUrl = new URL(topText);
232 else if (topText.length() > 0) {ok = false;}
233 } catch (MalformedURLException e) {
236 // looks ok if at least one url given
237 return (ok && (baseUrl != null || topUrl != null));
241 * Check the cloudmade panel if all details are complete
242 * @return true if details look ok
244 private boolean isCloudPanelOk()
246 boolean ok = _cNameField.getText().trim().length() > 1;
249 styleNum = Integer.parseInt(_cStyleField.getText());
251 catch (NumberFormatException nfe) {}
252 return (ok && styleNum > 0);
256 * Finish by adding the requested source and refreshing the parent
258 private void finish()
260 MapSource newSource = null;
261 if (_typeRadios[0].isSelected())
263 // Openstreetmap source
264 String sourceName = getUniqueSourcename(_oNameField.getText());
265 String url1 = _baseUrlField.getText().trim();
266 String url2 = _topUrlField.getText().trim();
267 newSource = new OsmMapSource(sourceName, url1, url2, _oZoomCombo.getSelectedIndex()+10);
269 else if (_typeRadios[1].isSelected())
271 String sourceName = getUniqueSourcename(_cNameField.getText());
272 newSource = new CloudmadeMapSource(sourceName, _cStyleField.getText(),
273 _cZoomCombo.getSelectedIndex()+10);
275 // Add new source if ok
276 if (newSource != null)
278 MapSourceLibrary.addSource(newSource);
279 // inform setmapbg dialog
280 _parent.updateList();
281 _addDialog.setVisible(false);
286 * Check the given source name if it exists in library already
287 * @param inName name to check
288 * @return unique name not yet in library
290 private static String getUniqueSourcename(String inName)
292 String name = inName;
293 if (name == null) {name = "";}
294 else {name = name.trim();}
295 if (name.equals("")) {
296 name = I18nManager.getText("dialog.addmapsource.noname");
298 // Check there isn't already a map source with this name
299 if (MapSourceLibrary.hasSourceName(name))
302 while (MapSourceLibrary.hasSourceName(name + suffix)) {