📄 metadata.java
字号:
/* * $Id: MetaData.java,v 1.3 2005/10/10 17:01:09 rbair Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library 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 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package org.jdesktop.binding.metadata;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;/** * <p> * Class for representing the meta-data for an field in a data model. * A "field" may map to a column on a RowSet, a property on a JavaBean, * or some other discrete data element on a data model. * The meta-data describes aspects of a data field such as it's name, * type, and edit constraints. This class may be used when such information * isn't encapsulated in the data model object itself and thus must be represented * in an external object. Meta-data is intended only for holding state about * a data model element and is not intended for implementing application semantics.</p> * <p> * A meta-data object should be initialized at a minimum with a name, class, * and label. Additional meta-data properties can be set as necessary. * For example:<br> * <pre><code> * MetaData metaData = new MetaData("firstname", String.class, * "First Name"); * metaData.setRequired(true); * </code></pre> * If the associated data model field requires application-specific validation * logic to verify the correctness of a potential value, one or more Validator * instances can be added to the meta-data object. Example:<br> * <pre><code> * MetaData metaData = new MetaData("creditcard", String.class, * "Credit Card Number" * metaData.setRequired(true); * metaData.addValidator(new MyCreditCardValidator()); * </code></pre> * </p> * * @author Amy Fowler * @author Rich Bair * @version 1.0 */public class MetaData { protected String name; protected Class klass = String.class; protected String label; protected Converter converter = null; protected Object decodeFormat = null; protected Object encodeFormat = null; protected boolean readOnly = false; protected int minValueCount = 0; // null value okay protected int maxValueCount = 1; // only one value allowed protected int displayWidth = 24; protected ArrayList validators = null; protected Map customProps = new HashMap(); protected PropertyChangeSupport pcs; /** * Instantiates a meta-data object with a default name "value" and * a default field class equal to <code>java.lang.String</code>. * This provides the no-argument constructor required for JavaBeans. * It is recommended that the program explicitly set a meaningful * "name" property. */ public MetaData() { this("value"); } /** * Instantiates a meta-data object with the specified name and * a default field class equal to <code>java.lang.String</code>. * @param name String containing the name of the data field */ public MetaData(String name) { this.name = name; } /** * Instantiates a meta-data object with the specified name and * field class. * @param name String containing the name of the data field * @param klass Class indicating type of data field */ public MetaData(String name, Class klass) { this(name); this.klass = klass; } /** * Instantiates a meta-data object with the specified name, * field class, and label. * @param name String containing the name of the data field * @param klass Class indicating type of data field * @param label String containing the user-displayable label for the * data field */ public MetaData(String name, Class klass, String label) { this(name, klass); this.label = label; } /** * Gets the meta-data "name" property which indicates * the logical name of the associated data field. * @see #setName * @return String containing the name of the data field. */ public String getName() { return name; } /** * Sets the meta-data "name" property. * @see #getName * @param name String containing the name of the data field */ public void setName(String name) { String oldName = this.name; this.name = name; firePropertyChange("name", oldName, name); } /** * Gets the meta-data's "elementClass" property which * indicates the type of the associated data field. * The default field class is <code>java.lang.String</code>. * @see #setElementClass * @return Class indicating type of data field */ public Class getElementClass() { return klass; } /** * Sets the meta-data's "elementClass" property. * This <code>set</code> method is provided for meta-data bean initialization * only; the field class is not intended to be modified after initialization, * since other aspects of a meta-data object may depend on this type setting. * @see #getElementClass * @param klass Class indicating type of data field */ public void setElementClass(Class klass) { Class oldClass = this.klass; this.klass = klass; firePropertyChange("elementClass", oldClass, klass); } /** * Gets the meta-data's "label" property, which provides * a label for the associated data field. The label is intended * for display to the end-user and may be localized. * If no label has been explicitly set, then the meta-data's name is * returned. * @see #setLabel * @return String containing the user-displayable label for the * data field */ public String getLabel() { return label == null? name : label; } /** * Sets the meta-data's "label" property. * @todo Rename to setTitle * * @see #getLabel * @param label String containing the user-displable label for the * data field */ public void setLabel(String label) { String oldLabel = this.label; this.label = label; firePropertyChange("label", oldLabel, label); } /** * Gets the meta-data's converter, which performs conversions between * string values and objects of the associated data field's type. * If no converter was explicitly set on this meta-data object, * this method will retrieve a default converter for this data * field's type from the converter registry. If no default converter * is registered, this method will return <code>null</code>. * @see Converters#get * @see #setConverter * @see #getElementClass * @return Converter used to perform conversions between string values * and objects of this field's type */ public Converter getConverter() { if (converter == null) { return Converters.get(klass); } return converter; } /** * Sets the meta-data's converter. * * @see #getConverter * @param converter Converter used to perform conversions between string values * and objects of this field's type */ public void setConverter(Converter converter) { Converter oldConverter = this.converter; this.converter = converter; firePropertyChange("converter", oldConverter, converter); } /** * Gets the meta-data's decode format which is used when converting * values from a string representation. This property must be used when * conversion requires format information on how the string representation * is structured. For example, a decode format should be used when decoding * date values. The default decode format is <code>null</code>. * @see #setDecodeFormat * @return format object used to describe format for string-to-object conversion */ public Object getDecodeFormat() { return decodeFormat; } /** * Sets the meta-data's decode format which is used when converting * values from a string representation. * @see #getDecodeFormat * @see java.text.DateFormat * @param format object used to describe format for string-to-object conversion */ public void setDecodeFormat(Object format) { Object oldDecodeFormat = this.decodeFormat; this.decodeFormat = format; firePropertyChange("decodeFormat", oldDecodeFormat, format); } /** * Gets the meta-data's encode format which is used when converting * values to a string representation. This property must be used when * conversion requires format information on how the string representation * should be generated. For example, an encode format should be used when * encoding date values. The default encode format is <code>null</code>. * @see #setEncodeFormat * @return format object used to describe format for object-to-string conversion */ public Object getEncodeFormat() { return encodeFormat; } /** * Sets the meta-data's encode format which is used when converting * values to a string representation. * @see #getEncodeFormat * @see java.text.DateFormat * @param format object used to describe format for object-to-string conversion */ public void setEncodeFormat(Object format) { Object oldEncodeFormat = this.encodeFormat; this.encodeFormat = format; firePropertyChange("encodeFormat", oldEncodeFormat, format); } /** * @return integer containing the number of characters required to * display the value */ public int getDisplayWidth() { return displayWidth;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -