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 @ 41053

History | View | Annotate | Download (6.95 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.Vector;
101

    
102
import javax.swing.AbstractListModel;
103
import javax.swing.SwingUtilities;
104

    
105
import org.gvsig.fmap.mapcontext.MapContextLocator;
106
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
107
import org.gvsig.tools.ToolsLocator;
108
import org.gvsig.tools.dispose.Disposable;
109
import org.gvsig.tools.exception.BaseException;
110
import org.gvsig.tools.task.CancellableTask;
111
import org.gvsig.tools.visitor.VisitCanceledException;
112
import org.gvsig.tools.visitor.Visitor;
113
import org.gvsig.utils.listManager.ListModel;
114
import org.slf4j.Logger;
115
import org.slf4j.LoggerFactory;
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
120
 * reject elements for it from a directory which is also specified when the
121
 * SymbolSelectorModel is created.
122
 * 
123
 */
124
public class SymbolSelectorListModel extends AbstractListModel implements
125
                ListModel, Disposable {
126

    
127
        private static Logger logger = LoggerFactory.getLogger(SymbolSelectorListModel.class);
128
        private String fileExtension;
129

    
130
        private SelectorFilter sfilter;
131
        private Vector<ISymbol> elements;
132
        private File folder;
133
        private CancellableTask symbolLoader = null;
134

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

    
166
        public synchronized void dispose() {
167
                /*
168
                 * Quien destruye la instancia de la clase se encarga de llamar al dispose
169
                 * pero no hace falta hacer seguimieto de las instancias de esta que hay
170
                 * en ejecucion, por eso no se a?ade en el constructor la instancia a la
171
                 * lista de disposable del DisposableManager.
172
                 */
173
                if( this.symbolLoader!=null ) {
174
                        this.symbolLoader.cancelRequest();
175
                }
176
        }
177

    
178
        public Object remove(int i) throws ArrayIndexOutOfBoundsException {
179
                ISymbol o = elements.remove(i);
180
                this.fireIntervalRemoved(this, i, i);
181
                return o;
182
        }
183

    
184
        public void insertAt(int i, Object o) {
185
                getObjects().insertElementAt((ISymbol) o, i);
186
                this.fireIntervalAdded(this, i, i);
187
        }
188

    
189
        protected boolean accepts(ISymbol symbol) {
190
                if (sfilter == null) {
191
                        return true;
192
                }
193
                return sfilter.accepts(symbol);
194
        }
195

    
196
        public void add(Object o) {
197
                ISymbol symbol = (ISymbol) o;
198
                if (!accepts(symbol)) {
199
                        return;
200
                }
201
                Vector<ISymbol> e = this.getObjects();
202
                e.add((ISymbol) o);
203
                try {
204
                        int n = e.size();
205
                        this.fireIntervalAdded(this, n - 1, n);
206
                } catch (Exception ex) {
207
                        // Do nothing.
208
                }
209
        }
210

    
211
        public Vector<ISymbol> getObjects() {
212
                if (elements == null) {
213
                        elements = new Vector<ISymbol>(0);
214
                        FileFilter fileFiler = new FileFilter() {
215
                                public boolean accept(File pathname) {
216
                                        return pathname.getAbsolutePath().toLowerCase().endsWith(fileExtension);
217
                                }
218
                        };
219
                        Visitor visitor = new Visitor() {
220
                                public void visit(final Object obj)
221
                                        throws VisitCanceledException, BaseException {
222
                                        SwingUtilities.invokeLater(new Runnable() {
223
                                                public void run() {
224
                                                        add(obj);
225
                                                }
226
                                        });
227
                                }
228
                        };
229
                        if( this.symbolLoader!=null ) {
230
                                this.symbolLoader.cancelRequest();
231
                        }
232
                        this.symbolLoader = MapContextLocator.getSymbolManager().loadSymbols(folder,fileFiler,visitor);
233
                         
234
                }
235
                return elements;
236
        }
237

    
238
        public int getSize() {
239
                return getObjects().size();
240
        }
241

    
242
        public Object getElementAt(int index) {
243
                return getObjects().get(index);
244
        }
245

    
246
}