Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolSelectorListModel.java @ 10232

History | View | Annotate | Download (5.61 KB)

1 9745 jaume
/* 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
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46 10231 jaume
* Revision 1.4.2.3  2007-02-09 11:00:03  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.4.2.2  2007/02/08 15:43:05  jaume
50 10218 jaume
* some bug fixes in the editor and removed unnecessary imports
51
*
52
* Revision 1.4.2.1  2007/01/26 13:49:03  jaume
53 9745 jaume
* *** empty log message ***
54
*
55 9934 jaume
* Revision 1.4  2007/01/16 11:52:11  jaume
56
* *** empty log message ***
57
*
58 9745 jaume
* Revision 1.7  2007/01/10 17:05:05  jaume
59
* moved to FMap and gvSIG
60
*
61
* Revision 1.6  2006/11/06 16:06:52  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.5  2006/11/06 07:33:54  jaume
65
* javadoc, source style
66
*
67
* Revision 1.4  2006/11/02 17:19:28  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.3  2006/10/30 19:30:35  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.2  2006/10/26 16:31:21  jaume
74
* GUI
75
*
76
* Revision 1.1  2006/10/25 10:50:41  jaume
77
* movement of classes and gui stuff
78
*
79
* Revision 1.2  2006/10/24 22:26:18  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.1  2006/10/24 16:31:12  jaume
83
* *** empty log message ***
84
*
85
*
86
*/
87
package com.iver.cit.gvsig.gui.styling;
88
89
import java.io.File;
90
import java.io.FileFilter;
91
import java.io.FileNotFoundException;
92
import java.io.FileReader;
93
import java.util.ArrayList;
94
import java.util.Comparator;
95
import java.util.TreeSet;
96
import java.util.Vector;
97
98
import javax.swing.event.ListDataListener;
99
100
import org.exolab.castor.xml.MarshalException;
101
import org.exolab.castor.xml.ValidationException;
102
103
import com.iver.andami.messages.NotificationManager;
104 10218 jaume
import com.iver.cit.gvsig.fmap.core.ISymbol;
105 10231 jaume
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
106 9745 jaume
import com.iver.utiles.XMLEntity;
107
import com.iver.utiles.listManager.ListModel;
108
import com.iver.utiles.xmlEntity.generate.XmlTag;
109 9934 jaume
110 9745 jaume
public class SymbolSelectorListModel implements ListModel {
111
        public static File SYMBOL_DIRECTORY;
112
        public static final String SYMBOL_FILE_EXTENSION = ".sym";
113
114 9934 jaume
        private static final FileFilter ffilter = new FileFilter() {
115 9745 jaume
                public boolean accept(File pathname) {
116
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SYMBOL_FILE_EXTENSION);
117
                }
118
        };
119 9934 jaume
        private SymbolFilter sfilter;
120 9745 jaume
        private Vector symbols;
121
        private ArrayList listeners;
122
123 9934 jaume
        public SymbolSelectorListModel(File dir, SymbolFilter filter) {
124
                SYMBOL_DIRECTORY = dir;
125
                this.sfilter = filter;
126 9745 jaume
        }
127
128
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
129
                return symbols.remove(i);
130
        }
131
132
        public void insertAt(int i, Object o) {
133
                getObjects().insertElementAt(o, i);
134
        }
135
136
        public void add(Object o) {
137
                TreeSet map = new TreeSet(new Comparator() {
138
139
                        public int compare(Object o1, Object o2) {
140
                                ISymbol sym1 = (ISymbol) o1;
141
                                ISymbol sym2 = (ISymbol) o2;
142 9934 jaume
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
143
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
144
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
145
146 9745 jaume
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
147
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
148
                                                                                                   the same value for description than
149
                                                                                                   a previous one. */
150
                        }
151
152
                });
153
154
                map.addAll(symbols);
155
                map.add(o);
156
                symbols = new Vector(map);
157
158
        }
159
160
        public Vector getObjects() {
161
                if (symbols == null) {
162
                        symbols = new Vector();
163
164 9934 jaume
                        File[] ff = SYMBOL_DIRECTORY.listFiles(ffilter);
165 9745 jaume
                        for (int i = 0; i < ff.length; i++) {
166
167
                                XMLEntity xml;
168
                                try {
169
                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(new FileReader(ff[i])));
170 10231 jaume
                                        ISymbol sym = SymbologyFactory.createSymbolFromXML(xml, ff[i].getName());
171 9934 jaume
                                        if (sfilter.accepts(sym))
172
                                                add(sym);
173 9745 jaume
                                } catch (MarshalException e) {
174
                                        NotificationManager.
175
                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
176
                                                                "File corrupted! Skiping it...", e);
177
                                } catch (ValidationException e) {
178
                                        NotificationManager.
179
                                                addWarning("Error validating symbol file ["+ff[i].getAbsolutePath()+"].", e);
180
                                } catch (FileNotFoundException e) {
181
                                        // unreachable code, but anyway...
182
                                        NotificationManager.
183
                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
184
                                }
185
186
                        }
187
                }
188
                return symbols;
189
        }
190
191
        public int getSize() {
192
                return getObjects().size();
193
        }
194
195
        public Object getElementAt(int index) {
196
                return getObjects().get(index);
197
        }
198
199
        public void addListDataListener(ListDataListener l) {
200
                if (listeners == null)
201
                        listeners = new ArrayList();
202
                listeners.add(l);
203
        }
204
205
        public void removeListDataListener(ListDataListener l) {
206
                if (listeners!=null)
207
                        listeners.remove(l);
208
        }
209
210
}