]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/AddMapSourceDialog.java
Version 12, December 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
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;
22
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;
28
29 /**
30  * Class to handle the adding of a new map source
31  */
32 public class AddMapSourceDialog
33 {
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;
47
48
49         /**
50          * Constructor
51          * @param inParent parent dialog
52          */
53         public AddMapSourceDialog(JDialog inParentDialog, SetMapBgFunction inParentFunction)
54         {
55                 _parent = inParentFunction;
56                 _addDialog = new JDialog(inParentDialog, I18nManager.getText("dialog.addmapsource.title"), true);
57                 _addDialog.add(makeDialogComponents());
58                 _addDialog.setLocationRelativeTo(inParentDialog);
59                 _addDialog.pack();
60         }
61
62
63         /**
64          * Create dialog components
65          * @return Panel containing all gui elements in dialog
66          */
67         private Component makeDialogComponents()
68         {
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);}
89                                 enableOK();
90                         }
91                 };
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);
96
97                 _cards = new JPanel();
98                 _cards.setLayout(new CardLayout());
99                 // listener
100                 KeyAdapter keyListener = new KeyAdapter() {
101                         public void keyReleased(KeyEvent e) {
102                                 super.keyReleased(e);
103                                 enableOK();
104                         }
105                 };
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);
114                 // Base layer
115                 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer1url")));
116                 _baseUrlField = new JTextField(18);
117                 _baseUrlField.addKeyListener(keyListener);
118                 osmPanel.add(_baseUrlField);
119                 // Top layer
120                 osmPanel.add(new JLabel(I18nManager.getText("dialog.addmapsource.layer2url")));
121                 _topUrlField = new JTextField(18);
122                 _topUrlField.addKeyListener(keyListener);
123                 osmPanel.add(_topUrlField);
124                 // Max zoom
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);
129                 }
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);
147                 }
148                 cloudPanel.add(_cZoomCombo);
149                 cloudPanel.add(new JLabel(" ")); // force four rows to space text boxes properly
150                 _cards.add(cloudPanel, "card2");
151                 // cards
152                 JPanel holderPanel = new JPanel();
153                 holderPanel.setLayout(new BorderLayout());
154                 holderPanel.add(_cards, BorderLayout.NORTH);
155                 dialogPanel.add(holderPanel, BorderLayout.CENTER);
156
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)
163                         {
164                                 finish();
165                         }
166                 };
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)
172                         {
173                                 _addDialog.dispose();
174                         }
175                 });
176                 buttonPanel.add(cancelButton);
177                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
178                 return dialogPanel;
179         }
180
181
182         /**
183          * Init and show the dialog
184          */
185         public void showDialog()
186         {
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);
196         }
197
198
199         /**
200          * Check the currently entered details and enable the OK button if it looks OK
201          */
202         private void enableOK()
203         {
204                 boolean ok = false;
205                 if (_typeRadios[0].isSelected()) {ok = isOsmPanelOk();}
206                 if (_typeRadios[1].isSelected()) {ok = isCloudPanelOk();}
207                 _okButton.setEnabled(ok);
208         }
209
210         /**
211          * Check the openstreetmap panel if all details are complete
212          * @return true if details look ok
213          */
214         private boolean isOsmPanelOk()
215         {
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));
228         }
229
230         /**
231          * Check the cloudmade panel if all details are complete
232          * @return true if details look ok
233          */
234         private boolean isCloudPanelOk()
235         {
236                 boolean ok = _cNameField.getText().trim().length() > 1;
237                 int styleNum = 0;
238                 try {
239                         styleNum = Integer.parseInt(_cStyleField.getText());
240                 }
241                 catch (NumberFormatException nfe) {}
242                 return (ok && styleNum > 0);
243         }
244
245         /**
246          * Finish by adding the requested source and refreshing the parent
247          */
248         private void finish()
249         {
250                 MapSource newSource = null;
251                 if (_typeRadios[0].isSelected())
252                 {
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);
258                 }
259                 else if (_typeRadios[1].isSelected())
260                 {
261                         String sourceName = getUniqueSourcename(_cNameField.getText());
262                         newSource = new CloudmadeMapSource(sourceName, _cStyleField.getText(),
263                                 _cZoomCombo.getSelectedIndex()+10);
264                 }
265                 // Add new source if ok
266                 if (newSource != null)
267                 {
268                         MapSourceLibrary.addSource(newSource);
269                         // inform setmapbg dialog
270                         _parent.updateList();
271                         _addDialog.setVisible(false);
272                 }
273         }
274
275         /**
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
279          */
280         private static String getUniqueSourcename(String inName)
281         {
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");
287                 }
288                 // Check there isn't already a map source with this name
289                 if (MapSourceLibrary.hasSourceName(name))
290                 {
291                         int suffix = 1;
292                         while (MapSourceLibrary.hasSourceName(name + suffix)) {
293                                 suffix++;
294                         }
295                         name += suffix;
296                 }
297                 return name;
298         }
299 }