]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/AddMapSourceDialog.java
Version 10, May 2010
[GpsPrune.git] / tim / prune / function / AddMapSourceDialog.java
1 package tim.prune.function;
2
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;
13 import java.net.URL;
14
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;
24
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;
30
31 /**
32  * Class to handle the adding of a new map source
33  */
34 public class AddMapSourceDialog
35 {
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;
49
50
51         /**
52          * Constructor
53          * @param inParent parent dialog
54          */
55         public AddMapSourceDialog(JDialog inParentDialog, SetMapBgFunction inParentFunction)
56         {
57                 _parent = inParentFunction;
58                 _addDialog = new JDialog(inParentDialog, I18nManager.getText("dialog.addmapsource.title"), true);
59                 _addDialog.add(makeDialogComponents());
60                 _addDialog.setLocationRelativeTo(inParentDialog);
61                 _addDialog.pack();
62         }
63
64
65         /**
66          * Create dialog components
67          * @return Panel containing all gui elements in dialog
68          */
69         private Component makeDialogComponents()
70         {
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);}
91                                 enableOK();
92                         }
93                 };
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);
98
99                 _cards = new JPanel();
100                 _cards.setLayout(new CardLayout());
101                 // listener
102                 KeyAdapter keyListener = new KeyAdapter() {
103                         public void keyReleased(KeyEvent e) {
104                                 super.keyReleased(e);
105                                 enableOK();
106                         }
107                 };
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);
116                 // Base layer
117                 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer1url")));
118                 _baseUrlField = new JTextField(18);
119                 _baseUrlField.addKeyListener(keyListener);
120                 osmPanel.add(_baseUrlField);
121                 // Top layer
122                 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer2url")));
123                 _topUrlField = new JTextField(18);
124                 _topUrlField.addKeyListener(keyListener);
125                 osmPanel.add(_topUrlField);
126                 // Max zoom
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);
131                 }
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);
149                 }
150                 cloudPanel.add(_cZoomCombo);
151                 cloudPanel.add(new JLabel(" ")); // force four rows to space text boxes properly
152                 _cards.add(cloudPanel, "card2");
153                 // cards
154                 JPanel holderPanel = new JPanel();
155                 holderPanel.setLayout(new BorderLayout());
156                 holderPanel.add(_cards, BorderLayout.NORTH);
157                 dialogPanel.add(holderPanel, BorderLayout.CENTER);
158
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)
165                         {
166                                 finish();
167                         }
168                 };
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)
174                         {
175                                 _addDialog.dispose();
176                         }
177                 });
178                 buttonPanel.add(cancelButton);
179                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
180                 return dialogPanel;
181         }
182
183
184         /**
185          * Init and show the dialog
186          */
187         public void showDialog()
188         {
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);
198         }
199
200
201         /**
202          * Check the currently entered details and enable the OK button if it looks OK
203          */
204         private void enableOK()
205         {
206                 boolean ok = false;
207                 if (_typeRadios[0].isSelected()) {ok = isOsmPanelOk();}
208                 if (_typeRadios[1].isSelected()) {ok = isCloudPanelOk();}
209                 _okButton.setEnabled(ok);
210         }
211
212         /**
213          * Check the openstreetmap panel if all details are complete
214          * @return true if details look ok
215          */
216         private boolean isOsmPanelOk()
217         {
218                 boolean ok = _oNameField.getText().trim().length() > 1;
219                 URL baseUrl = null, topUrl = null;
220                 try {
221                         // Try to parse base url if given
222                         String baseText = _baseUrlField.getText().trim();
223                         if (baseText.length() > 10) {
224                                 baseUrl = new URL(baseText);
225                         }
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);
231                         }
232                         else if (topText.length() > 0) {ok = false;}
233                 } catch (MalformedURLException e) {
234                         ok = false;
235                 }
236                 // looks ok if at least one url given
237                 return (ok && (baseUrl != null || topUrl != null));
238         }
239
240         /**
241          * Check the cloudmade panel if all details are complete
242          * @return true if details look ok
243          */
244         private boolean isCloudPanelOk()
245         {
246                 boolean ok = _cNameField.getText().trim().length() > 1;
247                 int styleNum = 0;
248                 try {
249                         styleNum = Integer.parseInt(_cStyleField.getText());
250                 }
251                 catch (NumberFormatException nfe) {}
252                 return (ok && styleNum > 0);
253         }
254
255         /**
256          * Finish by adding the requested source and refreshing the parent
257          */
258         private void finish()
259         {
260                 MapSource newSource = null;
261                 if (_typeRadios[0].isSelected())
262                 {
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);
268                 }
269                 else if (_typeRadios[1].isSelected())
270                 {
271                         String sourceName = getUniqueSourcename(_cNameField.getText());
272                         newSource = new CloudmadeMapSource(sourceName, _cStyleField.getText(),
273                                 _cZoomCombo.getSelectedIndex()+10);
274                 }
275                 // Add new source if ok
276                 if (newSource != null)
277                 {
278                         MapSourceLibrary.addSource(newSource);
279                         // inform setmapbg dialog
280                         _parent.updateList();
281                         _addDialog.setVisible(false);
282                 }
283         }
284
285         /**
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
289          */
290         private static String getUniqueSourcename(String inName)
291         {
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");
297                 }
298                 // Check there isn't already a map source with this name
299                 if (MapSourceLibrary.hasSourceName(name))
300                 {
301                         int suffix = 1;
302                         while (MapSourceLibrary.hasSourceName(name + suffix)) {
303                                 suffix++;
304                         }
305                         name += suffix;
306                 }
307                 return name;
308         }
309 }