Statistics
| Revision:

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

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

    
104
import java.io.File;
105
import java.io.FileFilter;
106
import java.io.FileNotFoundException;
107
import java.io.FileReader;
108
import java.util.ArrayList;
109
import java.util.Comparator;
110
import java.util.TreeSet;
111
import java.util.Vector;
112

    
113
import javax.swing.event.ListDataListener;
114

    
115
import org.exolab.castor.xml.MarshalException;
116
import org.exolab.castor.xml.ValidationException;
117

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

    
125
/**
126
*
127
* @author jaume dominguez faus - jaume.dominguez@iver.es
128
*
129
*/
130
public class SymbolSelectorListModel implements ListModel {
131

    
132
        private String fileExtension;
133
        public static final String SYMBOL_FILE_EXTENSION = ".sym";
134
        protected FileFilter ffilter = new FileFilter() {
135
                public boolean accept(File pathname) {
136
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SymbolSelectorListModel.this.fileExtension);
137
                }
138
        };
139
        protected SelectorFilter sfilter;
140
        protected Vector elements;
141
        private ArrayList listeners;
142
        protected Object currentElement;
143
        protected File dir;
144

    
145
        public SymbolSelectorListModel(File dir, Object currentElemet, SelectorFilter filter, String fileExtension) {
146
                this.fileExtension = fileExtension;
147
                this.dir = dir;
148
                this.currentElement = currentElemet;
149
                this.sfilter = filter;
150
        }
151

    
152
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
153
                return elements.remove(i);
154
        }
155

    
156
        public void insertAt(int i, Object o) {
157
                getObjects().insertElementAt(o, i);
158
        }
159

    
160
        public void add(Object o) {
161
                TreeSet map = new TreeSet(new Comparator() {
162

    
163
                        public int compare(Object o1, Object o2) {
164
                                // first will always be the current symbol
165
                                if (o1.equals(currentElement)) return -1;
166
                                if (o2.equals(currentElement)) return 1;
167

    
168
                                ISymbol sym1 = (ISymbol) o1;
169
                                ISymbol sym2 = (ISymbol) o2;
170
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
171
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
172
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
173

    
174
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
175
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
176
                                                                                                   the same value for description than
177
                                                                                                   a previous one. */
178
                        }
179

    
180
                });
181

    
182
                map.addAll(elements);
183
                map.add(o);
184
                elements = new Vector(map);
185

    
186
        }
187

    
188
        public Vector getObjects() {
189
                if (elements == null) {
190
                        elements = new Vector();
191

    
192
                        if (currentElement!= null)
193
                                elements.add(currentElement);
194

    
195
                        File[] ff = dir.listFiles(ffilter);
196
                        for (int i = 0; i < ff.length; i++) {
197

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

    
217
                        }
218
                }
219
                return elements;
220
        }
221

    
222
        public int getSize() {
223
                return getObjects().size();
224
        }
225

    
226
        public Object getElementAt(int index) {
227
                return getObjects().get(index);
228
        }
229

    
230
        public void addListDataListener(ListDataListener l) {
231
                if (listeners == null)
232
                        listeners = new ArrayList();
233
                listeners.add(l);
234
        }
235

    
236
        public void removeListDataListener(ListDataListener l) {
237
                if (listeners!=null)
238
                        listeners.remove(l);
239
        }
240

    
241
}
242