Statistics
| Revision:

root / org.gvsig.hyperlink.app / trunk / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / layers / VectLayerManager.java @ 412

History | View | Annotate | Download (10 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
package org.gvsig.hyperlink.app.extension.layers;
24

    
25
import java.awt.geom.Point2D;
26
import java.io.File;
27
import java.net.MalformedURLException;
28
import java.net.URI;
29
import java.net.URISyntaxException;
30
import java.net.URL;
31
import java.net.URLClassLoader;
32
import java.util.ArrayList;
33
import java.util.List;
34
import java.util.Map;
35
import org.apache.commons.io.FileUtils;
36
import org.apache.commons.lang3.StringUtils;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.messages.NotificationManager;
40
import org.gvsig.app.ApplicationLocator;
41
import org.gvsig.app.project.Project;
42
import org.gvsig.app.project.ProjectManager;
43
import org.gvsig.fmap.dal.DataTypes;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.feature.Feature;
46
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.FeatureSet;
48
import org.gvsig.fmap.dal.feature.FeatureType;
49
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
50
import org.gvsig.fmap.mapcontext.layers.FLayer;
51
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
52
import org.gvsig.hyperlink.app.extension.DefaultLinkTarget;
53
import org.gvsig.hyperlink.app.extension.LinkTarget;
54
import org.gvsig.tools.dispose.DisposableIterator;
55

    
56
public class VectLayerManager implements ILinkLayerManager {
57

    
58
    private FLyrVect _layer = null;
59

    
60
    @Override
61
    public LinkTarget[] getLink(Point2D point,
62
        double tolerance,
63
        String fieldName,
64
        String fileExtension) {
65
        FLyrVect lyrVect = (FLyrVect) _layer;
66
        //ArrayList<LinkTarget> uriList = new ArrayList();
67
        List<LinkTarget> uriList = new ArrayList();
68
        FeatureSet features;
69
        FeatureType featureType;
70

    
71
        try {
72
            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
73
            featureType = _layer.getFeatureStore().getDefaultFeatureType();
74
            features = lyrVect.queryByPoint(point, tolerance, featureType);
75
        } catch (DataException e) {
76
            return null;
77
        }
78

    
79
        // Si el conjunto creado no est? vac?o creamos el vector de URLS
80
        // correspondientes
81
        // a la consulta que hemos hecho.
82

    
83
        if (features != null) {
84
            try {
85
                DisposableIterator it;
86
                it = features.fastIterator();
87
                Object val = null;
88

    
89
                while (it.hasNext()) {
90
                    Feature feature = (Feature) it.next();
91

    
92
                            DefaultLinkTarget target = new DefaultLinkTarget(_layer, feature, fieldName, fileExtension);
93
                            uriList.add(target); //getURI(fieldValue, fileExtension));
94
                    }
95
                it.dispose();
96
                return uriList.toArray(new LinkTarget[0]);//(LinkTarget[]) uriList.toArray(); //(URI[]) uriList.toArray(new URI[0]);
97
            } catch (DataException e1) {
98
                PluginServices.getLogger()
99
                    .error("Hyperlink__cant_get_the_iterator", e1);
100
            }
101
        }
102
        return null; //new URI[0];
103
    }
104

    
105
    private URL toURL(File f) {
106
        try {
107
            return f.toURI().toURL();
108
        } catch (MalformedURLException ex) {
109
            return null;
110
        }
111
    }
112
    
113
    protected URI getBasicURI(String baseURI, String extension) throws URISyntaxException {
114
        String pathname;
115
        if( StringUtils.isEmpty(extension) ) {
116
            pathname = baseURI;
117
        } else {
118
            if (extension.startsWith(".")) {
119
                pathname = baseURI + extension;
120
            } else {
121
                pathname = baseURI + "." + extension;
122
            }
123
        }
124
        pathname = pathname.replace("\\","//");
125
        if( this._layer.getFeatureStore().getParameters() instanceof FilesystemStoreParameters ) {
126
            FilesystemStoreParameters params = (FilesystemStoreParameters) this._layer.getFeatureStore().getParameters();
127
            File f = params.getFile();
128
            URLClassLoader loader = new URLClassLoader(new URL[] {toURL(f)});
129
            URL url = loader.getResource(pathname);
130
            if( url != null ) {
131
                return url.toURI();
132
            }
133
            url = loader.getResource("/" + pathname);
134
            if( url != null ) {
135
                return url.toURI();
136
            }
137
            File ff = FileUtils.getFile(f.getParentFile(), pathname);
138
            if( ff.exists() ) {
139
                return ff.toURI();
140
            }
141
        }
142
        ProjectManager projectManager = ApplicationLocator.getProjectManager();
143
        Project project = projectManager.getCurrentProject();
144
        if( project.getFile()!=null ) {
145
            File ff = FileUtils.getFile(project.getFile().getParentFile(), pathname);
146
            if( ff.exists() ) {
147
                return ff.toURI();
148
            }
149
        }
150
        File ff = new File(pathname);
151
        if( ff.exists() ) {
152
            return ff.toURI();
153
        }
154
        return null;
155
    }
156
    
157

    
158
    @Override
159
    public FLayer getLayer() {
160
        return _layer;
161
    }
162

    
163
    @Override
164
    public void setLayer(FLayer layer) throws IncompatibleLayerException {
165
        try {
166
            _layer = (FLyrVect) layer;
167
        } catch (ClassCastException ex) {
168
            throw new IncompatibleLayerException(ex);
169
        }
170
    }
171

    
172
    public Object create() {
173
        return this;
174
    }
175

    
176
    public Object create(Object[] args) {
177
        return this;
178
    }
179

    
180
    public Object create(Map args) {
181
        return this;
182
    }
183

    
184
//    public URI[][] getLink(Point2D point,
185
//        double tolerance,
186
//        String[] fieldName,
187
//        String fileExtension) {
188
//        FLyrVect lyrVect = (FLyrVect) _layer;
189
//        FeatureSet features;
190
//        FeatureType featureType;
191
//        URI uri[][] = null;
192
//
193
//        try {
194
//            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
195
//            featureType = _layer.getFeatureStore().getDefaultFeatureType();
196
//            features = lyrVect.queryByPoint(point, tolerance, featureType);
197
//        } catch (Exception e) {
198
//            return null;
199
//        }
200
//
201
//        // Si el conjunto creado no est? vac?o creamos el vector de URLS
202
//        // correspondientes
203
//        // a la consulta que hemos hecho.
204
//
205
//        if (features != null) {
206
//            try {
207
//                // Creo el vector de URL?s con la misma longitud que features
208
//                uri = new URI[(int) features.getSize()][fieldName.length];
209
//
210
//                // Recorremos las features siguiendo el ejemplo de la clase que
211
//                // se
212
//                // proporciona en la API
213
//                int count = 0;
214
//                DisposableIterator it = features.fastIterator();
215
//                while (it.hasNext()) {
216
//                    Feature feat = (Feature) it.next();
217
//                    for (int fieldCount = 0; fieldCount < fieldName.length; fieldCount++) {
218
//                        // get the field ID using the field name
219
//                        String auxField =
220
//                            feat.get(fieldName[fieldCount]).toString();
221
//                        if (auxField != null) {
222
//                            if (auxField.startsWith("http:/")) {
223
//                                try {
224
//                                    uri[count][fieldCount] = new URI(auxField);
225
//                                } catch (URISyntaxException e) {
226
//                                    PluginServices.getLogger().error("", e);
227
//                                }
228
//                            } else {
229
//                                File file = new File(auxField);
230
//                                uri[count][fieldCount] = file.toURI();
231
//                            }
232
//                        } else {
233
//                            PluginServices.getLogger()
234
//                                .error("Hyperlink error. Field "
235
//                                    + fieldName[fieldCount] + "doesn't exist!!");
236
//                            uri[count][fieldCount] = null;
237
//                        }
238
//                    }
239
//                    count++;
240
//                }
241
//                it.dispose();
242
//
243
//                return uri;
244
//            } catch (DataException e) {
245
//                PluginServices.getLogger().error("", e);
246
//            }
247
//        }
248
//        return new URI[0][0];
249
//    }
250

    
251
    @Override
252
    public String[] getFieldCandidates() {
253
        try {
254
            FeatureType featureType =
255
                _layer.getFeatureStore().getDefaultFeatureType();
256
            ArrayList<String> fields = new ArrayList<String>();
257
            FeatureAttributeDescriptor[] descriptors =
258
                featureType.getAttributeDescriptors();
259
            for (int i = 0; i < descriptors.length; i++) {
260
                FeatureAttributeDescriptor descriptor = descriptors[i];
261
                if (  !(descriptor.getDataType().isObject() || 
262
                    descriptor.getDataType().getType() == DataTypes.GEOMETRY) ) {
263
                    fields.add(descriptor.getName());
264
                }
265
            }
266
            return (String[]) fields.toArray(new String[0]);
267
        } catch (DataException e) {
268
            NotificationManager.addError(PluginServices.getText(this,
269
                "Error_reading_layer_fields"), e);
270
        }
271
        return new String[0];
272
    }
273

    
274
}