/[schmitzm]/trunk/src/skrueger/AttributeMetadataImpl.java
ViewVC logotype

Diff of /trunk/src/skrueger/AttributeMetadataImpl.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/AttributeMetaData.java revision 93 by alfonx, Fri May 1 16:24:15 2009 UTC branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java revision 534 by alfonx, Fri Nov 20 10:28:01 2009 UTC
# Line 1  Line 1 
1  package skrueger;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import org.apache.log4j.Logger;   *
4     * This file is part of the SCHMITZM library - a collection of utility
5  import skrueger.geotools.StyledMapInterface;   * classes based on Java 1.6, focusing (not only) on Java Swing
6  import skrueger.i8n.Translation;   * and the Geotools library.
7     *
8  /**   * The SCHMITZM project is hosted at:
9   * This class holds meta information about an attribute/column. This   * http://wald.intevation.org/projects/schmitzm/
10   * information is used by {@link StyledMapInterface}.   *
11   *   * This program is free software; you can redistribute it and/or
12   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * modify it under the terms of the GNU Lesser General Public License
13   */   * as published by the Free Software Foundation; either version 3
14  public class AttributeMetaData {   * of the License, or (at your option) any later version.
15          static private final Logger LOGGER = Logger   *
16                          .getLogger(AttributeMetaData.class);   * This program is distributed in the hope that it will be useful,
17          protected Translation title = new Translation();   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18          protected Translation desc = new Translation();   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19          protected boolean visible = false;   * GNU General Public License for more details.
20          protected String unit = "";   *
21          protected int colIdx;   * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23          /**   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24           * Creates an {@link AttributeMetaData} object with the following information   * or try this link: http://www.gnu.org/licenses/lgpl.html
25           * @param colIdx The column index of this attribute in the underlying table/dbf/etc...   *
26           * @param visible Shall this attribute be displayed or hidden from the user?   * Contributors:
27           * @param title {@link Translation} for Name   *     Martin O. J. Schmitz - initial API and implementation
28           * @param desc {@link Translation} for an attribute description   *     Stefan A. Krüger - additional utility classes
29           * @param unit {@link String} of the unit that the information is in   ******************************************************************************/
30           */  package skrueger;
31          public AttributeMetaData(final int colIdx, final Boolean visible,  
32                          final Translation title, final Translation desc, final String unit) {  import java.util.ArrayList;
33            import java.util.List;
34                  this.colIdx = colIdx;  
35                  this.title = title;  import org.apache.log4j.Logger;
36                  this.desc = desc;  import org.geotools.feature.NameImpl;
37                  if (colIdx == 0){  import org.opengis.feature.type.AttributeDescriptor;
38                          // The first attribut is THE_GEOM and shall never be visible!  import org.opengis.feature.type.Name;
39                          this.visible = false;  
40                  }else  import skrueger.geotools.Copyable;
41                          this.visible = visible;  import skrueger.geotools.StyledLayerInterface;
42                  this.unit = unit;  import skrueger.i8n.I8NUtil;
43          }  import skrueger.i8n.Translation;
44    
45          /**  /**
46           * Creates a new visible {@link AttributeMetaData} with default (no) values.     * This class holds meta information about an attribute/column. This information
47           */   * is used by {@link StyledLayerInterface} and many others.<br/>
48          public AttributeMetaData(final Integer col, final String defaultName) {   *
49                  this(col, true, new Translation(defaultName), new Translation(), "");   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
50          }   */
51    public class AttributeMetadata implements Copyable<AttributeMetadata>,
52          public Boolean isVisible() {                  Comparable<AttributeMetadata> {
53                  return visible;          
54          }          static private final Logger LOGGER = Logger
55                            .getLogger(AttributeMetadata.class);
56          public void setVisible(final Boolean visible) {  
57                  this.visible = visible;          /** Translation of the attribute's description **/
58          }          protected Translation desc = new Translation();
59    
60          /** @return the index of this attribute in the underlying table/dbf **/          /**
61          public int getColIdx() {           * For numerical attributes the value can be transformed by VALUE*X+A when
62                  return colIdx;           * presented on screen. TODO not implemented yet
63          }           **/
64            protected Double functionA = 0.;
65          public Translation getTitle() {  
66                  return title;          /**
67          }           * For numerical attributes the value can be transformed by VALUE*X+A when
68             * presented on screen. TODO not implemented yet
69          public void setTitle(final Translation title) {           **/
70                  this.title = title;          protected Double functionX = 1.;
71          }  
72            /** The Name of the attribute **/
73          public Translation getDesc() {          private Name name;
74                  return desc;  
75          }          /**
76             * Allows to define general NODATA values for an attribute. e.g. -9999 can
77          public void setDesc(final Translation desc) {           * be set and will always be interpreted as NULL internally and will usually
78                  this.desc = desc;           * be ignored. TODO not implemented yet
79          }           **/
80            protected List<Object> nodataValues = new ArrayList<Object>();
81          public String getUnit() {  
82                  return unit;          /** Translation of the attribute's title **/
83          }          protected Translation title = new Translation();
84    
85          public void setUnit(final String unit) {          /**
86                  this.unit = unit;           * The unit append to all visualizations of values of this attribute (is not
87          }           * null)
88  }           **/
89            protected String unit = "";
90    
91            /** Is the attribute visible to the user or ignored where possible **/
92            protected boolean visible = true;
93    
94            /**
95             * When listed, the attributes are listed according to their {@link #weight} (heavier
96             * => further down)
97             * @see #compareTo(AttributeMetadata)
98             **/
99            protected int weight = 0;
100    
101            /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/
102            private AttributeMetadata() {
103            }
104    
105            public AttributeMetadata(final AttributeDescriptor attDesc, final int weight,
106                            final List<String> langs) {
107                    this(attDesc.getName(), langs);
108                    setWeight(weight);
109            }
110    
111            public AttributeMetadata(final AttributeDescriptor attDesc, final List<String> langs) {
112                    this(attDesc.getName(), langs);
113            }
114    
115            /**
116             * Creates an {@link AttributeMetadata} object with the following
117             * information
118             *
119             * @param colIdx
120             *            The column index of this attribute in the underlying
121             *            table/dbf/etc...
122             * @param visible
123             *            Shall this attribute be displayed or hidden from the user?
124             * @param title
125             *            {@link Translation} for Name
126             * @param desc
127             *            {@link Translation} for an attribute description
128             * @param unit
129             *            {@link String} of the unit that the information is in
130             */
131            public AttributeMetadata(final Name name, final Boolean visible,
132                            final Translation title, final Translation desc, final String unit) {
133    
134                    this.setName(name);
135                    this.title = title;
136                    this.desc = desc;
137    //
138    //              // The THE_GEOM and shall never be visible!
139    //              if (name.getLocalPart().equalsIgnoreCase("the_geom"))
140    //                      this.visible = false;
141    //              else
142    //                      this.visible = visible;
143    
144                    this.unit = unit;
145            }
146    
147            /**
148             * Creates a new visible {@link AttributeMetadata}
149             */
150            public AttributeMetadata(final Name name, final List<String> langs) {
151                    this(name, true, new Translation(langs, name.getLocalPart()),
152                                    new Translation(), "");
153            }
154    
155            /**
156             * Creates a new visible {@link AttributeMetadata}
157             */
158            public AttributeMetadata(final Name name, final String defaultTitle,
159                            final List<String> langs) {
160                    this(name, true, new Translation(langs, defaultTitle),
161                                    new Translation(), "");
162            }
163    
164            /**
165             * Creates an {@link AttributeMetadata} object with the following
166             * information
167             *
168             * @param visible
169             *            Shall this attribute be displayed or hidden from the user?
170             * @param title
171             *            {@link Translation} for Name
172             * @param desc
173             *            {@link Translation} for an attribute description
174             * @param unit
175             *            {@link String} of the unit that the information is in
176             */
177            public AttributeMetadata(final String localName, final Boolean visible,
178                            final Translation title, final Translation desc, final String unit) {
179                    this(new NameImpl(localName), true, title, desc, "");
180            }
181    
182            /**
183             * Creates a new visible {@link AttributeMetadata} with default (no) values.
184             */
185            public AttributeMetadata(final String localName, final List<String> langs) {
186                    this(localName, true, new Translation(langs, localName),
187                                    new Translation(), "");
188            }
189    
190            /**
191             * Creates a new visible {@link AttributeMetadata}
192             */
193            public AttributeMetadata(final String localName, final String defaultTitle,
194                            final List<String> langs) {
195                    this(localName, true, new Translation(langs, defaultTitle),
196                                    new Translation(), "");
197            }
198    
199            /**
200             * Orders the attributes according to their {@link #weight}. Heavier =>
201             * further down.
202             */
203            @Override
204            public int compareTo(final AttributeMetadata atm2) {
205                    return new Integer(weight).compareTo(atm2.getWeight());
206            }
207    
208            /**
209             * @see Copyable inferface
210             */
211            @Override
212            public AttributeMetadata copy() {
213                    return copyTo(new AttributeMetadata());
214            }
215    
216            /**
217             * @see Copyable inferface
218             */
219            @Override
220            public AttributeMetadata copyTo(final AttributeMetadata amd) {
221                    getTitle().copyTo(amd.getTitle());
222                    getDesc().copyTo(amd.getDesc());
223                    amd.setUnit(getUnit());
224                    amd.setVisible(isVisible());
225                    amd.setName(new NameImpl(getName().getNamespaceURI(), getName()
226                                    .getLocalPart()));
227    
228                    amd.setWeight(getWeight());
229                    amd.setFunctionX(getFunctionX());
230                    amd.setFunctionA(getFunctionA());
231    
232                    for (final Object nodataValue : getNodataValues()) {
233                            amd.getNodataValues().add(nodataValue);
234                    }
235    
236                    return amd;
237            }
238    
239            public Translation getDesc() {
240                    return desc;
241            }
242    
243            public Double getFunctionA() {
244                    return functionA;
245            }
246    
247            public Double getFunctionX() {
248                    return functionX;
249            }
250    
251            /**
252             * The local name. E.g. the name of the DBF column as a {@link String}
253             */
254            public String getLocalName() {
255                    return getName().getLocalPart();
256            }
257    
258            /**
259             * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
260             */
261            public Name getName() {
262                    return name;
263            }
264    
265            public List<Object> getNodataValues() {
266                    return nodataValues;
267            }
268    
269            /**
270             * @return a number between 0 (bad) and 1 (good) that is calculated from the
271             *         amount of translation available. If this attribute is not
272             *         {@link #visible}, it will return 1.
273             */
274            public double getQuality(final List<String> languages) {
275    
276                    if (!isVisible())
277                            return 1.;
278    
279                    return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil
280                                    .qmTranslation(languages, getDesc()) * 1.) / 3.;
281            }
282    
283            public Translation getTitle() {
284                    return title;
285            }
286    
287            public String getUnit() {
288                    return unit;
289            }
290    
291            public int getWeight() {
292                    return weight;
293            }
294    
295            /**
296             * Will the end-user see this attribute?
297             */
298            public boolean isVisible() {
299                    return visible;
300            }
301    
302            public void setDesc(final Translation desc) {
303                    this.desc = desc;
304            }
305    
306            public void setFunctionA(final Double functionA) {
307                    this.functionA = functionA;
308            }
309    
310            public void setFunctionX(final Double functionX) {
311                    this.functionX = functionX;
312            }
313    
314            /**
315             * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
316             */
317            public void setLocalName(final String localName) {
318                    this.name = new NameImpl(localName);
319            }
320    
321            /**
322             * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
323             */
324            public void setName(final Name name) {
325                    this.name = name;
326            }
327    
328            public void setNodataValues(final List<Object> nodataValues) {
329                    this.nodataValues = nodataValues;
330            }
331    
332            public void setTitle(final Translation title) {
333                    this.title = title;
334            }
335    
336            public void setUnit(final String unit) {
337                    this.unit = unit;
338            }
339    
340            public void setVisible(final boolean visible) {
341                    this.visible = visible;
342            }
343    
344            /**
345             * Shall the end-user see this attribute?
346             * @param visible
347             */
348            public void setVisible(final Boolean visible) {
349    //              // The THE_GEOM and shall never be visible!
350    //              if (name.getLocalPart().equalsIgnoreCase("the_geom"))
351    //                      this.visible = false;
352    //              else
353    //                      this.visible = visible;
354    
355                    this.visible = visible;
356            }
357    
358            public void setWeight(final int weight) {
359                    this.weight = weight;
360            }
361            
362            /**
363             * For nicer debugging
364             */
365            @Override
366            public String toString() {
367                    StringBuffer sb = new StringBuffer();
368                    if (name != null) sb.append(name.toString()+" ");
369                    sb.append("weight="+weight+" ");
370                    sb.append("title="+getTitle().toString());
371                    return sb.toString();
372            }
373    }

Legend:
Removed from v.93  
changed lines
  Added in v.534

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26