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;
13 import javax.swing.BorderFactory;
14 import javax.swing.ButtonGroup;
15 import javax.swing.JButton;
16 import javax.swing.JComboBox;
17 import javax.swing.JDialog;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.JRadioButton;
21 import javax.swing.JTextField;
23 import tim.prune.I18nManager;
24 import tim.prune.gui.map.CloudmadeMapSource;
25 import tim.prune.gui.map.MapSource;
26 import tim.prune.gui.map.MapSourceLibrary;
27 import tim.prune.gui.map.OsmMapSource;
30 * Class to handle the adding of a new map source
32 public class AddMapSourceDialog
34 private SetMapBgFunction _parent = null;
35 private JDialog _addDialog = null;
36 private JRadioButton[] _typeRadios = null;
37 private JPanel _cards = null;
38 // controls for osm panel
39 private JTextField _oNameField = null;
40 private JTextField _baseUrlField = null, _topUrlField = null;
41 private JComboBox _oZoomCombo = null;
42 // controls for cloudmade panel
43 private JTextField _cNameField = null;
44 private JTextField _cStyleField = null;
45 private JComboBox _cZoomCombo = null;
46 private JButton _okButton = null;
51 * @param inParent parent dialog
53 public AddMapSourceDialog(JDialog inParentDialog, SetMapBgFunction inParentFunction)
55 _parent = inParentFunction;
56 _addDialog = new JDialog(inParentDialog, I18nManager.getText("dialog.addmapsource.title"), true);
57 _addDialog.add(makeDialogComponents());
58 _addDialog.setLocationRelativeTo(inParentDialog);
64 * Create dialog components
65 * @return Panel containing all gui elements in dialog
67 private Component makeDialogComponents()
69 JPanel dialogPanel = new JPanel();
70 dialogPanel.setLayout(new BorderLayout());
71 // Top panel with two radio buttons to select source type
72 JPanel radioPanel = new JPanel();
73 ButtonGroup radioGroup = new ButtonGroup();
74 radioPanel.setLayout(new GridLayout(1, 0));
75 _typeRadios = new JRadioButton[2];
76 _typeRadios[0] = new JRadioButton("Openstreetmap");
77 radioGroup.add(_typeRadios[0]);
78 radioPanel.add(_typeRadios[0]);
79 _typeRadios[1] = new JRadioButton("Cloudmade");
80 radioGroup.add(_typeRadios[1]);
81 radioPanel.add(_typeRadios[1]);
82 _typeRadios[0].setSelected(true);
83 // listener for clicks on type radios
84 ActionListener typeListener = new ActionListener() {
85 public void actionPerformed(ActionEvent arg0) {
86 CardLayout cl = (CardLayout) _cards.getLayout();
87 if (_typeRadios[0].isSelected()) {cl.first(_cards);}
88 else {cl.last(_cards);}
92 _typeRadios[0].addActionListener(typeListener);
93 _typeRadios[1].addActionListener(typeListener);
94 radioPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
95 dialogPanel.add(radioPanel, BorderLayout.NORTH);
97 _cards = new JPanel();
98 _cards.setLayout(new CardLayout());
100 KeyAdapter keyListener = new KeyAdapter() {
101 public void keyReleased(KeyEvent e) {
102 super.keyReleased(e);
106 // openstreetmap panel
107 JPanel osmPanel = new JPanel();
108 osmPanel.setLayout(new GridLayout(0, 2, 5, 5));
109 osmPanel.setBorder(BorderFactory.createEmptyBorder(6, 2, 4, 2));
110 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.sourcename")));
111 _oNameField = new JTextField(18);
112 _oNameField.addKeyListener(keyListener);
113 osmPanel.add(_oNameField);
115 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer1url")));
116 _baseUrlField = new JTextField(18);
117 _baseUrlField.addKeyListener(keyListener);
118 osmPanel.add(_baseUrlField);
120 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer2url")));
121 _topUrlField = new JTextField(18);
122 _topUrlField.addKeyListener(keyListener);
123 osmPanel.add(_topUrlField);
125 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.maxzoom")));
126 _oZoomCombo = new JComboBox();
127 for (int i=10; i<=20; i++) {
128 _oZoomCombo.addItem("" + i);
130 osmPanel.add(_oZoomCombo);
131 _cards.add(osmPanel, "card1");
132 // Panel for cloudmade source
133 JPanel cloudPanel = new JPanel();
134 cloudPanel.setLayout(new GridLayout(0, 2, 5, 5));
135 cloudPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.sourcename")));
136 _cNameField = new JTextField(18);
137 _cNameField.addKeyListener(keyListener);
138 cloudPanel.add(_cNameField);
139 cloudPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.cloudstyle")));
140 _cStyleField = new JTextField(18);
141 _cStyleField.addKeyListener(keyListener);
142 cloudPanel.add(_cStyleField);
143 cloudPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.maxzoom")));
144 _cZoomCombo = new JComboBox();
145 for (int i=10; i<=20; i++) {
146 _cZoomCombo.addItem("" + i);
148 cloudPanel.add(_cZoomCombo);
149 cloudPanel.add(new JLabel(" ")); // force four rows to space text boxes properly
150 _cards.add(cloudPanel, "card2");
152 JPanel holderPanel = new JPanel();
153 holderPanel.setLayout(new BorderLayout());
154 holderPanel.add(_cards, BorderLayout.NORTH);
155 dialogPanel.add(holderPanel, BorderLayout.CENTER);
157 // button panel at bottom
158 JPanel buttonPanel = new JPanel();
159 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
160 _okButton = new JButton(I18nManager.getText("button.ok"));
161 ActionListener okListener = new ActionListener() {
162 public void actionPerformed(ActionEvent e)
167 _okButton.addActionListener(okListener);
168 buttonPanel.add(_okButton);
169 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
170 cancelButton.addActionListener(new ActionListener() {
171 public void actionPerformed(ActionEvent e)
173 _addDialog.dispose();
176 buttonPanel.add(cancelButton);
177 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
183 * Init and show the dialog
185 public void showDialog()
187 _oNameField.setText("");
188 _baseUrlField.setText("");
189 _topUrlField.setText("");
190 _oZoomCombo.setSelectedIndex(8);
191 _cNameField.setText("");
192 _cStyleField.setText("");
193 _cZoomCombo.setSelectedIndex(8);
194 _okButton.setEnabled(false);
195 _addDialog.setVisible(true);
200 * Check the currently entered details and enable the OK button if it looks OK
202 private void enableOK()
205 if (_typeRadios[0].isSelected()) {ok = isOsmPanelOk();}
206 if (_typeRadios[1].isSelected()) {ok = isCloudPanelOk();}
207 _okButton.setEnabled(ok);
211 * Check the openstreetmap panel if all details are complete
212 * @return true if details look ok
214 private boolean isOsmPanelOk()
216 boolean ok = _oNameField.getText().trim().length() > 1;
217 String baseUrl = null, topUrl = null;
218 // Try to parse base url if given
219 String baseText = _baseUrlField.getText().trim();
220 baseUrl = MapSource.fixBaseUrl(baseText);
221 if (baseText.length() > 0 && baseUrl == null) {ok = false;}
222 // Same again for top url if given
223 String topText = _topUrlField.getText().trim();
224 topUrl = MapSource.fixBaseUrl(topText);
225 if (topText.length() > 0 && topUrl == null) {ok = false;}
226 // looks ok if at least one url given
227 return (ok && (baseUrl != null || topUrl != null));
231 * Check the cloudmade panel if all details are complete
232 * @return true if details look ok
234 private boolean isCloudPanelOk()
236 boolean ok = _cNameField.getText().trim().length() > 1;
239 styleNum = Integer.parseInt(_cStyleField.getText());
241 catch (NumberFormatException nfe) {}
242 return (ok && styleNum > 0);
246 * Finish by adding the requested source and refreshing the parent
248 private void finish()
250 MapSource newSource = null;
251 if (_typeRadios[0].isSelected())
253 // Openstreetmap source
254 String sourceName = getUniqueSourcename(_oNameField.getText());
255 String url1 = _baseUrlField.getText().trim();
256 String url2 = _topUrlField.getText().trim();
257 newSource = new OsmMapSource(sourceName, url1, url2, _oZoomCombo.getSelectedIndex()+10);
259 else if (_typeRadios[1].isSelected())
261 String sourceName = getUniqueSourcename(_cNameField.getText());
262 newSource = new CloudmadeMapSource(sourceName, _cStyleField.getText(),
263 _cZoomCombo.getSelectedIndex()+10);
265 // Add new source if ok
266 if (newSource != null)
268 MapSourceLibrary.addSource(newSource);
269 // inform setmapbg dialog
270 _parent.updateList();
271 _addDialog.setVisible(false);
276 * Check the given source name if it exists in library already
277 * @param inName name to check
278 * @return unique name not yet in library
280 private static String getUniqueSourcename(String inName)
282 String name = inName;
283 if (name == null) {name = "";}
284 else {name = name.trim();}
285 if (name.equals("")) {
286 name = I18nManager.getText("dialog.addmapsource.noname");
288 // Check there isn't already a map source with this name
289 if (MapSourceLibrary.hasSourceName(name))
292 while (MapSourceLibrary.hasSourceName(name + suffix)) {