/[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

revision 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC revision 685 by alfonx, Wed Feb 10 15:04:02 2010 UTC
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger;  package skrueger;
31    
32    import java.util.HashSet;
33    import java.util.List;
34    import java.util.Set;
35    
36  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
37    import org.geotools.feature.NameImpl;
38    import org.opengis.feature.type.AttributeDescriptor;
39    import org.opengis.feature.type.Name;
40    
41    import skrueger.geotools.Copyable;
42  import skrueger.geotools.StyledLayerInterface;  import skrueger.geotools.StyledLayerInterface;
43    import skrueger.i8n.I8NUtil;
44  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
45    
46  /**  /**
47   * This class holds meta information about an attribute/column. This   * This class holds meta information about an attribute/column. This information
48   * information is used by {@link StyledLayerInterface}.   * is used by {@link StyledLayerInterface} and many others.<br/>
49   *   *
50   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
51   */   */
52  public class AttributeMetaData {  public class AttributeMetadata implements Copyable<AttributeMetadata>,
53                    Comparable<AttributeMetadata> {
54    
55          static private final Logger LOGGER = Logger          static private final Logger LOGGER = Logger
56                          .getLogger(AttributeMetaData.class);                          .getLogger(AttributeMetadata.class);
57          protected Translation title = new Translation();  
58            /** Translation of the attribute's description **/
59          protected Translation desc = new Translation();          protected Translation desc = new Translation();
60          protected boolean visible = false;  
61            /**
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    
67            /**
68             * For numerical attributes the value can be transformed by VALUE*X+A when
69             * presented on screen. TODO not implemented yet
70             **/
71            protected Double functionX = 1.;
72    
73            /** The Name of the attribute **/
74            private Name name;
75    
76            /**
77             * 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             * be ignored. This overcomes the problem, that
80             **/
81            protected HashSet<Object> nodataValues = new HashSet<Object>();
82    
83            /** Translation of the attribute's title **/
84            protected Translation title = new Translation();
85    
86            /**
87             * The unit append to all visualizations of values of this attribute (is not
88             * null)
89             **/
90          protected String unit = "";          protected String unit = "";
91          protected int colIdx;  
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 information           * Creates an {@link AttributeMetadata} object with the following
120           * @param colIdx The column index of this attribute in the underlying table/dbf/etc...           * information
121           * @param visible Shall this attribute be displayed or hidden from the user?           *
122           * @param title {@link Translation} for Name           * @param colIdx
123           * @param desc {@link Translation} for an attribute description           *            The column index of this attribute in the underlying
124           * @param unit {@link String} of the unit that the information is in           *            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 int colIdx, final Boolean visible,          public AttributeMetadata(final Name name, final Boolean visible,
135                          final Translation title, final Translation desc, final String unit) {                          final Translation title, final Translation desc, final String unit) {
136            
137                  this.colIdx = colIdx;                  this.setName(name);
138                  this.title = title;                  this.title = title;
139                  this.desc = desc;                  this.desc = desc;
140                  if (colIdx == 0){                  this.visible = visible;
                         // The first attribut is THE_GEOM and shall never be visible!  
                         this.visible = false;  
                 }else  
                         this.visible = visible;  
141                  this.unit = unit;                  this.unit = unit;
142          }          }
143    
144          /**          /**
145           * Creates a new visible {@link AttributeMetaData} with default (no) values.             * 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 Integer col, final String defaultName) {          public AttributeMetadata(final Name name, final Boolean visible,
157                  this(col, true, new Translation(defaultName), new Translation(), "");                          final String unit) {
158                    this.setName(name);
159                    this.visible = visible;
160                    this.unit = unit;
161          }          }
162    
163          public Boolean isVisible() {          /**
164                  return visible;           * 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          public void setVisible(final Boolean visible) {          /**
172                  this.visible = visible;           * 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          /** @return the index of this attribute in the underlying table/dbf **/          /**
181          public int getColIdx() {           * Creates an {@link AttributeMetadata} object with the following
182                  return colIdx;           * 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          public Translation getTitle() {          /**
199                  return title;           * 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          public void setTitle(final Translation title) {          /**
207                  this.title = title;           * 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() {          public Translation getDesc() {
259                  return desc;                  return desc;
260          }          }
261    
262          public void setDesc(final Translation desc) {          public Double getFunctionA() {
263                  this.desc = desc;                  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() {          public String getUnit() {
308                  return unit;                  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) {          public void setUnit(final String unit) {
362                  this.unit = unit;                  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.256  
changed lines
  Added in v.685

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26