Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / styling / SymbolSelectorListModel.java @ 29596

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

    
116
import java.io.File;
117
import java.io.FileFilter;
118
import java.io.FileNotFoundException;
119
import java.util.ArrayList;
120
import java.util.Comparator;
121
import java.util.TreeSet;
122
import java.util.Vector;
123

    
124
import javax.swing.event.ListDataListener;
125

    
126
import org.exolab.castor.xml.MarshalException;
127
import org.exolab.castor.xml.ValidationException;
128
import org.gvsig.andami.messages.NotificationManager;
129
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
130
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
131
import org.gvsig.utils.XMLEntity;
132
import org.gvsig.utils.listManager.ListModel;
133
import org.gvsig.utils.xml.XMLEncodingUtils;
134
import org.gvsig.utils.xmlEntity.generate.XmlTag;
135

    
136

    
137
/**
138
 * Class SymbolSelectorListModel implements a list to select symbols.This list
139
 * has the property that allows the user to stablish a filter to accept or reject
140
 * elements for it from a directory which is also specified when teh SymbolSelectorModel
141
 * is created.
142
 *
143
 * @author jaume dominguez faus - jaume.dominguez@iver.es
144
 *
145
 */
146
public class SymbolSelectorListModel implements ListModel {
147

    
148
        private String fileExtension;
149
        protected FileFilter ffilter = new FileFilter() {
150
                public boolean accept(File pathname) {
151
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SymbolSelectorListModel.this.fileExtension);
152
                }
153
        };
154
        protected SelectorFilter sfilter;
155
        protected Vector elements;
156
        private ArrayList listeners;
157
//        protected Object currentElement;
158
        protected File dir;
159

    
160
        /**
161
         * <p>
162
         * Creates a new instance of the model for the list in the Symbol Selector window
163
         * where the symbols are stored in the <b>dir</b> (root directory) param.<br>
164
         * </p>
165
         * <p>The <b>currentElement</b> defines which element is pre-selected.<br></p>
166
         * <p>The <b>filter</b> is a user defined filter used to know which elements in
167
         * the folder are accepted or rejected for this list and it is used to avoid
168
         * mixing marker symbols for polygons for example.<br></p>
169
         * <p><b>fileExtension</b> param defines the extension of the file to be parsed. This
170
         * is like that to enable inheritance of this class to other file selector such
171
         * as StyleSelector.
172
         *
173
         * @param dir, the root dir where symbols are located.
174
         * @param currentElemet, the element to be pre-selected.
175
         * @param filter, the filter used to show or hide some elements.
176
         * @param fileExtension, file extension used for the files to be parsed.
177
         */
178
        public SymbolSelectorListModel(File dir, SelectorFilter filter, String fileExtension) {
179
                this.fileExtension = fileExtension;
180
                this.dir = dir;
181
                this.sfilter = filter;
182
        }
183

    
184
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
185
                return elements.remove(i);
186
        }
187

    
188
        public void insertAt(int i, Object o) {
189
                getObjects().insertElementAt(o, i);
190
        }
191

    
192
        public void add(Object o) {
193
                TreeSet map = new TreeSet(new Comparator() {
194

    
195
                        public int compare(Object o1, Object o2) {
196
                                // first will always be the current symbol
197
                                
198
                                ISymbol sym1 = (ISymbol) o1;
199
                                ISymbol sym2 = (ISymbol) o2;
200
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
201
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
202
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
203

    
204
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
205
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
206
                                                                                                   the same value for description than
207
                                                                                                   a previous one. */
208
                        }
209

    
210
                });
211

    
212
                map.addAll(elements);
213
                map.add(o);
214
                elements = new Vector(map);
215

    
216
        }
217

    
218
        public Vector getObjects() {
219
                if (elements == null) {
220
                        elements = new Vector();
221

    
222
                
223
                        File[] ff = dir.listFiles(ffilter);
224
                        for (int i = 0; i < ff.length; i++) {
225

    
226
                                XMLEntity xml;
227
                                try {
228
                                
229
                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(XMLEncodingUtils.getReader(ff[i])));
230
                                        ISymbol sym = SymbologyFactory.createSymbolFromXML(xml, ff[i].getName());
231
                                        if (sfilter.accepts(sym))
232
                                                add(sym);
233
                                } catch (MarshalException e) {
234
                                        NotificationManager.
235
                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
236
                                                                "File corrupted! Skiping it...", e);
237
                                } catch (ValidationException e) {
238
                                        NotificationManager.
239
                                                addWarning("Error validating symbol file ["+ff[i].getAbsolutePath()+"].", e);
240
                                } catch (FileNotFoundException e) {
241
                                        // unreachable code, but anyway...
242
                                        NotificationManager.
243
                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
244
                                }
245

    
246
                        }
247
                }
248
                return elements;
249
        }
250

    
251
        public int getSize() {
252
                return getObjects().size();
253
        }
254

    
255
        public Object getElementAt(int index) {
256
                return getObjects().get(index);
257
        }
258

    
259
        public void addListDataListener(ListDataListener l) {
260
                if (listeners == null)
261
                        listeners = new ArrayList();
262
                listeners.add(l);
263
        }
264

    
265
        public void removeListDataListener(ListDataListener l) {
266
                if (listeners!=null)
267
                        listeners.remove(l);
268
        }
269

    
270
}
271