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

branches/2.0-RC2/src/skrueger/AttributeMetadata.java revision 678 by alfonx, Fri Feb 5 15:21:33 2010 UTC trunk/src/skrueger/AttributeMetadata.java revision 770 by alfonx, Sun Mar 21 11:36:11 2010 UTC
# Line 1  Line 1 
 /*******************************************************************************  
  * Copyright (c) 2009 Martin O. J. Schmitz.  
  *  
  * This file is part of the SCHMITZM library - a collection of utility  
  * classes based on Java 1.6, focusing (not only) on Java Swing  
  * and the Geotools library.  
  *  
  * The SCHMITZM project is hosted at:  
  * http://wald.intevation.org/projects/schmitzm/  
  *  
  * This program is free software; you can redistribute it and/or  
  * modify it under the terms of the GNU Lesser General Public License  
  * as published by the Free Software Foundation; either version 3  
  * of the License, or (at your option) any later version.  
  *  
  * This program is distributed in the hope that it will be useful,  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
  * GNU General Public License for more details.  
  *  
  * You should have received a copy of the GNU Lesser General Public License (license.txt)  
  * along with this program; if not, write to the Free Software  
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.  
  * or try this link: http://www.gnu.org/licenses/lgpl.html  
  *  
  * Contributors:  
  *     Martin O. J. Schmitz - initial API and implementation  
  *     Stefan A. Krüger - additional utility classes  
  ******************************************************************************/  
1  package skrueger;  package skrueger;
2    
3  import java.util.HashSet;  import java.util.HashSet;
 import java.util.List;  
4    
 import org.apache.log4j.Logger;  
5  import org.geotools.feature.NameImpl;  import org.geotools.feature.NameImpl;
 import org.opengis.feature.type.AttributeDescriptor;  
6  import org.opengis.feature.type.Name;  import org.opengis.feature.type.Name;
7    
8  import skrueger.geotools.Copyable;  import skrueger.geotools.Copyable;
 import skrueger.geotools.StyledLayerInterface;  
 import skrueger.i8n.I8NUtil;  
9  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
10    
11  /**  public interface AttributeMetadata extends Copyable<AttributeMetadata>,
  * This class holds meta information about an attribute/column. This information  
  * is used by {@link StyledLayerInterface} and many others.<br/>  
  *  
  * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>  
  */  
 public class AttributeMetadata implements Copyable<AttributeMetadata>,  
12                  Comparable<AttributeMetadata> {                  Comparable<AttributeMetadata> {
           
         static private final Logger LOGGER = Logger  
                         .getLogger(AttributeMetadata.class);  
   
         /** Translation of the attribute's description **/  
         protected Translation desc = new Translation();  
   
         /**  
          * For numerical attributes the value can be transformed by VALUE*X+A when  
          * presented on screen. TODO not implemented yet  
          **/  
         protected Double functionA = 0.;  
   
         /**  
          * For numerical attributes the value can be transformed by VALUE*X+A when  
          * presented on screen. TODO not implemented yet  
          **/  
         protected Double functionX = 1.;  
   
         /** The Name of the attribute **/  
         private Name name;  
13    
14          /**          /**
15           * Allows to define general NODATA values for an attribute. e.g. -9999 can           * @return a translatable title for this attribute..
          * be set and will always be interpreted as NULL internally and will usually  
          * be ignored. This overcomes the problem, that  
          **/  
         protected HashSet<Object> nodataValues = new HashSet<Object>();  
   
         /** Translation of the attribute's title **/  
         protected Translation title = new Translation();  
   
         /**  
          * The unit append to all visualizations of values of this attribute (is not  
          * null)  
          **/  
         protected String unit = "";  
   
         /** Is the attribute visible to the user or ignored where possible **/  
         protected boolean visible = true;  
   
         /**  
          * When listed, the attributes are listed according to their {@link #weight} (heavier  
          * => further down)  
          * @see #compareTo(AttributeMetadata)  
          **/  
         protected int weight = 0;  
   
         /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/  
         private AttributeMetadata() {  
         }  
   
         public AttributeMetadata(final AttributeDescriptor attDesc, final int weight,  
                         final List<String> langs) {  
                 this(attDesc.getName(), langs);  
                 setWeight(weight);  
         }  
   
         public AttributeMetadata(final AttributeDescriptor attDesc, final List<String> langs) {  
                 this(attDesc.getName(), langs);  
         }  
   
         /**  
          * Creates an {@link AttributeMetadata} object with the following  
          * information  
          *  
          * @param colIdx  
          *            The column index of this attribute in the underlying  
          *            table/dbf/etc...  
          * @param visible  
          *            Shall this attribute be displayed or hidden from the user?  
          * @param title  
          *            {@link Translation} for Name  
          * @param desc  
          *            {@link Translation} for an attribute description  
          * @param unit  
          *            {@link String} of the unit that the information is in  
          */  
         public AttributeMetadata(final Name name, final Boolean visible,  
                         final Translation title, final Translation desc, final String unit) {  
   
                 this.setName(name);  
                 this.title = title;  
                 this.desc = desc;  
                 this.visible = visible;  
                 this.unit = unit;  
         }  
           
   
         /**  
          * Creates an {@link AttributeMetadata} object with the following  
          * information  
          *  
          * @param colIdx  
          *            The column index of this attribute in the underlying  
          *            table/dbf/etc...  
          * @param visible  
          *            Shall this attribute be displayed or hidden from the user?  
          * @param unit  
          *            {@link String} of the unit that the information is in  
16           */           */
17          public AttributeMetadata(final Name name, final Boolean visible, final String unit) {          public Translation getTitle();
                 this.setName(name);  
                 this.visible = visible;  
                 this.unit = unit;  
         }  
18    
19          /**          /**
20           * Creates a new visible {@link AttributeMetadata}           * Set a translatable title for this attribute..
21           */           */
22          public AttributeMetadata(final Name name, final List<String> langs) {          public void setTitle(Translation title);
                 this(name, true, new Translation(langs, name.getLocalPart()),  
                                 new Translation(), "");  
         }  
23    
24          /**          /**
25           * Creates a new visible {@link AttributeMetadata}           * @return a translatable description for this attribute
26           */           */
27          public AttributeMetadata(final Name name, final String defaultTitle,          public Translation getDesc();
                         final List<String> langs) {  
                 this(name, true, new Translation(langs, defaultTitle),  
                                 new Translation(), "");  
         }  
28    
29          /**          /**
30           * Creates an {@link AttributeMetadata} object with the following           * Set a translatable description for this attribute.
          * information  
          *  
          * @param visible  
          *            Shall this attribute be displayed or hidden from the user?  
          * @param title  
          *            {@link Translation} for Name  
          * @param desc  
          *            {@link Translation} for an attribute description  
          * @param unit  
          *            {@link String} of the unit that the information is in  
31           */           */
32          public AttributeMetadata(final String localName, final Boolean visible,          public void setDesc(Translation desc);
                         final Translation title, final Translation desc, final String unit) {  
                 this(new NameImpl(localName), true, title, desc, "");  
         }  
33    
34          /**          /**
35           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * The local name. E.g. the name of the DBF column as a {@link String}.
36           */           */
37          public AttributeMetadata(final String localName, final List<String> langs) {          public String getLocalName();
                 this(localName, true, new Translation(langs, localName),  
                                 new Translation(), "");  
         }  
38    
39          /**          /**
40           * Creates a new visible {@link AttributeMetadata}           * A short form for #setName(new NameImpl(localName))
41           */           */
42          public AttributeMetadata(final String localName, final String defaultTitle,          public void setLocalName(String localName);
                         final List<String> langs) {  
                 this(localName, true, new Translation(langs, defaultTitle),  
                                 new Translation(), "");  
         }  
43    
44          /**          /**
45           * Orders the attributes according to their {@link #weight}. Heavier =>           * The fully qualified {@link Name} of the attribute, e.g.
46           * further down.           * <code>org.bla.plo:blub</code>. The second part equals the
47             * {@link #getLocalName()} value. The first may be <code>null</code> or
48             * represent the layer name.
49           */           */
50          @Override          public NameImpl getName();
         public int compareTo(final AttributeMetadata atm2) {  
                 return new Integer(weight).compareTo(atm2.getWeight());  
         }  
51    
52          /**          /**
53           * @see Copyable inferface           * set the fully qualified {@link Name} of this attribute.
54           */           */
55          @Override          public void setName(NameImpl name);
         public AttributeMetadata copy() {  
                 return copyTo(new AttributeMetadata());  
         }  
56    
57          /**          /**
58           * @see Copyable inferface           * A list og objects that represent NODATA-values for this attribute. The
59             * objects are supporsed to be correctly casted already. That means that the
60             * NODATA objects listed here have must have the same type as the values of
61             * this object.
62           */           */
63          @Override          public HashSet<Object> getNodataValues();
         public AttributeMetadata copyTo(final AttributeMetadata amd) {  
                 getTitle().copyTo(amd.getTitle());  
                 getDesc().copyTo(amd.getDesc());  
                 amd.setUnit(getUnit());  
                 amd.setVisible(isVisible());  
                 amd.setName(new NameImpl(getName().getNamespaceURI(), getName()  
                                 .getLocalPart()));  
   
                 amd.setWeight(getWeight());  
                 amd.setFunctionX(getFunctionX());  
                 amd.setFunctionA(getFunctionA());  
   
                 amd.setNodataValues(getNodataValues());  
   
                 return amd;  
         }  
   
         // only to be used by copyTo()  
         private void setNodataValues(HashSet<Object> nodataValues2) {  
                 nodataValues = nodataValues2;  
         }  
   
         public Translation getDesc() {  
                 return desc;  
         }  
   
         public Double getFunctionA() {  
                 return functionA;  
         }  
   
         public Double getFunctionX() {  
                 return functionX;  
         }  
64    
65          /**          /**
66           * The local name. E.g. the name of the DBF column as a {@link String}           * Takes any value object and checks it against the NODATA values. If the
67             * value equals a NODATA value, <code>null</code> is returned. Otherwise the
68             * same object is returned.
69             *
70             * Note: This method is called often.
71           */           */
72          public String getLocalName() {          public Object fiterNodata(Object value);
                 return getName().getLocalPart();  
         }  
73    
74          /**          /**
75           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>           * @return a non-translatable unit {@link String} for this attribute values.
76           */           */
77          public Name getName() {          public String getUnit();
                 return name;  
         }  
   
         public HashSet<Object> getNodataValues() {  
                 return nodataValues;  
         }  
78    
79          /**          /**
80           * @return a number between 0 (bad) and 1 (good) that is calculated from the           * Set a unit {@link String} for this attribute values.
          *         amount of translation available. If this attribute is not  
          *         {@link #visible}, it will return 1.  
81           */           */
82          public double getQuality(final List<String> languages) {          public void setUnit(String unit);
   
                 if (!isVisible())  
                         return 1.;  
   
                 return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil  
                                 .qmTranslation(languages, getDesc()) * 1.) / 3.;  
         }  
   
         public Translation getTitle() {  
                 return title;  
         }  
   
         public String getUnit() {  
                 return unit;  
         }  
   
         public int getWeight() {  
                 return weight;  
         }  
83    
84          /**          /**
85           * Will the end-user see this attribute?           * @return A value defining the position of this attribute whenever the
86             *         attributes are listed to an end-user. The higher the weight, the
87             *         lower the position. (heavy goes down, light goes up)
88           */           */
89          public boolean isVisible() {          public double getWeight();
                 return visible;  
         }  
   
         public void setDesc(final Translation desc) {  
                 this.desc = desc;  
         }  
   
         public void setFunctionA(final Double functionA) {  
                 this.functionA = functionA;  
         }  
   
         public void setFunctionX(final Double functionX) {  
                 this.functionX = functionX;  
         }  
90    
91          /**          /**
92           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * set a value defining the position of this attribute whenever the
93             * attributes are listed to an end-user. The higher the weight, the lower
94             * the position. (heavy goes down, light goes up)
95           */           */
96          public void setLocalName(final String localName) {          public void setWeight(double weight);
                 this.name = new NameImpl(localName);  
         }  
97    
98          /**          /**
99           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>           * @return <code>false</code> if this attribute should not be selectable or
100             *         shown to the end-user.
101           */           */
102          public void setName(final Name name) {          public boolean isVisible();
                 this.name = name;  
         }  
   
         public void addNodataValue(Object nodataValue) {  
                 this.nodataValues.add(nodataValue);  
         }  
           
         public void removeNodataValue(Object nodataValue) {  
                 this.nodataValues.remove(nodataValue);  
         }  
   
         public void setTitle(final Translation title) {  
                 this.title = title;  
         }  
   
         public void setUnit(final String unit) {  
                 this.unit = unit;  
         }  
   
         public void setVisible(final boolean visible) {  
                 this.visible = visible;  
         }  
103    
104          /**          /**
105           * Shall the end-user see this attribute?           * Set <code>false</code> if this attribute should not be shown to the
106           * @param visible           * end-user.
107           */           */
108          public void setVisible(final Boolean visible) {          public void setVisible(boolean visible);
 //              // The THE_GEOM and shall never be visible!  
 //              if (name.getLocalPart().equalsIgnoreCase("the_geom"))  
 //                      this.visible = false;  
 //              else  
 //                      this.visible = visible;  
109    
110                  this.visible = visible;          String getNoDataValuesFormatted();
         }  
   
         public void setWeight(final int weight) {  
                 this.weight = weight;  
         }  
           
         /**  
          * For nicer debugging  
          */  
         @Override  
         public String toString() {  
                 StringBuffer sb = new StringBuffer();  
                 if (name != null) sb.append(name.toString()+" ");  
                 sb.append("weight="+weight+" ");  
                 sb.append("title="+getTitle().toString());  
                 return sb.toString();  
         }  
111    
         /**  
          * Takes any value object and checks it against the NODATA values. If the  
          * value equals a NODATA value, <code>null</code> is returned. Otherwise the  
          * same object is returned.  
          *  
          * Note: This method is called often.  
          */  
         public Object fiterNodata(final Object value) {  
                 if (nodataValues.contains(value)) return null;  
                 return value;  
         }  
112  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26