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

Diff of /trunk/src/skrueger/AttributeMetadataInterface.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/AttributeMetaData.java revision 2 by mojays, Tue Feb 24 22:43:52 2009 UTC branches/2.0-RC2/src/skrueger/AttributeMetadata.java revision 681 by alfonx, Tue Feb 9 22:08:26 2010 UTC
# Line 1  Line 1 
1  package skrueger;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import org.apache.log4j.Logger;   *
4     * This file is part of the SCHMITZM library - a collection of utility
5  import skrueger.geotools.StyledMapInterface;   * classes based on Java 1.6, focusing (not only) on Java Swing
6  import skrueger.i8n.Translation;   * and the Geotools library.
7     *
8  /**   * The SCHMITZM project is hosted at:
9   * This class holds meta information about an attribute/column. This   * http://wald.intevation.org/projects/schmitzm/
10   * information is used by {@link StyledMapInterface}.   *
11   *   * This program is free software; you can redistribute it and/or
12   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * modify it under the terms of the GNU Lesser General Public License
13   */   * as published by the Free Software Foundation; either version 3
14  public class AttributeMetaData {   * of the License, or (at your option) any later version.
15          static private final Logger LOGGER = Logger   *
16                          .getLogger(AttributeMetaData.class);   * This program is distributed in the hope that it will be useful,
17          protected Translation title = new Translation();   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18          protected Translation desc = new Translation();   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19          protected boolean visible = false;   * GNU General Public License for more details.
20          protected String unit = "";   *
21          protected int colIdx;   * 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           * Creates an {@link AttributeMetaData} object with the following information   * or try this link: http://www.gnu.org/licenses/lgpl.html
25           * @param colIdx The column index of this attribute in the underlying table/dbf/etc...   *
26           * @param visible Shall this attribute be displayed or hidden from the user?   * Contributors:
27           * @param title {@link Translation} for Name   *     Martin O. J. Schmitz - initial API and implementation
28           * @param desc {@link Translation} for an attribute description   *     Stefan A. Krüger - additional utility classes
29           * @param unit {@link String} of the unit that the information is in   ******************************************************************************/
30           */  package skrueger;
31          public AttributeMetaData(final int colIdx, final Boolean visible,  
32                          final Translation title, final Translation desc, final String unit) {  import java.util.HashSet;
33            import java.util.List;
34                  this.colIdx = colIdx;  import java.util.Set;
35                  this.title = title;  
36                  this.desc = desc;  import org.apache.log4j.Logger;
37                  if (colIdx == 0){  import org.geotools.feature.NameImpl;
38                          // The first attribut is THE_GEOM and shall never be visible!  import org.opengis.feature.type.AttributeDescriptor;
39                          this.visible = false;  import org.opengis.feature.type.Name;
40                  }else  
41                          this.visible = visible;  import skrueger.geotools.Copyable;
42                  this.unit = unit;  import skrueger.geotools.StyledLayerInterface;
43          }  import skrueger.i8n.I8NUtil;
44    import skrueger.i8n.Translation;
45          /**  
46           * Creates a {@link AttributeMetaData} with default (no) values.  /**
47           */   * This class holds meta information about an attribute/column. This information
48          public AttributeMetaData(final Integer col, final String defaultName) {   * is used by {@link StyledLayerInterface} and many others.<br/>
49                  this(col, true, new Translation(defaultName), new Translation(""), "");   *
50          }   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
51     */
52          public Boolean isVisible() {  public class AttributeMetadata implements Copyable<AttributeMetadata>,
53                  return visible;                  Comparable<AttributeMetadata> {
54          }  
55            static private final Logger LOGGER = Logger
56          public void setVisible(final Boolean visible) {                          .getLogger(AttributeMetadata.class);
57                  this.visible = visible;  
58          }          /** Translation of the attribute's description **/
59            protected Translation desc = new Translation();
60          /** @return the index of this attribute in the underlying table/dbf **/  
61          public int getColIdx() {          /**
62                  return colIdx;           * For numerical attributes the value can be transformed by VALUE*X+A when
63          }           * presented on screen. TODO not implemented yet
64             **/
65          public Translation getTitle() {          protected Double functionA = 0.;
66                  return title;  
67          }          /**
68             * For numerical attributes the value can be transformed by VALUE*X+A when
69          public void setTitle(final Translation title) {           * presented on screen. TODO not implemented yet
70                  this.title = title;           **/
71          }          protected Double functionX = 1.;
72    
73          public Translation getDesc() {          /** The Name of the attribute **/
74                  return desc;          private Name name;
75          }  
76            /**
77          public void setDesc(final Translation desc) {           * Allows to define general NODATA values for an attribute. e.g. -9999 can
78                  this.desc = desc;           * be set and will always be interpreted as NULL internally and will usually
79          }           * be ignored. This overcomes the problem, that
80             **/
81          public String getUnit() {          protected HashSet<Object> nodataValues = new HashSet<Object>();
82                  return unit;  
83          }          /** Translation of the attribute's title **/
84            protected Translation title = new Translation();
85          public void setUnit(final String unit) {  
86                  this.unit = unit;          /**
87          }           * The unit append to all visualizations of values of this attribute (is not
88  }           * null)
89             **/
90            protected String unit = "";
91    
92            /** Is the attribute visible to the user or ignored where possible **/
93            protected boolean visible = true;
94    
95            /**
96             * When listed, the attributes are listed according to their {@link #weight}
97             * (heavier => further down)
98             *
99             * @see #compareTo(AttributeMetadata)
100             **/
101            protected int weight = 0;
102    
103            /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/
104            private AttributeMetadata() {
105            }
106    
107            public AttributeMetadata(final AttributeDescriptor attDesc,
108                            final int weight, final List<String> langs) {
109                    this(attDesc.getName(), langs);
110                    setWeight(weight);
111            }
112    
113            public AttributeMetadata(final AttributeDescriptor attDesc,
114                            final List<String> langs) {
115                    this(attDesc.getName(), langs);
116            }
117    
118            /**
119             * 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             */
134            public AttributeMetadata(final Name name, final Boolean visible,
135                            final Translation title, final Translation desc, final String unit) {
136    
137                    this.setName(name);
138                    this.title = title;
139                    this.desc = desc;
140                    this.visible = visible;
141                    this.unit = unit;
142            }
143    
144            /**
145             * 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,
157                            final String unit) {
158                    this.setName(name);
159                    this.visible = visible;
160                    this.unit = unit;
161            }
162    
163            /**
164             * 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             * Creates a new visible {@link AttributeMetadata}
173             */
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             * 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             */
193            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            }
197    
198            /**
199             * Creates a new visible {@link AttributeMetadata} with default (no) values.
200             */
201            public AttributeMetadata(final String localName, final List<String> langs) {
202                    this(localName, true, new Translation(langs, localName),
203                                    new Translation(), "");
204            }
205    
206            /**
207             * Creates a new visible {@link AttributeMetadata}
208             */
209            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            }
214    
215            /**
216             * Orders the attributes according to their {@link #weight}. Heavier =>
217             * further down.
218             */
219            @Override
220            public int compareTo(final AttributeMetadata atm2) {
221                    return new Integer(weight).compareTo(atm2.getWeight());
222            }
223    
224            /**
225             * @see Copyable inferface
226             */
227            @Override
228            public AttributeMetadata copy() {
229                    return copyTo(new AttributeMetadata());
230            }
231    
232            /**
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                    amd.setNodataValues(getNodataValues());
249    
250                    return amd;
251            }
252    
253            // only to be used by copyTo()
254            private void setNodataValues(HashSet<Object> nodataValues2) {
255                    nodataValues = nodataValues2;
256            }
257    
258            public Translation getDesc() {
259                    return desc;
260            }
261    
262            public Double getFunctionA() {
263                    return functionA;
264            }
265    
266            public Double getFunctionX() {
267                    return functionX;
268            }
269    
270            /**
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            }
276    
277            /**
278             * The fully qualified {@link Name} of the attribute, e.g.
279             * <code>org.bla.plo:blub</code>
280             */
281            public Name getName() {
282                    return name;
283            }
284    
285            public HashSet<Object> getNodataValues() {
286                    return nodataValues;
287            }
288    
289            /**
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            }
302    
303            public Translation getTitle() {
304                    return title;
305            }
306    
307            public String getUnit() {
308                    return unit;
309            }
310    
311            public int getWeight() {
312                    return weight;
313            }
314    
315            /**
316             * Will the end-user see this attribute?
317             */
318            public boolean isVisible() {
319                    return visible;
320            }
321    
322            public void setDesc(final Translation desc) {
323                    this.desc = desc;
324            }
325    
326            public void setFunctionA(final Double functionA) {
327                    this.functionA = functionA;
328            }
329    
330            public void setFunctionX(final Double functionX) {
331                    this.functionX = functionX;
332            }
333    
334            /**
335             * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
336             */
337            public void setLocalName(final String localName) {
338                    this.name = new NameImpl(localName);
339            }
340    
341            /**
342             * The fully qualified {@link Name} of the attribute, e.g.
343             * <code>org.bla.plo:blub</code>
344             */
345            public void setName(final Name name) {
346                    this.name = name;
347            }
348    
349            public void addNodataValue(Object nodataValue) {
350                    this.nodataValues.add(nodataValue);
351            }
352    
353            public void removeNodataValue(Object nodataValue) {
354                    this.nodataValues.remove(nodataValue);
355            }
356    
357            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            /**
370             * Shall the end-user see this attribute?
371             *
372             * @param visible
373             */
374            public void setVisible(final Boolean visible) {
375                    // // 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    
381                    this.visible = visible;
382            }
383    
384            public void setWeight(final int weight) {
385                    this.weight = weight;
386            }
387    
388            /**
389             * For nicer debugging
390             */
391            @Override
392            public String toString() {
393                    StringBuffer sb = new StringBuffer();
394                    if (name != null)
395                            sb.append(name.toString() + " ");
396                    sb.append("weight=" + weight + " ");
397                    sb.append("title=" + getTitle().toString());
398                    return sb.toString();
399            }
400    
401            /**
402             * Takes any value object and checks it against the NODATA values. If the
403             * 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                    if (nodataValues.contains(value))
410                            return null;
411                    return value;
412            }
413    
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    }

Legend:
Removed from v.2  
changed lines
  Added in v.681

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26