root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / styling / SymbolLibrary.java @ 29596
History | View | Annotate | Download (9.68 KB)
1 | 18623 | jdominguez | /* 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 | 29596 | jpiera | package org.gvsig.app.gui.styling; |
42 | 18623 | jdominguez | |
43 | 28081 | fdiaz | import java.awt.Component; |
44 | 18623 | jdominguez | 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 | 28081 | fdiaz | import javax.swing.JOptionPane; |
52 | 18623 | jdominguez | 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 | 29596 | jpiera | import org.gvsig.andami.PluginServices; |
60 | import org.gvsig.andami.messages.NotificationManager; |
||
61 | 20994 | jmvivo | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
62 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory; |
||
63 | 29596 | jpiera | import org.gvsig.utils.XMLEntity; |
64 | import org.gvsig.utils.XMLException; |
||
65 | 18623 | jdominguez | |
66 | /**
|
||
67 | 22752 | vcaballero | *
|
68 | 18623 | jdominguez | * SymbolLibrary.java
|
69 | *
|
||
70 | 22752 | vcaballero | *
|
71 | 18623 | jdominguez | * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 7, 2007
|
72 | *
|
||
73 | */
|
||
74 | public class SymbolLibrary extends DefaultTreeModel implements ILibraryModel { |
||
75 | static protected String rootDirString; |
||
76 | private File rootDir; |
||
77 | private Vector<TreeModelListener> listeners = new Vector<TreeModelListener>(); |
||
78 | private static SymbolLibrary instance = null; |
||
79 | 22752 | vcaballero | |
80 | 18623 | jdominguez | public static SymbolLibrary getInstance() { |
81 | if (instance == null || rootDirString != SymbologyFactory.SymbolLibraryPath) { |
||
82 | rootDirString = SymbologyFactory.SymbolLibraryPath; |
||
83 | instance = new SymbolLibrary(new File(rootDirString)); |
||
84 | } |
||
85 | return instance;
|
||
86 | } |
||
87 | 22752 | vcaballero | |
88 | 18623 | jdominguez | protected SymbolLibrary(File rootDir) { |
89 | super(new DefaultMutableTreeNode(rootDir)); |
||
90 | rootDirString = PluginServices.getText(this, "symbol_library"); |
||
91 | this.rootDir = rootDir;
|
||
92 | } |
||
93 | 22752 | vcaballero | |
94 | 18623 | jdominguez | final class MyFile extends File { |
95 | private static final long serialVersionUID = 6332989815291224773L; |
||
96 | |||
97 | public MyFile(String pathname) { |
||
98 | super(pathname);
|
||
99 | } |
||
100 | |||
101 | public String toString() { |
||
102 | |||
103 | String path = getAbsolutePath();
|
||
104 | if (path.equals(rootDir.getAbsolutePath()))
|
||
105 | return rootDirString;
|
||
106 | return path.substring(path.lastIndexOf(File.separator)+1, path.length()); |
||
107 | } |
||
108 | }; |
||
109 | 22752 | vcaballero | |
110 | 18623 | jdominguez | private FileFilter ff = new FileFilter() { |
111 | public boolean accept(File pathname) { |
||
112 | return pathname.isDirectory();
|
||
113 | } |
||
114 | }; |
||
115 | public static final String SYMBOL_FILE_EXTENSION = ".sym"; |
||
116 | |||
117 | @Override
|
||
118 | public Object getRoot() { |
||
119 | return new DefaultMutableTreeNode(new MyFile(rootDir.getAbsolutePath())); |
||
120 | } |
||
121 | 22752 | vcaballero | |
122 | 18623 | jdominguez | @Override
|
123 | public int getChildCount(Object parent) { |
||
124 | 22752 | vcaballero | File f = null; |
125 | if (parent instanceof DefaultMutableTreeNode) { |
||
126 | f = (File) ((DefaultMutableTreeNode) parent).getUserObject(); |
||
127 | |||
128 | } else {
|
||
129 | f = (File) parent;
|
||
130 | } |
||
131 | File[] files = f.listFiles(ff); |
||
132 | return files == null ? 0 : files.length; |
||
133 | 18623 | jdominguez | } |
134 | |||
135 | @Override
|
||
136 | public boolean isLeaf(Object node) { |
||
137 | return getChildCount(node)==0; |
||
138 | } |
||
139 | |||
140 | @Override
|
||
141 | public void addTreeModelListener(TreeModelListener l) { |
||
142 | listeners.add(l); |
||
143 | } |
||
144 | 22752 | vcaballero | |
145 | 18623 | jdominguez | @Override
|
146 | public void removeTreeModelListener(TreeModelListener l) { |
||
147 | listeners.remove(l); |
||
148 | } |
||
149 | 22752 | vcaballero | |
150 | 18623 | jdominguez | @Override
|
151 | public void valueForPathChanged(TreePath path, Object newValue) {} |
||
152 | |||
153 | 22752 | vcaballero | |
154 | 18623 | jdominguez | @Override
|
155 | public Object getChild(Object parent, int index) { |
||
156 | File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles(ff)[index]; |
||
157 | DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath())); |
||
158 | node.setParent((DefaultMutableTreeNode) parent);
|
||
159 | return node;
|
||
160 | } |
||
161 | |||
162 | @Override
|
||
163 | public int getIndexOfChild(Object parent, Object child) { |
||
164 | if (parent == null) |
||
165 | return -1; |
||
166 | File[] files = ((File) ((DefaultMutableTreeNode) parent).getUserObject()).listFiles(ff); |
||
167 | for (int i = 0; i < files.length; i++) { |
||
168 | if (files[i].equals(child))
|
||
169 | return i;
|
||
170 | } |
||
171 | return -1; |
||
172 | } |
||
173 | 22752 | vcaballero | |
174 | 18623 | jdominguez | private Object getChildFile(Object parent, int index) { |
175 | File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles()[index]; |
||
176 | DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath())); |
||
177 | node.setParent((DefaultMutableTreeNode) parent);
|
||
178 | return node;
|
||
179 | } |
||
180 | 22752 | vcaballero | |
181 | 18623 | jdominguez | private int getIndexOfChildFiles(Object parent, Object child) { |
182 | if (parent == null) |
||
183 | return -1; |
||
184 | File[] files = ((File) ((DefaultMutableTreeNode) parent).getUserObject()).listFiles(); |
||
185 | 22752 | vcaballero | for (int i = 0; files != null && i < files.length; i++) { |
186 | 18623 | jdominguez | if (files[i].getName().equals(child))
|
187 | return i;
|
||
188 | } |
||
189 | return -1; |
||
190 | } |
||
191 | |||
192 | public Object getElement(Object containerFolder, String elementName) { |
||
193 | 22752 | vcaballero | if (containerFolder instanceof File) { |
194 | containerFolder = new DefaultMutableTreeNode(containerFolder); |
||
195 | } |
||
196 | 18623 | jdominguez | int index = getIndexOfChildFiles(containerFolder, elementName);
|
197 | if (index != -1) { |
||
198 | return getChildFile(containerFolder, index);
|
||
199 | } else {
|
||
200 | for (int i = 0; i < getChildCount(containerFolder); i++) { |
||
201 | Object o = getElement(getChildFile(containerFolder, i), elementName);
|
||
202 | if (o != null) { |
||
203 | return o;
|
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | return null; |
||
208 | } |
||
209 | 22752 | vcaballero | |
210 | |||
211 | |||
212 | |||
213 | 18623 | jdominguez | public void addElement(Object element, String elementName, Object containerFolder) { |
214 | if (element instanceof ISymbol) { |
||
215 | ISymbol sym = (ISymbol) element; |
||
216 | if (containerFolder == null) { |
||
217 | containerFolder = rootDir; |
||
218 | } |
||
219 | 22752 | vcaballero | |
220 | 18623 | jdominguez | String fExtension = SymbolLibrary.SYMBOL_FILE_EXTENSION;
|
221 | File targetFile = new File(((File) containerFolder).getAbsolutePath() + File.separator + elementName); |
||
222 | // save it
|
||
223 | 23176 | vcaballero | XMLEntity xml=null;
|
224 | try {
|
||
225 | xml = sym.getXMLEntity(); |
||
226 | } catch (XMLException e) {
|
||
227 | NotificationManager.addWarning("Symbol layer", e);
|
||
228 | } |
||
229 | 18623 | jdominguez | if (!targetFile.
|
230 | getAbsolutePath(). |
||
231 | toLowerCase(). |
||
232 | endsWith(fExtension)) |
||
233 | targetFile = new File(targetFile.getAbsolutePath() + fExtension); |
||
234 | 28081 | fdiaz | if(targetFile.exists()){
|
235 | int resp = JOptionPane.showConfirmDialog( |
||
236 | (Component) PluginServices.getMainFrame(),
|
||
237 | PluginServices.getText(this,
|
||
238 | "fichero_ya_existe_seguro_desea_guardarlo"),
|
||
239 | PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
||
240 | if (resp != JOptionPane.YES_OPTION) { |
||
241 | return;
|
||
242 | } |
||
243 | } |
||
244 | |||
245 | 18623 | jdominguez | FileWriter writer;
|
246 | try {
|
||
247 | writer = new FileWriter(targetFile.getAbsolutePath()); |
||
248 | Marshaller m = new Marshaller(writer);
|
||
249 | m.setEncoding("ISO-8859-1");
|
||
250 | m.marshal(xml.getXmlTag()); |
||
251 | |||
252 | } catch (Exception ex) { |
||
253 | NotificationManager.addError( |
||
254 | PluginServices.getText(this, "save_error"), ex); |
||
255 | } |
||
256 | } else {
|
||
257 | throw new IllegalArgumentException( |
||
258 | PluginServices.getText(this, "adding_a_non_symbol_as_element") |
||
259 | ); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | public void addFolder(Object parentFolder, String folderName) { |
||
264 | if (parentFolder == null) { |
||
265 | parentFolder = rootDir; |
||
266 | } |
||
267 | try {
|
||
268 | File fParentFolder = (File) ((DefaultMutableTreeNode) parentFolder).getUserObject(); |
||
269 | File f = new File(fParentFolder.getAbsolutePath()+File.separator+folderName); |
||
270 | if (!f.exists())
|
||
271 | f.mkdir(); |
||
272 | for (int i = 0; i < listeners.size(); i++) { |
||
273 | listeners.get(i).treeNodesInserted(null);
|
||
274 | } |
||
275 | } catch (ConcurrentModificationException cme) { |
||
276 | cme.printStackTrace(); |
||
277 | } catch (Exception e) { |
||
278 | throw new IllegalArgumentException( |
||
279 | PluginServices.getText(this, "invalid_folder_name"), e |
||
280 | ); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | 22752 | vcaballero | |
285 | 18623 | jdominguez | public void removeElement(Object element, Object containerFolder) { |
286 | try {
|
||
287 | File fParentFolder = (File) containerFolder; |
||
288 | File f = new File(fParentFolder.getAbsolutePath()+File.separator+(String) element); |
||
289 | if (f.exists())
|
||
290 | 22752 | vcaballero | deleteRecursively(f); |
291 | |||
292 | 18623 | jdominguez | } catch (Exception e) { |
293 | throw new IllegalArgumentException( |
||
294 | PluginServices.getText(this, "invalid_folder_name") |
||
295 | ); |
||
296 | } |
||
297 | } |
||
298 | 22752 | vcaballero | private void deleteRecursively(File f) { |
299 | if (f.isDirectory()) {
|
||
300 | for (int i = f.list().length-1; i >= 0; i--) { |
||
301 | deleteRecursively(new File(f.getAbsolutePath()+File.separator+f.list()[i])); |
||
302 | } |
||
303 | } |
||
304 | f.delete(); |
||
305 | } |
||
306 | 18623 | jdominguez | public void removeFolder(Object folderToRemove) { |
307 | try {
|
||
308 | File f = (File) folderToRemove; |
||
309 | 22752 | vcaballero | |
310 | 18623 | jdominguez | TreePath tp = treePathNode(f, rootDir);
|
311 | if (f.exists())
|
||
312 | f.delete(); |
||
313 | } catch (Exception e) { |
||
314 | throw new IllegalArgumentException( |
||
315 | PluginServices.getText(this, "invalid_folder_name") |
||
316 | ); |
||
317 | } |
||
318 | } |
||
319 | |||
320 | |||
321 | 22752 | vcaballero | |
322 | 18623 | jdominguez | private TreePath treePathNode(File f, File startingNode) { |
323 | for (int i = 0; i < getChildCount(startingNode); i++) { |
||
324 | File child = (File) getChild(startingNode, i); |
||
325 | TreePath tp = treePathNode(f, child);
|
||
326 | if (tp == null) { |
||
327 | // it is not in this directory tree
|
||
328 | continue;
|
||
329 | } else {
|
||
330 | if (startingNode != rootDir)
|
||
331 | tp = tp.pathByAddingChild(startingNode); |
||
332 | return tp;
|
||
333 | } |
||
334 | } |
||
335 | 22752 | vcaballero | |
336 | 18623 | jdominguez | if (f.equals(startingNode)) {
|
337 | return new TreePath(f); |
||
338 | } |
||
339 | return null; |
||
340 | } |
||
341 | |||
342 | private void fireTreeNodesRemoved(TreePath removedObjectTreePath) { |
||
343 | TreeModelEvent e = new TreeModelEvent(this, removedObjectTreePath); |
||
344 | for (Iterator<TreeModelListener> iterator = listeners.iterator(); iterator.hasNext();) { |
||
345 | iterator.next().treeNodesRemoved(e); |
||
346 | } |
||
347 | } |
||
348 | 22752 | vcaballero | |
349 | |||
350 | 18623 | jdominguez | } |