/[schmitzm]/branches/2.0-RC2/src/skrueger/AttributeMetadata.java
ViewVC logotype

Annotation of /branches/2.0-RC2/src/skrueger/AttributeMetadata.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 678 - (hide annotations)
Fri Feb 5 15:21:33 2010 UTC (15 years ago) by alfonx
File size: 11103 byte(s)
BugFix: A new instance of HashSet<NODATA> has wrongly been created in ATMd.copyTo()
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 658 import java.util.HashSet;
33 alfonx 518 import java.util.List;
34    
35 alfonx 244 import org.apache.log4j.Logger;
36 alfonx 464 import org.geotools.feature.NameImpl;
37     import org.opengis.feature.type.AttributeDescriptor;
38     import org.opengis.feature.type.Name;
39 alfonx 244
40 alfonx 420 import skrueger.geotools.Copyable;
41 mojays 325 import skrueger.geotools.StyledLayerInterface;
42 alfonx 518 import skrueger.i8n.I8NUtil;
43 alfonx 244 import skrueger.i8n.Translation;
44    
45     /**
46 alfonx 464 * This class holds meta information about an attribute/column. This information
47 alfonx 518 * is used by {@link StyledLayerInterface} and many others.<br/>
48 alfonx 464 *
49 alfonx 244 * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
50     */
51 alfonx 518 public class AttributeMetadata implements Copyable<AttributeMetadata>,
52     Comparable<AttributeMetadata> {
53 alfonx 534
54 alfonx 244 static private final Logger LOGGER = Logger
55 alfonx 463 .getLogger(AttributeMetadata.class);
56 alfonx 464
57 alfonx 518 /** Translation of the attribute's description **/
58 alfonx 244 protected Translation desc = new Translation();
59 alfonx 518
60 alfonx 534 /**
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 alfonx 518
66     /**
67 alfonx 534 * For numerical attributes the value can be transformed by VALUE*X+A when
68     * presented on screen. TODO not implemented yet
69 alfonx 518 **/
70 alfonx 534 protected Double functionX = 1.;
71 alfonx 518
72     /** The Name of the attribute **/
73 alfonx 470 private Name name;
74 alfonx 464
75 alfonx 244 /**
76 alfonx 518 * 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 alfonx 658 * be ignored. This overcomes the problem, that
79 alfonx 518 **/
80 alfonx 678 protected HashSet<Object> nodataValues = new HashSet<Object>();
81 alfonx 518
82 alfonx 534 /** Translation of the attribute's title **/
83     protected Translation title = new Translation();
84    
85 alfonx 518 /**
86 alfonx 534 * The unit append to all visualizations of values of this attribute (is not
87     * null)
88 alfonx 518 **/
89 alfonx 534 protected String unit = "";
90 alfonx 518
91 alfonx 534 /** Is the attribute visible to the user or ignored where possible **/
92     protected boolean visible = true;
93    
94 alfonx 518 /**
95 alfonx 534 * When listed, the attributes are listed according to their {@link #weight} (heavier
96     * => further down)
97     * @see #compareTo(AttributeMetadata)
98 alfonx 518 **/
99 alfonx 534 protected int weight = 0;
100 alfonx 518
101 alfonx 534 /** 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 alfonx 518 /**
116 alfonx 464 * 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 alfonx 244 */
131 alfonx 464 public AttributeMetadata(final Name name, final Boolean visible,
132 alfonx 244 final Translation title, final Translation desc, final String unit) {
133 alfonx 464
134     this.setName(name);
135 alfonx 244 this.title = title;
136     this.desc = desc;
137 alfonx 546 this.visible = visible;
138 alfonx 244 this.unit = unit;
139     }
140 alfonx 658
141 alfonx 244
142     /**
143 alfonx 658 * 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 Name name, final Boolean visible, final String unit) {
155     this.setName(name);
156     this.visible = visible;
157     this.unit = unit;
158     }
159    
160     /**
161 alfonx 534 * 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     /**
169     * 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     /**
178 alfonx 464 * Creates an {@link AttributeMetadata} object with the following
179     * 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 alfonx 244 */
190 alfonx 464 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 alfonx 244 }
194    
195 alfonx 464 /**
196     * Creates a new visible {@link AttributeMetadata} with default (no) values.
197     */
198 alfonx 534 public AttributeMetadata(final String localName, final List<String> langs) {
199     this(localName, true, new Translation(langs, localName),
200     new Translation(), "");
201 alfonx 464 }
202    
203     /**
204 alfonx 534 * Creates a new visible {@link AttributeMetadata}
205 alfonx 464 */
206 alfonx 534 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 alfonx 464 }
211    
212     /**
213 alfonx 534 * Orders the attributes according to their {@link #weight}. Heavier =>
214     * further down.
215 alfonx 464 */
216 alfonx 534 @Override
217     public int compareTo(final AttributeMetadata atm2) {
218     return new Integer(weight).compareTo(atm2.getWeight());
219 alfonx 464 }
220    
221     /**
222 alfonx 534 * @see Copyable inferface
223 alfonx 464 */
224 alfonx 534 @Override
225     public AttributeMetadata copy() {
226     return copyTo(new AttributeMetadata());
227 alfonx 464 }
228    
229 alfonx 534 /**
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 alfonx 678 amd.setNodataValues(getNodataValues());
246 alfonx 534
247     return amd;
248 alfonx 420 }
249    
250 alfonx 678 // only to be used by copyTo()
251     private void setNodataValues(HashSet<Object> nodataValues2) {
252     nodataValues = nodataValues2;
253     }
254    
255 alfonx 534 public Translation getDesc() {
256     return desc;
257 alfonx 464 }
258    
259 alfonx 534 public Double getFunctionA() {
260     return functionA;
261 alfonx 523 }
262    
263 alfonx 534 public Double getFunctionX() {
264     return functionX;
265 alfonx 244 }
266    
267 alfonx 534 /**
268     * The local name. E.g. the name of the DBF column as a {@link String}
269     */
270     public String getLocalName() {
271     return getName().getLocalPart();
272 alfonx 244 }
273    
274 alfonx 534 /**
275     * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
276     */
277     public Name getName() {
278     return name;
279 alfonx 244 }
280    
281 alfonx 658 public HashSet<Object> getNodataValues() {
282 alfonx 534 return nodataValues;
283 alfonx 244 }
284    
285 alfonx 534 /**
286     * @return a number between 0 (bad) and 1 (good) that is calculated from the
287     * amount of translation available. If this attribute is not
288     * {@link #visible}, it will return 1.
289     */
290     public double getQuality(final List<String> languages) {
291    
292     if (!isVisible())
293     return 1.;
294    
295     return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil
296     .qmTranslation(languages, getDesc()) * 1.) / 3.;
297 alfonx 244 }
298    
299 alfonx 534 public Translation getTitle() {
300     return title;
301 alfonx 244 }
302    
303     public String getUnit() {
304     return unit;
305     }
306    
307 alfonx 534 public int getWeight() {
308     return weight;
309 alfonx 244 }
310 alfonx 420
311 alfonx 534 /**
312     * Will the end-user see this attribute?
313     */
314     public boolean isVisible() {
315     return visible;
316     }
317 alfonx 464
318 alfonx 534 public void setDesc(final Translation desc) {
319     this.desc = desc;
320 alfonx 420 }
321    
322 alfonx 534 public void setFunctionA(final Double functionA) {
323     this.functionA = functionA;
324 alfonx 420 }
325    
326 alfonx 534 public void setFunctionX(final Double functionX) {
327     this.functionX = functionX;
328 alfonx 420 }
329 alfonx 464
330     /**
331     * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
332     */
333 alfonx 534 public void setLocalName(final String localName) {
334     this.name = new NameImpl(localName);
335 alfonx 464 }
336    
337     /**
338 alfonx 534 * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
339 alfonx 464 */
340 alfonx 534 public void setName(final Name name) {
341 alfonx 464 this.name = name;
342     }
343    
344 alfonx 666 public void addNodataValue(Object nodataValue) {
345     this.nodataValues.add(nodataValue);
346 alfonx 464 }
347 alfonx 666
348     public void removeNodataValue(Object nodataValue) {
349     this.nodataValues.remove(nodataValue);
350     }
351 alfonx 518
352 alfonx 534 public void setTitle(final Translation title) {
353     this.title = title;
354     }
355    
356     public void setUnit(final String unit) {
357     this.unit = unit;
358     }
359    
360     public void setVisible(final boolean visible) {
361     this.visible = visible;
362     }
363    
364 alfonx 518 /**
365 alfonx 534 * Shall the end-user see this attribute?
366     * @param visible
367 alfonx 518 */
368 alfonx 534 public void setVisible(final Boolean visible) {
369     // // The THE_GEOM and shall never be visible!
370     // if (name.getLocalPart().equalsIgnoreCase("the_geom"))
371     // this.visible = false;
372     // else
373     // this.visible = visible;
374    
375     this.visible = visible;
376 alfonx 518 }
377    
378 alfonx 534 public void setWeight(final int weight) {
379     this.weight = weight;
380     }
381    
382 alfonx 518 /**
383 alfonx 534 * For nicer debugging
384 alfonx 518 */
385 alfonx 534 @Override
386     public String toString() {
387     StringBuffer sb = new StringBuffer();
388     if (name != null) sb.append(name.toString()+" ");
389     sb.append("weight="+weight+" ");
390     sb.append("title="+getTitle().toString());
391     return sb.toString();
392 alfonx 518 }
393 alfonx 658
394     /**
395 alfonx 666 * Takes any value object and checks it against the NODATA values. If the
396 alfonx 658 * value equals a NODATA value, <code>null</code> is returned. Otherwise the
397     * same object is returned.
398     *
399     * Note: This method is called often.
400     */
401     public Object fiterNodata(final Object value) {
402 alfonx 666 if (nodataValues.contains(value)) return null;
403 alfonx 658 return value;
404     }
405 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26