home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / text / StyleContext.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  8.7 KB  |  317 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.SwingUtilities;
  4. import com.sun.java.swing.event.ChangeListener;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Toolkit;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.Serializable;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import java.util.Vector;
  15.  
  16. public class StyleContext implements Serializable, AbstractDocument.AttributeContext {
  17.    private static StyleContext defaultContext;
  18.    public static final String DEFAULT_STYLE = "default";
  19.    private static Hashtable freezeKeyMap;
  20.    private static Hashtable thawKeyMap;
  21.    private Style styles = new NamedStyle(this, (Style)null);
  22.    private transient FontKey fontSearch = new FontKey((String)null, 0, 0);
  23.    private transient Hashtable fontTable = new Hashtable();
  24.    private transient Hashtable attributesPool = new Hashtable();
  25.    private transient MutableAttributeSet search = new SimpleAttributeSet();
  26.    private int unusedSets;
  27.    static final int THRESHOLD = 9;
  28.    // $FF: synthetic field
  29.    static Class class$com$sun$java$swing$event$ChangeListener;
  30.  
  31.    public static final StyleContext getDefaultStyleContext() {
  32.       if (defaultContext == null) {
  33.          defaultContext = new StyleContext();
  34.       }
  35.  
  36.       return defaultContext;
  37.    }
  38.  
  39.    public StyleContext() {
  40.       this.addStyle("default", (Style)null);
  41.    }
  42.  
  43.    public Style addStyle(String var1, Style var2) {
  44.       NamedStyle var3 = new NamedStyle(this, var1, var2);
  45.       if (var1 != null) {
  46.          this.styles.addAttribute(var1, var3);
  47.       }
  48.  
  49.       return var3;
  50.    }
  51.  
  52.    public void removeStyle(String var1) {
  53.       this.styles.removeAttribute(var1);
  54.    }
  55.  
  56.    public Style getStyle(String var1) {
  57.       return (Style)this.styles.getAttribute(var1);
  58.    }
  59.  
  60.    public Enumeration getStyleNames() {
  61.       return this.styles.getAttributeNames();
  62.    }
  63.  
  64.    public void addChangeListener(ChangeListener var1) {
  65.       this.styles.addChangeListener(var1);
  66.    }
  67.  
  68.    public void removeChangeListener(ChangeListener var1) {
  69.       this.styles.removeChangeListener(var1);
  70.    }
  71.  
  72.    public Font getFont(AttributeSet var1) {
  73.       int var2 = 0;
  74.       if (StyleConstants.isBold(var1)) {
  75.          var2 |= 1;
  76.       }
  77.  
  78.       if (StyleConstants.isItalic(var1)) {
  79.          var2 |= 2;
  80.       }
  81.  
  82.       String var3 = StyleConstants.getFontFamily(var1);
  83.       int var4 = StyleConstants.getFontSize(var1);
  84.       return this.getFont(var3, var2, var4);
  85.    }
  86.  
  87.    public Font getFont(String var1, int var2, int var3) {
  88.       this.fontSearch.setValue(var1, var2, var3);
  89.       Font var4 = (Font)this.fontTable.get(this.fontSearch);
  90.       if (var4 == null) {
  91.          var4 = new Font(var1, var2, var3);
  92.          FontKey var5 = new FontKey(var1, var2, var3);
  93.          this.fontTable.put(var5, var4);
  94.       }
  95.  
  96.       return var4;
  97.    }
  98.  
  99.    public FontMetrics getFontMetrics(Font var1) {
  100.       return Toolkit.getDefaultToolkit().getFontMetrics(var1);
  101.    }
  102.  
  103.    public synchronized AttributeSet addAttribute(AttributeSet var1, Object var2, Object var3) {
  104.       if (var1.getAttributeCount() + 1 <= this.getCompressionThreshold()) {
  105.          this.search.removeAttributes(this.search);
  106.          this.search.addAttributes(var1);
  107.          this.search.addAttribute(var2, var3);
  108.          this.reclaim(var1);
  109.          return this.getImmutableUniqueSet();
  110.       } else {
  111.          MutableAttributeSet var4 = this.getMutableAttributeSet(var1);
  112.          var4.addAttribute(var2, var3);
  113.          return var4;
  114.       }
  115.    }
  116.  
  117.    public synchronized AttributeSet addAttributes(AttributeSet var1, AttributeSet var2) {
  118.       if (var1.getAttributeCount() + var2.getAttributeCount() <= this.getCompressionThreshold()) {
  119.          this.search.removeAttributes(this.search);
  120.          this.search.addAttributes(var1);
  121.          this.search.addAttributes(var2);
  122.          this.reclaim(var1);
  123.          return this.getImmutableUniqueSet();
  124.       } else {
  125.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  126.          var3.addAttributes(var2);
  127.          return var3;
  128.       }
  129.    }
  130.  
  131.    public synchronized AttributeSet removeAttribute(AttributeSet var1, Object var2) {
  132.       if (var1.getAttributeCount() - 1 <= this.getCompressionThreshold()) {
  133.          this.search.removeAttributes(this.search);
  134.          this.search.addAttributes(var1);
  135.          this.search.removeAttribute(var2);
  136.          this.reclaim(var1);
  137.          return this.getImmutableUniqueSet();
  138.       } else {
  139.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  140.          var3.removeAttribute(var2);
  141.          return var3;
  142.       }
  143.    }
  144.  
  145.    public synchronized AttributeSet removeAttributes(AttributeSet var1, Enumeration var2) {
  146.       if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
  147.          this.search.removeAttributes(this.search);
  148.          this.search.addAttributes(var1);
  149.          this.search.removeAttributes(var2);
  150.          this.reclaim(var1);
  151.          return this.getImmutableUniqueSet();
  152.       } else {
  153.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  154.          var3.removeAttributes(var2);
  155.          return var3;
  156.       }
  157.    }
  158.  
  159.    public synchronized AttributeSet removeAttributes(AttributeSet var1, AttributeSet var2) {
  160.       if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
  161.          this.search.removeAttributes(this.search);
  162.          this.search.addAttributes(var1);
  163.          this.search.removeAttributes(var2);
  164.          this.reclaim(var1);
  165.          return this.getImmutableUniqueSet();
  166.       } else {
  167.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  168.          var3.removeAttributes(var2);
  169.          return var3;
  170.       }
  171.    }
  172.  
  173.    public AttributeSet getEmptySet() {
  174.       return SimpleAttributeSet.EMPTY;
  175.    }
  176.  
  177.    public void reclaim(AttributeSet var1) {
  178.       if (var1 instanceof SmallAttributeSet) {
  179.          SmallAttributeSet var2 = (SmallAttributeSet)var1;
  180.          --var2.nrefs;
  181.          if (var2.nrefs <= 0) {
  182.             ++this.unusedSets;
  183.             if (this.unusedSets > 10 && this.unusedSets > this.attributesPool.size() / 10) {
  184.                if (SwingUtilities.isEventDispatchThread()) {
  185.                   this.removeUnusedSets();
  186.                   return;
  187.                }
  188.  
  189.                1 var3 = new 1(this);
  190.                SwingUtilities.invokeLater(var3);
  191.             }
  192.          }
  193.       }
  194.  
  195.    }
  196.  
  197.    protected int getCompressionThreshold() {
  198.       return 9;
  199.    }
  200.  
  201.    synchronized void removeUnusedSets() {
  202.       Vector var1 = new Vector();
  203.       Enumeration var2 = this.attributesPool.keys();
  204.  
  205.       while(var2.hasMoreElements()) {
  206.          SmallAttributeSet var3 = (SmallAttributeSet)var2.nextElement();
  207.          if (var3.nrefs <= 0) {
  208.             var1.addElement(var3);
  209.          }
  210.       }
  211.  
  212.       var2 = var1.elements();
  213.  
  214.       while(var2.hasMoreElements()) {
  215.          this.attributesPool.remove(var2.nextElement());
  216.       }
  217.  
  218.       this.unusedSets = 0;
  219.    }
  220.  
  221.    AttributeSet getImmutableUniqueSet() {
  222.       SmallAttributeSet var1 = (SmallAttributeSet)this.attributesPool.get(this.search);
  223.       if (var1 == null) {
  224.          var1 = new SmallAttributeSet(this, this.search);
  225.          this.attributesPool.put(var1, var1);
  226.       }
  227.  
  228.       ++var1.nrefs;
  229.       return var1;
  230.    }
  231.  
  232.    MutableAttributeSet getMutableAttributeSet(AttributeSet var1) {
  233.       return (MutableAttributeSet)(var1 instanceof MutableAttributeSet ? (MutableAttributeSet)var1 : new SimpleAttributeSet(var1));
  234.    }
  235.  
  236.    public String toString() {
  237.       this.removeUnusedSets();
  238.       String var1 = "";
  239.  
  240.       SmallAttributeSet var3;
  241.       for(Enumeration var2 = this.attributesPool.keys(); var2.hasMoreElements(); var1 = var1 + var3 + "\n") {
  242.          var3 = (SmallAttributeSet)var2.nextElement();
  243.       }
  244.  
  245.       return var1;
  246.    }
  247.  
  248.    public static void writeAttributeSet(ObjectOutputStream var0, AttributeSet var1) throws IOException {
  249.       int var2 = var1.getAttributeCount();
  250.       var0.writeInt(var2);
  251.       Enumeration var3 = var1.getAttributeNames();
  252.  
  253.       while(var3.hasMoreElements()) {
  254.          Object var4 = var3.nextElement();
  255.          if (var4 instanceof Serializable) {
  256.             var0.writeObject(var4);
  257.          } else {
  258.             Object var5 = freezeKeyMap.get(var4);
  259.             var0.writeObject(var5);
  260.          }
  261.  
  262.          Object var6 = var1.getAttribute(var4);
  263.          var0.writeObject(var6);
  264.       }
  265.  
  266.    }
  267.  
  268.    public static void readAttributeSet(ObjectInputStream var0, MutableAttributeSet var1) throws ClassNotFoundException, IOException {
  269.       int var2 = var0.readInt();
  270.  
  271.       for(int var3 = 0; var3 < var2; ++var3) {
  272.          Object var4 = var0.readObject();
  273.          Object var5 = var0.readObject();
  274.          Object var6 = thawKeyMap.get(var4);
  275.          if (var6 != null) {
  276.             var4 = var6;
  277.          }
  278.  
  279.          var1.addAttribute(var4, var5);
  280.       }
  281.  
  282.    }
  283.  
  284.    public static void registerStaticAttributeKey(Object var0) {
  285.       String var1 = var0.getClass().getName() + "." + var0.toString();
  286.       if (freezeKeyMap == null) {
  287.          freezeKeyMap = new Hashtable();
  288.          thawKeyMap = new Hashtable();
  289.       }
  290.  
  291.       freezeKeyMap.put(var0, var1);
  292.       thawKeyMap.put(var1, var0);
  293.    }
  294.  
  295.    private void writeObject(ObjectOutputStream var1) throws IOException {
  296.       this.removeUnusedSets();
  297.       var1.defaultWriteObject();
  298.    }
  299.  
  300.    private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
  301.       this.fontSearch = new FontKey((String)null, 0, 0);
  302.       this.fontTable = new Hashtable();
  303.       this.search = new SimpleAttributeSet();
  304.       this.attributesPool = new Hashtable();
  305.       var1.defaultReadObject();
  306.    }
  307.  
  308.    // $FF: synthetic method
  309.    static Class class$(String var0) {
  310.       try {
  311.          return Class.forName(var0);
  312.       } catch (ClassNotFoundException var2) {
  313.          throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  314.       }
  315.    }
  316. }
  317.