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

History | View | Annotate | Download (8.24 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.URI;
28
import java.net.URISyntaxException;
29
import java.util.ArrayList;
30
import java.util.Map;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.andami.messages.NotificationManager;
34
import org.gvsig.fmap.dal.DataTypes;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39
import org.gvsig.fmap.dal.feature.FeatureType;
40
import org.gvsig.fmap.mapcontext.layers.FLayer;
41
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
42
import org.gvsig.tools.dispose.DisposableIterator;
43

    
44
public class VectLayerManager implements ILinkLayerManager {
45

    
46
    private FLyrVect _layer = null;
47

    
48
    public URI[] getLink(Point2D point,
49
        double tolerance,
50
        String fieldName,
51
        String fileExtension) {
52
        FLyrVect lyrVect = (FLyrVect) _layer;
53
        ArrayList<URI> uriList = new ArrayList();
54
        FeatureSet features;
55
        FeatureType featureType;
56

    
57
        try {
58
            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
59
            featureType = _layer.getFeatureStore().getDefaultFeatureType();
60
            features = lyrVect.queryByPoint(point, tolerance, featureType);
61
        } catch (Exception e) {
62
            return null;
63
        }
64

    
65
        // Si el conjunto creado no est? vac?o creamos el vector de URLS
66
        // correspondientes
67
        // a la consulta que hemos hecho.
68

    
69
        if (features != null) {
70
            try {
71
                DisposableIterator it;
72
                it = features.fastIterator();
73
                Object val = null;
74

    
75
                while (it.hasNext()) {
76
                    Feature feature = (Feature) it.next();
77
                    val = feature.get(fieldName);
78
                    String fieldValue = (val == null) ? "" : val.toString();
79
                    if (!fieldValue.equals("")) {
80
                        try {
81
                            uriList.add(getURI(fieldValue, fileExtension));
82
                        } catch (URISyntaxException e) {
83
                            NotificationManager.addWarning(PluginServices.getText(this,
84
                                "Hyperlink__field_value_is_not_valid_file"),
85
                                e);
86
                        }
87
                    }
88

    
89
                }
90
                it.dispose();
91
                return (URI[]) uriList.toArray(new URI[0]);
92
            } catch (DataException e1) {
93
                PluginServices.getLogger()
94
                    .error("Hyperlink__cant_get_the_iterator", e1);
95
            }
96
        }
97
        return new URI[0];
98
    }
99

    
100
    protected URI getURI(String baseURI, String extension) throws URISyntaxException {
101
        String stringURI;
102
        if (extension.equals("")) {
103
            stringURI = baseURI;
104
        } else
105
            if (extension.startsWith(".")) {
106
                stringURI = baseURI + extension;
107
            } else {
108
                stringURI = baseURI + "." + extension;
109
            }
110
        File file = new File(stringURI);
111
        if (file.exists()) {
112
            return file.toURI();
113
        } else {
114
            return new URI(stringURI);
115
        }
116
    }
117

    
118
    public FLayer getLayer() {
119
        return _layer;
120
    }
121

    
122
    public void setLayer(FLayer layer) throws IncompatibleLayerException {
123
        try {
124
            _layer = (FLyrVect) layer;
125
        } catch (ClassCastException ex) {
126
            throw new IncompatibleLayerException(ex);
127
        }
128
    }
129

    
130
    public Object create() {
131
        return this;
132
    }
133

    
134
    public Object create(Object[] args) {
135
        return this;
136
    }
137

    
138
    public Object create(Map args) {
139
        return this;
140
    }
141

    
142
    public URI[][] getLink(Point2D point,
143
        double tolerance,
144
        String[] fieldName,
145
        String fileExtension) {
146
        FLyrVect lyrVect = (FLyrVect) _layer;
147
        FeatureSet features;
148
        FeatureType featureType;
149
        URI uri[][] = null;
150

    
151
        try {
152
            // FIXME: Habr? que ver como lo hacemos con las capas multigeometr?a
153
            featureType = _layer.getFeatureStore().getDefaultFeatureType();
154
            features = lyrVect.queryByPoint(point, tolerance, featureType);
155
        } catch (Exception e) {
156
            return null;
157
        }
158

    
159
        // Si el conjunto creado no est? vac?o creamos el vector de URLS
160
        // correspondientes
161
        // a la consulta que hemos hecho.
162

    
163
        if (features != null) {
164
            try {
165
                // Creo el vector de URL?s con la misma longitud que features
166
                uri = new URI[(int) features.getSize()][fieldName.length];
167

    
168
                // Recorremos las features siguiendo el ejemplo de la clase que
169
                // se
170
                // proporciona en la API
171
                int count = 0;
172
                DisposableIterator it = features.fastIterator();
173
                while (it.hasNext()) {
174
                    Feature feat = (Feature) it.next();
175
                    for (int fieldCount = 0; fieldCount < fieldName.length; fieldCount++) {
176
                        // get the field ID using the field name
177
                        String auxField =
178
                            feat.get(fieldName[fieldCount]).toString();
179
                        if (auxField != null) {
180
                            if (auxField.startsWith("http:/")) {
181
                                try {
182
                                    uri[count][fieldCount] = new URI(auxField);
183
                                } catch (URISyntaxException e) {
184
                                    PluginServices.getLogger().error("", e);
185
                                }
186
                            } else {
187
                                File file = new File(auxField);
188
                                uri[count][fieldCount] = file.toURI();
189
                            }
190
                        } else {
191
                            PluginServices.getLogger()
192
                                .error("Hyperlink error. Field "
193
                                    + fieldName[fieldCount] + "doesn't exist!!");
194
                            uri[count][fieldCount] = null;
195
                        }
196
                    }
197
                    count++;
198
                }
199
                it.dispose();
200

    
201
                return uri;
202
            } catch (DataException e) {
203
                PluginServices.getLogger().error("", e);
204
            }
205
        }
206
        return new URI[0][0];
207
    }
208

    
209
    public String[] getFieldCandidates() {
210
        try {
211
            FeatureType featureType =
212
                _layer.getFeatureStore().getDefaultFeatureType();
213
            ArrayList<String> fields = new ArrayList<String>();
214
            FeatureAttributeDescriptor[] descriptors =
215
                featureType.getAttributeDescriptors();
216
            for (int i = 0; i < descriptors.length; i++) {
217
                FeatureAttributeDescriptor descriptor = descriptors[i];
218
                if (  !(descriptor.getDataType().isObject() || 
219
                    descriptor.getDataType().getType() == DataTypes.GEOMETRY) ) {
220
                    fields.add(descriptor.getName());
221
                }
222
            }
223
            return (String[]) fields.toArray(new String[0]);
224
        } catch (DataException e) {
225
            NotificationManager.addError(PluginServices.getText(this,
226
                "Error_reading_layer_fields"), e);
227
        }
228
        return new String[0];
229
    }
230

    
231
}