gvsig-osm / org.gvsig.raster.osm / trunk / org.gvsig.raster.osm / org.gvsig.raster.osm.swing / org.gvsig.raster.osm.swing.impl / src / main / java / org / gvsig / raster / osm / swing / impl / AddOSMLayerPanelImpl.java @ 309
History | View | Annotate | Download (17.4 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.osm.swing.impl; |
23 |
|
24 |
import java.awt.BorderLayout; |
25 |
import java.awt.FlowLayout; |
26 |
import java.awt.GridBagConstraints; |
27 |
import java.awt.GridBagLayout; |
28 |
import java.awt.Insets; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
import java.awt.event.MouseEvent; |
32 |
import java.awt.event.MouseListener; |
33 |
import java.io.File; |
34 |
import java.io.FileInputStream; |
35 |
import java.io.FileNotFoundException; |
36 |
import java.io.FileOutputStream; |
37 |
import java.io.IOException; |
38 |
import java.net.MalformedURLException; |
39 |
import java.net.URL; |
40 |
import java.util.Collection; |
41 |
import java.util.Iterator; |
42 |
import java.util.Properties; |
43 |
|
44 |
import javax.swing.BorderFactory; |
45 |
import javax.swing.JButton; |
46 |
import javax.swing.JComponent; |
47 |
import javax.swing.JLabel; |
48 |
import javax.swing.JOptionPane; |
49 |
import javax.swing.JPanel; |
50 |
import javax.swing.JScrollPane; |
51 |
import javax.swing.JTable; |
52 |
import javax.swing.JTextField; |
53 |
import javax.swing.ListSelectionModel; |
54 |
import javax.swing.table.DefaultTableModel; |
55 |
|
56 |
import org.gvsig.i18n.Messages; |
57 |
import org.gvsig.raster.osm.swing.AddOSMLayerPanel; |
58 |
import org.gvsig.tools.ToolsLocator; |
59 |
import org.gvsig.tools.i18n.I18nManager; |
60 |
|
61 |
import org.slf4j.Logger; |
62 |
import org.slf4j.LoggerFactory; |
63 |
|
64 |
/**
|
65 |
* Panel to add OSM layers
|
66 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
67 |
*/
|
68 |
public class AddOSMLayerPanelImpl extends JPanel implements AddOSMLayerPanel, MouseListener, ActionListener { |
69 |
private static final long serialVersionUID = 1; |
70 |
private static final String separator = "*"; |
71 |
private Logger log = LoggerFactory.getLogger(AddOSMLayerPanelImpl.class); |
72 |
private File pathToServerList = null; |
73 |
private JTextField textSelectedServer = null; |
74 |
private JTable tableServerList = null; |
75 |
private String propertiesFileName = "osmservers.properties"; |
76 |
private JPanel buttonsPanel = null; |
77 |
private JPanel crsWarningPanel = null; |
78 |
private JPanel selectedServerPanel = null; |
79 |
private String[] columnNames = null; |
80 |
private JButton addServerButton = null; |
81 |
private JButton updateServerButton = null; |
82 |
private JButton removeServerButton = null; |
83 |
private AddServerPanel serverPanel = null; |
84 |
private MouseListener mouseListener = null; |
85 |
private JLabel crsWarningLabel = null; |
86 |
private boolean showCrsWarning = false; |
87 |
|
88 |
|
89 |
public AddOSMLayerPanelImpl(File pathToServerList) { |
90 |
this(pathToServerList, false); |
91 |
} |
92 |
|
93 |
public AddOSMLayerPanelImpl(File pathToServerList, boolean showCrsWarning) { |
94 |
if (pathToServerList != null) { |
95 |
this.pathToServerList = pathToServerList;
|
96 |
} else {
|
97 |
this.pathToServerList = new File(System.getProperty("user.home")); |
98 |
} |
99 |
this.showCrsWarning = showCrsWarning;
|
100 |
columnNames = |
101 |
new String[] { |
102 |
Messages.getText("name"),
|
103 |
Messages.getText("url"),
|
104 |
Messages.getText("levels"),
|
105 |
Messages.getText("format"),
|
106 |
}; |
107 |
init(); |
108 |
} |
109 |
|
110 |
private void init() { |
111 |
GridBagConstraints gridBagConstraints;
|
112 |
setLayout(new GridBagLayout()); |
113 |
|
114 |
gridBagConstraints = new GridBagConstraints(); |
115 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
116 |
gridBagConstraints.gridx = 0;
|
117 |
gridBagConstraints.gridy = 0;
|
118 |
gridBagConstraints.weightx = 1.0;
|
119 |
gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
120 |
|
121 |
add(getJPanelSelectedServer(), gridBagConstraints); |
122 |
|
123 |
gridBagConstraints.fill = GridBagConstraints.BOTH;
|
124 |
gridBagConstraints.gridx = 0;
|
125 |
gridBagConstraints.gridy = 1;
|
126 |
gridBagConstraints.weightx = 1.0;
|
127 |
gridBagConstraints.weighty = 1.0;
|
128 |
|
129 |
JScrollPane scrollPane = new JScrollPane(getJTableServerList()); |
130 |
add(scrollPane, gridBagConstraints); |
131 |
|
132 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
133 |
gridBagConstraints.gridx = 0;
|
134 |
gridBagConstraints.gridy = 2;
|
135 |
gridBagConstraints.weightx = 1.0;
|
136 |
gridBagConstraints.weighty = 0.0;
|
137 |
|
138 |
add(getAddServerPanel(), gridBagConstraints); |
139 |
|
140 |
gridBagConstraints.gridy = 3;
|
141 |
add(getJPanelButtonsPanel(), gridBagConstraints); |
142 |
if(this.showCrsWarning){ |
143 |
gridBagConstraints.gridy = 4;
|
144 |
add(getJPanelCrsWarningPanel(), gridBagConstraints); |
145 |
} |
146 |
} |
147 |
|
148 |
private JPanel getJPanelButtonsPanel() { |
149 |
if(buttonsPanel == null) { |
150 |
FlowLayout fl = new FlowLayout(); |
151 |
buttonsPanel = new JPanel(fl); |
152 |
updateServerButton = new JButton(Messages.getText("update_server")); |
153 |
updateServerButton.addActionListener(this);
|
154 |
addServerButton = new JButton(Messages.getText("add_server")); |
155 |
addServerButton.addActionListener(this);
|
156 |
removeServerButton = new JButton(Messages.getText("remove_server")); |
157 |
removeServerButton.addActionListener(this);
|
158 |
buttonsPanel.add(updateServerButton, fl); |
159 |
buttonsPanel.add(addServerButton, fl); |
160 |
buttonsPanel.add(removeServerButton, fl); |
161 |
} |
162 |
return buttonsPanel;
|
163 |
} |
164 |
|
165 |
private JPanel getJPanelCrsWarningPanel() { |
166 |
if (crsWarningPanel == null) { |
167 |
FlowLayout fl = new FlowLayout(); |
168 |
crsWarningPanel = new JPanel(fl); |
169 |
// if(this.showCrsWarning){
|
170 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
171 |
StringBuilder builder = new StringBuilder(); |
172 |
builder.append("<html><font color=\"red\">");
|
173 |
builder.append(i18nManager.getTranslation("_crs_warning"));
|
174 |
builder.append("</font></body></html>");
|
175 |
crsWarningLabel = new JLabel(Messages.getText(builder.toString())); |
176 |
crsWarningPanel.add(crsWarningLabel, fl); |
177 |
// }
|
178 |
} |
179 |
return crsWarningPanel;
|
180 |
} |
181 |
|
182 |
private JPanel getJPanelSelectedServer() { |
183 |
if(selectedServerPanel == null) { |
184 |
selectedServerPanel = new JPanel(new BorderLayout()); |
185 |
selectedServerPanel.setBorder(BorderFactory.createTitledBorder(Messages.getText("server"))); |
186 |
selectedServerPanel.add(getJTextFieldSelectedServer(), BorderLayout.CENTER);
|
187 |
} |
188 |
return selectedServerPanel;
|
189 |
} |
190 |
|
191 |
private JTextField getJTextFieldSelectedServer() { |
192 |
if(textSelectedServer == null) { |
193 |
textSelectedServer = new JTextField(); |
194 |
textSelectedServer.setEditable(false);
|
195 |
} |
196 |
return textSelectedServer;
|
197 |
} |
198 |
|
199 |
private JTable getJTableServerList() { |
200 |
if(tableServerList == null) { |
201 |
DefaultTableModel model = null; |
202 |
try {
|
203 |
Object[][] objs = loadServerListFromProperties(); |
204 |
model = new DefaultTableModel(objs, columnNames); |
205 |
tableServerList = new JTable(model); |
206 |
tableServerList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
|
207 |
tableServerList.getColumnModel().getColumn(2).setMaxWidth(50); |
208 |
tableServerList.getColumnModel().getColumn(3).setMaxWidth(50); |
209 |
} catch (FileNotFoundException e) { |
210 |
log.debug("Properties file to store servers has not been located");
|
211 |
} catch (IOException e) { |
212 |
log.debug("Error reading properties file which stores servers");
|
213 |
} |
214 |
if(tableServerList == null) { |
215 |
model = new DefaultTableModel(null, columnNames); |
216 |
tableServerList = new JTable(model); |
217 |
} |
218 |
ListSelectionModel selectionModel = tableServerList.getSelectionModel();
|
219 |
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
220 |
tableServerList.addMouseListener(this);
|
221 |
} |
222 |
return tableServerList;
|
223 |
} |
224 |
|
225 |
private Object[][] loadServerListFromProperties() throws FileNotFoundException, IOException { |
226 |
if(!new File(getPropertiesFilePath()).exists()) |
227 |
return null; |
228 |
Properties prop = new Properties(); |
229 |
prop.load(new FileInputStream(getPropertiesFilePath())); |
230 |
Collection<Object> collect = prop.values(); |
231 |
if(collect == null) |
232 |
return null; |
233 |
|
234 |
Iterator<Object> it = collect.iterator(); |
235 |
Object[][] result = new Object[collect.size()][4]; |
236 |
|
237 |
int cont = 0; |
238 |
while(it.hasNext()) {
|
239 |
String value = (String)it.next(); |
240 |
result[cont] = value.split("\\" + separator);
|
241 |
cont ++; |
242 |
} |
243 |
return result;
|
244 |
} |
245 |
|
246 |
|
247 |
/*private void addServerToProperties(List<String> line) throws FileNotFoundException, IOException {
|
248 |
if(!new File(getPropertiesFile()).exists())
|
249 |
if(!new File(getPropertiesFile()).createNewFile())
|
250 |
return;
|
251 |
if(line == null || line.size() != 4)
|
252 |
return;
|
253 |
|
254 |
Properties prop = new Properties();
|
255 |
prop.setProperty(line.get(0),
|
256 |
line.get(0) + ":" + line.get(1) + ":" + line.get(2) + ":" + line.get(3));
|
257 |
prop.store(new FileOutputStream(getPropertiesFile()), null);
|
258 |
}*/
|
259 |
|
260 |
private void addServerToProperties(String name, String url, String levels, String format) { |
261 |
try {
|
262 |
if(!new File(getPropertiesFilePath()).exists()) |
263 |
if(!new File(getPropertiesFilePath()).createNewFile()) |
264 |
return;
|
265 |
|
266 |
Properties prop = new Properties(); |
267 |
prop.load(new FileInputStream(getPropertiesFilePath())); |
268 |
prop.setProperty(getKeyFromName(name), name + separator + url + separator + levels + separator + format); |
269 |
prop.store(new FileOutputStream(getPropertiesFilePath()), null); |
270 |
} catch (FileNotFoundException e1) { |
271 |
JOptionPane.showMessageDialog(this, Messages.getText("cannot_add_to_file"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
272 |
log.info(Messages.getText("cannot_add_to_file"), e1);
|
273 |
} catch (IOException e1) { |
274 |
JOptionPane.showMessageDialog(this, Messages.getText("cannot_add_to_file"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
275 |
log.info(Messages.getText("cannot_add_to_file"), e1);
|
276 |
} |
277 |
} |
278 |
|
279 |
private void removeServerFromProperties(String name) { |
280 |
try {
|
281 |
if(!new File(getPropertiesFilePath()).exists()) |
282 |
return;
|
283 |
|
284 |
Properties prop = new Properties(); |
285 |
prop.load(new FileInputStream(getPropertiesFilePath())); |
286 |
prop.remove(getKeyFromName(name)); |
287 |
prop.store(new FileOutputStream(getPropertiesFilePath()), null); |
288 |
} catch (FileNotFoundException e1) { |
289 |
JOptionPane.showMessageDialog(this, Messages.getText("cannot_remove_from_file"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
290 |
log.info(Messages.getText("cannot_remove_from_file"), e1);
|
291 |
} catch (IOException e1) { |
292 |
JOptionPane.showMessageDialog(this, Messages.getText("cannot_remove_from_file"), Messages.getText("Error"), JOptionPane.ERROR_MESSAGE); |
293 |
log.info(Messages.getText("cannot_remove_from_file"), e1);
|
294 |
} |
295 |
} |
296 |
|
297 |
|
298 |
public String getPropertiesFilePath() { |
299 |
return pathToServerList.getAbsolutePath() + File.separator + propertiesFileName; |
300 |
} |
301 |
|
302 |
public void mouseClicked(MouseEvent e) { |
303 |
|
304 |
} |
305 |
|
306 |
public void mouseEntered(MouseEvent e) { |
307 |
|
308 |
} |
309 |
|
310 |
public void mouseExited(MouseEvent e) { |
311 |
|
312 |
} |
313 |
|
314 |
public void mousePressed(MouseEvent e) { |
315 |
if(e.getSource() == getJTableServerList()) {
|
316 |
if(getJTableServerList().getSelectedRow() != -1) { |
317 |
int selectedRow = getJTableServerList().getSelectedRow();
|
318 |
String name = (String)getJTableServerList().getModel().getValueAt(selectedRow, 0); |
319 |
String url = (String)getJTableServerList().getModel().getValueAt(selectedRow, 1); |
320 |
String levels = (String)getJTableServerList().getModel().getValueAt(selectedRow, 2); |
321 |
String format = (String)getJTableServerList().getModel().getValueAt(selectedRow, 3); |
322 |
getJTextFieldSelectedServer().setText(name); |
323 |
getAddServerPanel().getJTextFieldName().setText(name); |
324 |
getAddServerPanel().getJTextFieldURL().setText(url); |
325 |
getAddServerPanel().getJTextFieldLevels().setText(levels); |
326 |
getAddServerPanel().getJTextFieldFormat().setText(format); |
327 |
if(mouseListener != null) |
328 |
mouseListener.mousePressed(e); |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
public void mouseReleased(MouseEvent e) { |
334 |
|
335 |
} |
336 |
|
337 |
public void addMouseListener(MouseListener listener) { |
338 |
this.mouseListener = listener;
|
339 |
} |
340 |
|
341 |
public JComponent getComponent() { |
342 |
return this; |
343 |
} |
344 |
|
345 |
public void actionPerformed(ActionEvent e) { |
346 |
if(e.getSource() == updateServerButton) {
|
347 |
if(getJTableServerList().getSelectedRow() == -1) { |
348 |
JOptionPane.showMessageDialog(this, Messages.getText("row_not_selected"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
349 |
return;
|
350 |
} |
351 |
|
352 |
if(getAddServerPanel().check()) {
|
353 |
String name = getAddServerPanel().getJTextFieldName().getText();
|
354 |
String url = getAddServerPanel().getJTextFieldURL().getText();
|
355 |
String levels = getAddServerPanel().getJTextFieldLevels().getText();
|
356 |
String format = getAddServerPanel().getJTextFieldFormat().getText();
|
357 |
|
358 |
int updateRow = getJTableServerList().getSelectedRow();
|
359 |
if(updateRow < 0) { |
360 |
JOptionPane.showMessageDialog(this, Messages.getText("row_not_selected"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
361 |
return;
|
362 |
} |
363 |
int res = JOptionPane.showConfirmDialog(this, Messages.getText("update_existing_row"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
364 |
if(res == JOptionPane.YES_OPTION) { |
365 |
String nameToRemove = (String)getJTableServerList().getModel().getValueAt(updateRow, 0); |
366 |
removeServerFromProperties(nameToRemove); |
367 |
((DefaultTableModel)getJTableServerList().getModel()).removeRow(updateRow);
|
368 |
((DefaultTableModel)getJTableServerList().getModel()).addRow(new Object[]{name, url, levels, format}); |
369 |
addServerToProperties(name, url, levels, format); |
370 |
} |
371 |
return;
|
372 |
} else {
|
373 |
JOptionPane.showMessageDialog(this, Messages.getText("format_not_valid"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
374 |
} |
375 |
|
376 |
} |
377 |
|
378 |
if(e.getSource() == addServerButton) {
|
379 |
if(getAddServerPanel().check()) {
|
380 |
String name = getAddServerPanel().getJTextFieldName().getText();
|
381 |
String url = getAddServerPanel().getJTextFieldURL().getText();
|
382 |
String levels = getAddServerPanel().getJTextFieldLevels().getText();
|
383 |
String format = getAddServerPanel().getJTextFieldFormat().getText();
|
384 |
|
385 |
int updateRow = existsServerInList(name);
|
386 |
if(updateRow != -1) { |
387 |
JOptionPane.showMessageDialog(this, Messages.getText("server_exists"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
388 |
return;
|
389 |
} |
390 |
((DefaultTableModel)getJTableServerList().getModel()).addRow(new Object[]{name, url, levels, format}); |
391 |
addServerToProperties(name, url, levels, format); |
392 |
return;
|
393 |
} else {
|
394 |
JOptionPane.showMessageDialog(this, Messages.getText("format_not_valid"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
395 |
} |
396 |
} |
397 |
|
398 |
if(e.getSource() == removeServerButton) {
|
399 |
int selected = getJTableServerList().getSelectedRow();
|
400 |
if(selected != -1) { |
401 |
int res = JOptionPane.showConfirmDialog(this, Messages.getText("remove_existing_row"), Messages.getText("Info"), JOptionPane.INFORMATION_MESSAGE); |
402 |
if(res == JOptionPane.YES_OPTION) { |
403 |
String name = (String)getJTableServerList().getModel().getValueAt(selected, 0); |
404 |
((DefaultTableModel)getJTableServerList().getModel()).removeRow(selected);
|
405 |
getJTextFieldSelectedServer().setText("");
|
406 |
removeServerFromProperties(name); |
407 |
} |
408 |
} |
409 |
} |
410 |
} |
411 |
|
412 |
/**
|
413 |
* Returns -1 if the element does not exists in the table and its position if exists
|
414 |
* @param name
|
415 |
* @return
|
416 |
*/
|
417 |
private int existsServerInList(String name) { |
418 |
for (int i = 0; i < getJTableServerList().getRowCount(); i++) { |
419 |
String value = (String)getJTableServerList().getModel().getValueAt(i, 0); |
420 |
if(value.equals(name))
|
421 |
return i;
|
422 |
} |
423 |
return -1; |
424 |
} |
425 |
|
426 |
private String getKeyFromName(String name) { |
427 |
return name.replace(' ', '_'); |
428 |
} |
429 |
|
430 |
private AddServerPanel getAddServerPanel() {
|
431 |
if(serverPanel == null) { |
432 |
serverPanel = new AddServerPanel(separator);
|
433 |
} |
434 |
return serverPanel;
|
435 |
} |
436 |
|
437 |
public String getPropertiesFileName() { |
438 |
return propertiesFileName;
|
439 |
} |
440 |
|
441 |
public boolean areSettingsValid() { |
442 |
return getJTableServerList().getSelectedRow() != -1 && |
443 |
getJTextFieldSelectedServer().getText() != null &&
|
444 |
!getJTextFieldSelectedServer().getText().equals("");
|
445 |
} |
446 |
|
447 |
public String getSelectedFormat() { |
448 |
if(getSelectedName() == null) |
449 |
return null; |
450 |
for (int i = 0; i < getJTableServerList().getRowCount(); i++) { |
451 |
String value = (String)getJTableServerList().getModel().getValueAt(i, 0); |
452 |
if(value.equals(getSelectedName()))
|
453 |
return (String)getJTableServerList().getModel().getValueAt(i, 3); |
454 |
} |
455 |
return null; |
456 |
} |
457 |
|
458 |
public String getSelectedLevel() { |
459 |
if(getSelectedName() == null) |
460 |
return null; |
461 |
for (int i = 0; i < getJTableServerList().getRowCount(); i++) { |
462 |
String value = (String)getJTableServerList().getModel().getValueAt(i, 0); |
463 |
if(value.equals(getSelectedName()))
|
464 |
return (String)getJTableServerList().getModel().getValueAt(i, 2); |
465 |
} |
466 |
return null; |
467 |
} |
468 |
|
469 |
public String getSelectedName() { |
470 |
return getJTextFieldSelectedServer().getText();
|
471 |
} |
472 |
|
473 |
public URL getSelectedURL() throws MalformedURLException { |
474 |
if(getSelectedName() == null) |
475 |
return null; |
476 |
for (int i = 0; i < getJTableServerList().getRowCount(); i++) { |
477 |
String value = (String)getJTableServerList().getModel().getValueAt(i, 0); |
478 |
if(value.equals(getSelectedName()))
|
479 |
return new URL((String)getJTableServerList().getModel().getValueAt(i, 1)); |
480 |
} |
481 |
return null; |
482 |
} |
483 |
} |