|
/******************************************************************************* |
|
|
* 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; |
|
|
import java.util.Set; |
|
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 AttributeMetadataInterface extends Copyable<AttributeMetadataInterface>, |
12 |
* This class holds meta information about an attribute/column. This information |
Comparable<AttributeMetadataInterface> { |
|
* is used by {@link StyledLayerInterface} and many others.<br/> |
|
|
* |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
|
|
*/ |
|
|
public class AttributeMetadata implements Copyable<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; |
|
|
|
|
|
/** |
|
|
* 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. 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); |
|
|
} |
|
13 |
|
|
14 |
/** |
/** |
15 |
* Creates an {@link AttributeMetadata} object with the following |
* @return a translatable title for this attribute.. |
16 |
* information |
*/ |
17 |
* |
public Translation getTitle(); |
|
* @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; |
|
|
} |
|
18 |
|
|
19 |
/** |
/** |
20 |
* Creates an {@link AttributeMetadata} object with the following |
* Set a translatable title for this attribute.. |
|
* 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 |
|
21 |
*/ |
*/ |
22 |
public AttributeMetadata(final Name name, final Boolean visible, |
public void setTitle(Translation title); |
23 |
final String unit) { |
|
|
this.setName(name); |
|
|
this.visible = visible; |
|
|
this.unit = unit; |
|
|
} |
|
|
|
|
24 |
/** |
/** |
25 |
* Creates a new visible {@link AttributeMetadata} |
* Set an untranslated title for this attribute or pass a {@link Translation} encryped as a {@link String} |
26 |
*/ |
*/ |
27 |
public AttributeMetadata(final Name name, final List<String> langs) { |
public void setTitle(String title); |
|
this(name, true, new Translation(langs, name.getLocalPart()), |
|
|
new Translation(), ""); |
|
|
} |
|
28 |
|
|
29 |
/** |
/** |
30 |
* Creates a new visible {@link AttributeMetadata} |
* @return a translatable description for this attribute |
31 |
*/ |
*/ |
32 |
public AttributeMetadata(final Name name, final String defaultTitle, |
public Translation getDesc(); |
|
final List<String> langs) { |
|
|
this(name, true, new Translation(langs, defaultTitle), |
|
|
new Translation(), ""); |
|
|
} |
|
33 |
|
|
34 |
/** |
/** |
35 |
* 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 |
|
36 |
*/ |
*/ |
37 |
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, ""); |
|
|
} |
|
38 |
|
|
39 |
/** |
/** |
40 |
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
* Set an untranslated description for this attribute or pass a {@link Translation} encryped as a {@link String} |
41 |
*/ |
*/ |
42 |
public AttributeMetadata(final String localName, final List<String> langs) { |
public void setDesc(String desc); |
43 |
this(localName, true, new Translation(langs, localName), |
|
|
new Translation(), ""); |
|
|
} |
|
44 |
|
|
45 |
/** |
/** |
46 |
* Creates a new visible {@link AttributeMetadata} |
* The local name. E.g. the name of the DBF column as a {@link String}. |
47 |
*/ |
*/ |
48 |
public AttributeMetadata(final String localName, final String defaultTitle, |
public String getLocalName(); |
|
final List<String> langs) { |
|
|
this(localName, true, new Translation(langs, defaultTitle), |
|
|
new Translation(), ""); |
|
|
} |
|
49 |
|
|
50 |
/** |
/** |
51 |
* Orders the attributes according to their {@link #weight}. Heavier => |
* A short form for #setName(new NameImpl(localName)) |
|
* further down. |
|
52 |
*/ |
*/ |
53 |
@Override |
public void setLocalName(String localName); |
|
public int compareTo(final AttributeMetadata atm2) { |
|
|
return new Integer(weight).compareTo(atm2.getWeight()); |
|
|
} |
|
54 |
|
|
55 |
/** |
/** |
56 |
* @see Copyable inferface |
* The fully qualified {@link Name} of the attribute, e.g. |
57 |
|
* <code>org.bla.plo:blub</code>. The second part equals the |
58 |
|
* {@link #getLocalName()} value. The first may be <code>null</code> or |
59 |
|
* represent the layer name. |
60 |
*/ |
*/ |
61 |
@Override |
public NameImpl getName(); |
|
public AttributeMetadata copy() { |
|
|
return copyTo(new AttributeMetadata()); |
|
|
} |
|
62 |
|
|
63 |
/** |
/** |
64 |
* @see Copyable inferface |
* set the fully qualified {@link Name} of this attribute. |
65 |
*/ |
*/ |
66 |
@Override |
public void setName(NameImpl name); |
|
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; |
|
|
} |
|
67 |
|
|
68 |
/** |
/** |
69 |
* The local name. E.g. the name of the DBF column as a {@link String} |
* A list og objects that represent NODATA-values for this attribute. The |
70 |
|
* objects are supporsed to be correctly casted already. That means that the |
71 |
|
* NODATA objects listed here have must have the same type as the values of |
72 |
|
* this object. |
73 |
*/ |
*/ |
74 |
public String getLocalName() { |
public HashSet<Object> getNodataValues(); |
|
return getName().getLocalPart(); |
|
|
} |
|
75 |
|
|
76 |
/** |
/** |
77 |
* The fully qualified {@link Name} of the attribute, e.g. |
* Takes any value object and checks it against the NODATA values. If the |
78 |
* <code>org.bla.plo:blub</code> |
* value equals a NODATA value, <code>null</code> is returned. Otherwise the |
79 |
|
* same object is returned. |
80 |
|
* |
81 |
|
* Note: This method is called often. |
82 |
*/ |
*/ |
83 |
public Name getName() { |
public Object fiterNodata(Object value); |
|
return name; |
|
|
} |
|
|
|
|
|
public HashSet<Object> getNodataValues() { |
|
|
return nodataValues; |
|
|
} |
|
84 |
|
|
85 |
/** |
/** |
86 |
* @return a number between 0 (bad) and 1 (good) that is calculated from the |
* @return a non-translatable unit {@link String} for this attribute values. |
|
* amount of translation available. If this attribute is not |
|
|
* {@link #visible}, it will return 1. |
|
87 |
*/ |
*/ |
88 |
public double getQuality(final List<String> languages) { |
public String getUnit(); |
|
|
|
|
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; |
|
|
} |
|
89 |
|
|
90 |
/** |
/** |
91 |
* Will the end-user see this attribute? |
* Set a unit {@link String} for this attribute values. |
92 |
*/ |
*/ |
93 |
public boolean isVisible() { |
public void setUnit(String unit); |
|
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; |
|
|
} |
|
94 |
|
|
95 |
/** |
/** |
96 |
* The fully qualified Name of the attribute, e.g. org.bla.plo:blub |
* @return A value defining the position of this attribute whenever the |
97 |
|
* attributes are listed to an end-user. The higher the weight, the |
98 |
|
* lower the position. (heavy goes down, light goes up) |
99 |
*/ |
*/ |
100 |
public void setLocalName(final String localName) { |
public double getWeight(); |
|
this.name = new NameImpl(localName); |
|
|
} |
|
101 |
|
|
102 |
/** |
/** |
103 |
* The fully qualified {@link Name} of the attribute, e.g. |
* set a value defining the position of this attribute whenever the |
104 |
* <code>org.bla.plo:blub</code> |
* attributes are listed to an end-user. The higher the weight, the lower |
105 |
|
* the position. (heavy goes down, light goes up) |
106 |
*/ |
*/ |
107 |
public void setName(final Name name) { |
public void setWeight(double weight); |
|
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; |
|
|
} |
|
108 |
|
|
109 |
/** |
/** |
110 |
* Shall the end-user see this attribute? |
* @return <code>false</code> if this attribute should not be selectable or |
111 |
* |
* shown to the end-user. |
|
* @param visible |
|
112 |
*/ |
*/ |
113 |
public void setVisible(final Boolean visible) { |
public boolean isVisible(); |
|
// // The THE_GEOM and shall never be visible! |
|
|
// if (name.getLocalPart().equalsIgnoreCase("the_geom")) |
|
|
// this.visible = false; |
|
|
// else |
|
|
// this.visible = visible; |
|
|
|
|
|
this.visible = visible; |
|
|
} |
|
|
|
|
|
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(); |
|
|
} |
|
114 |
|
|
115 |
/** |
/** |
116 |
* Takes any value object and checks it against the NODATA values. If the |
* Set <code>false</code> if this attribute should not be shown to the |
117 |
* value equals a NODATA value, <code>null</code> is returned. Otherwise the |
* end-user. |
|
* same object is returned. |
|
|
* |
|
|
* Note: This method is called often. |
|
118 |
*/ |
*/ |
119 |
public Object fiterNodata(final Object value) { |
public void setVisible(boolean visible); |
120 |
if (nodataValues.contains(value)) |
|
121 |
return null; |
String getNoDataValuesFormatted(); |
122 |
return value; |
|
|
} |
|
|
|
|
|
/** |
|
|
* @return a nicely formatted String containing all NODATA values. Strings |
|
|
* are quoted fo that the empty String can be seen. |
|
|
*/ |
|
|
public String getNoDataValuesFormatted() { |
|
|
return formatNoDataValues(getNodataValues()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* @return a nicely formatted String containing all NODATA values. Strings |
|
|
* are quoted fo that the empty String can be seen. |
|
|
*/ |
|
|
public static String formatNoDataValues(Set<Object> list) { |
|
|
String nicelyFormatted = ""; |
|
|
if (list != null) { |
|
|
if (list.size() == 0) |
|
|
nicelyFormatted = ""; |
|
|
else { |
|
|
for (Object ndo : list) { |
|
|
if (ndo instanceof String) |
|
|
nicelyFormatted += "\"" + ndo + "\""; |
|
|
else |
|
|
nicelyFormatted += ndo.toString(); |
|
|
|
|
|
nicelyFormatted += ","; |
|
|
} |
|
|
// Remove the extra comma |
|
|
nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted |
|
|
.length() - 1); |
|
|
} |
|
|
} |
|
|
return nicelyFormatted; |
|
|
} |
|
123 |
} |
} |