--- branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java 2009/10/13 11:29:54 463 +++ trunk/src/skrueger/AttributeMetadataImpl.java 2010/03/21 11:02:34 769 @@ -29,121 +29,235 @@ ******************************************************************************/ package skrueger; +import java.util.HashSet; +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 implements Copyable{ +public class AttributeMetadataImpl extends AbstractAttributeMetadata implements + QualityQuantizable { + static private final Logger LOGGER = Logger - .getLogger(AttributeMetadata.class); - protected Translation title = new Translation(); - protected Translation desc = new Translation(); - protected boolean visible = false; - protected String unit = ""; - protected int colIdx; - - /** - * 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 + .getLogger(AttributeMetadataImpl.class); + + /** + * 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.; + + /** Only used for {@link Copyable#copy()} **/ + private AttributeMetadataImpl() { + } + + public AttributeMetadataImpl(final AttributeDescriptor attDesc, + final int weight, final List langs) { + this(attDesc.getName(), langs); + setWeight(weight); + } + + public AttributeMetadataImpl(final AttributeDescriptor attDesc, + final List langs) { + this(attDesc.getName(), langs); + } + + /** + * Creates an {@link AttributeMetadataImpl} 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 AttributeMetadataImpl(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 attribute is THE_GEOM and shall never be visible! - this.visible = false; - }else - this.visible = visible; + this.visible = visible; this.unit = unit; } /** - * Creates a new visible {@link AttributeMetadata} with default (no) values. + * Creates an {@link AttributeMetadataImpl} 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 */ - public AttributeMetadata(final Integer col, final String defaultName) { - this(col, true, new Translation(defaultName), new Translation(), ""); - } - - /** Only used for {@link Copyable#copy()}**/ - private AttributeMetadata() { - } - - public boolean isVisible() { - return visible; - } - - public void setVisible(final Boolean visible) { + public AttributeMetadataImpl(final Name name, final Boolean visible, + final String unit) { + this.setName(name); this.visible = visible; + this.unit = unit; } - /** @return the index of this attribute in the underlying table/dbf **/ - public int getColIdx() { - return colIdx; + /** + * Creates a new visible {@link AttributeMetadataImpl} + */ + public AttributeMetadataImpl(final Name name, final List langs) { + this(name, true, new Translation(langs, name.getLocalPart()), + new Translation(), ""); } - public Translation getTitle() { - return title; + /** + * Creates a new visible {@link AttributeMetadataImpl} + */ + public AttributeMetadataImpl(final Name name, final String defaultTitle, + final List langs) { + this(name, true, new Translation(langs, defaultTitle), + new Translation(), ""); } - public void setTitle(final Translation title) { - this.title = title; + /** + * Creates an {@link AttributeMetadataImpl} 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 AttributeMetadataImpl(final String localName, final Boolean visible, + final Translation title, final Translation desc, final String unit) { + this(new NameImpl(localName), true, title, desc, ""); } - public Translation getDesc() { - return desc; + /** + * Creates a new visible {@link AttributeMetadataImpl} with default (no) + * values. + */ + public AttributeMetadataImpl(final String localName, + final List langs) { + this(localName, true, new Translation(langs, localName), + new Translation(), ""); } - public void setDesc(final Translation desc) { - this.desc = desc; + /** + * Creates a new visible {@link AttributeMetadataImpl} + */ + public AttributeMetadataImpl(final String localName, + final String defaultTitle, final List langs) { + this(localName, true, new Translation(langs, defaultTitle), + new Translation(), ""); } - public String getUnit() { - return unit; + /** + * Orders the attributes according to their {@link #weight}. Heavier => + * further down. + */ + @Override + public int compareTo(final AttributeMetadata atm2) { + return new Integer(weight).compareTo(new Double(atm2.getWeight()) + .intValue()); } - public void setUnit(final String unit) { - this.unit = unit; + /** + * @see Copyable inferface + */ + @Override + public AttributeMetadata copy() { + return copyTo(new AttributeMetadataImpl()); } + /** + * @see Copyable inferface + */ @Override - public AttributeMetadata copyTo(AttributeMetadata amd) { + public AttributeMetadata copyTo(final AttributeMetadata amd) { getTitle().copyTo(amd.getTitle()); getDesc().copyTo(amd.getDesc()); amd.setUnit(getUnit()); amd.setVisible(isVisible()); - amd.setColIdx(getColIdx()); - + amd.setName(new NameImpl(getName().getNamespaceURI(), getName() + .getLocalPart())); + + amd.setWeight(getWeight()); + + if (amd instanceof AttributeMetadataImpl) { + AttributeMetadataImpl amd_ = (AttributeMetadataImpl) amd; + + amd_.setFunctionX(getFunctionX()); + amd_.setFunctionA(getFunctionA()); + amd_.setNodataValues(getNodataValues()); + } + return amd; } + // only to be used by copyTo() + private void setNodataValues(HashSet nodataValues_) { + nodataValues = nodataValues_; + } + + public Double getFunctionA() { + return functionA; + } + + public Double getFunctionX() { + return functionX; + } + + /** + * @return a number between 0 (bad) and 1 (good) that is calculated from the + * amount of translation available. If this attribute is not + * {@link #visible}, it will return 1. + */ @Override - public AttributeMetadata copy() { - AttributeMetadata amd = new AttributeMetadata(); - getTitle().copyTo(amd.getTitle()); - getDesc().copyTo(amd.getDesc()); - amd.setUnit(getUnit()); - amd.setVisible(isVisible()); - amd.setColIdx(getColIdx()); - - return amd; + public double getQuality(final List languages) { + + if (!isVisible()) + return 1.; + + return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil + .qmTranslation(languages, getDesc()) * 1.) / 3.; } - private void setColIdx(int colIdx_) { - colIdx = colIdx_; + public void setFunctionA(final Double functionA) { + this.functionA = functionA; } + + public void setFunctionX(final Double functionX) { + this.functionX = functionX; + } + + }