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 @ 43510
History | View | Annotate | Download (5.18 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 |
|
25 |
package org.gvsig.app.gui.styling; |
26 |
|
27 |
import java.io.File; |
28 |
import java.io.FileFilter; |
29 |
import java.util.Vector; |
30 |
|
31 |
import javax.swing.AbstractListModel; |
32 |
import javax.swing.SwingUtilities; |
33 |
|
34 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
35 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dispose.Disposable; |
38 |
import org.gvsig.tools.exception.BaseException; |
39 |
import org.gvsig.tools.task.CancellableTask; |
40 |
import org.gvsig.tools.visitor.VisitCanceledException; |
41 |
import org.gvsig.tools.visitor.Visitor; |
42 |
import org.gvsig.utils.listManager.ListModel; |
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
/**
|
47 |
* Class SymbolSelectorListModel implements a list to select symbols.This list
|
48 |
* has the property that allows the user to stablish a filter to accept or
|
49 |
* reject elements for it from a directory which is also specified when the
|
50 |
* SymbolSelectorModel is created.
|
51 |
*
|
52 |
*/
|
53 |
public class SymbolSelectorListModel extends AbstractListModel implements |
54 |
ListModel, Disposable {
|
55 |
|
56 |
private static Logger logger = LoggerFactory.getLogger(SymbolSelectorListModel.class); |
57 |
private String fileExtension; |
58 |
|
59 |
private SelectorFilter sfilter;
|
60 |
private Vector<ISymbol> elements; |
61 |
private File folder; |
62 |
private CancellableTask symbolLoader = null; |
63 |
|
64 |
|
65 |
/**
|
66 |
* <p>
|
67 |
* Creates a new instance of the model for the list in the Symbol Selector
|
68 |
* window where the symbols are stored in the <b>dir</b> (root directory)
|
69 |
* param.<br>
|
70 |
* </p>
|
71 |
* <p>
|
72 |
* The <b>filter</b> is a user defined filter used to know which elements in
|
73 |
* the folder are accepted or rejected for this list and it is used to avoid
|
74 |
* mixing marker symbols for polygons for example.<br>
|
75 |
* </p>
|
76 |
* <p>
|
77 |
* <b>fileExtension</b> param defines the extension of the file to be
|
78 |
* parsed. This is like that to enable inheritance of this class to other
|
79 |
* file selector such as StyleSelector.
|
80 |
*
|
81 |
* @param dir
|
82 |
* , the root dir where symbols are located.
|
83 |
* @param filter
|
84 |
* , the filter used to show or hide some elements.
|
85 |
* @param fileExtension
|
86 |
* , file extension used for the files to be parsed.
|
87 |
*/
|
88 |
public SymbolSelectorListModel(File dir, SelectorFilter filter, |
89 |
String fileExtension) {
|
90 |
this.fileExtension = fileExtension;
|
91 |
this.folder = dir;
|
92 |
this.sfilter = filter;
|
93 |
} |
94 |
|
95 |
public synchronized void dispose() { |
96 |
/*
|
97 |
* Quien destruye la instancia de la clase se encarga de llamar al dispose
|
98 |
* pero no hace falta hacer seguimieto de las instancias de esta que hay
|
99 |
* en ejecucion, por eso no se a?ade en el constructor la instancia a la
|
100 |
* lista de disposable del DisposableManager.
|
101 |
*/
|
102 |
if( this.symbolLoader!=null ) { |
103 |
this.symbolLoader.cancelRequest();
|
104 |
} |
105 |
} |
106 |
|
107 |
public Object remove(int i) throws ArrayIndexOutOfBoundsException { |
108 |
ISymbol o = elements.remove(i); |
109 |
this.fireIntervalRemoved(this, i, i); |
110 |
return o;
|
111 |
} |
112 |
|
113 |
public void insertAt(int i, Object o) { |
114 |
getObjects().insertElementAt((ISymbol) o, i); |
115 |
this.fireIntervalAdded(this, i, i); |
116 |
} |
117 |
|
118 |
protected boolean accepts(ISymbol symbol) { |
119 |
if (sfilter == null) { |
120 |
return true; |
121 |
} |
122 |
return sfilter.accepts(symbol);
|
123 |
} |
124 |
|
125 |
public void add(Object o) { |
126 |
ISymbol symbol = (ISymbol) o; |
127 |
if (!accepts(symbol)) {
|
128 |
return;
|
129 |
} |
130 |
Vector<ISymbol> e = this.getObjects(); |
131 |
e.add((ISymbol) o); |
132 |
try {
|
133 |
int n = e.size();
|
134 |
this.fireIntervalAdded(this, n - 1, n); |
135 |
} catch (Exception ex) { |
136 |
// Do nothing.
|
137 |
} |
138 |
} |
139 |
|
140 |
public Vector<ISymbol> getObjects() { |
141 |
if (elements == null) { |
142 |
elements = new Vector<ISymbol>(0); |
143 |
FileFilter fileFiler = new FileFilter() { |
144 |
public boolean accept(File pathname) { |
145 |
return pathname.getAbsolutePath().toLowerCase().endsWith(fileExtension);
|
146 |
} |
147 |
}; |
148 |
Visitor visitor = new Visitor() {
|
149 |
public void visit(final Object obj) |
150 |
throws VisitCanceledException, BaseException {
|
151 |
SwingUtilities.invokeLater(new Runnable() { |
152 |
public void run() { |
153 |
add(obj); |
154 |
} |
155 |
}); |
156 |
} |
157 |
}; |
158 |
if( this.symbolLoader!=null ) { |
159 |
this.symbolLoader.cancelRequest();
|
160 |
} |
161 |
this.symbolLoader = MapContextLocator.getSymbolManager().loadSymbols(folder,fileFiler,visitor);
|
162 |
|
163 |
} |
164 |
return elements;
|
165 |
} |
166 |
|
167 |
public int getSize() { |
168 |
return getObjects().size();
|
169 |
} |
170 |
|
171 |
public Object getElementAt(int index) { |
172 |
return getObjects().get(index);
|
173 |
} |
174 |
|
175 |
} |