Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolSelectorListModel.java @ 34294

History | View | Annotate | Download (8.08 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.gvsig.andami.messages.NotificationManager;
126
import org.gvsig.fmap.mapcontext.MapContextLocator;
127
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
128
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
129
import org.gvsig.utils.listManager.ListModel;
130

    
131

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

    
143
        private String fileExtension;
144
        protected FileFilter ffilter = new FileFilter() {
145
                public boolean accept(File pathname) {
146
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SymbolSelectorListModel.this.fileExtension);
147
                }
148
        };
149
        protected SelectorFilter sfilter;
150
        protected Vector<ISymbol> elements;
151
        private ArrayList listeners;
152
//        protected Object currentElement;
153
        protected File dir;
154

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

    
179
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
180
                return elements.remove(i);
181
        }
182

    
183
        public void insertAt(int i, Object o) {
184
                getObjects().insertElementAt((ISymbol) o, i);
185
        }
186

    
187
        public void add(Object o) {
188
                TreeSet map = new TreeSet(new Comparator() {
189

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

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

    
205
                });
206

    
207
                map.addAll(elements);
208
                map.add(o);
209
                elements = new Vector(map);
210

    
211
        }
212

    
213
        /**
214
         * TODO: replace with the new Persistence API
215
         */
216
        public Vector<ISymbol> getObjects() {
217
                if (elements == null) {
218
                
219
                        try {
220
                                ISymbol[] symbols = MapContextLocator.getSymbolManager()
221
                                                .loadSymbols(dir, ffilter);
222
                                
223
                                elements = symbols == null ? new Vector<ISymbol>(0)
224
                                                : new Vector<ISymbol>(symbols.length);
225
                                
226
                                for (int i = 0; i < symbols.length; i++) {
227
                                        elements.add(symbols[i]);
228
                                }
229
                        } catch (SymbolException e) {
230
                                NotificationManager.addWarning(
231
                                                "Error loading the symbols from the folder: "
232
                                                                + dir.getAbsolutePath(), e);
233
                        }
234

    
235
//                        File[] ff = dir.listFiles(ffilter);
236
//                        for (int i = 0; i < ff.length; i++) {
237
//
238
//                                XMLEntity xml;
239
//                                try {
240
//                                
241
//                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(XMLEncodingUtils.getReader(ff[i])));
242
//                                        ISymbol sym = SymbologyFactory.createSymbolFromXML(xml, ff[i].getName());
243
//                                        if (sfilter.accepts(sym))
244
//                                                add(sym);
245
//                                } catch (MarshalException e) {
246
//                                        NotificationManager.
247
//                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
248
//                                                                "File corrupted! Skiping it...", e);
249
//                                } catch (ValidationException e) {
250
//                                        NotificationManager.
251
//                                                addWarning("Error validating symbol file ["+ff[i].getAbsolutePath()+"].", e);
252
//                                } catch (FileNotFoundException e) {
253
//                                        // unreachable code, but anyway...
254
//                                        NotificationManager.
255
//                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
256
//                                }
257
//
258
//                        }
259
                }
260
                return elements;
261
        }
262

    
263
        public int getSize() {
264
                return getObjects().size();
265
        }
266

    
267
        public Object getElementAt(int index) {
268
                return getObjects().get(index);
269
        }
270

    
271
        public void addListDataListener(ListDataListener l) {
272
                if (listeners == null)
273
                        listeners = new ArrayList();
274
                listeners.add(l);
275
        }
276

    
277
        public void removeListDataListener(ListDataListener l) {
278
                if (listeners!=null)
279
                        listeners.remove(l);
280
        }
281

    
282
}
283