Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / layers / VectLayerManager.java @ 33402

History | View | Annotate | Download (8.15 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.exception.DataException;
35
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
37
import org.gvsig.fmap.dal.feature.FeatureSet;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
41
import org.gvsig.tools.dataTypes.DataTypes;
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.iterator();
73
                while (it.hasNext()) {
74
                    Feature feature = (Feature) it.next();
75
                    String fieldValue = feature.get(fieldName).toString();
76
                    if (!fieldValue.equals("")) {
77
                        try {
78
                            uriList.add(getURI(fieldValue, fileExtension));
79
                        } catch (URISyntaxException e) {
80
                            NotificationManager.addWarning(PluginServices.getText(this,
81
                                "Hyperlink__field_value_is_not_valid_file"),
82
                                e);
83
                        }
84
                    }
85

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

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

    
115
    public FLayer getLayer() {
116
        return _layer;
117
    }
118

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

    
127
    public Object create() {
128
        return this;
129
    }
130

    
131
    public Object create(Object[] args) {
132
        return this;
133
    }
134

    
135
    public Object create(Map args) {
136
        return this;
137
    }
138

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

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

    
156
        // Si el conjunto creado no est? vac?o creamos el vector de URLS
157
        // correspondientes
158
        // a la consulta que hemos hecho.
159

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

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

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

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

    
228
}