12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text.html; 26 27 import javax.swing.text.*; 28 import java.io.Serializable; 29 import java.util.*; 30 31 /** 32 * An implementation of <code>AttributeSet</code> that can multiplex 33 * across a set of <code>AttributeSet</code>s. 34 * 35 */ 36 @SuppressWarnings("serial") // Same-version serialization only 37 class MuxingAttributeSet implements AttributeSet, Serializable { 38 /** 39 * Creates a <code>MuxingAttributeSet</code> with the passed in 40 * attributes. 41 */ 42 public MuxingAttributeSet(AttributeSet[] attrs) { 43 this.attrs = attrs; 44 } 45 46 /** 47 * Creates an empty <code>MuxingAttributeSet</code>. This is intended for 48 * use by subclasses only, and it is also intended that subclasses will 49 * set the constituent <code>AttributeSet</code>s before invoking any 50 * of the <code>AttributeSet</code> methods. 51 */ 52 protected MuxingAttributeSet() { 53 } 54 55 /** 56 * Directly sets the <code>AttributeSet</code>s that comprise this 57 * <code>MuxingAttributeSet</code>. 58 */ 59 protected synchronized void setAttributes(AttributeSet[] attrs) { 60 this.attrs = attrs; 61 } 62 63 /** 64 * Returns the <code>AttributeSet</code>s multiplexing too. When the 65 * <code>AttributeSet</code>s need to be referenced, this should be called. 66 */ 67 protected synchronized AttributeSet[] getAttributes() { 68 return attrs; 69 } 70 71 /** 72 * Inserts <code>as</code> at <code>index</code>. This assumes 73 * the value of <code>index</code> is between 0 and attrs.length, 74 * inclusive. 75 */ 76 protected synchronized void insertAttributeSetAt(AttributeSet as, 77 int index) { 78 int numAttrs = attrs.length; 79 AttributeSet newAttrs[] = new AttributeSet[numAttrs + 1]; 80 if (index < numAttrs) { 81 if (index > 0) { 82 System.arraycopy(attrs, 0, newAttrs, 0, index); 83 System.arraycopy(attrs, index, newAttrs, index + 1, 84 numAttrs - index); 85 } 86 else { 87 System.arraycopy(attrs, 0, newAttrs, 1, numAttrs); 88 } 89 } 90 else { 91 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs); 92 } 93 newAttrs[index] = as; 94 attrs = newAttrs; 95 } 96 97 /** 98 * Removes the AttributeSet at <code>index</code>. This assumes 99 * the value of <code>index</code> is greater than or equal to 0, 100 * and less than attrs.length. 101 */ 102 protected synchronized void removeAttributeSetAt(int index) { 103 int numAttrs = attrs.length; 104 AttributeSet[] newAttrs = new AttributeSet[numAttrs - 1]; 105 if (numAttrs > 0) { 106 if (index == 0) { 107 // FIRST 108 System.arraycopy(attrs, 1, newAttrs, 0, numAttrs - 1); 109 } 110 else if (index < (numAttrs - 1)) { 111 // MIDDLE 112 System.arraycopy(attrs, 0, newAttrs, 0, index); 113 System.arraycopy(attrs, index + 1, newAttrs, index, 114 numAttrs - index - 1); 115 } 116 else { 117 // END 118 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs - 1); 119 } 241 boolean result = true; 242 243 Enumeration<?> names = attrs.getAttributeNames(); 244 while (result && names.hasMoreElements()) { 245 Object name = names.nextElement(); 246 result = attrs.getAttribute(name).equals(getAttribute(name)); 247 } 248 249 return result; 250 } 251 252 /** 253 * Returns null, subclasses may wish to do something more 254 * intelligent with this. 255 */ 256 public AttributeSet getResolveParent() { 257 return null; 258 } 259 260 /** 261 * The <code>AttributeSet</code>s that make up the resulting 262 * <code>AttributeSet</code>. 263 */ 264 private AttributeSet[] attrs; 265 266 267 /** 268 * An Enumeration of the Attribute names in a MuxingAttributeSet. 269 * This may return the same name more than once. 270 */ 271 private class MuxingAttributeNameEnumeration implements Enumeration<Object> { 272 273 MuxingAttributeNameEnumeration() { 274 updateEnum(); 275 } 276 277 public boolean hasMoreElements() { 278 if (currentEnum == null) { 279 return false; 280 } 281 return currentEnum.hasMoreElements(); 282 } | 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text.html; 26 27 import javax.swing.text.*; 28 import java.io.Serializable; 29 import java.util.*; 30 31 /** 32 * An implementation of {@code AttributeSet} that can multiplex 33 * across a set of {@code AttributeSet}s. 34 * 35 */ 36 @SuppressWarnings("serial") // Same-version serialization only 37 class MuxingAttributeSet implements AttributeSet, Serializable { 38 /** 39 * Creates a {@code MuxingAttributeSet} with the passed in 40 * attributes. 41 */ 42 public MuxingAttributeSet(AttributeSet[] attrs) { 43 this.attrs = attrs; 44 } 45 46 /** 47 * Creates an empty {@code MuxingAttributeSet}. This is intended for 48 * use by subclasses only, and it is also intended that subclasses will 49 * set the constituent {@code AttributeSet}s before invoking any 50 * of the {@code AttributeSet} methods. 51 */ 52 protected MuxingAttributeSet() { 53 } 54 55 /** 56 * Directly sets the {@code AttributeSet}s that comprise this 57 * {@code MuxingAttributeSet}. 58 */ 59 protected synchronized void setAttributes(AttributeSet[] attrs) { 60 this.attrs = attrs; 61 } 62 63 /** 64 * Returns the {@code AttributeSet}s multiplexing too. When the 65 * {@code AttributeSet}s need to be referenced, this should be called. 66 */ 67 protected synchronized AttributeSet[] getAttributes() { 68 return attrs; 69 } 70 71 /** 72 * Inserts {@code as} at {@code index}. This assumes 73 * the value of {@code index} is between 0 and attrs.length, 74 * inclusive. 75 */ 76 protected synchronized void insertAttributeSetAt(AttributeSet as, 77 int index) { 78 int numAttrs = attrs.length; 79 AttributeSet newAttrs[] = new AttributeSet[numAttrs + 1]; 80 if (index < numAttrs) { 81 if (index > 0) { 82 System.arraycopy(attrs, 0, newAttrs, 0, index); 83 System.arraycopy(attrs, index, newAttrs, index + 1, 84 numAttrs - index); 85 } 86 else { 87 System.arraycopy(attrs, 0, newAttrs, 1, numAttrs); 88 } 89 } 90 else { 91 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs); 92 } 93 newAttrs[index] = as; 94 attrs = newAttrs; 95 } 96 97 /** 98 * Removes the AttributeSet at {@code index}. This assumes 99 * the value of {@code index} is greater than or equal to 0, 100 * and less than attrs.length. 101 */ 102 protected synchronized void removeAttributeSetAt(int index) { 103 int numAttrs = attrs.length; 104 AttributeSet[] newAttrs = new AttributeSet[numAttrs - 1]; 105 if (numAttrs > 0) { 106 if (index == 0) { 107 // FIRST 108 System.arraycopy(attrs, 1, newAttrs, 0, numAttrs - 1); 109 } 110 else if (index < (numAttrs - 1)) { 111 // MIDDLE 112 System.arraycopy(attrs, 0, newAttrs, 0, index); 113 System.arraycopy(attrs, index + 1, newAttrs, index, 114 numAttrs - index - 1); 115 } 116 else { 117 // END 118 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs - 1); 119 } 241 boolean result = true; 242 243 Enumeration<?> names = attrs.getAttributeNames(); 244 while (result && names.hasMoreElements()) { 245 Object name = names.nextElement(); 246 result = attrs.getAttribute(name).equals(getAttribute(name)); 247 } 248 249 return result; 250 } 251 252 /** 253 * Returns null, subclasses may wish to do something more 254 * intelligent with this. 255 */ 256 public AttributeSet getResolveParent() { 257 return null; 258 } 259 260 /** 261 * The {@code AttributeSet}s that make up the resulting 262 * {@code AttributeSet}. 263 */ 264 private AttributeSet[] attrs; 265 266 267 /** 268 * An Enumeration of the Attribute names in a MuxingAttributeSet. 269 * This may return the same name more than once. 270 */ 271 private class MuxingAttributeNameEnumeration implements Enumeration<Object> { 272 273 MuxingAttributeNameEnumeration() { 274 updateEnum(); 275 } 276 277 public boolean hasMoreElements() { 278 if (currentEnum == null) { 279 return false; 280 } 281 return currentEnum.hasMoreElements(); 282 } |