svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / JUrlFileChooser.java @ 40560
History | View | Annotate | Download (2.81 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.gui.styling; |
25 |
|
26 |
import java.io.File; |
27 |
import java.net.MalformedURLException; |
28 |
import java.net.URL; |
29 |
|
30 |
import javax.swing.filechooser.FileFilter; |
31 |
|
32 |
import org.gvsig.gui.beans.swing.JFileChooser; |
33 |
import org.gvsig.i18n.Messages; |
34 |
|
35 |
import org.gvsig.andami.PluginServices; |
36 |
|
37 |
public class JUrlFileChooser extends JFileChooser { |
38 |
|
39 |
private static final long serialVersionUID = 1844355534608274984L; |
40 |
|
41 |
public JUrlFileChooser(String fileChooserID, String defaultDirectory) { |
42 |
super(fileChooserID, defaultDirectory);
|
43 |
this.setFileFilter(ff);
|
44 |
this.setFileSelectionMode(JFileChooser.FILES_ONLY); |
45 |
this.setMultiSelectionEnabled(false); |
46 |
} |
47 |
|
48 |
FileFilter ff = new FileFilter() { |
49 |
public boolean accept(File f) { |
50 |
if (f.isDirectory()) return true; |
51 |
String fName = f.getAbsolutePath();
|
52 |
if (fName!=null) { |
53 |
fName = fName.toLowerCase(); |
54 |
return fName.endsWith(".png") |
55 |
|| fName.endsWith(".gif")
|
56 |
|| fName.endsWith(".jpg")
|
57 |
|| fName.endsWith(".jpeg")
|
58 |
|| fName.endsWith(".bmp")
|
59 |
|| fName.endsWith(".svg");
|
60 |
} |
61 |
return false; |
62 |
} |
63 |
|
64 |
public String getDescription() { |
65 |
return Messages.getText("bitmap_and_svg_image_files")+ "," + |
66 |
Messages.getText("URL");
|
67 |
} |
68 |
}; |
69 |
|
70 |
public URL getSelectedURL() { |
71 |
String path;
|
72 |
File f = getSelectedFile();
|
73 |
|
74 |
try {
|
75 |
if(f.canRead()) {
|
76 |
return f.toURI().toURL();
|
77 |
} |
78 |
else {
|
79 |
path = f.toString(); |
80 |
|
81 |
if (!path.startsWith("http")) |
82 |
if(path.contains("http") && |
83 |
(path.endsWith(".png")
|
84 |
|| path.endsWith(".gif")
|
85 |
|| path.endsWith(".jpg")
|
86 |
|| path.endsWith(".jpeg")
|
87 |
|| path.endsWith(".bmp")
|
88 |
|| path.endsWith(".svg"))) {
|
89 |
|
90 |
path = path.substring(path.indexOf("http"), path.length());
|
91 |
path = path.replace('\\', '/'); |
92 |
path = path.replaceFirst("/", "//"); |
93 |
|
94 |
return new URL(path); |
95 |
} |
96 |
} |
97 |
} catch (MalformedURLException e) { |
98 |
return null; |
99 |
} |
100 |
return null; |
101 |
} |
102 |
|
103 |
|
104 |
} |