home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / text / StyleContext.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  9.5 KB  |  373 lines

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