001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: PropertyGroup.java,v 1.6 2004/09/22 14:32:50 jesper Exp $
023: package net.infonode.properties.base;
024:
025: import java.util.ArrayList;
026:
027: /**
028: * A group of properties. The group have a name and a description. It can also have a super group from which it inherit
029: * all it's properties. You can think of a property group as similar to a Java class, and properties similar to class
030: * fields.
031: *
032: * @author $Author: jesper $
033: * @version $Revision: 1.6 $
034: */
035: public class PropertyGroup {
036: private PropertyGroup super Group;
037: private String name;
038: private String description;
039: private ArrayList properties = new ArrayList(10);
040:
041: /**
042: * Creates a property group.
043: *
044: * @param name the name of the group
045: * @param description the group description
046: */
047: public PropertyGroup(String name, String description) {
048: this .name = name;
049: this .description = description;
050: }
051:
052: /**
053: * Creates a property group with a super group.
054: * All properties in the super group will be inherited to this group.
055: *
056: * @param superGroup the super group of this group
057: * @param name the name of the group
058: * @param description the group description
059: */
060: public PropertyGroup(PropertyGroup super Group, String name,
061: String description) {
062: this (name, description);
063: this .super Group = super Group;
064: }
065:
066: /**
067: * Returns the super group of this group.
068: *
069: * @return the super group of this group, null if it has no super group
070: */
071: public PropertyGroup getSuperGroup() {
072: return super Group;
073: }
074:
075: /**
076: * Returns the description for this group.
077: *
078: * @return the description for this group
079: */
080: public String getDescription() {
081: return description;
082: }
083:
084: /**
085: * Returns the name of this group.
086: *
087: * @return the name of this group
088: */
089: public String getName() {
090: return name;
091: }
092:
093: /**
094: * Add a property to this group.
095: *
096: * @param property the property to add
097: */
098: public void addProperty(Property property) {
099: properties.add(property);
100: }
101:
102: /**
103: * Returns the number of properties in this group.
104: * This does not include properties in super groups.
105: *
106: * @return the number of properties in this group
107: */
108: public int getPropertyCount() {
109: return properties.size();
110: }
111:
112: /**
113: * Returns true if this group or one of it's super groups contains the property.
114: *
115: * @param property the property
116: * @return true if this group or one of it's super groups contains the property
117: */
118: public boolean hasProperty(Property property) {
119: return isA(property.getGroup());
120: }
121:
122: /**
123: * Returns the property at the index,
124: * This does not include properties in super groups.
125: *
126: * @param index the property index
127: * @return the property at the index
128: */
129: public Property getProperty(int index) {
130: return (Property) properties.get(index);
131: }
132:
133: /**
134: * Returns an array with the properties in this group.
135: * This does not include properties in super groups.
136: *
137: * @return an array with the properties in this group
138: */
139: public Property[] getProperties() {
140: return (Property[]) properties.toArray(new Property[properties
141: .size()]);
142: }
143:
144: public String toString() {
145: return getName();
146: }
147:
148: /**
149: * Returns the property with the given name.
150: * This includes properties in super groups.
151: *
152: * @param name the property name
153: * @return the property with the given name, null if no property was found
154: */
155: public Property getProperty(String name) {
156: for (int i = 0; i < getPropertyCount(); i++) {
157: if (getProperty(i).getName().equals(name))
158: return getProperty(i);
159: }
160:
161: return super Group == null ? null : super Group.getProperty(name);
162: }
163:
164: /**
165: * Returns true if the group is this group or one of it's super groups.
166: *
167: * @param group the group
168: * @return true if the group is this group or one of it's super groups
169: */
170: private boolean isA(PropertyGroup group) {
171: return group == this
172: || (getSuperGroup() != null && getSuperGroup().isA(
173: group));
174: }
175:
176: }
|