1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
|
* Stefan A. Krüger - additional utility classes |
29 |
|
|
******************************************************************************/ |
30 |
|
|
package skrueger; |
31 |
|
|
|
32 |
alfonx |
518 |
import java.util.ArrayList; |
33 |
alfonx |
658 |
import java.util.HashSet; |
34 |
alfonx |
518 |
import java.util.List; |
35 |
|
|
|
36 |
alfonx |
244 |
import org.apache.log4j.Logger; |
37 |
alfonx |
464 |
import org.geotools.feature.NameImpl; |
38 |
alfonx |
658 |
import org.geotools.feature.Feature.NULL; |
39 |
alfonx |
464 |
import org.opengis.feature.type.AttributeDescriptor; |
40 |
|
|
import org.opengis.feature.type.Name; |
41 |
alfonx |
244 |
|
42 |
alfonx |
420 |
import skrueger.geotools.Copyable; |
43 |
mojays |
325 |
import skrueger.geotools.StyledLayerInterface; |
44 |
alfonx |
518 |
import skrueger.i8n.I8NUtil; |
45 |
alfonx |
244 |
import skrueger.i8n.Translation; |
46 |
|
|
|
47 |
|
|
/** |
48 |
alfonx |
464 |
* This class holds meta information about an attribute/column. This information |
49 |
alfonx |
518 |
* is used by {@link StyledLayerInterface} and many others.<br/> |
50 |
alfonx |
464 |
* |
51 |
alfonx |
244 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
52 |
|
|
*/ |
53 |
alfonx |
518 |
public class AttributeMetadata implements Copyable<AttributeMetadata>, |
54 |
|
|
Comparable<AttributeMetadata> { |
55 |
alfonx |
534 |
|
56 |
alfonx |
244 |
static private final Logger LOGGER = Logger |
57 |
alfonx |
463 |
.getLogger(AttributeMetadata.class); |
58 |
alfonx |
464 |
|
59 |
alfonx |
518 |
/** Translation of the attribute's description **/ |
60 |
alfonx |
244 |
protected Translation desc = new Translation(); |
61 |
alfonx |
518 |
|
62 |
alfonx |
534 |
/** |
63 |
|
|
* For numerical attributes the value can be transformed by VALUE*X+A when |
64 |
|
|
* presented on screen. TODO not implemented yet |
65 |
|
|
**/ |
66 |
|
|
protected Double functionA = 0.; |
67 |
alfonx |
518 |
|
68 |
|
|
/** |
69 |
alfonx |
534 |
* For numerical attributes the value can be transformed by VALUE*X+A when |
70 |
|
|
* presented on screen. TODO not implemented yet |
71 |
alfonx |
518 |
**/ |
72 |
alfonx |
534 |
protected Double functionX = 1.; |
73 |
alfonx |
518 |
|
74 |
|
|
/** The Name of the attribute **/ |
75 |
alfonx |
470 |
private Name name; |
76 |
alfonx |
464 |
|
77 |
alfonx |
244 |
/** |
78 |
alfonx |
518 |
* Allows to define general NODATA values for an attribute. e.g. -9999 can |
79 |
|
|
* be set and will always be interpreted as NULL internally and will usually |
80 |
alfonx |
658 |
* be ignored. This overcomes the problem, that |
81 |
alfonx |
518 |
**/ |
82 |
alfonx |
658 |
protected HashSet<Object> nodataValues = new HashSet<Object>(); |
83 |
alfonx |
518 |
|
84 |
alfonx |
534 |
/** Translation of the attribute's title **/ |
85 |
|
|
protected Translation title = new Translation(); |
86 |
|
|
|
87 |
alfonx |
518 |
/** |
88 |
alfonx |
534 |
* The unit append to all visualizations of values of this attribute (is not |
89 |
|
|
* null) |
90 |
alfonx |
518 |
**/ |
91 |
alfonx |
534 |
protected String unit = ""; |
92 |
alfonx |
518 |
|
93 |
alfonx |
534 |
/** Is the attribute visible to the user or ignored where possible **/ |
94 |
|
|
protected boolean visible = true; |
95 |
|
|
|
96 |
alfonx |
518 |
/** |
97 |
alfonx |
534 |
* When listed, the attributes are listed according to their {@link #weight} (heavier |
98 |
|
|
* => further down) |
99 |
|
|
* @see #compareTo(AttributeMetadata) |
100 |
alfonx |
518 |
**/ |
101 |
alfonx |
534 |
protected int weight = 0; |
102 |
alfonx |
518 |
|
103 |
alfonx |
534 |
/** Only used for {@link Copyable<AttributeMetaData>#copy()} **/ |
104 |
|
|
private AttributeMetadata() { |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
public AttributeMetadata(final AttributeDescriptor attDesc, final int weight, |
108 |
|
|
final List<String> langs) { |
109 |
|
|
this(attDesc.getName(), langs); |
110 |
|
|
setWeight(weight); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
public AttributeMetadata(final AttributeDescriptor attDesc, final List<String> langs) { |
114 |
|
|
this(attDesc.getName(), langs); |
115 |
|
|
} |
116 |
|
|
|
117 |
alfonx |
518 |
/** |
118 |
alfonx |
464 |
* Creates an {@link AttributeMetadata} object with the following |
119 |
|
|
* information |
120 |
|
|
* |
121 |
|
|
* @param colIdx |
122 |
|
|
* The column index of this attribute in the underlying |
123 |
|
|
* table/dbf/etc... |
124 |
|
|
* @param visible |
125 |
|
|
* Shall this attribute be displayed or hidden from the user? |
126 |
|
|
* @param title |
127 |
|
|
* {@link Translation} for Name |
128 |
|
|
* @param desc |
129 |
|
|
* {@link Translation} for an attribute description |
130 |
|
|
* @param unit |
131 |
|
|
* {@link String} of the unit that the information is in |
132 |
alfonx |
244 |
*/ |
133 |
alfonx |
464 |
public AttributeMetadata(final Name name, final Boolean visible, |
134 |
alfonx |
244 |
final Translation title, final Translation desc, final String unit) { |
135 |
alfonx |
464 |
|
136 |
|
|
this.setName(name); |
137 |
alfonx |
244 |
this.title = title; |
138 |
|
|
this.desc = desc; |
139 |
alfonx |
546 |
this.visible = visible; |
140 |
alfonx |
244 |
this.unit = unit; |
141 |
|
|
} |
142 |
alfonx |
658 |
|
143 |
alfonx |
244 |
|
144 |
|
|
/** |
145 |
alfonx |
658 |
* Creates an {@link AttributeMetadata} object with the following |
146 |
|
|
* information |
147 |
|
|
* |
148 |
|
|
* @param colIdx |
149 |
|
|
* The column index of this attribute in the underlying |
150 |
|
|
* table/dbf/etc... |
151 |
|
|
* @param visible |
152 |
|
|
* Shall this attribute be displayed or hidden from the user? |
153 |
|
|
* @param unit |
154 |
|
|
* {@link String} of the unit that the information is in |
155 |
|
|
*/ |
156 |
|
|
public AttributeMetadata(final Name name, final Boolean visible, final String unit) { |
157 |
|
|
this.setName(name); |
158 |
|
|
this.visible = visible; |
159 |
|
|
this.unit = unit; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
/** |
163 |
alfonx |
534 |
* Creates a new visible {@link AttributeMetadata} |
164 |
|
|
*/ |
165 |
|
|
public AttributeMetadata(final Name name, final List<String> langs) { |
166 |
|
|
this(name, true, new Translation(langs, name.getLocalPart()), |
167 |
|
|
new Translation(), ""); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/** |
171 |
|
|
* Creates a new visible {@link AttributeMetadata} |
172 |
|
|
*/ |
173 |
|
|
public AttributeMetadata(final Name name, final String defaultTitle, |
174 |
|
|
final List<String> langs) { |
175 |
|
|
this(name, true, new Translation(langs, defaultTitle), |
176 |
|
|
new Translation(), ""); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
alfonx |
464 |
* Creates an {@link AttributeMetadata} object with the following |
181 |
|
|
* information |
182 |
|
|
* |
183 |
|
|
* @param visible |
184 |
|
|
* Shall this attribute be displayed or hidden from the user? |
185 |
|
|
* @param title |
186 |
|
|
* {@link Translation} for Name |
187 |
|
|
* @param desc |
188 |
|
|
* {@link Translation} for an attribute description |
189 |
|
|
* @param unit |
190 |
|
|
* {@link String} of the unit that the information is in |
191 |
alfonx |
244 |
*/ |
192 |
alfonx |
464 |
public AttributeMetadata(final String localName, final Boolean visible, |
193 |
|
|
final Translation title, final Translation desc, final String unit) { |
194 |
|
|
this(new NameImpl(localName), true, title, desc, ""); |
195 |
alfonx |
244 |
} |
196 |
|
|
|
197 |
alfonx |
464 |
/** |
198 |
|
|
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
199 |
|
|
*/ |
200 |
alfonx |
534 |
public AttributeMetadata(final String localName, final List<String> langs) { |
201 |
|
|
this(localName, true, new Translation(langs, localName), |
202 |
|
|
new Translation(), ""); |
203 |
alfonx |
464 |
} |
204 |
|
|
|
205 |
|
|
/** |
206 |
alfonx |
534 |
* Creates a new visible {@link AttributeMetadata} |
207 |
alfonx |
464 |
*/ |
208 |
alfonx |
534 |
public AttributeMetadata(final String localName, final String defaultTitle, |
209 |
|
|
final List<String> langs) { |
210 |
|
|
this(localName, true, new Translation(langs, defaultTitle), |
211 |
|
|
new Translation(), ""); |
212 |
alfonx |
464 |
} |
213 |
|
|
|
214 |
|
|
/** |
215 |
alfonx |
534 |
* Orders the attributes according to their {@link #weight}. Heavier => |
216 |
|
|
* further down. |
217 |
alfonx |
464 |
*/ |
218 |
alfonx |
534 |
@Override |
219 |
|
|
public int compareTo(final AttributeMetadata atm2) { |
220 |
|
|
return new Integer(weight).compareTo(atm2.getWeight()); |
221 |
alfonx |
464 |
} |
222 |
|
|
|
223 |
|
|
/** |
224 |
alfonx |
534 |
* @see Copyable inferface |
225 |
alfonx |
464 |
*/ |
226 |
alfonx |
534 |
@Override |
227 |
|
|
public AttributeMetadata copy() { |
228 |
|
|
return copyTo(new AttributeMetadata()); |
229 |
alfonx |
464 |
} |
230 |
|
|
|
231 |
alfonx |
534 |
/** |
232 |
|
|
* @see Copyable inferface |
233 |
|
|
*/ |
234 |
|
|
@Override |
235 |
|
|
public AttributeMetadata copyTo(final AttributeMetadata amd) { |
236 |
|
|
getTitle().copyTo(amd.getTitle()); |
237 |
|
|
getDesc().copyTo(amd.getDesc()); |
238 |
|
|
amd.setUnit(getUnit()); |
239 |
|
|
amd.setVisible(isVisible()); |
240 |
|
|
amd.setName(new NameImpl(getName().getNamespaceURI(), getName() |
241 |
|
|
.getLocalPart())); |
242 |
|
|
|
243 |
|
|
amd.setWeight(getWeight()); |
244 |
|
|
amd.setFunctionX(getFunctionX()); |
245 |
|
|
amd.setFunctionA(getFunctionA()); |
246 |
|
|
|
247 |
|
|
for (final Object nodataValue : getNodataValues()) { |
248 |
|
|
amd.getNodataValues().add(nodataValue); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
return amd; |
252 |
alfonx |
420 |
} |
253 |
|
|
|
254 |
alfonx |
534 |
public Translation getDesc() { |
255 |
|
|
return desc; |
256 |
alfonx |
464 |
} |
257 |
|
|
|
258 |
alfonx |
534 |
public Double getFunctionA() { |
259 |
|
|
return functionA; |
260 |
alfonx |
523 |
} |
261 |
|
|
|
262 |
alfonx |
534 |
public Double getFunctionX() { |
263 |
|
|
return functionX; |
264 |
alfonx |
244 |
} |
265 |
|
|
|
266 |
alfonx |
534 |
/** |
267 |
|
|
* The local name. E.g. the name of the DBF column as a {@link String} |
268 |
|
|
*/ |
269 |
|
|
public String getLocalName() { |
270 |
|
|
return getName().getLocalPart(); |
271 |
alfonx |
244 |
} |
272 |
|
|
|
273 |
alfonx |
534 |
/** |
274 |
|
|
* The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code> |
275 |
|
|
*/ |
276 |
|
|
public Name getName() { |
277 |
|
|
return name; |
278 |
alfonx |
244 |
} |
279 |
|
|
|
280 |
alfonx |
658 |
public HashSet<Object> getNodataValues() { |
281 |
alfonx |
534 |
return nodataValues; |
282 |
alfonx |
244 |
} |
283 |
|
|
|
284 |
alfonx |
534 |
/** |
285 |
|
|
* @return a number between 0 (bad) and 1 (good) that is calculated from the |
286 |
|
|
* amount of translation available. If this attribute is not |
287 |
|
|
* {@link #visible}, it will return 1. |
288 |
|
|
*/ |
289 |
|
|
public double getQuality(final List<String> languages) { |
290 |
|
|
|
291 |
|
|
if (!isVisible()) |
292 |
|
|
return 1.; |
293 |
|
|
|
294 |
|
|
return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil |
295 |
|
|
.qmTranslation(languages, getDesc()) * 1.) / 3.; |
296 |
alfonx |
244 |
} |
297 |
|
|
|
298 |
alfonx |
534 |
public Translation getTitle() { |
299 |
|
|
return title; |
300 |
alfonx |
244 |
} |
301 |
|
|
|
302 |
|
|
public String getUnit() { |
303 |
|
|
return unit; |
304 |
|
|
} |
305 |
|
|
|
306 |
alfonx |
534 |
public int getWeight() { |
307 |
|
|
return weight; |
308 |
alfonx |
244 |
} |
309 |
alfonx |
420 |
|
310 |
alfonx |
534 |
/** |
311 |
|
|
* Will the end-user see this attribute? |
312 |
|
|
*/ |
313 |
|
|
public boolean isVisible() { |
314 |
|
|
return visible; |
315 |
|
|
} |
316 |
alfonx |
464 |
|
317 |
alfonx |
534 |
public void setDesc(final Translation desc) { |
318 |
|
|
this.desc = desc; |
319 |
alfonx |
420 |
} |
320 |
|
|
|
321 |
alfonx |
534 |
public void setFunctionA(final Double functionA) { |
322 |
|
|
this.functionA = functionA; |
323 |
alfonx |
420 |
} |
324 |
|
|
|
325 |
alfonx |
534 |
public void setFunctionX(final Double functionX) { |
326 |
|
|
this.functionX = functionX; |
327 |
alfonx |
420 |
} |
328 |
alfonx |
464 |
|
329 |
|
|
/** |
330 |
|
|
* The fully qualified Name of the attribute, e.g. org.bla.plo:blub |
331 |
|
|
*/ |
332 |
alfonx |
534 |
public void setLocalName(final String localName) { |
333 |
|
|
this.name = new NameImpl(localName); |
334 |
alfonx |
464 |
} |
335 |
|
|
|
336 |
|
|
/** |
337 |
alfonx |
534 |
* The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code> |
338 |
alfonx |
464 |
*/ |
339 |
alfonx |
534 |
public void setName(final Name name) { |
340 |
alfonx |
464 |
this.name = name; |
341 |
|
|
} |
342 |
|
|
|
343 |
alfonx |
658 |
public void setNodataValues(final HashSet<Object> nodataValues) { |
344 |
alfonx |
534 |
this.nodataValues = nodataValues; |
345 |
alfonx |
464 |
} |
346 |
alfonx |
518 |
|
347 |
alfonx |
534 |
public void setTitle(final Translation title) { |
348 |
|
|
this.title = title; |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
public void setUnit(final String unit) { |
352 |
|
|
this.unit = unit; |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
public void setVisible(final boolean visible) { |
356 |
|
|
this.visible = visible; |
357 |
|
|
} |
358 |
|
|
|
359 |
alfonx |
518 |
/** |
360 |
alfonx |
534 |
* Shall the end-user see this attribute? |
361 |
|
|
* @param visible |
362 |
alfonx |
518 |
*/ |
363 |
alfonx |
534 |
public void setVisible(final Boolean visible) { |
364 |
|
|
// // The THE_GEOM and shall never be visible! |
365 |
|
|
// if (name.getLocalPart().equalsIgnoreCase("the_geom")) |
366 |
|
|
// this.visible = false; |
367 |
|
|
// else |
368 |
|
|
// this.visible = visible; |
369 |
|
|
|
370 |
|
|
this.visible = visible; |
371 |
alfonx |
518 |
} |
372 |
|
|
|
373 |
alfonx |
534 |
public void setWeight(final int weight) { |
374 |
|
|
this.weight = weight; |
375 |
|
|
} |
376 |
|
|
|
377 |
alfonx |
518 |
/** |
378 |
alfonx |
534 |
* For nicer debugging |
379 |
alfonx |
518 |
*/ |
380 |
alfonx |
534 |
@Override |
381 |
|
|
public String toString() { |
382 |
|
|
StringBuffer sb = new StringBuffer(); |
383 |
|
|
if (name != null) sb.append(name.toString()+" "); |
384 |
|
|
sb.append("weight="+weight+" "); |
385 |
|
|
sb.append("title="+getTitle().toString()); |
386 |
|
|
return sb.toString(); |
387 |
alfonx |
518 |
} |
388 |
alfonx |
658 |
|
389 |
|
|
/** |
390 |
|
|
* Takes any value object and checks it againsts the NODATA values. If the |
391 |
|
|
* value equals a NODATA value, <code>null</code> is returned. Otherwise the |
392 |
|
|
* same object is returned. |
393 |
|
|
* |
394 |
|
|
* Note: This method is called often. |
395 |
|
|
*/ |
396 |
|
|
public Object fiterNodata(final Object value) { |
397 |
|
|
|
398 |
|
|
// contains.. hash |
399 |
|
|
for (final Object nodataValue : getNodataValues()) { |
400 |
|
|
if (nodataValue.equals(value)) |
401 |
|
|
return null; |
402 |
|
|
} |
403 |
|
|
return value; |
404 |
|
|
} |
405 |
alfonx |
244 |
} |