Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2057 / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolSelectorListModel.java @ 39193

History | View | Annotate | Download (8.45 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 30838 2009-09-18 13:20:17Z cordinyana $
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.util.ArrayList;
119
import java.util.Comparator;
120
import java.util.TreeSet;
121
import java.util.Vector;
122

    
123
import javax.swing.event.ListDataListener;
124

    
125
import org.slf4j.Logger;
126
import org.slf4j.LoggerFactory;
127

    
128
import org.gvsig.andami.messages.NotificationManager;
129
import org.gvsig.fmap.mapcontext.MapContextLocator;
130
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
131
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
132
import org.gvsig.utils.listManager.ListModel;
133

    
134

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

    
146
    private static Logger logger = LoggerFactory.getLogger(SymbolSelectorListModel.class);
147
        private String fileExtension;
148
        protected FileFilter ffilter = new FileFilter() {
149
                public boolean accept(File pathname) {
150
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SymbolSelectorListModel.this.fileExtension);
151
                }
152
        };
153
        protected SelectorFilter sfilter;
154
        protected Vector<ISymbol> elements;
155
        private ArrayList listeners;
156
//        protected Object currentElement;
157
        protected File dir;
158

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

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

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

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

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

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

    
209
                });
210

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

    
215
        }
216

    
217
        /**
218
         * TODO: replace with the new Persistence API
219
         */
220
        public Vector<ISymbol> getObjects() {
221
                if (elements == null) {
222
                
223
                        try {
224
                                ISymbol[] symbols = MapContextLocator.getSymbolManager()
225
                                                .loadSymbols(dir, ffilter);
226
                                
227
                                if (symbols == null) {
228
                                    
229
                                    logger.info("Warning: Did not load any symbol from folder: "
230
                                        + dir.getAbsolutePath());
231
                                    elements = new Vector<ISymbol>(0);
232
                                    
233
                                    
234
                                } else {
235
                        elements = new Vector<ISymbol>(symbols.length);
236
                        for (int i = 0; i < symbols.length; i++) {
237
                            elements.add(symbols[i]);
238
                        }
239
                                }
240
                                
241
                        } catch (SymbolException e) {
242
                                NotificationManager.addWarning(
243
                                                "Error loading the symbols from the folder: "
244
                                                                + dir.getAbsolutePath(), e);
245
                        }
246

    
247
//                        File[] ff = dir.listFiles(ffilter);
248
//                        for (int i = 0; i < ff.length; i++) {
249
//
250
//                                XMLEntity xml;
251
//                                try {
252
//                                
253
//                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(XMLEncodingUtils.getReader(ff[i])));
254
//                                        ISymbol sym = SymbologyFactory.createSymbolFromXML(xml, ff[i].getName());
255
//                                        if (sfilter.accepts(sym))
256
//                                                add(sym);
257
//                                } catch (MarshalException e) {
258
//                                        NotificationManager.
259
//                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
260
//                                                                "File corrupted! Skiping it...", e);
261
//                                } catch (ValidationException e) {
262
//                                        NotificationManager.
263
//                                                addWarning("Error validating symbol file ["+ff[i].getAbsolutePath()+"].", e);
264
//                                } catch (FileNotFoundException e) {
265
//                                        // unreachable code, but anyway...
266
//                                        NotificationManager.
267
//                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
268
//                                }
269
//
270
//                        }
271
                }
272
                return elements;
273
        }
274

    
275
        public int getSize() {
276
                return getObjects().size();
277
        }
278

    
279
        public Object getElementAt(int index) {
280
                return getObjects().get(index);
281
        }
282

    
283
        public void addListDataListener(ListDataListener l) {
284
                if (listeners == null)
285
                        listeners = new ArrayList();
286
                listeners.add(l);
287
        }
288

    
289
        public void removeListDataListener(ListDataListener l) {
290
                if (listeners!=null)
291
                        listeners.remove(l);
292
        }
293

    
294
}
295