FileDocCategorySizeDatePackage
StylishDocument.javaAPI DocExample5377Mon Nov 09 12:45:52 GMT 1998None

StylishDocument

public class StylishDocument extends DefaultStyledDocument implements DocumentListener

Fields Summary
private Hashtable
styleHash
Constructors Summary
public StylishDocument(Content c, StyleContext styles)

    super(c, styles);
    init();
  
public StylishDocument(StyleContext styles)

    super(styles);
    init();
  
public StylishDocument()

    super();
    init();
  
Methods Summary
protected voidaddToStyleHash(javax.swing.text.Element para)

    AttributeSet attrs = para.getAttributes();
    if (attrs != null) {
      Style style = (Style)attrs.getResolveParent();
      if (style != null) {

        // We've got the Style, now see if we've got a set of Elements that
        // use this Style
        Hashtable ht = (Hashtable)styleHash.get(style);
        if (ht == null) {
          // First user of this Style . . .add a new set
          ht = new Hashtable();
          styleHash.put(style, ht);
        }
        // If this paragraph isn't already in the set, we add it. We really want
        // a Set, not a Hashtable, but to be JDK 1.1 friendly here, we'll use a
        // Hashtable with a throw-away value. We only care about the keys.
        if (ht.containsKey(para) == false) {
          ht.put(para, new Object());
        }
      }
    }
  
public voidchangedUpdate(javax.swing.event.DocumentEvent ev)

    int offset = ev.getOffset();
    Element para = getParagraphElement(offset);
    addToStyleHash(para);
  
protected voidinit()

    addDocumentListener(this);
    addToStyleHash(getParagraphElement(0));
  
public voidinsertUpdate(javax.swing.event.DocumentEvent ev)

 updateStyleHash(ev); 
protected voidremoveFromStyleHash(javax.swing.text.Element para)

    AttributeSet attrs = para.getAttributes();
    if (attrs != null) {
      Style style = (Style)attrs.getResolveParent();
      if (style != null) {
        Hashtable ht = (Hashtable)styleHash.get(style);
        if (ht != null) {
          ht.remove(para);
        }
      }
    }
  
public voidremoveUpdate(javax.swing.event.DocumentEvent ev)

 updateStyleHash(ev); 
public voidstyleUpdated(javax.swing.text.Style style)

    // Find the set of Elements that use this style . . .

    Hashtable ht = (Hashtable)styleHash.get(style);

    if (ht != null) {
      // somebody's using it if we get here.

      // Create a Vector of Elements that shouldn't be in this table because they
      // no longer use this Style (we don't remove them when they change Styles,
      // so they will still be here)
      Vector cleanUp = new Vector();

      // Update each Element . . .
      Enumeration e = ht.keys();
      while (e.hasMoreElements()) {
        Element el = (Element)e.nextElement();
        int start = el.getStartOffset();
        int end = el.getEndOffset();
        Style check = getLogicalStyle(start);

        // Fire an event only if this Element is still using this Style.
        if (check == style) {
          DefaultDocumentEvent ev = new DefaultDocumentEvent
            (start, end-start, DocumentEvent.EventType.CHANGE);
          fireChangedUpdate(ev);
        }
        else {
          // If not, remove this Element, since it no longer uses this Style
          cleanUp.addElement(el);
        }
      }

      // Clean up . . .
      e = cleanUp.elements();
      while (e.hasMoreElements()) {
        Element bad = (Element)e.nextElement();
        ht.remove(bad);
      }
    }
  
protected voidupdateStyleHash(javax.swing.event.DocumentEvent ev)

    DocumentEvent.ElementChange chg =
      ev.getChange(getDefaultRootElement());
        
    if (chg != null) {

      // Something was added or removed (or both) . . .
      Element[] removed = chg.getChildrenRemoved();
      for (int i=0; i<removed.length; i++) {
        removeFromStyleHash(removed[i]);
      }

      Element[] added = chg.getChildrenAdded();
      for (int i=0;i<added.length;i++) {
        addToStyleHash(added[i]);
      }
    }