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 / StyleSelectorListModel.java @ 40688
History | View | Annotate | Download (7.07 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: StyleSelectorListModel.java 30011 2009-07-17 11:07:14Z cordinyana $
|
27 |
* $Log$
|
28 |
* Revision 1.5 2007-08-14 11:10:20 jvidal
|
29 |
* javadoc updated
|
30 |
*
|
31 |
* Revision 1.4 2007/08/07 11:21:42 jvidal
|
32 |
* javadoc
|
33 |
*
|
34 |
* Revision 1.3 2007/05/08 15:44:07 jaume
|
35 |
* *** empty log message ***
|
36 |
*
|
37 |
* Revision 1.2 2007/04/04 16:01:14 jaume
|
38 |
* *** empty log message ***
|
39 |
*
|
40 |
* Revision 1.2 2007/03/09 11:25:00 jaume
|
41 |
* Advanced symbology (start committing)
|
42 |
*
|
43 |
* Revision 1.1.2.4 2007/02/21 07:35:14 jaume
|
44 |
* *** empty log message ***
|
45 |
*
|
46 |
* Revision 1.1.2.3 2007/02/08 15:43:04 jaume
|
47 |
* some bug fixes in the editor and removed unnecessary imports
|
48 |
*
|
49 |
* Revision 1.1.2.2 2007/01/30 18:10:10 jaume
|
50 |
* start commiting labeling stuff
|
51 |
*
|
52 |
* Revision 1.1.2.1 2007/01/26 13:49:03 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
*
|
56 |
*/
|
57 |
package org.gvsig.app.gui.styling; |
58 |
|
59 |
import java.io.File; |
60 |
import java.io.FileFilter; |
61 |
import java.io.FileInputStream; |
62 |
import java.io.FileNotFoundException; |
63 |
import java.io.FileReader; |
64 |
import java.io.IOException; |
65 |
import java.util.Comparator; |
66 |
import java.util.TreeSet; |
67 |
import java.util.Vector; |
68 |
|
69 |
import javax.swing.event.ListDataListener; |
70 |
|
71 |
import org.exolab.castor.xml.MarshalException; |
72 |
import org.exolab.castor.xml.ValidationException; |
73 |
import org.gvsig.andami.messages.NotificationManager; |
74 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
75 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException; |
76 |
import org.gvsig.fmap.mapcontext.rendering.symbols.impl.LoadSymbolException; |
77 |
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle; |
78 |
import org.gvsig.tools.ToolsLocator; |
79 |
import org.gvsig.tools.persistence.PersistenceManager; |
80 |
import org.gvsig.tools.persistence.PersistentState; |
81 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
82 |
import org.gvsig.utils.XMLEntity; |
83 |
import org.gvsig.utils.listManager.ListModel; |
84 |
import org.gvsig.utils.xmlEntity.generate.XmlTag; |
85 |
import org.slf4j.Logger; |
86 |
import org.slf4j.LoggerFactory; |
87 |
|
88 |
|
89 |
/**
|
90 |
* Implements a list to select styles.This list
|
91 |
* has the property that allows the user to stablish a filter to accept or reject
|
92 |
* elements for it from a directory which is also specified when the StyleSelectorModel
|
93 |
* is created.
|
94 |
*
|
95 |
*
|
96 |
*/
|
97 |
public class StyleSelectorListModel implements ListModel { |
98 |
|
99 |
private static Logger logger = LoggerFactory.getLogger( |
100 |
StyleSelectorListModel.class); |
101 |
|
102 |
public static final String STYLE_FILE_EXTENSION = ".gvssty"; |
103 |
|
104 |
protected Vector<IStyle> elements = null; |
105 |
protected File folder = null; |
106 |
protected FileFilter fFilter = null; |
107 |
protected SelectorFilter sFilter = null; |
108 |
protected String fileExt = ""; |
109 |
|
110 |
/**
|
111 |
* <p>
|
112 |
* Creates a new instance of the model for the list in the Style Selector window
|
113 |
* where the styles are stored in the <b>dir</b> (root directory) param.<br>
|
114 |
* </p>
|
115 |
* <p>The <b>currentElement</b> defines which element is pre-selected.<br></p>
|
116 |
* <p>The <b>filter</b> is a user defined filter used to know which elements in
|
117 |
* the folder are accepted or rejected for this list and it is used to avoid
|
118 |
* mixing marker styles for polygons for example.<br></p>
|
119 |
* <p><b>fileExtension</b> param defines the extension of the file to be parsed. This
|
120 |
* is like that to enable inheritance of this class to other file selector such
|
121 |
* as StyleSelector.
|
122 |
*
|
123 |
* @param dir, the root dir where styles are located.
|
124 |
* @param currentElemet, the element to be pre-selected.
|
125 |
* @param filter, the filter used to show or hide some elements.
|
126 |
* @param fileExtension, file extension used for the files to be parsed.
|
127 |
*/
|
128 |
public StyleSelectorListModel(
|
129 |
File dir,
|
130 |
SelectorFilter filter, |
131 |
String fileExtension) {
|
132 |
|
133 |
fileExt = fileExtension.toLowerCase(); |
134 |
folder = dir; |
135 |
sFilter = filter; |
136 |
fFilter = new FileFilter() { |
137 |
public boolean accept(File pathname) { |
138 |
if (!pathname.isFile()) {
|
139 |
return false; |
140 |
} else {
|
141 |
return pathname.getAbsolutePath().toLowerCase().endsWith(
|
142 |
StyleSelectorListModel.this.fileExt); |
143 |
} |
144 |
} |
145 |
}; |
146 |
} |
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
/**
|
153 |
* TODO: use the new Persistence API.
|
154 |
*/
|
155 |
public Vector<IStyle> getObjects() { |
156 |
|
157 |
if (elements == null) { |
158 |
|
159 |
elements = new Vector(); |
160 |
File[] ff = folder.listFiles(fFilter); |
161 |
for (int i = 0; i < ff.length; i++) { |
162 |
|
163 |
try {
|
164 |
IStyle sty = this.loadStyle(ff[i]);
|
165 |
if (sFilter.accepts(sty)) {
|
166 |
add(sty); |
167 |
} |
168 |
|
169 |
} catch (Exception e) { |
170 |
NotificationManager. |
171 |
addWarning("Error in file ["+ff[i].getAbsolutePath()+"]. ", e); |
172 |
} |
173 |
|
174 |
} |
175 |
} |
176 |
return elements;
|
177 |
} |
178 |
|
179 |
|
180 |
public void add(Object o) { |
181 |
TreeSet map = new TreeSet(new Comparator() { |
182 |
|
183 |
public int compare(Object o1, Object o2) { |
184 |
|
185 |
IStyle sym1 = (IStyle) o1; |
186 |
IStyle sym2 = (IStyle) o2; |
187 |
if (sym1.getDescription() == null && sym2.getDescription() != null) return -1; |
188 |
if (sym1.getDescription() != null && sym2.getDescription() == null) return 1; |
189 |
if (sym1.getDescription() == null && sym2.getDescription() == null) return 1; |
190 |
|
191 |
int result = sym1.getDescription().compareTo(sym2.getDescription());
|
192 |
return (result!=0) ? result: 1; /* this will allow adding symbols with |
193 |
the same value for description than
|
194 |
a previous one. */
|
195 |
} |
196 |
|
197 |
}); |
198 |
|
199 |
map.addAll(elements); |
200 |
map.add(o); |
201 |
elements = new Vector(map); |
202 |
|
203 |
} |
204 |
|
205 |
public int getSize() { |
206 |
return getObjects().size();
|
207 |
} |
208 |
|
209 |
public Object getElementAt(int index) { |
210 |
return getObjects().get(index);
|
211 |
} |
212 |
|
213 |
|
214 |
public void addListDataListener(ListDataListener l) { |
215 |
// TODO Auto-generated method stub
|
216 |
|
217 |
} |
218 |
|
219 |
public void removeListDataListener(ListDataListener l) { |
220 |
// TODO Auto-generated method stub
|
221 |
|
222 |
} |
223 |
|
224 |
public Object remove(int i) throws ArrayIndexOutOfBoundsException { |
225 |
return elements.remove(i);
|
226 |
} |
227 |
|
228 |
public void insertAt(int i, Object o) { |
229 |
getObjects().insertElementAt((IStyle) o, i); |
230 |
} |
231 |
|
232 |
// =========================================================
|
233 |
|
234 |
private IStyle loadStyle(File file) throws Exception { |
235 |
if (file.exists()) {
|
236 |
FileInputStream fis = new FileInputStream(file); |
237 |
|
238 |
PersistenceManager persistenceManager = ToolsLocator |
239 |
.getPersistenceManager(); |
240 |
|
241 |
PersistentState state = persistenceManager.loadState(fis); |
242 |
IStyle sty = (IStyle) persistenceManager.create(state); |
243 |
fis.close(); |
244 |
|
245 |
return sty;
|
246 |
} else {
|
247 |
return null; |
248 |
} |
249 |
|
250 |
} |
251 |
} |