Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / explorer / filesystem / FilesystemExplorer.java @ 24079

History | View | Annotate | Download (4.57 KB)

1
package org.gvsig.fmap.data.explorer.filesystem;
2

    
3
import java.io.File;
4
import java.io.FileFilter;
5
import java.io.FileNotFoundException;
6
import java.util.ArrayList;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Map;
10

    
11
import org.gvsig.fmap.data.DataExplorerParameters;
12
import org.gvsig.fmap.data.DataExplorerType;
13
import org.gvsig.fmap.data.DataStoreParameters;
14
import org.gvsig.fmap.data.exceptions.DataException;
15
import org.gvsig.fmap.data.exceptions.InitializeException;
16
import org.gvsig.fmap.data.exceptions.ReadException;
17
import org.gvsig.fmap.data.spi.DataExplorerProvider;
18
import org.gvsig.tools.extensionPoint.ExtensionPoint;
19
import org.gvsig.tools.extensionPoint.ExtensionPoints;
20
import org.gvsig.tools.extensionPoint.ExtensionPointsSingleton;
21

    
22

    
23
public class FilesystemExplorer implements DataExplorerProvider {
24
        // FIXME: , IPersistence{
25

    
26

    
27
        final private static String FILE_FILTER_EPSNAME = "FilesystemExplorerFilters";
28
        final private static String FILE_FILTER_EPSDESCRIPTION = "Registro de filtros asociados al explorador del sistema de ficheros.";
29

    
30
        FilesystemExplorerParameters parameters;
31
        private File path;
32

    
33
        public static class FilterWraper {
34

    
35
                private FileFilter filter;
36

    
37
                public FilterWraper(FileFilter filter) {
38
                        this.filter = filter;
39
                }
40

    
41
                public Object create() {
42
                        return filter;
43
                }
44

    
45
                public Object create(Object[] args) {
46
                        return filter;
47
                }
48

    
49
                public Object create(Map args) {
50
                        return filter;
51
                }
52

    
53
        }
54

    
55
        public static class FilterWraperIterator implements Iterator {
56
                private Iterator it;
57

    
58
                public FilterWraperIterator(Iterator it) {
59
                        this.it = it;
60
                }
61

    
62
                public boolean hasNext() {
63
                        return this.it.hasNext();
64
                }
65

    
66
                public Object next() {
67
                        return ((FilterWraper) this.it.next()).create();
68
                }
69

    
70
                public void remove() {
71
                        throw new UnsupportedOperationException();
72
                }
73

    
74
        }
75

    
76
        public static void registerFilter(FilesystemFileFilter filter) {
77
                ExtensionPoints eps = ExtensionPointsSingleton.getInstance();
78
                ExtensionPoint ep = (ExtensionPoint) eps.get(FILE_FILTER_EPSNAME);
79
                if (ep == null) {
80
                        ep = new ExtensionPoint(FILE_FILTER_EPSNAME,
81
                                        FILE_FILTER_EPSDESCRIPTION);
82
                        eps.put(ep);
83
                }
84
                FilterWraper filterWraper = new FilterWraper(filter);
85
                ep.put(filter.getDataStoreProviderName(), filter.getDescription(),filterWraper);
86
        }
87

    
88
        public static Iterator getFilters() {
89
                ExtensionPoints eps = ExtensionPointsSingleton.getInstance();
90
                ExtensionPoint ep = (ExtensionPoint) eps.get(FILE_FILTER_EPSNAME);
91
                Iterator it = new FilterWraperIterator(ep.values().iterator());
92
                return it;
93
        }
94

    
95
        public void init(DataExplorerParameters parameters)
96
                        throws InitializeException {
97
                this.parameters = (FilesystemExplorerParameters) parameters;
98
                this.path = new File(this.parameters.getPath());
99
        }
100

    
101
        public DataExplorerType getType() {
102
                return parameters.getType();
103
        }
104

    
105
        public DataExplorerParameters getParameters() {
106
                return parameters;
107
        }
108

    
109
        public void dispose() throws DataException {
110

    
111
        }
112

    
113
        public List list() throws DataException {
114
                if (!this.path.exists()) {
115
                        //TODO crear excepcion de Data??
116
                        new FileNotFoundException(this.getName() + ": Path not found '"
117
                                        + this.path + "'");
118
                }
119
                // DataManager dsm=DataManager.getManager();
120

    
121
                String files[] = this.path.list();
122
                int i;
123
                File theFile;
124
                ArrayList fileList = new ArrayList();
125
                DataStoreParameters dsp = null;
126

    
127
                for (i = 0; i < files.length; i++) {
128
                        theFile = new File(this.path, files[i]);
129
                        if (this.accept(theFile)) {
130
                                fileList.add(theFile);
131
                        }
132

    
133
                }
134
                return fileList;
135
        }
136

    
137
        private boolean accept(File file) {
138
                if (!file.exists()) {
139
                        return false;
140
                }
141
                if (!file.isFile()) {
142
                        return false;
143
                }
144
                if (!file.canRead()) {
145
                        return false;
146
                }
147
                if (file.isHidden()) {
148
                        return false;
149
                }
150
                Iterator filters = getFilters();
151
                while (filters.hasNext()) {
152
                        FileFilter filter = (FileFilter) filters.next();
153
                        if (filter.accept(file)) {
154
                                return true;
155
                        }
156
                }
157
                return false;
158
        }
159

    
160
        public void remove(DataStoreParameters dsp) throws ReadException {
161
                // TODO Auto-generated method stub
162

    
163
        }
164

    
165
        public boolean add(DataStoreParameters ndsp) throws DataException {
166
                // TODO Auto-generated method stub
167
                return false;
168
        }
169

    
170
        public boolean canCreate() {
171
                return this.path.canWrite();
172
        }
173

    
174
        public String getName() {
175
                // TODO Auto-generated method stub
176
                return null;
177
        }
178

    
179
        public DataStoreParameters getCreationParameters(String storeName) {
180
                // TODO Auto-generated method stub
181
                return null;
182
        }
183

    
184
        // ==========================================
185

    
186

    
187
}