Source for javax.swing.text.DefaultStyledDocument

   1: /* DefaultStyledDocument.java --
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.text;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Font;
  43: import java.io.Serializable;
  44: 
  45: /**
  46:  * @author Michael Koch (konqueror@gmx.de)
  47:  */
  48: public class DefaultStyledDocument extends AbstractDocument
  49:   implements StyledDocument
  50: {
  51:   public class ElementBuffer
  52:     implements Serializable
  53:   {
  54:     private Element root;
  55:     
  56:     public ElementBuffer(Element root)
  57:     {
  58:       this.root = root;
  59:     }
  60: 
  61:     public Element getRootElement()
  62:     {
  63:       return root;
  64:     }
  65:   }
  66:   
  67:   public static final int BUFFER_SIZE_DEFAULT = 4096;
  68: 
  69:   protected DefaultStyledDocument.ElementBuffer buffer;
  70:   
  71:   public DefaultStyledDocument()
  72:   {
  73:     this(new GapContent(BUFFER_SIZE_DEFAULT), new StyleContext());
  74:   }
  75: 
  76:   public DefaultStyledDocument(StyleContext context)
  77:   {
  78:     this(new GapContent(BUFFER_SIZE_DEFAULT), context);
  79:   }
  80: 
  81:   public DefaultStyledDocument(AbstractDocument.Content content,
  82:                    StyleContext context)
  83:   {
  84:     super(content, context);
  85:     buffer = new ElementBuffer(createDefaultRoot());
  86:     setLogicalStyle(0, context.getStyle(StyleContext.DEFAULT_STYLE));
  87:   }
  88: 
  89:   public Style addStyle(String nm, Style parent)
  90:   {
  91:     StyleContext context = (StyleContext) getAttributeContext();
  92:     return context.addStyle(nm, parent);
  93:   }
  94:   
  95:   protected AbstractDocument.AbstractElement createDefaultRoot()
  96:   {
  97:     Element[] tmp;
  98:     BranchElement section = new BranchElement(null, null);
  99:     
 100:     BranchElement paragraph = new BranchElement(section, null);
 101:     tmp = new Element[1];
 102:     tmp[0] = paragraph;
 103:     section.replace(0, 0, tmp);
 104: 
 105:     LeafElement leaf = new LeafElement(paragraph, null, 0, 1);
 106:     tmp = new Element[1];
 107:     tmp[0] = leaf;
 108:     paragraph.replace(0, 0, tmp);
 109:     
 110:     return section;
 111:   }
 112:   
 113:   public Element getCharacterElement(int position)
 114:   {
 115:     Element element = getDefaultRootElement();
 116: 
 117:     while (! element.isLeaf())
 118:       {
 119:     int index = element.getElementIndex(position);
 120:     element = element.getElement(index);
 121:       }
 122:     
 123:     return element;
 124:   }
 125:   
 126:   public Color getBackground(AttributeSet attributes)
 127:   {
 128:     StyleContext context = (StyleContext) getAttributeContext();
 129:     return context.getBackground(attributes);
 130:   }
 131:   
 132:   public Element getDefaultRootElement()
 133:   {
 134:     return buffer.getRootElement();
 135:   }
 136:   
 137:   public Font getFont(AttributeSet attributes)
 138:   {
 139:     StyleContext context = (StyleContext) getAttributeContext();
 140:     return context.getFont(attributes);
 141:   }
 142:   
 143:   public Color getForeground(AttributeSet attributes)
 144:   {
 145:     StyleContext context = (StyleContext) getAttributeContext();
 146:     return context.getForeground(attributes);
 147:   }
 148:   
 149:   public Style getLogicalStyle(int position)
 150:   {
 151:     Element paragraph = getParagraphElement(position);
 152:     AttributeSet attributes = paragraph.getAttributes();
 153:     return (Style) attributes.getResolveParent();
 154:   }
 155:   
 156:   public Element getParagraphElement(int position)
 157:   {
 158:     Element element = getCharacterElement(position);
 159:     return element.getParentElement();
 160:   }
 161: 
 162:   public Style getStyle(String nm)
 163:   {
 164:     StyleContext context = (StyleContext) getAttributeContext();
 165:     return context.getStyle(nm);
 166:   }
 167: 
 168:   public void removeStyle(String nm)
 169:   {
 170:     StyleContext context = (StyleContext) getAttributeContext();
 171:     context.removeStyle(nm);
 172:   }
 173: 
 174:   public void setCharacterAttributes(int offset, int length,
 175:                      AttributeSet attributes,
 176:                      boolean replace)
 177:   {
 178:     // FIXME: Implement me.
 179:     throw new Error("not implemented");
 180:   }
 181:   
 182:   public void setLogicalStyle(int position, Style style)
 183:   {
 184:     Element el = getParagraphElement(position);
 185:     if (el instanceof AbstractElement)
 186:       {
 187:         AbstractElement ael = (AbstractElement) el;
 188:         ael.setResolveParent(style);
 189:       }
 190:     else
 191:       throw new AssertionError("paragraph elements are expected to be"
 192:          + "instances of javax.swing.text.AbstractDocument.AbstractElement");
 193:   }
 194: 
 195:   public void setParagraphAttributes(int offset, int length,
 196:                      AttributeSet attributes,
 197:                      boolean replace)
 198:   {
 199:     // FIXME: Implement me.
 200:     throw new Error("not implemented");
 201:   }
 202: }