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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 546 - (hide annotations)
Sun Nov 22 18:56:40 2009 UTC (15 years, 3 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java
File size: 10025 byte(s)
* Mouseclicks on the HeapBar will now trigger a GarbageCollectin
* AttributeMetadata constructor didn't correctly handle the visbile state
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     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 534 * be ignored. TODO not implemented yet
79 alfonx 518 **/
80     protected List<Object> nodataValues = new ArrayList<Object>();
81    
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    
141     /**
142 alfonx 534 * Creates a new visible {@link AttributeMetadata}
143     */
144     public AttributeMetadata(final Name name, final List<String> langs) {
145     this(name, true, new Translation(langs, name.getLocalPart()),
146     new Translation(), "");
147     }
148    
149     /**
150     * Creates a new visible {@link AttributeMetadata}
151     */
152     public AttributeMetadata(final Name name, final String defaultTitle,
153     final List<String> langs) {
154     this(name, true, new Translation(langs, defaultTitle),
155     new Translation(), "");
156     }
157    
158     /**
159 alfonx 464 * Creates an {@link AttributeMetadata} object with the following
160     * information
161     *
162     * @param visible
163     * Shall this attribute be displayed or hidden from the user?
164     * @param title
165     * {@link Translation} for Name
166     * @param desc
167     * {@link Translation} for an attribute description
168     * @param unit
169     * {@link String} of the unit that the information is in
170 alfonx 244 */
171 alfonx 464 public AttributeMetadata(final String localName, final Boolean visible,
172     final Translation title, final Translation desc, final String unit) {
173     this(new NameImpl(localName), true, title, desc, "");
174 alfonx 244 }
175    
176 alfonx 464 /**
177     * Creates a new visible {@link AttributeMetadata} with default (no) values.
178     */
179 alfonx 534 public AttributeMetadata(final String localName, final List<String> langs) {
180     this(localName, true, new Translation(langs, localName),
181     new Translation(), "");
182 alfonx 464 }
183    
184     /**
185 alfonx 534 * Creates a new visible {@link AttributeMetadata}
186 alfonx 464 */
187 alfonx 534 public AttributeMetadata(final String localName, final String defaultTitle,
188     final List<String> langs) {
189     this(localName, true, new Translation(langs, defaultTitle),
190     new Translation(), "");
191 alfonx 464 }
192    
193     /**
194 alfonx 534 * Orders the attributes according to their {@link #weight}. Heavier =>
195     * further down.
196 alfonx 464 */
197 alfonx 534 @Override
198     public int compareTo(final AttributeMetadata atm2) {
199     return new Integer(weight).compareTo(atm2.getWeight());
200 alfonx 464 }
201    
202     /**
203 alfonx 534 * @see Copyable inferface
204 alfonx 464 */
205 alfonx 534 @Override
206     public AttributeMetadata copy() {
207     return copyTo(new AttributeMetadata());
208 alfonx 464 }
209    
210 alfonx 534 /**
211     * @see Copyable inferface
212     */
213     @Override
214     public AttributeMetadata copyTo(final AttributeMetadata amd) {
215     getTitle().copyTo(amd.getTitle());
216     getDesc().copyTo(amd.getDesc());
217     amd.setUnit(getUnit());
218     amd.setVisible(isVisible());
219     amd.setName(new NameImpl(getName().getNamespaceURI(), getName()
220     .getLocalPart()));
221    
222     amd.setWeight(getWeight());
223     amd.setFunctionX(getFunctionX());
224     amd.setFunctionA(getFunctionA());
225    
226     for (final Object nodataValue : getNodataValues()) {
227     amd.getNodataValues().add(nodataValue);
228     }
229    
230     return amd;
231 alfonx 420 }
232    
233 alfonx 534 public Translation getDesc() {
234     return desc;
235 alfonx 464 }
236    
237 alfonx 534 public Double getFunctionA() {
238     return functionA;
239 alfonx 523 }
240    
241 alfonx 534 public Double getFunctionX() {
242     return functionX;
243 alfonx 244 }
244    
245 alfonx 534 /**
246     * The local name. E.g. the name of the DBF column as a {@link String}
247     */
248     public String getLocalName() {
249     return getName().getLocalPart();
250 alfonx 244 }
251    
252 alfonx 534 /**
253     * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
254     */
255     public Name getName() {
256     return name;
257 alfonx 244 }
258    
259 alfonx 534 public List<Object> getNodataValues() {
260     return nodataValues;
261 alfonx 244 }
262    
263 alfonx 534 /**
264     * @return a number between 0 (bad) and 1 (good) that is calculated from the
265     * amount of translation available. If this attribute is not
266     * {@link #visible}, it will return 1.
267     */
268     public double getQuality(final List<String> languages) {
269    
270     if (!isVisible())
271     return 1.;
272    
273     return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil
274     .qmTranslation(languages, getDesc()) * 1.) / 3.;
275 alfonx 244 }
276    
277 alfonx 534 public Translation getTitle() {
278     return title;
279 alfonx 244 }
280    
281     public String getUnit() {
282     return unit;
283     }
284    
285 alfonx 534 public int getWeight() {
286     return weight;
287 alfonx 244 }
288 alfonx 420
289 alfonx 534 /**
290     * Will the end-user see this attribute?
291     */
292     public boolean isVisible() {
293     return visible;
294     }
295 alfonx 464
296 alfonx 534 public void setDesc(final Translation desc) {
297     this.desc = desc;
298 alfonx 420 }
299    
300 alfonx 534 public void setFunctionA(final Double functionA) {
301     this.functionA = functionA;
302 alfonx 420 }
303    
304 alfonx 534 public void setFunctionX(final Double functionX) {
305     this.functionX = functionX;
306 alfonx 420 }
307 alfonx 464
308     /**
309     * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
310     */
311 alfonx 534 public void setLocalName(final String localName) {
312     this.name = new NameImpl(localName);
313 alfonx 464 }
314    
315     /**
316 alfonx 534 * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
317 alfonx 464 */
318 alfonx 534 public void setName(final Name name) {
319 alfonx 464 this.name = name;
320     }
321    
322 alfonx 534 public void setNodataValues(final List<Object> nodataValues) {
323     this.nodataValues = nodataValues;
324 alfonx 464 }
325 alfonx 518
326 alfonx 534 public void setTitle(final Translation title) {
327     this.title = title;
328     }
329    
330     public void setUnit(final String unit) {
331     this.unit = unit;
332     }
333    
334     public void setVisible(final boolean visible) {
335     this.visible = visible;
336     }
337    
338 alfonx 518 /**
339 alfonx 534 * Shall the end-user see this attribute?
340     * @param visible
341 alfonx 518 */
342 alfonx 534 public void setVisible(final Boolean visible) {
343     // // The THE_GEOM and shall never be visible!
344     // if (name.getLocalPart().equalsIgnoreCase("the_geom"))
345     // this.visible = false;
346     // else
347     // this.visible = visible;
348    
349     this.visible = visible;
350 alfonx 518 }
351    
352 alfonx 534 public void setWeight(final int weight) {
353     this.weight = weight;
354     }
355    
356 alfonx 518 /**
357 alfonx 534 * For nicer debugging
358 alfonx 518 */
359 alfonx 534 @Override
360     public String toString() {
361     StringBuffer sb = new StringBuffer();
362     if (name != null) sb.append(name.toString()+" ");
363     sb.append("weight="+weight+" ");
364     sb.append("title="+getTitle().toString());
365     return sb.toString();
366 alfonx 518 }
367 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26