gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.swing / org.gvsig.lrs.swing.impl / src / main / java / org / gvsig / lrs / swing / impl / PointLayersComboBoxModel.java @ 63
History | View | Annotate | Download (6.3 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.lrs.swing.impl; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.List; |
27 |
|
28 |
import javax.swing.AbstractListModel; |
29 |
import javax.swing.ComboBoxModel; |
30 |
|
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.fmap.geom.DataTypes; |
38 |
import org.gvsig.fmap.geom.Geometry; |
39 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
40 |
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection; |
41 |
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor; |
42 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
43 |
import org.gvsig.tools.exception.BaseException; |
44 |
import org.gvsig.tools.visitor.VisitCanceledException; |
45 |
|
46 |
/**
|
47 |
* @author dmartinez
|
48 |
*
|
49 |
*/
|
50 |
public class PointLayersComboBoxModel extends AbstractListModel<FLayer> implements ComboBoxModel<FLayer> { |
51 |
|
52 |
/**
|
53 |
*
|
54 |
*/
|
55 |
private static final long serialVersionUID = -1271707498886332136L; |
56 |
|
57 |
private static final Logger logger = LoggerFactory.getLogger(PointLayersComboBoxModel.class); |
58 |
|
59 |
private LayerCollection layers;
|
60 |
private List<FLyrVect> vectLayers; |
61 |
private FLayer selectedLayer;
|
62 |
|
63 |
/**
|
64 |
*
|
65 |
*/
|
66 |
public PointLayersComboBoxModel(LayerCollection layers) {
|
67 |
this.layers = layers;
|
68 |
vectLayers = new ArrayList<FLyrVect>(); |
69 |
try {
|
70 |
layers.accept(new LayersVisitor() {
|
71 |
|
72 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
73 |
// TODO Auto-generated method stub
|
74 |
|
75 |
} |
76 |
|
77 |
public void visit(FLayer layer) throws BaseException { |
78 |
if (layer instanceof FLyrVect) { |
79 |
FLyrVect lyrVect = (FLyrVect) layer; |
80 |
if (lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.POINT)
|
81 |
|| lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT)) { |
82 |
FeatureStore featureStore = lyrVect.getFeatureStore(); |
83 |
try {
|
84 |
FeatureType type = featureStore.getDefaultFeatureType(); |
85 |
|
86 |
FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
87 |
Boolean nonGeometricDataTypeFound=false; |
88 |
Boolean numericDataTypeFound=false; |
89 |
Boolean dataTypesFound=false; |
90 |
Boolean validateNext=true; |
91 |
for (int i = 0; i < attributeDescriptors.length&&!dataTypesFound; i++) { |
92 |
FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
93 |
validateNext=true;
|
94 |
if (featureAttributeDescriptor.getDataType().isNumeric()&&!numericDataTypeFound) {
|
95 |
numericDataTypeFound = true;
|
96 |
validateNext=false;
|
97 |
} |
98 |
//This is done to avoid to validate twice the same numeric field
|
99 |
if (validateNext){
|
100 |
if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
101 |
nonGeometricDataTypeFound = true;
|
102 |
} |
103 |
} |
104 |
dataTypesFound=nonGeometricDataTypeFound&&numericDataTypeFound; |
105 |
} |
106 |
if(dataTypesFound){
|
107 |
logger.debug("Correct point layer found");
|
108 |
vectLayers.add(lyrVect); |
109 |
if(lyrVect.isActive()){
|
110 |
selectedLayer = lyrVect; |
111 |
} |
112 |
} |
113 |
} catch (DataException e1) {
|
114 |
logger.warn( |
115 |
new StringBuilder().append("Error getting default feature type from layer ").append(lyrVect.getName()) |
116 |
.toString(), e1); |
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
}); |
122 |
} catch (BaseException e1) {
|
123 |
logger.warn("Can't get linear vector layers", e1);
|
124 |
} |
125 |
} |
126 |
|
127 |
/*
|
128 |
* (non-Javadoc)
|
129 |
*
|
130 |
* @see javax.swing.ListModel#getSize()
|
131 |
*/
|
132 |
public int getSize() { |
133 |
return vectLayers.size(); //.getLayersCount(); |
134 |
} |
135 |
|
136 |
/*
|
137 |
* (non-Javadoc)
|
138 |
*
|
139 |
* @see javax.swing.ListModel#getElementAt(int)
|
140 |
*/
|
141 |
public FLayer getElementAt(int index) { |
142 |
return vectLayers.get(index); //.getLayer(index); |
143 |
} |
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
*
|
148 |
* @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
|
149 |
*/
|
150 |
public void setSelectedItem(final Object anItem) { |
151 |
selectedLayer = (FLayer) anItem; |
152 |
fireContentsChanged(this, -1, -1); |
153 |
} |
154 |
|
155 |
/*
|
156 |
* (non-Javadoc)
|
157 |
*
|
158 |
* @see javax.swing.ComboBoxModel#getSelectedItem()
|
159 |
*/
|
160 |
public Object getSelectedItem() { |
161 |
return selectedLayer;
|
162 |
} |
163 |
|
164 |
} |