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