home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / text / StyleContext.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  8.2 KB  |  377 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.  
  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.       if (StyleConstants.isSuperscript(var1) || StyleConstants.isSubscript(var1)) {
  85.          var4 -= 2;
  86.       }
  87.  
  88.       return this.getFont(var3, var2, var4);
  89.    }
  90.  
  91.    public Color getForeground(AttributeSet var1) {
  92.       return StyleConstants.getForeground(var1);
  93.    }
  94.  
  95.    public Color getBackground(AttributeSet var1) {
  96.       return StyleConstants.getBackground(var1);
  97.    }
  98.  
  99.    public Font getFont(String var1, int var2, int var3) {
  100.       this.fontSearch.setValue(var1, var2, var3);
  101.       Font var4 = (Font)this.fontTable.get(this.fontSearch);
  102.       if (var4 == null) {
  103.          var4 = new Font(var1, var2, var3);
  104.          FontKey var5 = new FontKey(var1, var2, var3);
  105.          this.fontTable.put(var5, var4);
  106.       }
  107.  
  108.       return var4;
  109.    }
  110.  
  111.    public FontMetrics getFontMetrics(Font var1) {
  112.       return Toolkit.getDefaultToolkit().getFontMetrics(var1);
  113.    }
  114.  
  115.    public synchronized AttributeSet addAttribute(AttributeSet var1, Object var2, Object var3) {
  116.       if (var1.getAttributeCount() + 1 <= this.getCompressionThreshold()) {
  117.          this.search.removeAttributes(this.search);
  118.          this.search.addAttributes(var1);
  119.          this.search.addAttribute(var2, var3);
  120.          this.reclaim(var1);
  121.          return this.getImmutableUniqueSet();
  122.       } else {
  123.          MutableAttributeSet var4 = this.getMutableAttributeSet(var1);
  124.          var4.addAttribute(var2, var3);
  125.          return var4;
  126.       }
  127.    }
  128.  
  129.    public synchronized AttributeSet addAttributes(AttributeSet var1, AttributeSet var2) {
  130.       if (var1.getAttributeCount() + var2.getAttributeCount() <= this.getCompressionThreshold()) {
  131.          this.search.removeAttributes(this.search);
  132.          this.search.addAttributes(var1);
  133.          this.search.addAttributes(var2);
  134.          this.reclaim(var1);
  135.          return this.getImmutableUniqueSet();
  136.       } else {
  137.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  138.          var3.addAttributes(var2);
  139.          return var3;
  140.       }
  141.    }
  142.  
  143.    public synchronized AttributeSet removeAttribute(AttributeSet var1, Object var2) {
  144.       if (var1.getAttributeCount() - 1 <= this.getCompressionThreshold()) {
  145.          this.search.removeAttributes(this.search);
  146.          this.search.addAttributes(var1);
  147.          this.search.removeAttribute(var2);
  148.          this.reclaim(var1);
  149.          return this.getImmutableUniqueSet();
  150.       } else {
  151.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  152.          var3.removeAttribute(var2);
  153.          return var3;
  154.       }
  155.    }
  156.  
  157.    public synchronized AttributeSet removeAttributes(AttributeSet var1, Enumeration var2) {
  158.       if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
  159.          this.search.removeAttributes(this.search);
  160.          this.search.addAttributes(var1);
  161.          this.search.removeAttributes(var2);
  162.          this.reclaim(var1);
  163.          return this.getImmutableUniqueSet();
  164.       } else {
  165.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  166.          var3.removeAttributes(var2);
  167.          return var3;
  168.       }
  169.    }
  170.  
  171.    public synchronized AttributeSet removeAttributes(AttributeSet var1, AttributeSet var2) {
  172.       if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
  173.          this.search.removeAttributes(this.search);
  174.          this.search.addAttributes(var1);
  175.          this.search.removeAttributes(var2);
  176.          this.reclaim(var1);
  177.          return this.getImmutableUniqueSet();
  178.       } else {
  179.          MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
  180.          var3.removeAttributes(var2);
  181.          return var3;
  182.       }
  183.    }
  184.  
  185.    public AttributeSet getEmptySet() {
  186.       return SimpleAttributeSet.EMPTY;
  187.    }
  188.  
  189.    public void reclaim(AttributeSet var1) {
  190.       if (var1 instanceof SmallAttributeSet) {
  191.          SmallAttributeSet var2 = (SmallAttributeSet)var1;
  192.          --var2.nrefs;
  193.          if (var2.nrefs <= 0) {
  194.             ++this.unusedSets;
  195.             if (this.unusedSets > 10 && this.unusedSets > this.attributesPool.size() / 10) {
  196.                if (SwingUtilities.isEventDispatchThread()) {
  197.                   this.removeUnusedSets();
  198.                } else {
  199.                   1 var3 = new 1(this);
  200.                   SwingUtilities.invokeLater(var3);
  201.                }
  202.             }
  203.          }
  204.       }
  205.  
  206.    }
  207.  
  208.    protected int getCompressionThreshold() {
  209.       return 9;
  210.    }
  211.  
  212.    protected SmallAttributeSet createSmallAttributeSet(AttributeSet var1) {
  213.       return new SmallAttributeSet(this, var1);
  214.    }
  215.  
  216.    protected MutableAttributeSet createLargeAttributeSet(AttributeSet var1) {
  217.       return new SimpleAttributeSet(var1);
  218.    }
  219.  
  220.    synchronized void removeUnusedSets() {
  221.       Vector var1 = new Vector();
  222.       Enumeration var2 = this.attributesPool.keys();
  223.  
  224.       while(var2.hasMoreElements()) {
  225.          SmallAttributeSet var3 = (SmallAttributeSet)var2.nextElement();
  226.          if (var3.nrefs <= 0) {
  227.             var1.addElement(var3);
  228.          }
  229.       }
  230.  
  231.       var2 = var1.elements();
  232.  
  233.       while(var2.hasMoreElements()) {
  234.          this.attributesPool.remove(var2.nextElement());
  235.       }
  236.  
  237.       this.unusedSets = 0;
  238.    }
  239.  
  240.    AttributeSet getImmutableUniqueSet() {
  241.       SmallAttributeSet var1 = this.createSmallAttributeSet(this.search);
  242.       SmallAttributeSet var2 = (SmallAttributeSet)this.attributesPool.get(var1);
  243.       if (var2 == null) {
  244.          var2 = var1;
  245.          this.attributesPool.put(var1, var1);
  246.       }
  247.  
  248.       ++var2.nrefs;
  249.       return var2;
  250.    }
  251.  
  252.    MutableAttributeSet getMutableAttributeSet(AttributeSet var1) {
  253.       return var1 instanceof MutableAttributeSet && var1 != SimpleAttributeSet.EMPTY ? (MutableAttributeSet)var1 : this.createLargeAttributeSet(var1);
  254.    }
  255.  
  256.    public String toString() {
  257.       this.removeUnusedSets();
  258.       String var1 = "";
  259.  
  260.       SmallAttributeSet var3;
  261.       for(Enumeration var2 = this.attributesPool.keys(); var2.hasMoreElements(); var1 = var1 + var3 + "\n") {
  262.          var3 = (SmallAttributeSet)var2.nextElement();
  263.       }
  264.  
  265.       return var1;
  266.    }
  267.  
  268.    public void writeAttributes(ObjectOutputStream var1, AttributeSet var2) throws IOException {
  269.       writeAttributeSet(var1, var2);
  270.    }
  271.  
  272.    public void readAttributes(ObjectInputStream var1, MutableAttributeSet var2) throws ClassNotFoundException, IOException {
  273.       readAttributeSet(var1, var2);
  274.    }
  275.  
  276.    public static void writeAttributeSet(ObjectOutputStream var0, AttributeSet var1) throws IOException {
  277.       int var2 = var1.getAttributeCount();
  278.       var0.writeInt(var2);
  279.       Enumeration var3 = var1.getAttributeNames();
  280.  
  281.       while(var3.hasMoreElements()) {
  282.          Object var4 = var3.nextElement();
  283.          if (var4 instanceof Serializable) {
  284.             var0.writeObject(var4);
  285.          } else {
  286.             Object var5 = freezeKeyMap.get(var4);
  287.             if (var5 == null) {
  288.                throw new NotSerializableException(var4.getClass().getName() + " is not serializable as a key in an AttributeSet");
  289.             }
  290.  
  291.             var0.writeObject(var5);
  292.          }
  293.  
  294.          Object var7 = var1.getAttribute(var4);
  295.          if (var7 instanceof Serializable) {
  296.             var0.writeObject(var7);
  297.          } else {
  298.             Object var6 = freezeKeyMap.get(var7);
  299.             if (var6 == null) {
  300.                throw new NotSerializableException(var7.getClass().getName() + " is not serializable as a value in an AttributeSet");
  301.             }
  302.  
  303.             var0.writeObject(var6);
  304.          }
  305.       }
  306.  
  307.    }
  308.  
  309.    public static void readAttributeSet(ObjectInputStream var0, MutableAttributeSet var1) throws ClassNotFoundException, IOException {
  310.       int var2 = var0.readInt();
  311.  
  312.       for(int var3 = 0; var3 < var2; ++var3) {
  313.          Object var4 = var0.readObject();
  314.          Object var5 = var0.readObject();
  315.          if (thawKeyMap != null) {
  316.             Object var6 = thawKeyMap.get(var4);
  317.             if (var6 != null) {
  318.                var4 = var6;
  319.             }
  320.  
  321.             Object var7 = thawKeyMap.get(var5);
  322.             if (var7 != null) {
  323.                var5 = var7;
  324.             }
  325.          }
  326.  
  327.          var1.addAttribute(var4, var5);
  328.       }
  329.  
  330.    }
  331.  
  332.    public static void registerStaticAttributeKey(Object var0) {
  333.       String var1 = var0.getClass().getName() + "." + var0.toString();
  334.       if (freezeKeyMap == null) {
  335.          freezeKeyMap = new Hashtable();
  336.          thawKeyMap = new Hashtable();
  337.       }
  338.  
  339.       freezeKeyMap.put(var0, var1);
  340.       thawKeyMap.put(var1, var0);
  341.    }
  342.  
  343.    public static Object getStaticAttribute(Object var0) {
  344.       return thawKeyMap != null && var0 != null ? thawKeyMap.get(var0) : null;
  345.    }
  346.  
  347.    public static Object getStaticAttributeKey(Object var0) {
  348.       return var0.getClass().getName() + "." + var0.toString();
  349.    }
  350.  
  351.    private void writeObject(ObjectOutputStream var1) throws IOException {
  352.       this.removeUnusedSets();
  353.       var1.defaultWriteObject();
  354.    }
  355.  
  356.    private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
  357.       this.fontSearch = new FontKey((String)null, 0, 0);
  358.       this.fontTable = new Hashtable();
  359.       this.search = new SimpleAttributeSet();
  360.       this.attributesPool = new Hashtable();
  361.       var1.defaultReadObject();
  362.    }
  363.  
  364.    static {
  365.       try {
  366.          int var0 = StyleConstants.keys.length;
  367.  
  368.          for(int var1 = 0; var1 < var0; ++var1) {
  369.             registerStaticAttributeKey(StyleConstants.keys[var1]);
  370.          }
  371.       } catch (Throwable var2) {
  372.          var2.printStackTrace();
  373.       }
  374.  
  375.    }
  376. }
  377.