--- trunk/src/skrueger/AttributeMetaData.java 2009/07/29 09:33:33 244 +++ branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java 2009/11/13 18:16:38 518 @@ -2,7 +2,7 @@ * 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, focussing (not only) on Java Swing + * classes based on Java 1.6, focusing (not only) on Java Swing * and the Geotools library. * * The SCHMITZM project is hosted at: @@ -29,66 +29,221 @@ ******************************************************************************/ package skrueger; +import java.util.ArrayList; +import java.util.List; + import org.apache.log4j.Logger; +import org.geotools.feature.NameImpl; +import org.opengis.feature.type.AttributeDescriptor; +import org.opengis.feature.type.Name; +import skrueger.geotools.Copyable; import skrueger.geotools.StyledLayerInterface; +import skrueger.i8n.I8NUtil; import skrueger.i8n.Translation; /** - * This class holds meta information about an attribute/column. This - * information is used by {@link StyledLayerInterface}. - * + * This class holds meta information about an attribute/column. This information + * is used by {@link StyledLayerInterface} and many others.
+ * * @author Stefan Alfons Krüger */ -public class AttributeMetaData { +public class AttributeMetadata implements Copyable, + Comparable { static private final Logger LOGGER = Logger - .getLogger(AttributeMetaData.class); + .getLogger(AttributeMetadata.class); + + // public class WeightComparator implements Comparator + // { + // + // @Override + // public int compare(AttributeDescriptor o1, AttributeDescriptor o2) { + // // TODO Auto-generated method stub + // return 0; + // } + // + // }; + + /** Translation of the attribute's title **/ protected Translation title = new Translation(); + + /** Translation of the attribute's description **/ protected Translation desc = new Translation(); - protected boolean visible = false; + + /** Is the attribute visible to the user or ignored where possible **/ + protected boolean visible = true; + + /** + * The unit append to all visualizations of values of this attribute (is not + * null) + **/ protected String unit = ""; - protected int colIdx; + + /** The Name of the attribute **/ + private Name name; + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + public List getNodataValues() { + return nodataValues; + } + + public void setNodataValues(List nodataValues) { + this.nodataValues = nodataValues; + } + + public Double getFunctionX() { + return functionX; + } + + public void setFunctionX(Double functionX) { + this.functionX = functionX; + } + + public Double getFunctionA() { + return functionA; + } + + public void setFunctionA(Double functionA) { + this.functionA = functionA; + } + + public void setVisible(boolean visible) { + this.visible = visible; + } + + /** + * When listed, the attributes are listed according to their weight (heavier + * => further down) + **/ + protected int weight = 0; + + /** + * Allows to define general NODATA values for an attribute. e.g. -9999 can + * be set and will always be interpreted as NULL internally and will usually + * be ignored. + **/ + protected List nodataValues = new ArrayList(); /** - * 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 + * For numerical attributes the value can be transformed by VALUE*X+A when + * presented on screen + **/ + protected Double functionX = 1.; + + /** + * For numerical attributes the value can be transformed by VALUE*X+A when + * presented on screen + **/ + protected Double functionA = 0.; + + /** + * 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 int colIdx, final Boolean visible, + public AttributeMetadata(final Name name, final Boolean visible, final Translation title, final Translation desc, final String unit) { - - this.colIdx = colIdx; + + this.setName(name); this.title = title; this.desc = desc; - if (colIdx == 0){ - // The first attribut is THE_GEOM and shall never be visible! + + // The THE_GEOM and shall never be visible! + if (name.getLocalPart().equalsIgnoreCase("the_geom")) this.visible = false; - }else + else this.visible = visible; + this.unit = unit; } /** - * Creates a new visible {@link AttributeMetaData} with default (no) values. + * Creates an {@link AttributeMetadata} object with the following + * 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 */ - public AttributeMetaData(final Integer col, final String defaultName) { - this(col, true, new Translation(defaultName), new Translation(), ""); + public AttributeMetadata(final String localName, final Boolean visible, + final Translation title, final Translation desc, final String unit) { + this(new NameImpl(localName), true, title, desc, ""); } - public Boolean isVisible() { + /** + * Creates a new visible {@link AttributeMetadata} with default (no) values. + */ + public AttributeMetadata(final String localName, final String defaultTitle) { + this(localName, true, new Translation(defaultTitle), new Translation(), + ""); + } + + /** + * Creates a new visible {@link AttributeMetadata} with default (no) values. + */ + public AttributeMetadata(final Name name, final String defaultTitle) { + this(name, true, new Translation(defaultTitle), new Translation(), ""); + } + + /** + * Creates a new visible {@link AttributeMetadata} with default (no) values. + */ + public AttributeMetadata(final Name name) { + this(name, true, new Translation(name.getLocalPart()), + new Translation(), ""); + } + + /** + * Creates a new visible {@link AttributeMetadata} with default (no) values. + */ + public AttributeMetadata(final String localName) { + this(localName, true, new Translation(localName), new Translation(), ""); + } + + /** Only used for {@link Copyable#copy()} **/ + private AttributeMetadata() { + } + + public AttributeMetadata(AttributeDescriptor attDesc) { + this(attDesc.getName()); + } + + public boolean isVisible() { return visible; } public void setVisible(final Boolean visible) { - this.visible = visible; - } - /** @return the index of this attribute in the underlying table/dbf **/ - public int getColIdx() { - return colIdx; + // The THE_GEOM and shall never be visible! // TODO MAKE BETTER + if (name.getLocalPart().equalsIgnoreCase("the_geom")) + this.visible = false; + else + this.visible = visible; + + this.visible = visible; } public Translation getTitle() { @@ -114,4 +269,75 @@ public void setUnit(final String unit) { this.unit = unit; } + + @Override + public AttributeMetadata copyTo(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()); + + for (Object nodataValue : getNodataValues()) { + amd.getNodataValues().add(nodataValue); + } + + return amd; + } + + @Override + public AttributeMetadata copy() { + return copyTo(new AttributeMetadata()); + } + + /** + * The local Name. E.g. the name of the DBF column as a String + */ + public String getLocalName() { + return getName().getLocalPart(); + } + + /** + * The fully qualified Name of the attribute, e.g. org.bla.plo:blub + */ + public Name getName() { + return name; + } + + /** + * The fully qualified Name of the attribute, e.g. org.bla.plo:blub + */ + public void setName(org.opengis.feature.type.Name name) { + this.name = name; + } + + /** + * The fully qualified Name of the attribute, e.g. org.bla.plo:blub + */ + public void setLocalName(String localName) { + this.name = new NameImpl(localName); + } + + /** + * Orders the Attributes according to their weight. Heavier => further down. + */ + @Override + public int compareTo(AttributeMetadata atm2) { + // Double double1 = new Double(1./weight); + // double double2 = 1./atm2.getWeight(); + return new Integer(weight).compareTo(atm2.getWeight()); + } + + /** + * @return a number between 0 (bad) and 1 (good) that is calculated from the amount of translation available in the visible attributes + */ + public double getQuality(List languages) { + return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil + .qmTranslation(languages, getDesc()) * 1.) / 3.; + } }