1 7 package javax.swing.text; 8 9 import java.awt.*; 10 import java.util.*; 11 import java.io.*; 12 13 import javax.swing.SwingUtilities ; 14 import javax.swing.event.ChangeListener ; 15 import javax.swing.event.EventListenerList ; 16 import javax.swing.event.ChangeEvent ; 17 import java.lang.ref.WeakReference ; 18 import java.util.WeakHashMap ; 19 20 import sun.font.FontManager; 21 22 48 public class StyleContext implements Serializable, AbstractDocument.AttributeContext { 49 50 56 public static final StyleContext getDefaultStyleContext() { 57 if (defaultContext == null) { 58 defaultContext = new StyleContext (); 59 } 60 return defaultContext; 61 } 62 63 private static StyleContext defaultContext; 64 65 68 public StyleContext() { 69 styles = new NamedStyle(null); 70 addStyle(DEFAULT_STYLE, null); 71 } 72 73 89 public Style addStyle(String nm, Style parent) { 90 Style style = new NamedStyle(nm, parent); 91 if (nm != null) { 92 styles.addAttribute(nm, style); 94 } 95 return style; 96 } 97 98 103 public void removeStyle(String nm) { 104 styles.removeAttribute(nm); 105 } 106 107 113 public Style getStyle(String nm) { 114 return (Style ) styles.getAttribute(nm); 115 } 116 117 122 public Enumeration<?> getStyleNames() { 123 return styles.getAttributeNames(); 124 } 125 126 132 public void addChangeListener(ChangeListener l) { 133 styles.addChangeListener(l); 134 } 135 136 142 public void removeChangeListener(ChangeListener l) { 143 styles.removeChangeListener(l); 144 } 145 146 154 public ChangeListener [] getChangeListeners() { 155 return ((NamedStyle)styles).getChangeListeners(); 156 } 157 158 168 public Font getFont(AttributeSet attr) { 169 int style = Font.PLAIN; 171 if (StyleConstants.isBold(attr)) { 172 style |= Font.BOLD; 173 } 174 if (StyleConstants.isItalic(attr)) { 175 style |= Font.ITALIC; 176 } 177 String family = StyleConstants.getFontFamily(attr); 178 int size = StyleConstants.getFontSize(attr); 179 180 185 if (StyleConstants.isSuperscript(attr) || 186 StyleConstants.isSubscript(attr)) { 187 size -= 2; 188 } 189 190 return getFont(family, style, size); 191 } 192 193 202 public Color getForeground(AttributeSet attr) { 203 return StyleConstants.getForeground(attr); 204 } 205 206 215 public Color getBackground(AttributeSet attr) { 216 return StyleConstants.getBackground(attr); 217 } 218 219 230 public Font getFont(String family, int style, int size) { 231 fontSearch.setValue(family, style, size); 232 Font f = (Font) fontTable.get(fontSearch); 233 if (f == null) { 234 Style defaultStyle = 236 getStyle(StyleContext.DEFAULT_STYLE); 237 if (defaultStyle != null) { 238 final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY"; 239 Font defaultFont = 240 (Font) defaultStyle.getAttribute(FONT_ATTRIBUTE_KEY); 241 if (defaultFont != null 242 && defaultFont.getFamily().equalsIgnoreCase(family)) { 243 f = defaultFont.deriveFont(style, size); 244 } 245 } 246 if (f == null) { 247 f = new Font(family, style, size); 248 } 249 if (! FontManager.fontSupportsDefaultEncoding(f)) { 250 f = FontManager.getCompositeFontUIResource(f); 251 } 252 FontKey key = new FontKey(family, style, size); 253 fontTable.put(key, f); 254 } 255 return f; 256 } 257 258 264 public FontMetrics getFontMetrics(Font f) { 265 return Toolkit.getDefaultToolkit().getFontMetrics(f); 268 } 269 270 272 287 public synchronized AttributeSet addAttribute(AttributeSet old, Object name, Object value) { 288 if ((old.getAttributeCount() + 1) <= getCompressionThreshold()) { 289 search.removeAttributes(search); 292 search.addAttributes(old); 293 search.addAttribute(name, value); 294 reclaim(old); 295 return getImmutableUniqueSet(); 296 } 297 MutableAttributeSet ma = getMutableAttributeSet(old); 298 ma.addAttribute(name, value); 299 return ma; 300 } 301 302 315 public synchronized AttributeSet addAttributes(AttributeSet old, AttributeSet attr) { 316 if ((old.getAttributeCount() + attr.getAttributeCount()) <= getCompressionThreshold()) { 317 search.removeAttributes(search); 320 search.addAttributes(old); 321 search.addAttributes(attr); 322 reclaim(old); 323 return getImmutableUniqueSet(); 324 } 325 MutableAttributeSet ma = getMutableAttributeSet(old); 326 ma.addAttributes(attr); 327 return ma; 328 } 329 330 343 public synchronized AttributeSet removeAttribute(AttributeSet old, Object name) { 344 if ((old.getAttributeCount() - 1) <= getCompressionThreshold()) { 345 search.removeAttributes(search); 348 search.addAttributes(old); 349 search.removeAttribute(name); 350 reclaim(old); 351 return getImmutableUniqueSet(); 352 } 353 MutableAttributeSet ma = getMutableAttributeSet(old); 354 ma.removeAttribute(name); 355 return ma; 356 } 357 358 371 public synchronized AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names) { 372 if (old.getAttributeCount() <= getCompressionThreshold()) { 373 search.removeAttributes(search); 376 search.addAttributes(old); 377 search.removeAttributes(names); 378 reclaim(old); 379 return getImmutableUniqueSet(); 380 } 381 MutableAttributeSet ma = getMutableAttributeSet(old); 382 ma.removeAttributes(names); 383 return ma; 384 } 385 386 399 public synchronized AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) { 400 if (old.getAttributeCount() <= getCompressionThreshold()) { 401 search.removeAttributes(search); 404 search.addAttributes(old); 405 search.removeAttributes(attrs); 406 reclaim(old); 407 return getImmutableUniqueSet(); 408 } 409 MutableAttributeSet ma = getMutableAttributeSet(old); 410 ma.removeAttributes(attrs); 411 return ma; 412 } 413 414 419 public AttributeSet getEmptySet() { 420 return SimpleAttributeSet.EMPTY; 421 } 422 423 436 public void reclaim(AttributeSet a) { 437 if (SwingUtilities.isEventDispatchThread()) { 438 attributesPool.size(); } 440 } 443 444 446 453 protected int getCompressionThreshold() { 454 return THRESHOLD; 455 } 456 457 467 protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) { 468 return new SmallAttributeSet(a); 469 } 470 471 483 protected MutableAttributeSet createLargeAttributeSet(AttributeSet a) { 484 return new SimpleAttributeSet (a); 485 } 486 487 490 synchronized void removeUnusedSets() { 491 attributesPool.size(); } 493 494 499 AttributeSet getImmutableUniqueSet() { 500 SmallAttributeSet key = createSmallAttributeSet(search); 503 WeakReference reference = (WeakReference )attributesPool.get(key); 504 SmallAttributeSet a; 505 if (reference == null 506 || (a = (SmallAttributeSet)reference.get()) == null) { 507 a = key; 508 attributesPool.put(a, new WeakReference (a)); 509 } 510 return a; 511 } 512 513 517 MutableAttributeSet getMutableAttributeSet(AttributeSet a) { 518 if (a instanceof MutableAttributeSet && 519 a != SimpleAttributeSet.EMPTY) { 520 return (MutableAttributeSet ) a; 521 } 522 return createLargeAttributeSet(a); 523 } 524 525 530 public String toString() { 531 removeUnusedSets(); 532 String s = ""; 533 Iterator iterator = attributesPool.keySet().iterator(); 534 while (iterator.hasNext()) { 535 SmallAttributeSet set = (SmallAttributeSet)iterator.next(); 536 s = s + set + "\n"; 537 } 538 return s; 539 } 540 541 543 546 public void writeAttributes(ObjectOutputStream out, 547 AttributeSet a) throws IOException { 548 writeAttributeSet(out, a); 549 } 550 551 554 public void readAttributes(ObjectInputStream in, 555 MutableAttributeSet a) throws ClassNotFoundException , IOException { 556 readAttributeSet(in, a); 557 } 558 559 573 public static void writeAttributeSet(ObjectOutputStream out, 574 AttributeSet a) throws IOException { 575 int n = a.getAttributeCount(); 576 out.writeInt(n); 577 Enumeration keys = a.getAttributeNames(); 578 while (keys.hasMoreElements()) { 579 Object key = keys.nextElement(); 580 if (key instanceof Serializable) { 581 out.writeObject(key); 582 } else { 583 Object ioFmt = freezeKeyMap.get(key); 584 if (ioFmt == null) { 585 throw new NotSerializableException(key.getClass(). 586 getName() + " is not serializable as a key in an AttributeSet"); 587 } 588 out.writeObject(ioFmt); 589 } 590 Object value = a.getAttribute(key); 591 Object ioFmt = freezeKeyMap.get(value); 592 if (value instanceof Serializable) { 593 out.writeObject((ioFmt != null) ? ioFmt : value); 594 } else { 595 if (ioFmt == null) { 596 throw new NotSerializableException(value.getClass(). 597 getName() + " is not serializable as a value in an AttributeSet"); 598 } 599 out.writeObject(ioFmt); 600 } 601 } 602 } 603 604 623 public static void readAttributeSet(ObjectInputStream in, 624 MutableAttributeSet a) throws ClassNotFoundException , IOException { 625 626 int n = in.readInt(); 627 for (int i = 0; i < n; i++) { 628 Object key = in.readObject(); 629 Object value = in.readObject(); 630 if (thawKeyMap != null) { 631 Object staticKey = thawKeyMap.get(key); 632 if (staticKey != null) { 633 key = staticKey; 634 } 635 Object staticValue = thawKeyMap.get(value); 636 if (staticValue != null) { 637 value = staticValue; 638 } 639 } 640 a.addAttribute(key, value); 641 } 642 } 643 644 659 public static void registerStaticAttributeKey(Object key) { 660 String ioFmt = key.getClass().getName() + "." + key.toString(); 661 if (freezeKeyMap == null) { 662 freezeKeyMap = new Hashtable(); 663 thawKeyMap = new Hashtable(); 664 } 665 freezeKeyMap.put(key, ioFmt); 666 thawKeyMap.put(ioFmt, key); 667 } 668 669 673 public static Object getStaticAttribute(Object key) { 674 if (thawKeyMap == null || key == null) { 675 return null; 676 } 677 return thawKeyMap.get(key); 678 } 679 680 685 public static Object getStaticAttributeKey(Object key) { 686 return key.getClass().getName() + "." + key.toString(); 687 } 688 689 private void writeObject(java.io.ObjectOutputStream s) 690 throws IOException 691 { 692 removeUnusedSets(); 694 695 s.defaultWriteObject(); 696 } 697 698 private void readObject(ObjectInputStream s) 699 throws ClassNotFoundException , IOException 700 { 701 fontSearch = new FontKey(null, 0, 0); 702 fontTable = new Hashtable(); 703 search = new SimpleAttributeSet (); 704 attributesPool = Collections. 705 synchronizedMap(new WeakHashMap ()); 706 s.defaultReadObject(); 707 } 708 709 711 715 public static final String DEFAULT_STYLE = "default"; 716 717 private static Hashtable freezeKeyMap; 718 private static Hashtable thawKeyMap; 719 720 private Style styles; 721 private transient FontKey fontSearch = new FontKey(null, 0, 0); 722 private transient Hashtable fontTable = new Hashtable(); 723 724 private transient Map attributesPool = Collections. 725 synchronizedMap(new WeakHashMap ()); 726 private transient MutableAttributeSet search = new SimpleAttributeSet (); 727 728 734 private int unusedSets; 735 736 740 static final int THRESHOLD = 9; 741 742 749 public class SmallAttributeSet implements AttributeSet { 750 751 public SmallAttributeSet(Object [] attributes) { 752 this.attributes = attributes; 753 updateResolveParent(); 754 } 755 756 public SmallAttributeSet(AttributeSet attrs) { 757 int n = attrs.getAttributeCount(); 758 Object [] tbl = new Object [2 * n]; 759 Enumeration names = attrs.getAttributeNames(); 760 int i = 0; 761 while (names.hasMoreElements()) { 762 tbl[i] = names.nextElement(); 763 tbl[i+1] = attrs.getAttribute(tbl[i]); 764 i += 2; 765 } 766 attributes = tbl; 767 updateResolveParent(); 768 } 769 770 private void updateResolveParent() { 771 resolveParent = null; 772 Object [] tbl = attributes; 773 for (int i = 0; i < tbl.length; i += 2) { 774 if (tbl[i] == StyleConstants.ResolveAttribute) { 775 resolveParent = (AttributeSet )tbl[i + 1]; 776 break; 777 } 778 } 779 } 780 781 Object getLocalAttribute(Object nm) { 782 if (nm == StyleConstants.ResolveAttribute) { 783 return resolveParent; 784 } 785 Object [] tbl = attributes; 786 for (int i = 0; i < tbl.length; i += 2) { 787 if (nm.equals(tbl[i])) { 788 return tbl[i+1]; 789 } 790 } 791 return null; 792 } 793 794 796 799 public String toString() { 800 String s = "{"; 801 Object [] tbl = attributes; 802 for (int i = 0; i < tbl.length; i += 2) { 803 if (tbl[i+1] instanceof AttributeSet ) { 804 s = s + tbl[i] + "=" + "AttributeSet" + ","; 806 } else { 807 s = s + tbl[i] + "=" + tbl[i+1] + ","; 808 } 809 } 810 s = s + "}"; 811 return s; 812 } 813 814 818 public int hashCode() { 819 int code = 0; 820 Object [] tbl = attributes; 821 for (int i = 1; i < tbl.length; i += 2) { 822 code ^= tbl[i].hashCode(); 823 } 824 return code; 825 } 826 827 835 public boolean equals(Object obj) { 836 if (obj instanceof AttributeSet ) { 837 AttributeSet attrs = (AttributeSet ) obj; 838 return ((getAttributeCount() == attrs.getAttributeCount()) && 839 containsAttributes(attrs)); 840 } 841 return false; 842 } 843 844 850 public Object clone() { 851 return this; 852 } 853 854 856 862 public int getAttributeCount() { 863 return attributes.length / 2; 864 } 865 866 873 public boolean isDefined(Object key) { 874 Object [] a = attributes; 875 int n = a.length; 876 for (int i = 0; i < n; i += 2) { 877 if (key.equals(a[i])) { 878 return true; 879 } 880 } 881 return false; 882 } 883 884 891 public boolean isEqual(AttributeSet attr) { 892 if (attr instanceof SmallAttributeSet) { 893 return attr == this; 894 } 895 return ((getAttributeCount() == attr.getAttributeCount()) && 896 containsAttributes(attr)); 897 } 898 899 905 public AttributeSet copyAttributes() { 906 return this; 907 } 908 909 916 public Object getAttribute(Object key) { 917 Object value = getLocalAttribute(key); 918 if (value == null) { 919 AttributeSet parent = getResolveParent(); 920 if (parent != null) 921 value = parent.getAttribute(key); 922 } 923 return value; 924 } 925 926 932 public Enumeration<?> getAttributeNames() { 933 return new KeyEnumeration(attributes); 934 } 935 936 944 public boolean containsAttribute(Object name, Object value) { 945 return value.equals(getAttribute(name)); 946 } 947 948 956 public boolean containsAttributes(AttributeSet attrs) { 957 boolean result = true; 958 959 Enumeration names = attrs.getAttributeNames(); 960 while (result && names.hasMoreElements()) { 961 Object name = names.nextElement(); 962 result = attrs.getAttribute(name).equals(getAttribute(name)); 963 } 964 965 return result; 966 } 967 968 975 public AttributeSet getResolveParent() { 976 return resolveParent; 977 } 978 979 981 Object [] attributes; 982 AttributeSet resolveParent; 984 } 985 986 989 class KeyEnumeration implements Enumeration<Object > { 990 991 KeyEnumeration(Object [] attr) { 992 this.attr = attr; 993 i = 0; 994 } 995 996 1003 public boolean hasMoreElements() { 1004 return i < attr.length; 1005 } 1006 1007 1014 public Object nextElement() { 1015 if (i < attr.length) { 1016 Object o = attr[i]; 1017 i += 2; 1018 return o; 1019 } 1020 throw new NoSuchElementException(); 1021 } 1022 1023 Object [] attr; 1024 int i; 1025 } 1026 1027 1031 class KeyBuilder { 1032 1033 public void initialize(AttributeSet a) { 1034 if (a instanceof SmallAttributeSet) { 1035 initialize(((SmallAttributeSet)a).attributes); 1036 } else { 1037 keys.removeAllElements(); 1038 data.removeAllElements(); 1039 Enumeration names = a.getAttributeNames(); 1040 while (names.hasMoreElements()) { 1041 Object name = names.nextElement(); 1042 addAttribute(name, a.getAttribute(name)); 1043 } 1044 } 1045 } 1046 1047 1051 private void initialize(Object [] sorted) { 1052 keys.removeAllElements(); 1053 data.removeAllElements(); 1054 int n = sorted.length; 1055 for (int i = 0; i < n; i += 2) { 1056 keys.addElement(sorted[i]); 1057 data.addElement(sorted[i+1]); 1058 } 1059 } 1060 1061 1066 public Object [] createTable() { 1067 int n = keys.size(); 1068 Object [] tbl = new Object [2 * n]; 1069 for (int i = 0; i < n; i ++) { 1070 int offs = 2 * i; 1071 tbl[offs] = keys.elementAt(i); 1072 tbl[offs + 1] = data.elementAt(i); 1073 } 1074 return tbl; 1075 } 1076 1077 1081 int getCount() { 1082 return keys.size(); 1083 } 1084 1085 1088 public void addAttribute(Object key, Object value) { 1089 keys.addElement(key); 1090 data.addElement(value); 1091 } 1092 1093 1096 public void addAttributes(AttributeSet attr) { 1097 if (attr instanceof SmallAttributeSet) { 1098 Object [] tbl = ((SmallAttributeSet)attr).attributes; 1100 int n = tbl.length; 1101 for (int i = 0; i < n; i += 2) { 1102 addAttribute(tbl[i], tbl[i+1]); 1103 } 1104 } else { 1105 Enumeration names = attr.getAttributeNames(); 1106 while (names.hasMoreElements()) { 1107 Object name = names.nextElement(); 1108 addAttribute(name, attr.getAttribute(name)); 1109 } 1110 } 1111 } 1112 1113 1116 public void removeAttribute(Object key) { 1117 int n = keys.size(); 1118 for (int i = 0; i < n; i++) { 1119 if (keys.elementAt(i).equals(key)) { 1120 keys.removeElementAt(i); 1121 data.removeElementAt(i); 1122 return; 1123 } 1124 } 1125 } 1126 1127 1130 public void removeAttributes(Enumeration names) { 1131 while (names.hasMoreElements()) { 1132 Object name = names.nextElement(); 1133 removeAttribute(name); 1134 } 1135 } 1136 1137 1140 public void removeAttributes(AttributeSet attr) { 1141 Enumeration names = attr.getAttributeNames(); 1142 while (names.hasMoreElements()) { 1143 Object name = names.nextElement(); 1144 Object value = attr.getAttribute(name); 1145 removeSearchAttribute(name, value); 1146 } 1147 } 1148 1149 private void removeSearchAttribute(Object ikey, Object value) { 1150 int n = keys.size(); 1151 for (int i = 0; i < n; i++) { 1152 if (keys.elementAt(i).equals(ikey)) { 1153 if (data.elementAt(i).equals(value)) { 1154 keys.removeElementAt(i); 1155 data.removeElementAt(i); 1156 } 1157 return; 1158 } 1159 } 1160 } 1161 1162 private Vector keys = new Vector(); 1163 private Vector data = new Vector(); 1164 } 1165 1166 1169 static class FontKey { 1170 1171 private String family; 1172 private int style; 1173 private int size; 1174 1175 1178 public FontKey(String family, int style, int size) { 1179 setValue(family, style, size); 1180 } 1181 1182 public void setValue(String family, int style, int size) { 1183 this.family = (family != null) ? family.intern() : null; 1184 this.style = style; 1185 this.size = size; 1186 } 1187 1188 1192 public int hashCode() { 1193 int fhash = (family != null) ? family.hashCode() : 0; 1194 return fhash ^ style ^ size; 1195 } 1196 1197 1206 public boolean equals(Object obj) { 1207 if (obj instanceof FontKey) { 1208 FontKey font = (FontKey)obj; 1209 return (size == font.size) && (style == font.style) && (family == font.family); 1210 } 1211 return false; 1212 } 1213 1214 } 1215 1216 1233 public class NamedStyle implements Style , Serializable { 1234 1235 1241 public NamedStyle(String name, Style parent) { 1242 attributes = getEmptySet(); 1243 if (name != null) { 1244 setName(name); 1245 } 1246 if (parent != null) { 1247 setResolveParent(parent); 1248 } 1249 } 1250 1251 1256 public NamedStyle(Style parent) { 1257 this(null, parent); 1258 } 1259 1260 1263 public NamedStyle() { 1264 attributes = getEmptySet(); 1265 } 1266 1267 1272 public String toString() { 1273 return "NamedStyle:" + getName() + " " + attributes; 1274 } 1275 1276 1282 public String getName() { 1283 if (isDefined(StyleConstants.NameAttribute)) { 1284 return getAttribute(StyleConstants.NameAttribute).toString(); 1285 } 1286 return null; 1287 } 1288 1289 1294 public void setName(String name) { 1295 if (name != null) { 1296 this.addAttribute(StyleConstants.NameAttribute, name); 1297 } 1298 } 1299 1300 1305 public void addChangeListener(ChangeListener l) { 1306 listenerList.add(ChangeListener .class, l); 1307 } 1308 1309 1314 public void removeChangeListener(ChangeListener l) { 1315 listenerList.remove(ChangeListener .class, l); 1316 } 1317 1318 1319 1327 public ChangeListener [] getChangeListeners() { 1328 return (ChangeListener [])listenerList.getListeners( 1329 ChangeListener .class); 1330 } 1331 1332 1333 1341 protected void fireStateChanged() { 1342 Object [] listeners = listenerList.getListenerList(); 1344 for (int i = listeners.length-2; i>=0; i-=2) { 1347 if (listeners[i]==ChangeListener .class) { 1348 if (changeEvent == null) 1350 changeEvent = new ChangeEvent (this); 1351 ((ChangeListener )listeners[i+1]).stateChanged(changeEvent); 1352 } 1353 } 1354 } 1355 1356 1365 public <T extends EventListener> T[] getListeners(Class <T> listenerType) { 1366 return listenerList.getListeners(listenerType); 1367 } 1368 1369 1372 1378 public int getAttributeCount() { 1379 return attributes.getAttributeCount(); 1380 } 1381 1382 1389 public boolean isDefined(Object attrName) { 1390 return attributes.isDefined(attrName); 1391 } 1392 1393 1400 public boolean isEqual(AttributeSet attr) { 1401 return attributes.isEqual(attr); 1402 } 1403 1404 1410 public AttributeSet copyAttributes() { 1411 NamedStyle a = new NamedStyle(); 1412 a.attributes = attributes.copyAttributes(); 1413 return a; 1414 } 1415 1416 1423 public Object getAttribute(Object attrName) { 1424 return attributes.getAttribute(attrName); 1425 } 1426 1427 1433 public Enumeration<?> getAttributeNames() { 1434 return attributes.getAttributeNames(); 1435 } 1436 1437 1445 public boolean containsAttribute(Object name, Object value) { 1446 return attributes.containsAttribute(name, value); 1447 } 1448 1449 1450 1457 public boolean containsAttributes(AttributeSet attrs) { 1458 return attributes.containsAttributes(attrs); 1459 } 1460 1461 1469 public AttributeSet getResolveParent() { 1470 return attributes.getResolveParent(); 1471 } 1472 1473 1477 1484 public void addAttribute(Object name, Object value) { 1485 StyleContext context = StyleContext.this; 1486 attributes = context.addAttribute(attributes, name, value); 1487 fireStateChanged(); 1488 } 1489 1490 1496 public void addAttributes(AttributeSet attr) { 1497 StyleContext context = StyleContext.this; 1498 attributes = context.addAttributes(attributes, attr); 1499 fireStateChanged(); 1500 } 1501 1502 1508 public void removeAttribute(Object name) { 1509 StyleContext context = StyleContext.this; 1510 attributes = context.removeAttribute(attributes, name); 1511 fireStateChanged(); 1512 } 1513 1514 1520 public void removeAttributes(Enumeration<?> names) { 1521 StyleContext context = StyleContext.this; 1522 attributes = context.removeAttributes(attributes, names); 1523 fireStateChanged(); 1524 } 1525 1526 1532 public void removeAttributes(AttributeSet attrs) { 1533 StyleContext context = StyleContext.this; 1534 if (attrs == this) { 1535 attributes = context.getEmptySet(); 1536 } else { 1537 attributes = context.removeAttributes(attributes, attrs); 1538 } 1539 fireStateChanged(); 1540 } 1541 1542 1548 public void setResolveParent(AttributeSet parent) { 1549 if (parent != null) { 1550 addAttribute(StyleConstants.ResolveAttribute, parent); 1551 } else { 1552 removeAttribute(StyleConstants.ResolveAttribute); 1553 } 1554 } 1555 1556 1558 private void writeObject(ObjectOutputStream s) throws IOException { 1559 s.defaultWriteObject(); 1560 writeAttributeSet(s, attributes); 1561 } 1562 1563 private void readObject(ObjectInputStream s) 1564 throws ClassNotFoundException , IOException 1565 { 1566 s.defaultReadObject(); 1567 attributes = SimpleAttributeSet.EMPTY; 1568 readAttributeSet(s, this); 1569 } 1570 1571 1573 1576 protected EventListenerList listenerList = new EventListenerList (); 1577 1578 1583 protected transient ChangeEvent changeEvent = null; 1584 1585 1589 private transient AttributeSet attributes; 1590 1591 } 1592 1593 static { 1594 try { 1596 int n = StyleConstants.keys.length; 1597 for (int i = 0; i < n; i++) { 1598 StyleContext.registerStaticAttributeKey(StyleConstants.keys[i]); 1599 } 1600 } catch (Throwable e) { 1601 e.printStackTrace(); 1602 } 1603 } 1604 1605 1606} 1607 | Popular Tags |