--- branches/1.0-gt2-2.6/src/skrueger/AttributeMetaData.java 2009/10/01 20:22:48 420
+++ branches/2.0-RC2/src/skrueger/AttributeMetadata.java 2010/01/28 10:06:05 621
@@ -29,121 +29,339 @@
******************************************************************************/
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 implements Copyable{
+public class AttributeMetadata implements Copyable,
+ Comparable {
+
static private final Logger LOGGER = Logger
- .getLogger(AttributeMetaData.class);
- protected Translation title = new Translation();
+ .getLogger(AttributeMetadata.class);
+
+ /** Translation of the attribute's description **/
protected Translation desc = new Translation();
- protected boolean visible = false;
+
+ /**
+ * 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;
+
+ /**
+ * 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. TODO not implemented yet
+ **/
+ protected List nodataValues = new ArrayList();
+
+ /** 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 = "";
- protected int colIdx;
-
+
+ /** Is the attribute visible to the user or ignored where possible **/
+ protected boolean visible = true;
+
/**
- * 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
+ * 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#copy()} **/
+ private AttributeMetadata() {
+ }
+
+ public AttributeMetadata(final AttributeDescriptor attDesc, final int weight,
+ final List langs) {
+ this(attDesc.getName(), langs);
+ setWeight(weight);
+ }
+
+ public AttributeMetadata(final AttributeDescriptor attDesc, final List 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 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 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 a new visible {@link AttributeMetadata}
*/
- public AttributeMetaData(final Integer col, final String defaultName) {
- this(col, true, new Translation(defaultName), new Translation(), "");
+ public AttributeMetadata(final Name name, final List langs) {
+ this(name, true, new Translation(langs, name.getLocalPart()),
+ new Translation(), "");
}
- /** Only used for {@link Copyable#copy()}**/
- private AttributeMetaData() {
+ /**
+ * Creates a new visible {@link AttributeMetadata}
+ */
+ public AttributeMetadata(final Name name, final String defaultTitle,
+ final List langs) {
+ this(name, true, new Translation(langs, defaultTitle),
+ new Translation(), "");
}
- public boolean isVisible() {
- return visible;
+ /**
+ * 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 String localName, final Boolean visible,
+ final Translation title, final Translation desc, final String unit) {
+ this(new NameImpl(localName), true, title, desc, "");
}
- public void setVisible(final Boolean visible) {
- this.visible = visible;
+ /**
+ * Creates a new visible {@link AttributeMetadata} with default (no) values.
+ */
+ public AttributeMetadata(final String localName, final List langs) {
+ this(localName, true, new Translation(langs, localName),
+ new Translation(), "");
}
- /** @return the index of this attribute in the underlying table/dbf **/
- public int getColIdx() {
- return colIdx;
+ /**
+ * Creates a new visible {@link AttributeMetadata}
+ */
+ public AttributeMetadata(final String localName, final String defaultTitle,
+ final List langs) {
+ this(localName, true, new Translation(langs, defaultTitle),
+ new Translation(), "");
}
- public Translation getTitle() {
- return title;
+ /**
+ * Orders the attributes according to their {@link #weight}. Heavier =>
+ * further down.
+ */
+ @Override
+ public int compareTo(final AttributeMetadata atm2) {
+ return new Integer(weight).compareTo(atm2.getWeight());
}
- public void setTitle(final Translation title) {
- this.title = title;
+ /**
+ * @see Copyable inferface
+ */
+ @Override
+ public AttributeMetadata copy() {
+ return copyTo(new AttributeMetadata());
+ }
+
+ /**
+ * @see Copyable inferface
+ */
+ @Override
+ 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());
+
+ for (final Object nodataValue : getNodataValues()) {
+ amd.getNodataValues().add(nodataValue);
+ }
+
+ return amd;
}
public Translation getDesc() {
return desc;
}
- public void setDesc(final Translation desc) {
- this.desc = desc;
+ public Double getFunctionA() {
+ return functionA;
+ }
+
+ public Double getFunctionX() {
+ return functionX;
+ }
+
+ /**
+ * The local name. E.g. the name of the DBF column as a {@link String}
+ */
+ public String getLocalName() {
+ return getName().getLocalPart();
+ }
+
+ /**
+ * The fully qualified {@link Name} of the attribute, e.g. org.bla.plo:blub
+ */
+ public Name getName() {
+ return name;
+ }
+
+ public List getNodataValues() {
+ return nodataValues;
+ }
+
+ /**
+ * @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.
+ */
+ public double getQuality(final List languages) {
+
+ 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;
+ }
+
+ /**
+ * Will the end-user see this attribute?
+ */
+ public boolean isVisible() {
+ 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;
+ }
+
+ /**
+ * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
+ */
+ public void setLocalName(final String localName) {
+ this.name = new NameImpl(localName);
+ }
+
+ /**
+ * The fully qualified {@link Name} of the attribute, e.g. org.bla.plo:blub
+ */
+ public void setName(final Name name) {
+ this.name = name;
+ }
+
+ public void setNodataValues(final List nodataValues) {
+ this.nodataValues = nodataValues;
+ }
+
+ public void setTitle(final Translation title) {
+ this.title = title;
+ }
+
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.setColIdx(getColIdx());
-
- return amd;
+ public void setVisible(final boolean visible) {
+ this.visible = visible;
}
- @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;
+ /**
+ * Shall the end-user see this attribute?
+ * @param visible
+ */
+ public void setVisible(final Boolean visible) {
+// // The THE_GEOM and shall never be visible!
+// if (name.getLocalPart().equalsIgnoreCase("the_geom"))
+// this.visible = false;
+// else
+// this.visible = visible;
+
+ this.visible = visible;
}
- private void setColIdx(int colIdx_) {
- colIdx = colIdx_;
+ 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();
}
}