Source for javax.swing.text.ParagraphView

   1: /* ParagraphView.java -- A composite View
   2:    Copyright (C) 2005  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.Shape;
  42: 
  43: import javax.swing.event.DocumentEvent;
  44: 
  45: /**
  46:  * A {@link FlowView} that flows it's children horizontally and boxes the rows
  47:  * vertically.
  48:  *
  49:  * @author Roman Kennke (roman@kennke.org)
  50:  */
  51: public class ParagraphView extends FlowView implements TabExpander
  52: {
  53:   /**
  54:    * A specialized horizontal <code>BoxView</code> that represents exactly
  55:    * one row in a <code>ParagraphView</code>.
  56:    */
  57:   class Row extends BoxView
  58:   {
  59:     /**
  60:      * Creates a new instance of <code>Row</code>.
  61:      */
  62:     Row(Element el)
  63:     {
  64:       super(el, X_AXIS);
  65:     }
  66:     public float getAlignment(int axis)
  67:     {
  68:       // FIXME: This is very likely not 100% correct. Work this out.
  69:       return 0.0F;
  70:     }
  71:   }
  72: 
  73:   /**
  74:    * The indentation of the first line of the paragraph.
  75:    */
  76:   protected int firstLineIndent;
  77: 
  78:   /**
  79:    * The justification of the paragraph.
  80:    */
  81:   private int justification;
  82: 
  83:   /**
  84:    * The line spacing of this paragraph.
  85:    */
  86:   private float lineSpacing;
  87: 
  88:   /**
  89:    * The TabSet of this paragraph.
  90:    */
  91:   private TabSet tabSet;
  92: 
  93:   /**
  94:    * Creates a new <code>ParagraphView</code> for the given
  95:    * <code>Element</code>.
  96:    *
  97:    * @param element the element that is rendered by this ParagraphView
  98:    */
  99:   public ParagraphView(Element element)
 100:   {
 101:     super(element, Y_AXIS);
 102:   }
 103: 
 104:   public float nextTabStop(float x, int tabOffset)
 105:   {
 106:     throw new InternalError("Not implemented yet");
 107:   }
 108: 
 109:   /**
 110:    * Creates a new view that represents a row within a flow.
 111:    *
 112:    * @return a view for a new row
 113:    */
 114:   protected View createRow()
 115:   {
 116:     return new Row(getElement());
 117:   }
 118: 
 119:   /**
 120:    * Returns the alignment for this paragraph view for the specified axis.
 121:    * For the X_AXIS the paragraph view will be aligned at it's left edge
 122:    * (0.0F). For the Y_AXIS the paragraph view will be aligned at the
 123:    * center of it's first row.
 124:    *
 125:    * @param axis the axis which is examined
 126:    *
 127:    * @return the alignment for this paragraph view for the specified axis
 128:    */
 129:   public float getAlignment(int axis)
 130:   {
 131:     if (axis == X_AXIS)
 132:       return 0.0F;
 133:     else if (getViewCount() > 0)
 134:       {
 135: 
 136:         float prefHeight = getPreferredSpan(Y_AXIS);
 137:         float firstRowHeight = getView(0).getPreferredSpan(Y_AXIS);
 138:         return (firstRowHeight / 2.F) / prefHeight;
 139:       }
 140:     else
 141:       return 0.0F;
 142:   }
 143: 
 144:   /**
 145:    * Receives notification when some attributes of the displayed element
 146:    * changes. This triggers a refresh of the cached attributes of this
 147:    * paragraph.
 148:    *
 149:    * @param ev the document event
 150:    * @param a the allocation of this view
 151:    * @param fv the view factory to use for creating new child views
 152:    */
 153:   public void changedUpdate(DocumentEvent ev, Shape a, ViewFactory fv)
 154:   {
 155:     setPropertiesFromAttributes();
 156:   }
 157: 
 158:   /**
 159:    * Fetches the cached properties from the element's attributes.
 160:    */
 161:   protected void setPropertiesFromAttributes()
 162:   {
 163:     Element el = getElement();
 164:     AttributeSet atts = el.getAttributes();
 165:     setFirstLineIndent(StyleConstants.getFirstLineIndent(atts));
 166:     setLineSpacing(StyleConstants.getLineSpacing(atts));
 167:     setJustification(StyleConstants.getAlignment(atts));
 168:     tabSet = StyleConstants.getTabSet(atts);
 169:   }
 170: 
 171:   /**
 172:    * Sets the indentation of the first line of the paragraph.
 173:    *
 174:    * @param i the indentation to set
 175:    */
 176:   protected void setFirstLineIndent(float i)
 177:   {
 178:     firstLineIndent = (int) i;
 179:   }
 180: 
 181:   /**
 182:    * Sets the justification of the paragraph.
 183:    *
 184:    * @param j the justification to set 
 185:    */
 186:   protected void setJustification(int j)
 187:   {
 188:     justification = j;
 189:   }
 190: 
 191:   /**
 192:    * Sets the line spacing for this paragraph.
 193:    *
 194:    * @param s the line spacing to set
 195:    */
 196:   protected void setLineSpacing(float s)
 197:   {
 198:     lineSpacing = s;
 199:   }
 200: 
 201:   /**
 202:    * Returns the i-th view from the logical views, before breaking into rows.
 203:    *
 204:    * @param i the index of the logical view to return
 205:    *
 206:    * @return the i-th view from the logical views, before breaking into rows
 207:    */
 208:   protected View getLayoutView(int i)
 209:   {
 210:     return layoutPool.getView(i);
 211:   }
 212: 
 213:   /**
 214:    * Returns the number of logical child views.
 215:    *
 216:    * @return the number of logical child views
 217:    */
 218:   protected int getLayoutViewCount()
 219:   {
 220:     return layoutPool.getViewCount();
 221:   }
 222: 
 223:   /**
 224:    * Returns the TabSet used by this ParagraphView.
 225:    *
 226:    * @return the TabSet used by this ParagraphView
 227:    */
 228:   protected TabSet getTabSet()
 229:   {
 230:     return tabSet;
 231:   }
 232: }