1 /* 2 ***************************************************************************** 3 * Copyright (C) 2000-2004, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ***************************************************************************** 6 */ 7 package com.ibm.rbm; 8 9 import java.io.IOException; 10 import java.io.PrintStream; 11 import java.io.Writer; 12 import java.util.*; 13 14 /** 15 * A class representing a group of BundleItems and the meta data associated with that group 16 * 17 * @author Jared Jackson 18 * @see com.ibm.rbm.RBManager 19 */ 20 public class BundleGroup { 21 private String name; // The name of the group 22 private String comment; // A comment describing this group 23 private TreeSet items; // The NLS items contained in this group 24 private Bundle bundle; // The parent Bundle object of this group 25 26 /** 27 * Basic data constructor. 28 * Creates a BundleGroup with a parent bundle and a given name. 29 */ BundleGroup(Bundle parent, String name)30 public BundleGroup(Bundle parent, String name) { 31 bundle = parent; 32 this.name = name; 33 comment = null; 34 items = new TreeSet(new Comparator(){ 35 public boolean equals(Object o) { return false; } 36 public int compare(Object o1, Object o2) { 37 if (!(o1 instanceof BundleItem) || !(o2 instanceof BundleItem)) 38 return 0; 39 BundleItem i1 = (BundleItem)o1; 40 BundleItem i2 = (BundleItem)o2; 41 return i1.getKey().compareTo(i2.getKey()); 42 } 43 }); 44 } 45 46 /** 47 * Two bundle groups are considered equal iff their names are the same. 48 */ equals(Object o)49 public boolean equals(Object o) { 50 return (o instanceof BundleGroup && ((BundleGroup)o).getName().equals(name)); 51 } 52 53 // This should be changed anywhere it is used 54 getItemsAsVector()55 public Vector getItemsAsVector() { 56 Vector v = new Vector(); 57 Iterator iter = items.iterator(); 58 while (iter.hasNext()) { 59 v.addElement(iter.next()); 60 } 61 return v; 62 } 63 64 /** 65 * Adds a BundleItem to the group as long as that item is not currently in the group. 66 * If the item.group is not equal to this group, then it is changed to be this group. 67 * This method should, in most cases, only be called from the Bundle class. 68 */ addBundleItem(BundleItem item)69 public void addBundleItem(BundleItem item) { 70 if (items.contains(item)) { 71 items.remove(item); 72 } 73 item.setParentGroup(this); 74 items.add(item); 75 } 76 77 /** 78 * Remove an item of the given name from the group 79 */ removeBundleItem(String itemName)80 public void removeBundleItem(String itemName) { 81 Iterator iter = items.iterator(); 82 while(iter.hasNext()) { 83 BundleItem item = (BundleItem)iter.next(); 84 if (item.getKey().equals(itemName)) { 85 items.remove(item); 86 break; 87 } 88 } 89 } 90 91 /** 92 * Returns the number of items stored in the group 93 */ getItemCount()94 public int getItemCount() { 95 return items.size(); 96 } 97 98 /** 99 * Returns a BundleItem from the set of items at a particular index point. 100 * If the index is greater than or equal to the number of items in the set, 101 * null is returned. 102 */ getBundleItem(int index)103 public BundleItem getBundleItem(int index) { 104 if (index >= items.size()) 105 return null; 106 Iterator iter = items.iterator(); 107 for (int i=0; i < index; i++) 108 iter.next(); 109 return (BundleItem)iter.next(); 110 } 111 112 /** 113 * Returns the bundle to which this group belongs 114 */ getParentBundle()115 public Bundle getParentBundle() { 116 return bundle; 117 } 118 119 /** 120 * Returns the comment associated with this bundle 121 */ getComment()122 public String getComment() { 123 return comment; 124 } 125 126 /** 127 * Returns the name of the bundle 128 */ getName()129 public String getName() { 130 return name; 131 } 132 setParentBundle(Bundle bundle)133 protected void setParentBundle(Bundle bundle) { 134 this.bundle = bundle; 135 } 136 setComment(String comment)137 public void setComment(String comment) { 138 this.comment = comment; 139 } 140 setName(String name)141 public void setName(String name) { 142 this.name = name; 143 } 144 145 /** 146 * The translation to a string returns the name of the group 147 */ toString()148 public String toString() { 149 return name; 150 } 151 152 /** 153 * Returns the output for a group heading. 154 * This will be found in comment lines above the group items 155 */ toOutputString()156 public String toOutputString() { 157 String retStr = "\n#\n# @group " + name + "\n#\n"; 158 if (comment != null) 159 retStr += "# @groupComment " + comment + "\n"; 160 return retStr; 161 } 162 163 /** 164 * Writes the output contents to a particular PrintStream. 165 * The output will be suitable for a properly formatted .properties file. 166 */ writeContents(PrintStream ps)167 public void writeContents(PrintStream ps) { 168 if (!name.equals("Ungrouped Items")) 169 ps.println(this.toOutputString()); 170 Iterator iter = items.iterator(); 171 while (iter.hasNext()) { 172 ((BundleItem) iter.next()).writeContents(ps); 173 } 174 } 175 176 /** 177 * Writes the output contents to a particular Writer. 178 * The output will be suitable for a properly formatted .properties file. 179 */ writeContents(Writer w)180 public void writeContents(Writer w) throws IOException { 181 if (!name.equals("Ungrouped Items")) 182 w.write(this.toOutputString() + "\n"); 183 Iterator iter = items.iterator(); 184 while (iter.hasNext()) { 185 ((BundleItem) iter.next()).writeContents(w); 186 } 187 } 188 }