/[schmitzm]/trunk/src/skrueger/AttributeMetadataImpl.java
ViewVC logotype

Annotation of /trunk/src/skrueger/AttributeMetadataImpl.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 681 - (hide annotations)
Tue Feb 9 22:08:26 2010 UTC (15 years ago) by alfonx
Original Path: branches/2.0-RC2/src/skrueger/AttributeMetadata.java
File size: 12073 byte(s)
* NODATA handling in the attribute table
* NODATA buttons in DesignAtlasChartDialog
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 alfonx 681 import java.util.Set;
35 alfonx 518
36 alfonx 244 import org.apache.log4j.Logger;
37 alfonx 464 import org.geotools.feature.NameImpl;
38     import org.opengis.feature.type.AttributeDescriptor;
39     import org.opengis.feature.type.Name;
40 alfonx 244
41 alfonx 420 import skrueger.geotools.Copyable;
42 mojays 325 import skrueger.geotools.StyledLayerInterface;
43 alfonx 518 import skrueger.i8n.I8NUtil;
44 alfonx 244 import skrueger.i8n.Translation;
45    
46     /**
47 alfonx 464 * This class holds meta information about an attribute/column. This information
48 alfonx 518 * is used by {@link StyledLayerInterface} and many others.<br/>
49 alfonx 464 *
50 alfonx 244 * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
51     */
52 alfonx 518 public class AttributeMetadata implements Copyable<AttributeMetadata>,
53     Comparable<AttributeMetadata> {
54 alfonx 681
55 alfonx 244 static private final Logger LOGGER = Logger
56 alfonx 463 .getLogger(AttributeMetadata.class);
57 alfonx 464
58 alfonx 518 /** Translation of the attribute's description **/
59 alfonx 244 protected Translation desc = new Translation();
60 alfonx 518
61 alfonx 534 /**
62     * For numerical attributes the value can be transformed by VALUE*X+A when
63     * presented on screen. TODO not implemented yet
64     **/
65     protected Double functionA = 0.;
66 alfonx 518
67     /**
68 alfonx 534 * For numerical attributes the value can be transformed by VALUE*X+A when
69     * presented on screen. TODO not implemented yet
70 alfonx 518 **/
71 alfonx 534 protected Double functionX = 1.;
72 alfonx 518
73     /** The Name of the attribute **/
74 alfonx 470 private Name name;
75 alfonx 464
76 alfonx 244 /**
77 alfonx 518 * Allows to define general NODATA values for an attribute. e.g. -9999 can
78     * be set and will always be interpreted as NULL internally and will usually
79 alfonx 681 * be ignored. This overcomes the problem, that
80 alfonx 518 **/
81 alfonx 678 protected HashSet<Object> nodataValues = new HashSet<Object>();
82 alfonx 518
83 alfonx 534 /** Translation of the attribute's title **/
84     protected Translation title = new Translation();
85    
86 alfonx 518 /**
87 alfonx 534 * The unit append to all visualizations of values of this attribute (is not
88     * null)
89 alfonx 518 **/
90 alfonx 534 protected String unit = "";
91 alfonx 518
92 alfonx 534 /** Is the attribute visible to the user or ignored where possible **/
93     protected boolean visible = true;
94    
95 alfonx 518 /**
96 alfonx 681 * When listed, the attributes are listed according to their {@link #weight}
97     * (heavier => further down)
98     *
99 alfonx 534 * @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 alfonx 681 public AttributeMetadata(final AttributeDescriptor attDesc,
108     final int weight, final List<String> langs) {
109 alfonx 534 this(attDesc.getName(), langs);
110     setWeight(weight);
111     }
112    
113 alfonx 681 public AttributeMetadata(final AttributeDescriptor attDesc,
114     final List<String> langs) {
115 alfonx 534 this(attDesc.getName(), langs);
116     }
117    
118 alfonx 518 /**
119 alfonx 464 * Creates an {@link AttributeMetadata} object with the following
120     * information
121     *
122     * @param colIdx
123     * The column index of this attribute in the underlying
124     * table/dbf/etc...
125     * @param visible
126     * Shall this attribute be displayed or hidden from the user?
127     * @param title
128     * {@link Translation} for Name
129     * @param desc
130     * {@link Translation} for an attribute description
131     * @param unit
132     * {@link String} of the unit that the information is in
133 alfonx 244 */
134 alfonx 464 public AttributeMetadata(final Name name, final Boolean visible,
135 alfonx 244 final Translation title, final Translation desc, final String unit) {
136 alfonx 464
137     this.setName(name);
138 alfonx 244 this.title = title;
139     this.desc = desc;
140 alfonx 546 this.visible = visible;
141 alfonx 244 this.unit = unit;
142     }
143    
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 alfonx 681 public AttributeMetadata(final Name name, final Boolean visible,
157     final String unit) {
158 alfonx 658 this.setName(name);
159     this.visible = visible;
160     this.unit = unit;
161     }
162    
163     /**
164 alfonx 534 * Creates a new visible {@link AttributeMetadata}
165     */
166     public AttributeMetadata(final Name name, final List<String> langs) {
167     this(name, true, new Translation(langs, name.getLocalPart()),
168     new Translation(), "");
169     }
170    
171     /**
172 alfonx 681 * Creates a new visible {@link AttributeMetadata}
173 alfonx 534 */
174     public AttributeMetadata(final Name name, final String defaultTitle,
175     final List<String> langs) {
176     this(name, true, new Translation(langs, defaultTitle),
177     new Translation(), "");
178     }
179    
180     /**
181 alfonx 464 * Creates an {@link AttributeMetadata} object with the following
182     * information
183     *
184     * @param visible
185     * Shall this attribute be displayed or hidden from the user?
186     * @param title
187     * {@link Translation} for Name
188     * @param desc
189     * {@link Translation} for an attribute description
190     * @param unit
191     * {@link String} of the unit that the information is in
192 alfonx 244 */
193 alfonx 464 public AttributeMetadata(final String localName, final Boolean visible,
194     final Translation title, final Translation desc, final String unit) {
195     this(new NameImpl(localName), true, title, desc, "");
196 alfonx 244 }
197    
198 alfonx 464 /**
199     * Creates a new visible {@link AttributeMetadata} with default (no) values.
200     */
201 alfonx 534 public AttributeMetadata(final String localName, final List<String> langs) {
202     this(localName, true, new Translation(langs, localName),
203     new Translation(), "");
204 alfonx 464 }
205    
206     /**
207 alfonx 681 * Creates a new visible {@link AttributeMetadata}
208 alfonx 464 */
209 alfonx 534 public AttributeMetadata(final String localName, final String defaultTitle,
210     final List<String> langs) {
211     this(localName, true, new Translation(langs, defaultTitle),
212     new Translation(), "");
213 alfonx 464 }
214    
215     /**
216 alfonx 534 * Orders the attributes according to their {@link #weight}. Heavier =>
217     * further down.
218 alfonx 464 */
219 alfonx 534 @Override
220     public int compareTo(final AttributeMetadata atm2) {
221     return new Integer(weight).compareTo(atm2.getWeight());
222 alfonx 464 }
223    
224     /**
225 alfonx 534 * @see Copyable inferface
226 alfonx 464 */
227 alfonx 534 @Override
228     public AttributeMetadata copy() {
229     return copyTo(new AttributeMetadata());
230 alfonx 464 }
231    
232 alfonx 534 /**
233     * @see Copyable inferface
234     */
235     @Override
236     public AttributeMetadata copyTo(final AttributeMetadata amd) {
237     getTitle().copyTo(amd.getTitle());
238     getDesc().copyTo(amd.getDesc());
239     amd.setUnit(getUnit());
240     amd.setVisible(isVisible());
241     amd.setName(new NameImpl(getName().getNamespaceURI(), getName()
242     .getLocalPart()));
243    
244     amd.setWeight(getWeight());
245     amd.setFunctionX(getFunctionX());
246     amd.setFunctionA(getFunctionA());
247    
248 alfonx 678 amd.setNodataValues(getNodataValues());
249 alfonx 534
250     return amd;
251 alfonx 420 }
252    
253 alfonx 678 // only to be used by copyTo()
254     private void setNodataValues(HashSet<Object> nodataValues2) {
255     nodataValues = nodataValues2;
256     }
257    
258 alfonx 534 public Translation getDesc() {
259     return desc;
260 alfonx 464 }
261    
262 alfonx 534 public Double getFunctionA() {
263     return functionA;
264 alfonx 523 }
265    
266 alfonx 534 public Double getFunctionX() {
267     return functionX;
268 alfonx 244 }
269    
270 alfonx 534 /**
271     * The local name. E.g. the name of the DBF column as a {@link String}
272     */
273     public String getLocalName() {
274     return getName().getLocalPart();
275 alfonx 244 }
276    
277 alfonx 534 /**
278 alfonx 681 * The fully qualified {@link Name} of the attribute, e.g.
279     * <code>org.bla.plo:blub</code>
280 alfonx 534 */
281     public Name getName() {
282     return name;
283 alfonx 244 }
284    
285 alfonx 658 public HashSet<Object> getNodataValues() {
286 alfonx 534 return nodataValues;
287 alfonx 244 }
288    
289 alfonx 534 /**
290     * @return a number between 0 (bad) and 1 (good) that is calculated from the
291     * amount of translation available. If this attribute is not
292     * {@link #visible}, it will return 1.
293     */
294     public double getQuality(final List<String> languages) {
295    
296     if (!isVisible())
297     return 1.;
298    
299     return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil
300     .qmTranslation(languages, getDesc()) * 1.) / 3.;
301 alfonx 244 }
302    
303 alfonx 534 public Translation getTitle() {
304     return title;
305 alfonx 244 }
306    
307     public String getUnit() {
308     return unit;
309     }
310    
311 alfonx 534 public int getWeight() {
312     return weight;
313 alfonx 244 }
314 alfonx 420
315 alfonx 534 /**
316     * Will the end-user see this attribute?
317     */
318     public boolean isVisible() {
319     return visible;
320     }
321 alfonx 464
322 alfonx 534 public void setDesc(final Translation desc) {
323     this.desc = desc;
324 alfonx 420 }
325    
326 alfonx 534 public void setFunctionA(final Double functionA) {
327     this.functionA = functionA;
328 alfonx 420 }
329    
330 alfonx 534 public void setFunctionX(final Double functionX) {
331     this.functionX = functionX;
332 alfonx 420 }
333 alfonx 464
334     /**
335     * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
336     */
337 alfonx 534 public void setLocalName(final String localName) {
338     this.name = new NameImpl(localName);
339 alfonx 464 }
340    
341     /**
342 alfonx 681 * The fully qualified {@link Name} of the attribute, e.g.
343     * <code>org.bla.plo:blub</code>
344 alfonx 464 */
345 alfonx 534 public void setName(final Name name) {
346 alfonx 464 this.name = name;
347     }
348    
349 alfonx 666 public void addNodataValue(Object nodataValue) {
350     this.nodataValues.add(nodataValue);
351 alfonx 464 }
352 alfonx 681
353 alfonx 666 public void removeNodataValue(Object nodataValue) {
354     this.nodataValues.remove(nodataValue);
355     }
356 alfonx 518
357 alfonx 534 public void setTitle(final Translation title) {
358     this.title = title;
359     }
360    
361     public void setUnit(final String unit) {
362     this.unit = unit;
363     }
364    
365     public void setVisible(final boolean visible) {
366     this.visible = visible;
367     }
368    
369 alfonx 518 /**
370 alfonx 534 * Shall the end-user see this attribute?
371 alfonx 681 *
372 alfonx 534 * @param visible
373 alfonx 518 */
374 alfonx 534 public void setVisible(final Boolean visible) {
375 alfonx 681 // // The THE_GEOM and shall never be visible!
376     // if (name.getLocalPart().equalsIgnoreCase("the_geom"))
377     // this.visible = false;
378     // else
379     // this.visible = visible;
380 alfonx 534
381     this.visible = visible;
382 alfonx 518 }
383    
384 alfonx 534 public void setWeight(final int weight) {
385     this.weight = weight;
386     }
387 alfonx 681
388 alfonx 518 /**
389 alfonx 534 * For nicer debugging
390 alfonx 518 */
391 alfonx 534 @Override
392     public String toString() {
393     StringBuffer sb = new StringBuffer();
394 alfonx 681 if (name != null)
395     sb.append(name.toString() + " ");
396     sb.append("weight=" + weight + " ");
397     sb.append("title=" + getTitle().toString());
398 alfonx 534 return sb.toString();
399 alfonx 518 }
400 alfonx 658
401     /**
402 alfonx 666 * Takes any value object and checks it against the NODATA values. If the
403 alfonx 658 * value equals a NODATA value, <code>null</code> is returned. Otherwise the
404     * same object is returned.
405     *
406     * Note: This method is called often.
407     */
408     public Object fiterNodata(final Object value) {
409 alfonx 681 if (nodataValues.contains(value))
410     return null;
411 alfonx 658 return value;
412     }
413 alfonx 681
414     /**
415     * @return a nicely formatted String containing all NODATA values. Strings
416     * are quoted fo that the empty String can be seen.
417     */
418     public String getNoDataValuesFormatted() {
419     return formatNoDataValues(getNodataValues());
420     }
421    
422     /**
423     * @return a nicely formatted String containing all NODATA values. Strings
424     * are quoted fo that the empty String can be seen.
425     */
426     public static String formatNoDataValues(Set<Object> list) {
427     String nicelyFormatted = "";
428     if (list != null) {
429     if (list.size() == 0)
430     nicelyFormatted = "";
431     else {
432     for (Object ndo : list) {
433     if (ndo instanceof String)
434     nicelyFormatted += "\"" + ndo + "\"";
435     else
436     nicelyFormatted += ndo.toString();
437    
438     nicelyFormatted += ",";
439     }
440     // Remove the extra comma
441     nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted
442     .length() - 1);
443     }
444     }
445     return nicelyFormatted;
446     }
447 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26