001: /*
002: *****************************************************************************
003: * Copyright (C) 2000-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *****************************************************************************
006: */
007: package com.ibm.rbm;
008:
009: import java.io.IOException;
010: import java.io.PrintStream;
011: import java.io.Writer;
012: import java.util.*;
013:
014: /**
015: * A class representing a group of BundleItems and the meta data associated with that group
016: *
017: * @author Jared Jackson
018: * @see com.ibm.rbm.RBManager
019: */
020: public class BundleGroup {
021: private String name; // The name of the group
022: private String comment; // A comment describing this group
023: private TreeSet items; // The NLS items contained in this group
024: private Bundle bundle; // The parent Bundle object of this group
025:
026: /**
027: * Basic data constructor.
028: * Creates a BundleGroup with a parent bundle and a given name.
029: */
030: public BundleGroup(Bundle parent, String name) {
031: bundle = parent;
032: this .name = name;
033: comment = null;
034: items = new TreeSet(new Comparator() {
035: public boolean equals(Object o) {
036: return false;
037: }
038:
039: public int compare(Object o1, Object o2) {
040: if (!(o1 instanceof BundleItem)
041: || !(o2 instanceof BundleItem))
042: return 0;
043: BundleItem i1 = (BundleItem) o1;
044: BundleItem i2 = (BundleItem) o2;
045: return i1.getKey().compareTo(i2.getKey());
046: }
047: });
048: }
049:
050: /**
051: * Two bundle groups are considered equal iff their names are the same.
052: */
053: public boolean equals(Object o) {
054: return (o instanceof BundleGroup && ((BundleGroup) o).getName()
055: .equals(name));
056: }
057:
058: // This should be changed anywhere it is used
059:
060: public Vector getItemsAsVector() {
061: Vector v = new Vector();
062: Iterator iter = items.iterator();
063: while (iter.hasNext()) {
064: v.addElement(iter.next());
065: }
066: return v;
067: }
068:
069: /**
070: * Adds a BundleItem to the group as long as that item is not currently in the group.
071: * If the item.group is not equal to this group, then it is changed to be this group.
072: * This method should, in most cases, only be called from the Bundle class.
073: */
074: public void addBundleItem(BundleItem item) {
075: if (items.contains(item)) {
076: items.remove(item);
077: }
078: item.setParentGroup(this );
079: items.add(item);
080: }
081:
082: /**
083: * Remove an item of the given name from the group
084: */
085: public void removeBundleItem(String itemName) {
086: Iterator iter = items.iterator();
087: while (iter.hasNext()) {
088: BundleItem item = (BundleItem) iter.next();
089: if (item.getKey().equals(itemName)) {
090: items.remove(item);
091: break;
092: }
093: }
094: }
095:
096: /**
097: * Returns the number of items stored in the group
098: */
099: public int getItemCount() {
100: return items.size();
101: }
102:
103: /**
104: * Returns a BundleItem from the set of items at a particular index point.
105: * If the index is greater than or equal to the number of items in the set,
106: * null is returned.
107: */
108: public BundleItem getBundleItem(int index) {
109: if (index >= items.size())
110: return null;
111: Iterator iter = items.iterator();
112: for (int i = 0; i < index; i++)
113: iter.next();
114: return (BundleItem) iter.next();
115: }
116:
117: /**
118: * Returns the bundle to which this group belongs
119: */
120: public Bundle getParentBundle() {
121: return bundle;
122: }
123:
124: /**
125: * Returns the comment associated with this bundle
126: */
127: public String getComment() {
128: return comment;
129: }
130:
131: /**
132: * Returns the name of the bundle
133: */
134: public String getName() {
135: return name;
136: }
137:
138: protected void setParentBundle(Bundle bundle) {
139: this .bundle = bundle;
140: }
141:
142: public void setComment(String comment) {
143: this .comment = comment;
144: }
145:
146: public void setName(String name) {
147: this .name = name;
148: }
149:
150: /**
151: * The translation to a string returns the name of the group
152: */
153: public String toString() {
154: return name;
155: }
156:
157: /**
158: * Returns the output for a group heading.
159: * This will be found in comment lines above the group items
160: */
161: public String toOutputString() {
162: String retStr = "\n#\n# @group " + name + "\n#\n";
163: if (comment != null)
164: retStr += "# @groupComment " + comment + "\n";
165: return retStr;
166: }
167:
168: /**
169: * Writes the output contents to a particular PrintStream.
170: * The output will be suitable for a properly formatted .properties file.
171: */
172: public void writeContents(PrintStream ps) {
173: if (!name.equals("Ungrouped Items"))
174: ps.println(this .toOutputString());
175: Iterator iter = items.iterator();
176: while (iter.hasNext()) {
177: ((BundleItem) iter.next()).writeContents(ps);
178: }
179: }
180:
181: /**
182: * Writes the output contents to a particular Writer.
183: * The output will be suitable for a properly formatted .properties file.
184: */
185: public void writeContents(Writer w) throws IOException {
186: if (!name.equals("Ungrouped Items"))
187: w.write(this .toOutputString() + "\n");
188: Iterator iter = items.iterator();
189: while (iter.hasNext()) {
190: ((BundleItem) iter.next()).writeContents(w);
191: }
192: }
193: }
|