Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolLibrary.java @ 22072

History | View | Annotate | Download (9.15 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.io.File;
44
import java.io.FileFilter;
45
import java.io.FileWriter;
46
import java.util.ConcurrentModificationException;
47
import java.util.Iterator;
48
import java.util.Vector;
49

    
50
import javax.swing.event.TreeModelEvent;
51
import javax.swing.event.TreeModelListener;
52
import javax.swing.tree.DefaultMutableTreeNode;
53
import javax.swing.tree.DefaultTreeModel;
54
import javax.swing.tree.TreePath;
55

    
56
import org.exolab.castor.xml.Marshaller;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
61
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
62
import com.iver.utiles.XMLEntity;
63
/**
64
 * 
65
 * SymbolLibrary.java
66
 *
67
 * 
68
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 7, 2007
69
 *
70
 */
71
public class SymbolLibrary extends DefaultTreeModel implements ILibraryModel {
72
        static protected String rootDirString;
73
        private File rootDir;
74
        private Vector<TreeModelListener> listeners = new Vector<TreeModelListener>();
75
        private static SymbolLibrary instance = null;
76
        
77
        public static SymbolLibrary getInstance() {
78
                if (instance == null || rootDirString != SymbologyFactory.SymbolLibraryPath) {
79
                        rootDirString = SymbologyFactory.SymbolLibraryPath;
80
                        instance = new SymbolLibrary(new File(rootDirString));
81
                }
82
                return instance;
83
        }
84
        
85
        protected SymbolLibrary(File rootDir) {
86
                super(new DefaultMutableTreeNode(rootDir));
87
                rootDirString = PluginServices.getText(this, "symbol_library");
88
                this.rootDir = rootDir;
89
        }
90
        
91
        final class MyFile extends File {
92
                private static final long serialVersionUID = 6332989815291224773L;
93

    
94
                public MyFile(String pathname) {
95
                        super(pathname);
96
                }
97

    
98
                public String toString() {
99

    
100
                        String path = getAbsolutePath();
101
                        if (path.equals(rootDir.getAbsolutePath()))
102
                                return rootDirString;
103
                        return path.substring(path.lastIndexOf(File.separator)+1, path.length());
104
                }
105
        };
106
        
107
        private FileFilter ff = new FileFilter() {
108
                public boolean accept(File pathname) {
109
                        return pathname.isDirectory();
110
                }
111
        };
112
        public static final String SYMBOL_FILE_EXTENSION = ".sym";
113

    
114
        @Override
115
        public Object getRoot() {
116
                return new DefaultMutableTreeNode(new MyFile(rootDir.getAbsolutePath()));
117
        }
118
        
119
        @Override
120
        public int getChildCount(Object parent) {
121
                File f = null;
122
                if (parent instanceof DefaultMutableTreeNode) {
123
                        f = (File) ((DefaultMutableTreeNode) parent).getUserObject();
124
                        
125
                } else {
126
                        f = (File) parent;
127
                }
128
                File[] files = f.listFiles(ff);
129
                return files == null ? 0 : files.length;
130
        }
131

    
132
        @Override
133
        public boolean isLeaf(Object node) {
134
                return getChildCount(node)==0;
135
        }
136

    
137
        @Override
138
        public void addTreeModelListener(TreeModelListener l) {
139
                listeners.add(l);
140
        }
141
        
142
        @Override
143
        public void removeTreeModelListener(TreeModelListener l) {
144
                listeners.remove(l);
145
        }
146
        
147
        @Override
148
        public void valueForPathChanged(TreePath path, Object newValue) {}
149

    
150
        
151
        @Override
152
        public Object getChild(Object parent, int index) {
153
                File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles(ff)[index];
154
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath()));
155
                node.setParent((DefaultMutableTreeNode) parent);
156
                return node;
157
        }
158

    
159
        @Override
160
        public int getIndexOfChild(Object parent, Object child) {
161
                if (parent == null)
162
                        return -1;
163
                File[] files = ((File) ((DefaultMutableTreeNode) parent).getUserObject()).listFiles(ff);
164
                for (int i = 0; i < files.length; i++) {
165
                        if (files[i].equals(child))
166
                                return i;
167
                }
168
                return -1;
169
        }
170
        
171
        private Object getChildFile(Object parent, int index) {
172
                File file = ((File) (((DefaultMutableTreeNode) parent).getUserObject())).listFiles()[index];
173
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(new MyFile(file.getAbsolutePath()));
174
                node.setParent((DefaultMutableTreeNode) parent);
175
                return node;
176
        }
177
        
178
        private int getIndexOfChildFiles(Object parent, Object child) {
179
                if (parent == null)
180
                        return -1;
181
                File[] files = ((File) ((DefaultMutableTreeNode) parent).getUserObject()).listFiles();
182
                for (int i = 0; files != null && i < files.length; i++) {
183
                        if (files[i].getName().equals(child))
184
                                return i;
185
                }
186
                return -1;
187
        }
188

    
189
        public Object getElement(Object containerFolder, String elementName) {
190
                if (containerFolder instanceof File) {
191
                        containerFolder = new DefaultMutableTreeNode(containerFolder);
192
                }
193
                int index = getIndexOfChildFiles(containerFolder, elementName);
194
                if (index != -1) {
195
                        return getChildFile(containerFolder, index);
196
                } else {
197
                        for (int i = 0; i < getChildCount(containerFolder); i++) {
198
                                Object o = getElement(getChildFile(containerFolder, i), elementName);
199
                                if (o != null) {
200
                                        return o;
201
                                }
202
                        }
203
                }
204
                return null;
205
        }
206
        
207
        
208
        
209
        public void addElement(Object element, String elementName, Object containerFolder) {
210
                if (element instanceof ISymbol) {
211
                        ISymbol sym = (ISymbol) element;
212
                        if (containerFolder == null) {
213
                                containerFolder = rootDir;
214
                        }
215
                        
216
                        String fExtension = SymbolLibrary.SYMBOL_FILE_EXTENSION;
217
                        File targetFile = new File(((File) containerFolder).getAbsolutePath() + File.separator + elementName);
218
                        // save it
219
                        XMLEntity xml = sym.getXMLEntity();
220
                        if (!targetFile.
221
                                        getAbsolutePath().
222
                                        toLowerCase().
223
                                        endsWith(fExtension))
224
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
225
                        FileWriter writer;
226
                        try {
227
                                writer = new FileWriter(targetFile.getAbsolutePath());
228
                                Marshaller m = new Marshaller(writer);
229
                                m.setEncoding("ISO-8859-1");
230
                                m.marshal(xml.getXmlTag());
231

    
232
                        } catch (Exception ex) {
233
                                NotificationManager.addError(
234
                                                PluginServices.getText(this, "save_error"), ex);
235
                        }
236
                } else {
237
                        throw new IllegalArgumentException(
238
                                        PluginServices.getText(this, "adding_a_non_symbol_as_element")
239
                                        );
240
                }
241
        }
242

    
243
        public void addFolder(Object parentFolder, String folderName) {
244
                if (parentFolder == null) {
245
                        parentFolder = rootDir;
246
                }
247
                try {
248
                        File fParentFolder = (File) ((DefaultMutableTreeNode) parentFolder).getUserObject();
249
                        File f = new File(fParentFolder.getAbsolutePath()+File.separator+folderName);
250
                        if (!f.exists())
251
                                f.mkdir();
252
                        for (int i = 0; i < listeners.size(); i++) {
253
                                listeners.get(i).treeNodesInserted(null);
254
                        }
255
                } catch (ConcurrentModificationException cme) {
256
                        cme.printStackTrace();
257
                } catch (Exception e) {
258
                        throw new IllegalArgumentException(
259
                                        PluginServices.getText(this, "invalid_folder_name"), e
260
                        );
261
                }
262
        }
263

    
264
        
265
        public void removeElement(Object element, Object containerFolder) {
266
                try {
267
                        File fParentFolder = (File) containerFolder;
268
                        File f = new File(fParentFolder.getAbsolutePath()+File.separator+(String) element);
269
                        if (f.exists())
270
                                deleteRecursively(f);
271
                        
272
                } catch (Exception e) {
273
                        throw new IllegalArgumentException(
274
                                        PluginServices.getText(this, "invalid_folder_name")
275
                        );
276
                }
277
        }
278

    
279
        public void removeFolder(Object folderToRemove) {
280
                try {
281
                        File f = (File) folderToRemove;
282
                        
283
                        TreePath tp = treePathNode(f, rootDir);
284
                        if (f.exists())
285
                                f.delete();
286
                } catch (Exception e) {
287
                        throw new IllegalArgumentException(
288
                                        PluginServices.getText(this, "invalid_folder_name")
289
                        );
290
                }
291
        }
292

    
293
        private void deleteRecursively(File f) {
294
                if (f.isDirectory()) {
295
                        for (int i = f.list().length-1; i >= 0; i--) {
296
                                deleteRecursively(new File(f.getAbsolutePath()+File.separator+f.list()[i]));
297
                        }
298
                }
299
                f.delete();
300
        }
301

    
302
        private TreePath treePathNode(File f, File startingNode) {
303
                for (int i = 0; i < getChildCount(startingNode); i++) {
304
                        File child = (File) getChild(startingNode, i);
305
                        TreePath tp = treePathNode(f, child);
306
                        if (tp == null) {
307
                                // it is not in this directory tree
308
                                continue;
309
                        } else {
310
                                if (startingNode != rootDir)
311
                                        tp = tp.pathByAddingChild(startingNode);
312
                                return tp;
313
                        }
314
                }
315
                
316
                if (f.equals(startingNode)) {
317
                        return new TreePath(f);
318
                }
319
                return null;
320
        }
321

    
322
        private void fireTreeNodesRemoved(TreePath removedObjectTreePath) {
323
                TreeModelEvent e = new TreeModelEvent(this, removedObjectTreePath);
324
                for (Iterator<TreeModelListener> iterator = listeners.iterator(); iterator.hasNext();) {
325
                        iterator.next().treeNodesRemoved(e);
326
                }
327
        }
328
        
329
        
330
}