Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleSelectorListModel.java @ 22651

History | View | Annotate | Download (5.6 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: StyleSelectorListModel.java 18623 2008-02-08 11:02:05Z jdominguez $
45
 * $Log$
46
 * Revision 1.5  2007-08-14 11:10:20  jvidal
47
 * javadoc updated
48
 *
49
 * Revision 1.4  2007/08/07 11:21:42  jvidal
50
 * javadoc
51
 *
52
 * Revision 1.3  2007/05/08 15:44:07  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.2  2007/04/04 16:01:14  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.2  2007/03/09 11:25:00  jaume
59
 * Advanced symbology (start committing)
60
 *
61
 * Revision 1.1.2.4  2007/02/21 07:35:14  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.1.2.3  2007/02/08 15:43:04  jaume
65
 * some bug fixes in the editor and removed unnecessary imports
66
 *
67
 * Revision 1.1.2.2  2007/01/30 18:10:10  jaume
68
 * start commiting labeling stuff
69
 *
70
 * Revision 1.1.2.1  2007/01/26 13:49:03  jaume
71
 * *** empty log message ***
72
 *
73
 *
74
 */
75
package com.iver.cit.gvsig.gui.styling;
76

    
77
import java.io.File;
78
import java.io.FileNotFoundException;
79
import java.io.FileReader;
80
import java.util.Comparator;
81
import java.util.TreeSet;
82
import java.util.Vector;
83

    
84
import org.exolab.castor.xml.MarshalException;
85
import org.exolab.castor.xml.ValidationException;
86

    
87
import com.iver.andami.messages.NotificationManager;
88
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
89
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
90
import com.iver.utiles.XMLEntity;
91
import com.iver.utiles.xmlEntity.generate.XmlTag;
92

    
93
/**
94
 * Implements a list to select styles.This list
95
 * has the property that allows the user to stablish a filter to accept or reject
96
 * elements for it from a directory which is also specified when the StyleSelectorModel
97
 * is created.
98
 *
99
 * @author jaume dominguez faus - jaume.dominguez@iver.es
100
 *
101
 */
102
public class StyleSelectorListModel extends SymbolSelectorListModel {
103

    
104
        public static final String STYLE_FILE_EXTENSION = ".style";
105

    
106
        /**
107
         * <p>
108
         * Creates a new instance of the model for the list in the Style Selector window
109
         * where the styles are stored in the <b>dir</b> (root directory) param.<br>
110
         * </p>
111
         * <p>The <b>currentElement</b> defines which element is pre-selected.<br></p>
112
         * <p>The <b>filter</b> is a user defined filter used to know which elements in
113
         * the folder are accepted or rejected for this list and it is used to avoid
114
         * mixing marker styles for polygons for example.<br></p>
115
         * <p><b>fileExtension</b> param defines the extension of the file to be parsed. This
116
         * is like that to enable inheritance of this class to other file selector such
117
         * as StyleSelector.
118
         *
119
         * @param dir, the root dir where styles are located.
120
         * @param currentElemet, the element to be pre-selected.
121
         * @param filter, the filter used to show or hide some elements.
122
         * @param fileExtension, file extension used for the files to be parsed.
123
         */
124
        public StyleSelectorListModel(File dir, SelectorFilter filter, String fileExtension) {
125
                super(dir, filter, fileExtension);
126
                // TODO Auto-generated constructor stub
127
        }
128

    
129
        public Vector getObjects() {
130
                if (elements == null) {
131
                        elements = new Vector();
132

    
133
                        File[] ff = dir.listFiles(ffilter);
134
                        for (int i = 0; i < ff.length; i++) {
135

    
136
                                XMLEntity xml;
137
                                try {
138
                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(new FileReader(ff[i])));
139
                                        IStyle sty = SymbologyFactory.createStyleFromXML(xml, ff[i].getName());
140
                                        if (sfilter.accepts(sty))
141
                                                add(sty);
142
                                } catch (MarshalException e) {
143
                                        NotificationManager.
144
                                        addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
145
                                                        "File corrupted! Skiping it...", e);
146
                                } catch (ValidationException e) {
147
                                        NotificationManager.
148
                                        addWarning("Error validating style file ["+ff[i].getAbsolutePath()+"].", e);
149
                                } catch (FileNotFoundException e) {
150
                                        // unreachable code, but anyway...
151
                                        NotificationManager.
152
                                        addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
153
                                }
154

    
155
                        }
156
                }
157
                return elements;
158
        }
159

    
160

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

    
164
                        public int compare(Object o1, Object o2) {
165

    
166
                                IStyle sym1 = (IStyle) o1;
167
                                IStyle sym2 = (IStyle) o2;
168
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
169
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
170
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
171

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

    
178
                });
179

    
180
                map.addAll(elements);
181
                map.add(o);
182
                elements = new Vector(map);
183

    
184
        }
185
}