Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleSelectorListModel.java @ 11055

History | View | Annotate | Download (4.5 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 11055 2007-04-04 16:01:14Z jaume $
45
* $Log$
46
* Revision 1.2  2007-04-04 16:01:14  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2007/03/09 11:25:00  jaume
50
* Advanced symbology (start committing)
51
*
52
* Revision 1.1.2.4  2007/02/21 07:35:14  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.3  2007/02/08 15:43:04  jaume
56
* some bug fixes in the editor and removed unnecessary imports
57
*
58
* Revision 1.1.2.2  2007/01/30 18:10:10  jaume
59
* start commiting labeling stuff
60
*
61
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
62
* *** empty log message ***
63
*
64
*
65
*/
66
package com.iver.cit.gvsig.gui.styling;
67

    
68
import java.io.File;
69
import java.io.FileNotFoundException;
70
import java.io.FileReader;
71
import java.util.Comparator;
72
import java.util.TreeSet;
73
import java.util.Vector;
74

    
75
import org.exolab.castor.xml.MarshalException;
76
import org.exolab.castor.xml.ValidationException;
77

    
78
import com.iver.andami.messages.NotificationManager;
79
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
80
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
81
import com.iver.utiles.XMLEntity;
82
import com.iver.utiles.xmlEntity.generate.XmlTag;
83

    
84
/**
85
*
86
* @author jaume dominguez faus - jaume.dominguez@iver.es
87
*
88
*/
89
public class StyleSelectorListModel extends SymbolSelectorListModel {
90

    
91
        public static final String STYLE_FILE_EXTENSION = ".style";
92

    
93
        public StyleSelectorListModel(File dir, Object currentElemet, SelectorFilter filter, String fileExtension) {
94
                super(dir, currentElemet, filter, fileExtension);
95
                // TODO Auto-generated constructor stub
96
        }
97

    
98
        public Vector getObjects() {
99
                if (elements == null) {
100
                        elements = new Vector();
101

    
102
                        if (currentElement!= null)
103
                                elements.add(currentElement);
104

    
105
                        File[] ff = dir.listFiles(ffilter);
106
                        for (int i = 0; i < ff.length; i++) {
107

    
108
                                XMLEntity xml;
109
                                try {
110
                                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(new FileReader(ff[i])));
111
                                        IStyle sty = SymbologyFactory.createStyleFromXML(xml, ff[i].getName());
112
                                        if (sfilter.accepts(sty))
113
                                                add(sty);
114
                                } catch (MarshalException e) {
115
                                        NotificationManager.
116
                                                addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. " +
117
                                                                "File corrupted! Skiping it...", e);
118
                                } catch (ValidationException e) {
119
                                        NotificationManager.
120
                                                addWarning("Error validating style file ["+ff[i].getAbsolutePath()+"].", e);
121
                                } catch (FileNotFoundException e) {
122
                                        // unreachable code, but anyway...
123
                                        NotificationManager.
124
                                                addWarning("File not found: "+ ff[i].getAbsolutePath(), e);
125
                                }
126

    
127
                        }
128
                }
129
                return elements;
130
        }
131

    
132

    
133
        public void add(Object o) {
134
                TreeSet map = new TreeSet(new Comparator() {
135

    
136
                        public int compare(Object o1, Object o2) {
137
                                // first will always be the current symbol
138
                                if (o1.equals(currentElement)) return -1;
139
                                if (o2.equals(currentElement)) return 1;
140

    
141
                                IStyle sym1 = (IStyle) o1;
142
                                IStyle sym2 = (IStyle) o2;
143
                                if (sym1.getDescription() == null && sym2.getDescription() != null) return -1;
144
                                if (sym1.getDescription() != null && sym2.getDescription() == null) return 1;
145
                                if (sym1.getDescription() == null && sym2.getDescription() == null) return 1;
146

    
147
                                int result = sym1.getDescription().compareTo(sym2.getDescription());
148
                                return (result!=0) ? result: 1; /* this will allow adding symbols with
149
                                                                                                   the same value for description than
150
                                                                                                   a previous one. */
151
                        }
152

    
153
                });
154

    
155
                map.addAll(elements);
156
                map.add(o);
157
                elements = new Vector(map);
158

    
159
        }
160
}