Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolSelectorListModel.java @ 10995

History | View | Annotate | Download (6.29 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: SymbolSelectorListModel.java 10995 2007-04-02 00:10:04Z jaume $
45
* $Log$
46
* Revision 1.6  2007-04-02 00:08:05  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2007/03/28 16:44:08  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1  2007/03/09 11:25:00  jaume
53
* Advanced symbology (start committing)
54
*
55
* Revision 1.4.2.4  2007/02/21 07:35:14  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4.2.3  2007/02/09 11:00:03  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.4.2.2  2007/02/08 15:43:05  jaume
62
* some bug fixes in the editor and removed unnecessary imports
63
*
64
* Revision 1.4.2.1  2007/01/26 13:49:03  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.4  2007/01/16 11:52:11  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.7  2007/01/10 17:05:05  jaume
71
* moved to FMap and gvSIG
72
*
73
* Revision 1.6  2006/11/06 16:06:52  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.5  2006/11/06 07:33:54  jaume
77
* javadoc, source style
78
*
79
* Revision 1.4  2006/11/02 17:19:28  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.3  2006/10/30 19:30:35  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.2  2006/10/26 16:31:21  jaume
86
* GUI
87
*
88
* Revision 1.1  2006/10/25 10:50:41  jaume
89
* movement of classes and gui stuff
90
*
91
* Revision 1.2  2006/10/24 22:26:18  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.1  2006/10/24 16:31:12  jaume
95
* *** empty log message ***
96
*
97
*
98
*/
99
package com.iver.cit.gvsig.gui.styling;
100

    
101
import java.io.File;
102
import java.io.FileFilter;
103
import java.io.FileNotFoundException;
104
import java.io.FileReader;
105
import java.util.ArrayList;
106
import java.util.Comparator;
107
import java.util.TreeSet;
108
import java.util.Vector;
109

    
110
import javax.swing.event.ListDataListener;
111

    
112
import org.exolab.castor.xml.MarshalException;
113
import org.exolab.castor.xml.ValidationException;
114

    
115
import com.iver.andami.messages.NotificationManager;
116
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
117
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
118
import com.iver.utiles.XMLEntity;
119
import com.iver.utiles.listManager.ListModel;
120
import com.iver.utiles.xmlEntity.generate.XmlTag;
121

    
122
public class SymbolSelectorListModel implements ListModel {
123

    
124
        private String fileExtension;
125
        public static final String SYMBOL_FILE_EXTENSION = ".sym";
126
        protected FileFilter ffilter = new FileFilter() {
127
                public boolean accept(File pathname) {
128
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SymbolSelectorListModel.this.fileExtension);
129
                }
130
        };
131
        protected SelectorFilter sfilter;
132
        protected Vector elements;
133
        private ArrayList listeners;
134
        protected Object currentElement;
135
        protected File dir;
136

    
137
        public SymbolSelectorListModel(File dir, Object currentElemet, SelectorFilter filter, String fileExtension) {
138
                this.fileExtension = fileExtension;
139
                this.dir = dir;
140
                this.currentElement = currentElemet;
141
                this.sfilter = filter;
142
        }
143

    
144
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
145
                return elements.remove(i);
146
        }
147

    
148
        public void insertAt(int i, Object o) {
149
                getObjects().insertElementAt(o, i);
150
        }
151

    
152
        public void add(Object o) {
153
                TreeSet map = new TreeSet(new Comparator() {
154

    
155
                        public int compare(Object o1, Object o2) {
156
                                // first will always be the current symbol
157
                                if (o1.equals(currentElement)) return -1;
158
                                if (o2.equals(currentElement)) return 1;
159

    
160
                                ISymbol sym1 = (ISymbol) o1;
161
                                ISymbol sym2 = (ISymbol) o2;
162
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
163
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
164
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
165

    
166
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
167
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
168
                                                                                                   the same value for description than
169
                                                                                                   a previous one. */
170
                        }
171

    
172
                });
173

    
174
                map.addAll(elements);
175
                map.add(o);
176
                elements = new Vector(map);
177

    
178
        }
179

    
180
        public Vector getObjects() {
181
                if (elements == null) {
182
                        elements = new Vector();
183

    
184
                        if (currentElement!= null)
185
                                elements.add(currentElement);
186

    
187
                        File[] ff = dir.listFiles(ffilter);
188
                        for (int i = 0; i < ff.length; i++) {
189

    
190
                                XMLEntity xml;
191
                                try {
192
                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(new FileReader(ff[i])));
193
                                        ISymbol sym = SymbologyFactory.createSymbolFromXML(xml, ff[i].getName());
194
                                        if (sfilter.accepts(sym))
195
                                                add(sym);
196
                                } catch (MarshalException e) {
197
                                        NotificationManager.
198
                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
199
                                                                "File corrupted! Skiping it...", e);
200
                                } catch (ValidationException e) {
201
                                        NotificationManager.
202
                                                addWarning("Error validating symbol file ["+ff[i].getAbsolutePath()+"].", e);
203
                                } catch (FileNotFoundException e) {
204
                                        // unreachable code, but anyway...
205
                                        NotificationManager.
206
                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
207
                                }
208

    
209
                        }
210
                }
211
                return elements;
212
        }
213

    
214
        public int getSize() {
215
                return getObjects().size();
216
        }
217

    
218
        public Object getElementAt(int index) {
219
                return getObjects().get(index);
220
        }
221

    
222
        public void addListDataListener(ListDataListener l) {
223
                if (listeners == null)
224
                        listeners = new ArrayList();
225
                listeners.add(l);
226
        }
227

    
228
        public void removeListDataListener(ListDataListener l) {
229
                if (listeners!=null)
230
                        listeners.remove(l);
231
        }
232

    
233
}
234