svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolLibrary.java @ 27576
History | View | Annotate | Download (9.51 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.gui.styling; |
42 |
|
43 |
import java.awt.Component; |
44 |
import java.io.File; |
45 |
import java.io.FileFilter; |
46 |
import java.io.FileWriter; |
47 |
import java.util.ConcurrentModificationException; |
48 |
import java.util.Iterator; |
49 |
import java.util.Vector; |
50 |
|
51 |
import javax.swing.JOptionPane; |
52 |
import javax.swing.event.TreeModelEvent; |
53 |
import javax.swing.event.TreeModelListener; |
54 |
import javax.swing.tree.DefaultMutableTreeNode; |
55 |
import javax.swing.tree.DefaultTreeModel; |
56 |
import javax.swing.tree.TreePath; |
57 |
|
58 |
import org.exolab.castor.xml.Marshaller; |
59 |
|
60 |
import com.iver.andami.PluginServices; |
61 |
import com.iver.andami.messages.NotificationManager; |
62 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
63 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
64 |
import com.iver.utiles.XMLEntity; |
65 |
/**
|
66 |
*
|
67 |
* SymbolLibrary.java
|
68 |
*
|
69 |
*
|
70 |
* @author jaume dominguez faus - jaume.dominguez@iver.es Dec 7, 2007
|
71 |
*
|
72 |
*/
|
73 |
public class SymbolLibrary extends DefaultTreeModel implements ILibraryModel { |
74 |
static protected String rootDirString; |
75 |
private File rootDir; |
76 |
private Vector<TreeModelListener> listeners = new Vector<TreeModelListener>(); |
77 |
private static SymbolLibrary instance = null; |
78 |
|
79 |
public static SymbolLibrary getInstance() { |
80 |
if (instance == null || rootDirString != SymbologyFactory.SymbolLibraryPath) { |
81 |
rootDirString = SymbologyFactory.SymbolLibraryPath; |
82 |
instance = new SymbolLibrary(new File(rootDirString)); |
83 |
} |
84 |
return instance;
|
85 |
} |
86 |
|
87 |
protected SymbolLibrary(File rootDir) { |
88 |
super(new DefaultMutableTreeNode(rootDir)); |
89 |
rootDirString = PluginServices.getText(this, "symbol_library"); |
90 |
this.rootDir = rootDir;
|
91 |
} |
92 |
|
93 |
final class MyFile extends File { |
94 |
private static final long serialVersionUID = 6332989815291224773L; |
95 |
|
96 |
public MyFile(String pathname) { |
97 |
super(pathname);
|
98 |
} |
99 |
|
100 |
public String toString() { |
101 |
|
102 |
String path = getAbsolutePath();
|
103 |
if (path.equals(rootDir.getAbsolutePath()))
|
104 |
return rootDirString;
|
105 |
return path.substring(path.lastIndexOf(File.separator)+1, path.length()); |
106 |
} |
107 |
}; |
108 |
|
109 |
private FileFilter ff = new FileFilter() { |
110 |
public boolean accept(File pathname) { |
111 |
return pathname.isDirectory();
|
112 |
} |
113 |
}; |
114 |
public static final String SYMBOL_FILE_EXTENSION = ".sym"; |
115 |
|
116 |
@Override
|
117 |
public Object getRoot() { |
118 |
return new DefaultMutableTreeNode(new MyFile(rootDir.getAbsolutePath())); |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public int getChildCount(Object parent) { |
123 |
File f = null; |
124 |
if (parent instanceof DefaultMutableTreeNode) { |
125 |
f = (File) ((DefaultMutableTreeNode) parent).getUserObject(); |
126 |
|
127 |
} else {
|
128 |
f = (File) parent;
|
129 |
} |
130 |
File[] files = f.listFiles(ff); |
131 |
return files == null ? 0 : files.length; |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public boolean isLeaf(Object node) { |
136 |
return getChildCount(node)==0; |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void addTreeModelListener(TreeModelListener l) { |
141 |
listeners.add(l); |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public void removeTreeModelListener(TreeModelListener l) { |
146 |
listeners.remove(l); |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void valueForPathChanged(TreePath path, Object newValue) {} |
151 |
|
152 |
|
153 |
@Override
|
154 |
public Object getChild(Object parent, int index) { |
155 |
File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles(ff)[index]; |
156 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath())); |
157 |
node.setParent((DefaultMutableTreeNode) parent);
|
158 |
return node;
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public int getIndexOfChild(Object parent, Object child) { |
163 |
if (parent == null) |
164 |
return -1; |
165 |
File[] files = ((File) ((DefaultMutableTreeNode) parent).getUserObject()).listFiles(ff); |
166 |
for (int i = 0; i < files.length; i++) { |
167 |
if (files[i].equals(child))
|
168 |
return i;
|
169 |
} |
170 |
return -1; |
171 |
} |
172 |
|
173 |
private Object getChildFile(Object parent, int index) { |
174 |
File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles()[index]; |
175 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath())); |
176 |
node.setParent((DefaultMutableTreeNode) parent);
|
177 |
return node;
|
178 |
} |
179 |
|
180 |
private int getIndexOfChildFiles(Object parent, Object child) { |
181 |
if (parent == null) |
182 |
return -1; |
183 |
File[] files = ((File) ((DefaultMutableTreeNode) parent).getUserObject()).listFiles(); |
184 |
for (int i = 0; files != null && i < files.length; i++) { |
185 |
if (files[i].getName().equals(child))
|
186 |
return i;
|
187 |
} |
188 |
return -1; |
189 |
} |
190 |
|
191 |
public Object getElement(Object containerFolder, String elementName) { |
192 |
if (containerFolder instanceof File) { |
193 |
containerFolder = new DefaultMutableTreeNode(containerFolder); |
194 |
} |
195 |
int index = getIndexOfChildFiles(containerFolder, elementName);
|
196 |
if (index != -1) { |
197 |
return getChildFile(containerFolder, index);
|
198 |
} else {
|
199 |
for (int i = 0; i < getChildCount(containerFolder); i++) { |
200 |
Object o = getElement(getChildFile(containerFolder, i), elementName);
|
201 |
if (o != null) { |
202 |
return o;
|
203 |
} |
204 |
} |
205 |
} |
206 |
return null; |
207 |
} |
208 |
|
209 |
|
210 |
|
211 |
public void addElement(Object element, String elementName, Object containerFolder) { |
212 |
if (element instanceof ISymbol) { |
213 |
ISymbol sym = (ISymbol) element; |
214 |
if (containerFolder == null) { |
215 |
containerFolder = rootDir; |
216 |
} |
217 |
|
218 |
String fExtension = SymbolLibrary.SYMBOL_FILE_EXTENSION;
|
219 |
File targetFile = new File(((File) containerFolder).getAbsolutePath() + File.separator + elementName); |
220 |
// save it
|
221 |
XMLEntity xml = sym.getXMLEntity(); |
222 |
if (!targetFile.
|
223 |
getAbsolutePath(). |
224 |
toLowerCase(). |
225 |
endsWith(fExtension)) |
226 |
targetFile = new File(targetFile.getAbsolutePath() + fExtension); |
227 |
if(targetFile.exists()){
|
228 |
int resp = JOptionPane.showConfirmDialog( |
229 |
(Component) PluginServices.getMainFrame(),
|
230 |
PluginServices.getText(this,
|
231 |
"fichero_ya_existe_seguro_desea_guardarlo"),
|
232 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
233 |
if (resp != JOptionPane.YES_OPTION) { |
234 |
return;
|
235 |
} |
236 |
} |
237 |
FileWriter writer;
|
238 |
try {
|
239 |
writer = new FileWriter(targetFile.getAbsolutePath()); |
240 |
Marshaller m = new Marshaller(writer);
|
241 |
m.setEncoding("ISO-8859-1");
|
242 |
m.marshal(xml.getXmlTag()); |
243 |
|
244 |
} catch (Exception ex) { |
245 |
NotificationManager.addError( |
246 |
PluginServices.getText(this, "save_error"), ex); |
247 |
} |
248 |
} else {
|
249 |
throw new IllegalArgumentException( |
250 |
PluginServices.getText(this, "adding_a_non_symbol_as_element") |
251 |
); |
252 |
} |
253 |
} |
254 |
|
255 |
public void addFolder(Object parentFolder, String folderName) { |
256 |
if (parentFolder == null) { |
257 |
parentFolder = rootDir; |
258 |
} |
259 |
try {
|
260 |
File fParentFolder = (File) ((DefaultMutableTreeNode) parentFolder).getUserObject(); |
261 |
File f = new File(fParentFolder.getAbsolutePath()+File.separator+folderName); |
262 |
if (!f.exists())
|
263 |
f.mkdir(); |
264 |
for (int i = 0; i < listeners.size(); i++) { |
265 |
listeners.get(i).treeNodesInserted(null);
|
266 |
} |
267 |
} catch (ConcurrentModificationException cme) { |
268 |
cme.printStackTrace(); |
269 |
} catch (Exception e) { |
270 |
throw new IllegalArgumentException( |
271 |
PluginServices.getText(this, "invalid_folder_name"), e |
272 |
); |
273 |
} |
274 |
} |
275 |
|
276 |
|
277 |
public void removeElement(Object element, Object containerFolder) { |
278 |
try {
|
279 |
File fParentFolder = (File) containerFolder; |
280 |
File f = new File(fParentFolder.getAbsolutePath()+File.separator+(String) element); |
281 |
if (f.exists())
|
282 |
deleteRecursively(f); |
283 |
|
284 |
} catch (Exception e) { |
285 |
throw new IllegalArgumentException( |
286 |
PluginServices.getText(this, "invalid_folder_name") |
287 |
); |
288 |
} |
289 |
} |
290 |
|
291 |
public void removeFolder(Object folderToRemove) { |
292 |
try {
|
293 |
File f = (File) folderToRemove; |
294 |
|
295 |
TreePath tp = treePathNode(f, rootDir);
|
296 |
if (f.exists())
|
297 |
f.delete(); |
298 |
} catch (Exception e) { |
299 |
throw new IllegalArgumentException( |
300 |
PluginServices.getText(this, "invalid_folder_name") |
301 |
); |
302 |
} |
303 |
} |
304 |
|
305 |
private void deleteRecursively(File f) { |
306 |
if (f.isDirectory()) {
|
307 |
for (int i = f.list().length-1; i >= 0; i--) { |
308 |
deleteRecursively(new File(f.getAbsolutePath()+File.separator+f.list()[i])); |
309 |
} |
310 |
} |
311 |
f.delete(); |
312 |
} |
313 |
|
314 |
private TreePath treePathNode(File f, File startingNode) { |
315 |
for (int i = 0; i < getChildCount(startingNode); i++) { |
316 |
File child = (File) getChild(startingNode, i); |
317 |
TreePath tp = treePathNode(f, child);
|
318 |
if (tp == null) { |
319 |
// it is not in this directory tree
|
320 |
continue;
|
321 |
} else {
|
322 |
if (startingNode != rootDir)
|
323 |
tp = tp.pathByAddingChild(startingNode); |
324 |
return tp;
|
325 |
} |
326 |
} |
327 |
|
328 |
if (f.equals(startingNode)) {
|
329 |
return new TreePath(f); |
330 |
} |
331 |
return null; |
332 |
} |
333 |
|
334 |
private void fireTreeNodesRemoved(TreePath removedObjectTreePath) { |
335 |
TreeModelEvent e = new TreeModelEvent(this, removedObjectTreePath); |
336 |
for (Iterator<TreeModelListener> iterator = listeners.iterator(); iterator.hasNext();) { |
337 |
iterator.next().treeNodesRemoved(e); |
338 |
} |
339 |
} |
340 |
|
341 |
|
342 |
} |