001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.model2;
024:
025: /**
026: * This class contains information about a property. The property may be in the base
027: * bookkeeping class or in an extension bookkeeping class.
028: *
029: * This object contains the metadata.
030: *
031: * Some properties may be allowed to take null values and
032: * some may not. The determination is made by the JMoney framework
033: * by looking at the type returned by the getter method.
034: * <P>
035: * The following properties may take null values:
036: * <UL>
037: * <LI>All properties that reference extendable objects</LI>
038: * <LI>All properties of type String, Date or other such simple class</LI>
039: * <LI>All properties where the type is one of the classes representing intrinsic types,
040: * such as Integer, Long</LI>
041: * </UL>
042: * The following properties may not take null values:
043: * <UL>
044: * <LI>All properties where the type is an intrinsic type, such as int, long</LI>
045: * </UL>
046: *
047: * @author Nigel Westbury
048: */
049: public abstract class PropertyAccessor {
050:
051: protected PropertySet propertySet;
052:
053: protected String localName;
054:
055: protected String displayName;
056:
057: /**
058: * Index into the list of parameters passed to the constructor.
059: * Zero indicates that this property is passed as the first
060: * parameter to the constructor.
061: *
062: */
063: protected int indexIntoConstructorParameters = -1;
064:
065: public PropertyAccessor(PropertySet propertySet, String localName,
066: String displayName) {
067: this .propertySet = propertySet;
068: this .localName = localName;
069: this .displayName = displayName;
070: }
071:
072: /**
073: * Create a property accessor for a list property.
074: *
075: * @param set
076: * @param name
077: * @param listItemClass
078: * @param displayName
079: * @param propertyDependency
080: */
081: public PropertyAccessor(PropertySet propertySet, String localName,
082: String displayName,
083: Class<? extends ExtendableObject> listItemClass) {
084: this .propertySet = propertySet;
085: this .localName = localName;
086: this .displayName = displayName;
087: }
088:
089: /**
090: * Returns the property set which contains this property.
091: */
092: // TODO: Check all the uses of this. Some of the uses require
093: // that for extension property sets, the property set being
094: // extended should be returned. This saves the caller from having
095: // to test the property set.
096: public PropertySet getPropertySet() {
097: return propertySet;
098: }
099:
100: /**
101: * Returns the extendable property set which contains this property.
102: *
103: * If the property is in an extendable property set then this
104: * method returns the same value as <code>getPropertySet()</code>.
105: * If the property is in an extension property set then
106: * the property set being extended is returned.
107: */
108: // TODO: Consider removing this method. It is not used.
109: /*
110: public PropertySet getExtendablePropertySet() {
111: if (propertySet.isExtension()) {
112: return propertySet.getExtendablePropertySet();
113: } else {
114: return propertySet;
115: }
116: }
117: */
118: /**
119: * Returns the PropertySet for the values of this property.
120: * This property must contain a value or values that are
121: * extendable objects.
122: */
123: /* not used.
124: public PropertySet getValuePropertySet() {
125: for (Iterator iter = PropertySet.getPropertySetIterator(); iter.hasNext(); ) {
126: PropertySet propertySet = (PropertySet)iter.next();
127: if (!propertySet.isExtension()) {
128: if (propertySet.getImplementationClass() == propertyClass) {
129: return propertySet;
130: }
131: }
132: }
133:
134: throw new RuntimeException("No property set found for extendable class object" + propertyClass.getName() + ".");
135: }
136: */
137: /**
138: * Return a name for this property.
139: *
140: * This name is used by the framework for persisting information about the property
141: * in configuration files etc. For example, if the user sorts a column based on a
142: * property then that information can be stored in a configuration file so that the
143: * data is sorted on the column the next time the user loads the view.
144: */
145: public String getName() {
146: // We must uniquify the name, so prepend the property set id.
147: // TODO: this does not uniquify the name because a property may
148: // exist with the same name as both, say, an account property
149: // and an entry property. We should probably add the base
150: // class name here too.
151: return propertySet.toString() + "." + localName;
152: }
153:
154: /**
155: * The local name of the property is just the last part of the name, after
156: * the last dot. This will be unique within an extension but may not be
157: * unique across all plugins or even across extensions to different types of
158: * bookkeeping objects (entries, categories, transactions, or commodities)
159: * within a plug-in.
160: */
161: public String getLocalName() {
162: return localName;
163: }
164:
165: /**
166: * A short description that is suitable as a column header when this
167: * property is displayed in a table.
168: */
169: public String getDisplayName() {
170: return displayName;
171: }
172:
173: /**
174: * Indicates if the property is a list of intrinsic values or objects.
175: */
176: public abstract boolean isList();
177:
178: /**
179: * Indicates if the property is a single intrinsic value or object
180: * (not a list of values)
181: */
182: public boolean isScalar() {
183: return !isList();
184: }
185:
186: /**
187: * @return the index into the constructor parameters, where
188: * an index of zero indicates that the property is the
189: * first parameter to the constructor. An index of -1
190: * indicates that the property is not passed to the
191: * constructor (the property value is redundant and the
192: * object can be fully re-constructed from the other
193: * properties).
194: */
195: public int getIndexIntoConstructorParameters() {
196: return indexIntoConstructorParameters;
197: }
198:
199: /**
200: *
201: * @param indexIntoConstructorParameters
202: */
203: // TODO: This method should be accessible only from within the package.
204: public void setIndexIntoConstructorParameters(
205: int indexIntoConstructorParameters) {
206: this.indexIntoConstructorParameters = indexIntoConstructorParameters;
207: }
208: }
|