Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolSelectorListModel.java @ 40560

History | View | Annotate | Download (8.29 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: SymbolSelectorListModel.java 30838 2009-09-18 13:20:17Z cordinyana $
27
* $Log$
28
* Revision 1.11  2007-09-19 15:36:36  jaume
29
* removed unnecessary imports
30
*
31
* Revision 1.10  2007/09/17 15:27:21  jaume
32
* *** empty log message ***
33
*
34
* Revision 1.9  2007/08/07 11:22:06  jvidal
35
* javadoc
36
*
37
* Revision 1.8  2007/05/08 15:44:07  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.7  2007/04/04 16:01:14  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.6  2007/04/02 00:08:05  jaume
44
* *** empty log message ***
45
*
46
* Revision 1.2  2007/03/28 16:44:08  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1  2007/03/09 11:25:00  jaume
50
* Advanced symbology (start committing)
51
*
52
* Revision 1.4.2.4  2007/02/21 07:35:14  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.4.2.3  2007/02/09 11:00:03  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4.2.2  2007/02/08 15:43:05  jaume
59
* some bug fixes in the editor and removed unnecessary imports
60
*
61
* Revision 1.4.2.1  2007/01/26 13:49:03  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.4  2007/01/16 11:52:11  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.7  2007/01/10 17:05:05  jaume
68
* moved to FMap and gvSIG
69
*
70
* Revision 1.6  2006/11/06 16:06:52  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.5  2006/11/06 07:33:54  jaume
74
* javadoc, source style
75
*
76
* Revision 1.4  2006/11/02 17:19:28  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.3  2006/10/30 19:30:35  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2  2006/10/26 16:31:21  jaume
83
* GUI
84
*
85
* Revision 1.1  2006/10/25 10:50:41  jaume
86
* movement of classes and gui stuff
87
*
88
* Revision 1.2  2006/10/24 22:26:18  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.1  2006/10/24 16:31:12  jaume
92
* *** empty log message ***
93
*
94
*
95
*/
96
package org.gvsig.app.gui.styling;
97

    
98
import java.io.File;
99
import java.io.FileFilter;
100
import java.util.ArrayList;
101
import java.util.Comparator;
102
import java.util.TreeSet;
103
import java.util.Vector;
104

    
105
import javax.swing.event.ListDataListener;
106

    
107
import org.slf4j.Logger;
108
import org.slf4j.LoggerFactory;
109

    
110
import org.gvsig.andami.messages.NotificationManager;
111
import org.gvsig.fmap.mapcontext.MapContextLocator;
112
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
113
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
114
import org.gvsig.utils.listManager.ListModel;
115

    
116

    
117
/**
118
 * Class SymbolSelectorListModel implements a list to select symbols.This list
119
 * has the property that allows the user to stablish a filter to accept or reject
120
 * elements for it from a directory which is also specified when teh SymbolSelectorModel
121
 * is created.
122
 *
123
 * @author jaume dominguez faus - jaume.dominguez@iver.es
124
 *
125
 */
126
public class SymbolSelectorListModel implements ListModel {
127

    
128
    private static Logger logger = LoggerFactory.getLogger(SymbolSelectorListModel.class);
129
        private String fileExtension;
130
        protected FileFilter ffilter = new FileFilter() {
131
                public boolean accept(File pathname) {
132
                        return pathname.getAbsolutePath().toLowerCase().endsWith(SymbolSelectorListModel.this.fileExtension);
133
                }
134
        };
135
        protected SelectorFilter sfilter;
136
        protected Vector<ISymbol> elements;
137
        private ArrayList listeners;
138
//        protected Object currentElement;
139
        protected File dir;
140

    
141
        /**
142
         * <p>
143
         * Creates a new instance of the model for the list in the Symbol Selector window
144
         * where the symbols are stored in the <b>dir</b> (root directory) param.<br>
145
         * </p>
146
         * <p>The <b>currentElement</b> defines which element is pre-selected.<br></p>
147
         * <p>The <b>filter</b> is a user defined filter used to know which elements in
148
         * the folder are accepted or rejected for this list and it is used to avoid
149
         * mixing marker symbols for polygons for example.<br></p>
150
         * <p><b>fileExtension</b> param defines the extension of the file to be parsed. This
151
         * is like that to enable inheritance of this class to other file selector such
152
         * as StyleSelector.
153
         *
154
         * @param dir, the root dir where symbols are located.
155
         * @param currentElemet, the element to be pre-selected.
156
         * @param filter, the filter used to show or hide some elements.
157
         * @param fileExtension, file extension used for the files to be parsed.
158
         */
159
        public SymbolSelectorListModel(File dir, SelectorFilter filter, String fileExtension) {
160
                this.fileExtension = fileExtension;
161
                this.dir = dir;
162
                this.sfilter = filter;
163
        }
164

    
165
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
166
                return elements.remove(i);
167
        }
168

    
169
        public void insertAt(int i, Object o) {
170
                getObjects().insertElementAt((ISymbol) o, i);
171
        }
172

    
173
        public void add(Object o) {
174
                TreeSet map = new TreeSet(new Comparator() {
175

    
176
                        public int compare(Object o1, Object o2) {
177
                                // first will always be the current symbol
178
                                
179
                                ISymbol sym1 = (ISymbol) o1;
180
                                ISymbol sym2 = (ISymbol) o2;
181
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
182
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
183
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
184

    
185
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
186
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
187
                                                                                                   the same value for description than
188
                                                                                                   a previous one. */
189
                        }
190

    
191
                });
192

    
193
                map.addAll(elements);
194
                map.add(o);
195
                elements = new Vector(map);
196

    
197
        }
198

    
199
        /**
200
         * TODO: replace with the new Persistence API
201
         */
202
        public Vector<ISymbol> getObjects() {
203
                if (elements == null) {
204
                
205
                        try {
206
                                ISymbol[] symbols = MapContextLocator.getSymbolManager()
207
                                                .loadSymbols(dir, ffilter);
208
                                
209
                                if (symbols == null) {
210
                                    
211
                                    logger.info("Warning: Did not load any symbol from folder: "
212
                                        + dir.getAbsolutePath());
213
                                    elements = new Vector<ISymbol>(0);
214
                                    
215
                                    
216
                                } else {
217
                        elements = new Vector<ISymbol>(symbols.length);
218
                        for (int i = 0; i < symbols.length; i++) {
219
                            if (sfilter == null || sfilter.accepts(symbols[i])) {
220
                                elements.add(symbols[i]);
221
                            }
222
                        }
223
                                }
224
                                
225
                        } catch (SymbolException e) {
226
                                NotificationManager.addWarning(
227
                                                "Error loading the symbols from the folder: "
228
                                                                + dir.getAbsolutePath(), e);
229
                        }
230

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

    
259
        public int getSize() {
260
                return getObjects().size();
261
        }
262

    
263
        public Object getElementAt(int index) {
264
                return getObjects().get(index);
265
        }
266

    
267
        public void addListDataListener(ListDataListener l) {
268
                if (listeners == null)
269
                        listeners = new ArrayList();
270
                listeners.add(l);
271
        }
272

    
273
        public void removeListDataListener(ListDataListener l) {
274
                if (listeners!=null)
275
                        listeners.remove(l);
276
        }
277

    
278
}
279