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