40 |
40 |
*/
|
41 |
41 |
package com.iver.cit.gvsig.gui.styling;
|
42 |
42 |
|
|
43 |
import java.awt.Component;
|
43 |
44 |
import java.io.File;
|
44 |
45 |
import java.io.FileFilter;
|
45 |
46 |
import java.io.FileWriter;
|
... | ... | |
47 |
48 |
import java.util.Iterator;
|
48 |
49 |
import java.util.Vector;
|
49 |
50 |
|
|
51 |
import javax.swing.JOptionPane;
|
50 |
52 |
import javax.swing.event.TreeModelEvent;
|
51 |
53 |
import javax.swing.event.TreeModelListener;
|
52 |
54 |
import javax.swing.tree.DefaultMutableTreeNode;
|
... | ... | |
61 |
63 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
|
62 |
64 |
import com.iver.utiles.XMLEntity;
|
63 |
65 |
/**
|
64 |
|
*
|
|
66 |
*
|
65 |
67 |
* SymbolLibrary.java
|
66 |
68 |
*
|
67 |
|
*
|
|
69 |
*
|
68 |
70 |
* @author jaume dominguez faus - jaume.dominguez@iver.es Dec 7, 2007
|
69 |
71 |
*
|
70 |
72 |
*/
|
... | ... | |
73 |
75 |
private File rootDir;
|
74 |
76 |
private Vector<TreeModelListener> listeners = new Vector<TreeModelListener>();
|
75 |
77 |
private static SymbolLibrary instance = null;
|
76 |
|
|
|
78 |
|
77 |
79 |
public static SymbolLibrary getInstance() {
|
78 |
80 |
if (instance == null || rootDirString != SymbologyFactory.SymbolLibraryPath) {
|
79 |
81 |
rootDirString = SymbologyFactory.SymbolLibraryPath;
|
... | ... | |
81 |
83 |
}
|
82 |
84 |
return instance;
|
83 |
85 |
}
|
84 |
|
|
|
86 |
|
85 |
87 |
protected SymbolLibrary(File rootDir) {
|
86 |
88 |
super(new DefaultMutableTreeNode(rootDir));
|
87 |
89 |
rootDirString = PluginServices.getText(this, "symbol_library");
|
88 |
90 |
this.rootDir = rootDir;
|
89 |
91 |
}
|
90 |
|
|
|
92 |
|
91 |
93 |
final class MyFile extends File {
|
92 |
94 |
private static final long serialVersionUID = 6332989815291224773L;
|
93 |
95 |
|
... | ... | |
103 |
105 |
return path.substring(path.lastIndexOf(File.separator)+1, path.length());
|
104 |
106 |
}
|
105 |
107 |
};
|
106 |
|
|
|
108 |
|
107 |
109 |
private FileFilter ff = new FileFilter() {
|
108 |
110 |
public boolean accept(File pathname) {
|
109 |
111 |
return pathname.isDirectory();
|
... | ... | |
115 |
117 |
public Object getRoot() {
|
116 |
118 |
return new DefaultMutableTreeNode(new MyFile(rootDir.getAbsolutePath()));
|
117 |
119 |
}
|
118 |
|
|
|
120 |
|
119 |
121 |
@Override
|
120 |
122 |
public int getChildCount(Object parent) {
|
121 |
123 |
File f = null;
|
122 |
124 |
if (parent instanceof DefaultMutableTreeNode) {
|
123 |
125 |
f = (File) ((DefaultMutableTreeNode) parent).getUserObject();
|
124 |
|
|
|
126 |
|
125 |
127 |
} else {
|
126 |
128 |
f = (File) parent;
|
127 |
129 |
}
|
... | ... | |
138 |
140 |
public void addTreeModelListener(TreeModelListener l) {
|
139 |
141 |
listeners.add(l);
|
140 |
142 |
}
|
141 |
|
|
|
143 |
|
142 |
144 |
@Override
|
143 |
145 |
public void removeTreeModelListener(TreeModelListener l) {
|
144 |
146 |
listeners.remove(l);
|
145 |
147 |
}
|
146 |
|
|
|
148 |
|
147 |
149 |
@Override
|
148 |
150 |
public void valueForPathChanged(TreePath path, Object newValue) {}
|
149 |
151 |
|
150 |
|
|
|
152 |
|
151 |
153 |
@Override
|
152 |
154 |
public Object getChild(Object parent, int index) {
|
153 |
155 |
File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles(ff)[index];
|
... | ... | |
167 |
169 |
}
|
168 |
170 |
return -1;
|
169 |
171 |
}
|
170 |
|
|
|
172 |
|
171 |
173 |
private Object getChildFile(Object parent, int index) {
|
172 |
174 |
File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles()[index];
|
173 |
175 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath()));
|
174 |
176 |
node.setParent((DefaultMutableTreeNode) parent);
|
175 |
177 |
return node;
|
176 |
178 |
}
|
177 |
|
|
|
179 |
|
178 |
180 |
private int getIndexOfChildFiles(Object parent, Object child) {
|
179 |
181 |
if (parent == null)
|
180 |
182 |
return -1;
|
... | ... | |
203 |
205 |
}
|
204 |
206 |
return null;
|
205 |
207 |
}
|
206 |
|
|
207 |
|
|
208 |
|
|
|
208 |
|
|
209 |
|
|
210 |
|
209 |
211 |
public void addElement(Object element, String elementName, Object containerFolder) {
|
210 |
212 |
if (element instanceof ISymbol) {
|
211 |
213 |
ISymbol sym = (ISymbol) element;
|
212 |
214 |
if (containerFolder == null) {
|
213 |
215 |
containerFolder = rootDir;
|
214 |
216 |
}
|
215 |
|
|
|
217 |
|
216 |
218 |
String fExtension = SymbolLibrary.SYMBOL_FILE_EXTENSION;
|
217 |
219 |
File targetFile = new File(((File) containerFolder).getAbsolutePath() + File.separator + elementName);
|
218 |
220 |
// save it
|
... | ... | |
222 |
224 |
toLowerCase().
|
223 |
225 |
endsWith(fExtension))
|
224 |
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 |
}
|
225 |
237 |
FileWriter writer;
|
226 |
238 |
try {
|
227 |
239 |
writer = new FileWriter(targetFile.getAbsolutePath());
|
... | ... | |
261 |
273 |
}
|
262 |
274 |
}
|
263 |
275 |
|
264 |
|
|
|
276 |
|
265 |
277 |
public void removeElement(Object element, Object containerFolder) {
|
266 |
278 |
try {
|
267 |
279 |
File fParentFolder = (File) containerFolder;
|
268 |
280 |
File f = new File(fParentFolder.getAbsolutePath()+File.separator+(String) element);
|
269 |
281 |
if (f.exists())
|
270 |
282 |
deleteRecursively(f);
|
271 |
|
|
|
283 |
|
272 |
284 |
} catch (Exception e) {
|
273 |
285 |
throw new IllegalArgumentException(
|
274 |
286 |
PluginServices.getText(this, "invalid_folder_name")
|
... | ... | |
279 |
291 |
public void removeFolder(Object folderToRemove) {
|
280 |
292 |
try {
|
281 |
293 |
File f = (File) folderToRemove;
|
282 |
|
|
|
294 |
|
283 |
295 |
TreePath tp = treePathNode(f, rootDir);
|
284 |
296 |
if (f.exists())
|
285 |
297 |
f.delete();
|
... | ... | |
312 |
324 |
return tp;
|
313 |
325 |
}
|
314 |
326 |
}
|
315 |
|
|
|
327 |
|
316 |
328 |
if (f.equals(startingNode)) {
|
317 |
329 |
return new TreePath(f);
|
318 |
330 |
}
|
... | ... | |
325 |
337 |
iterator.next().treeNodesRemoved(e);
|
326 |
338 |
}
|
327 |
339 |
}
|
328 |
|
|
329 |
|
|
|
340 |
|
|
341 |
|
330 |
342 |
}
|