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

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

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

branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java revision 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC branches/2.0-RC2/src/skrueger/AttributeMetadata.java revision 678 by alfonx, Fri Feb 5 15:21:33 2010 UTC
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger;  package skrueger;
31    
32    import java.util.HashSet;
33    import java.util.List;
34    
35  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
36  import org.geotools.feature.NameImpl;  import org.geotools.feature.NameImpl;
37  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
# Line 36  import org.opengis.feature.type.Name; Line 39  import org.opengis.feature.type.Name;
39    
40  import skrueger.geotools.Copyable;  import skrueger.geotools.Copyable;
41  import skrueger.geotools.StyledLayerInterface;  import skrueger.geotools.StyledLayerInterface;
42    import skrueger.i8n.I8NUtil;
43  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
44    
45  /**  /**
46   * This class holds meta information about an attribute/column. This information   * This class holds meta information about an attribute/column. This information
47   * is used by {@link StyledLayerInterface}.   * is used by {@link StyledLayerInterface} and many others.<br/>
48   *   *
49   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
50   */   */
51  public class AttributeMetadata implements Copyable<AttributeMetadata> {  public class AttributeMetadata implements Copyable<AttributeMetadata>,
52                    Comparable<AttributeMetadata> {
53            
54          static private final Logger LOGGER = Logger          static private final Logger LOGGER = Logger
55                          .getLogger(AttributeMetadata.class);                          .getLogger(AttributeMetadata.class);
56    
57          protected Translation title = new Translation();          /** Translation of the attribute's description **/
58          protected Translation desc = new Translation();          protected Translation desc = new Translation();
59          protected boolean visible = true;  
60            /**
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    
66            /**
67             * For numerical attributes the value can be transformed by VALUE*X+A when
68             * presented on screen. TODO not implemented yet
69             **/
70            protected Double functionX = 1.;
71    
72            /** The Name of the attribute **/
73            private Name name;
74    
75            /**
76             * 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             * be ignored. This overcomes the problem, that
79             **/
80            protected HashSet<Object> nodataValues = new HashSet<Object>();
81    
82            /** Translation of the attribute's title **/
83            protected Translation title = new Translation();
84    
85            /**
86             * The unit append to all visualizations of values of this attribute (is not
87             * null)
88             **/
89          protected String unit = "";          protected String unit = "";
90          protected int colIdx;  
91          private org.opengis.feature.type.Name name;          /** Is the attribute visible to the user or ignored where possible **/
92            protected boolean visible = true;
93    
94            /**
95             * When listed, the attributes are listed according to their {@link #weight} (heavier
96             * => further down)
97             * @see #compareTo(AttributeMetadata)
98             **/
99            protected int weight = 0;
100    
101            /** 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          /**          /**
116           * Creates an {@link AttributeMetadata} object with the following           * Creates an {@link AttributeMetadata} object with the following
# Line 77  public class AttributeMetadata implement Line 134  public class AttributeMetadata implement
134                  this.setName(name);                  this.setName(name);
135                  this.title = title;                  this.title = title;
136                  this.desc = desc;                  this.desc = desc;
137                                    this.visible = visible;
138                  // The THE_GEOM and shall never be visible!                  this.unit = unit;
139                  if (name.getLocalPart().equalsIgnoreCase("the_geom")) this.visible = false;          }
140                            
141    
142            /**
143             * 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;                  this.unit = unit;
158          }          }
159    
160          /**          /**
161             * 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           * Creates an {@link AttributeMetadata} object with the following           * Creates an {@link AttributeMetadata} object with the following
179           * information           * information
180           *           *
# Line 105  public class AttributeMetadata implement Line 195  public class AttributeMetadata implement
195          /**          /**
196           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Creates a new visible {@link AttributeMetadata} with default (no) values.
197           */           */
198          public AttributeMetadata(final String localName, final String defaultTitle) {          public AttributeMetadata(final String localName, final List<String> langs) {
199                  this(localName, true, new Translation(defaultTitle), new Translation(),                  this(localName, true, new Translation(langs, localName),
200                                  "");                                  new Translation(), "");
201          }          }
202    
203          /**          /**
204           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Creates a new visible {@link AttributeMetadata}
205           */           */
206          public AttributeMetadata(final Name name, final String defaultTitle) {          public AttributeMetadata(final String localName, final String defaultTitle,
207                  this(name, true, new Translation(defaultTitle), new Translation(), "");                          final List<String> langs) {
208                    this(localName, true, new Translation(langs, defaultTitle),
209                                    new Translation(), "");
210          }          }
211    
212          /**          /**
213           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Orders the attributes according to their {@link #weight}. Heavier =>
214             * further down.
215           */           */
216          public AttributeMetadata(final Name name) {          @Override
217                  this(name, true, new Translation(name.getLocalPart()),          public int compareTo(final AttributeMetadata atm2) {
218                                  new Translation(), "");                  return new Integer(weight).compareTo(atm2.getWeight());
219          }          }
220    
221          /**          /**
222           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * @see Copyable inferface
223           */           */
224          public AttributeMetadata(final String localName) {          @Override
225                  this(localName, true, new Translation(localName), new Translation(), "");          public AttributeMetadata copy() {
226                    return copyTo(new AttributeMetadata());
227          }          }
228    
229          /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/          /**
230          private AttributeMetadata() {           * @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                    amd.setNodataValues(getNodataValues());
246    
247                    return amd;
248          }          }
249    
250          public AttributeMetadata(AttributeDescriptor attDesc) {          // only to be used by copyTo()
251                  this(attDesc.getName());          private void setNodataValues(HashSet<Object> nodataValues2) {
252                    nodataValues = nodataValues2;
253          }          }
254    
255          public boolean isVisible() {          public Translation getDesc() {
256                  return visible;                  return desc;
257          }          }
258    
259          public void setVisible(final Boolean visible) {          public Double getFunctionA() {
260                  this.visible = visible;                  return functionA;
261          }          }
 //  
 //      /** @return the index of this attribute in the underlying table/dbf **/  
 //      public int getColIdx() {  
 //              return colIdx;  
 //      }  
262    
263          public Translation getTitle() {          public Double getFunctionX() {
264                  return title;                  return functionX;
265          }          }
266    
267          public void setTitle(final Translation title) {          /**
268                  this.title = title;           * 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          }          }
273    
274          public Translation getDesc() {          /**
275                  return desc;           * 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          }          }
280    
281          public void setDesc(final Translation desc) {          public HashSet<Object> getNodataValues() {
282                  this.desc = desc;                  return nodataValues;
283            }
284    
285            /**
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            }
298    
299            public Translation getTitle() {
300                    return title;
301          }          }
302    
303          public String getUnit() {          public String getUnit() {
304                  return unit;                  return unit;
305          }          }
306    
307          public void setUnit(final String unit) {          public int getWeight() {
308                  this.unit = unit;                  return weight;
309          }          }
310    
311          @Override          /**
312          public AttributeMetadata copyTo(AttributeMetadata amd) {           * Will the end-user see this attribute?
313                  getTitle().copyTo(amd.getTitle());           */
314                  getDesc().copyTo(amd.getDesc());          public boolean isVisible() {
315                  amd.setUnit(getUnit());                  return visible;
316                  amd.setVisible(isVisible());          }
                 amd.setName(new NameImpl(getName().getNamespaceURI(), getName().getLocalPart()));  
317    
318                  return amd;          public void setDesc(final Translation desc) {
319                    this.desc = desc;
320          }          }
321    
322          @Override          public void setFunctionA(final Double functionA) {
323          public AttributeMetadata copy() {                  this.functionA = functionA;
                 return copyTo( new AttributeMetadata());  
324          }          }
325    
326          /**          public void setFunctionX(final Double functionX) {
327           * The local Name. E.g. the name of the DBF column as a String                  this.functionX = functionX;
          */  
         public String getLocalName() {  
                 return getName().getLocalPart();  
328          }          }
329    
330          /**          /**
331           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
332           */           */
333          public Name getName() {          public void setLocalName(final String localName) {
334                  return name;                  this.name = new NameImpl(localName);
335          }          }
336    
337          /**          /**
338           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
339           */           */
340          public void setName(org.opengis.feature.type.Name name) {          public void setName(final Name name) {
341                  this.name = name;                  this.name = name;
342          }          }
343    
344            public void addNodataValue(Object nodataValue) {
345                    this.nodataValues.add(nodataValue);
346            }
347            
348            public void removeNodataValue(Object nodataValue) {
349                    this.nodataValues.remove(nodataValue);
350            }
351    
352            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          /**          /**
365           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * Shall the end-user see this attribute?
366             * @param visible
367           */           */
368          public void setLocalName(String localName) {          public void setVisible(final Boolean visible) {
369                  this.name = new NameImpl(localName);  //              // 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            }
377    
378            public void setWeight(final int weight) {
379                    this.weight = weight;
380            }
381            
382            /**
383             * For nicer debugging
384             */
385            @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            }
393    
394            /**
395             * Takes any value object and checks it against the NODATA values. If the
396             * 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                    if (nodataValues.contains(value)) return null;
403                    return value;
404          }          }
405  }  }

Legend:
Removed from v.464  
changed lines
  Added in v.678

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26