Source for javax.swing.text.html.BlockView

   1: /* BlockView.java -- 
   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.html;
  40: 
  41: import java.awt.Graphics;
  42: import java.awt.Rectangle;
  43: import java.awt.Shape;
  44: 
  45: import javax.swing.SizeRequirements;
  46: import javax.swing.event.DocumentEvent;
  47: import javax.swing.text.AttributeSet;
  48: import javax.swing.text.BoxView;
  49: import javax.swing.text.Element;
  50: import javax.swing.text.View;
  51: import javax.swing.text.ViewFactory;
  52: 
  53: /**
  54:  * @author Lillian Angel <langel@redhat.com>
  55:  */
  56: public class BlockView extends BoxView
  57: {
  58:   
  59:   /**
  60:    * Creates a new view that represents an html box. 
  61:    * This can be used for a number of elements.
  62:    * 
  63:    * @param elem - the element to create a view for
  64:    * @param axis - either View.X_AXIS or View.Y_AXIS
  65:    */
  66:   public BlockView(Element elem, int axis)
  67:   {
  68:     super(elem, axis);
  69:   }
  70:   
  71:   /**
  72:    * Creates the parent view for this. It is called before
  73:    * any other methods, if the parent view is working properly.
  74:    * Implemented to forward to the superclass and call
  75:    * setPropertiesFromAttributes to set the paragraph 
  76:    * properties.
  77:    * 
  78:    * @param parent - the new parent, or null if the view
  79:    * is being removed from a parent it was added to. 
  80:    */
  81:   public void setParent(View parent)
  82:   {
  83:     super.setParent(parent);
  84:     
  85:     if (parent != null)
  86:       setPropertiesFromAttributes();
  87:   }
  88:   
  89:   /**
  90:    * Calculates the requirements along the major axis.
  91:    * This is implemented to call the superclass and then
  92:    * adjust it if the CSS width or height attribute is specified
  93:    * and applicable.
  94:    * 
  95:    * @param axis - the axis to check the requirements for.
  96:    * @param r - the SizeRequirements. If null, one is created.
  97:    * @return the new SizeRequirements object.
  98:    */
  99:   protected SizeRequirements calculateMajorAxisRequirements(int axis,
 100:                                                             SizeRequirements r)
 101:   {
 102:     SizeRequirements sr = super.calculateMajorAxisRequirements(axis, r);
 103:     // FIXME: adjust it if the CSS width or height attribute is specified
 104:     // and applicable
 105:     return sr;
 106:   }
 107:   
 108:   /**
 109:    * Calculates the requirements along the minor axis.
 110:    * This is implemented to call the superclass and then
 111:    * adjust it if the CSS width or height attribute is specified
 112:    * and applicable.
 113:    * 
 114:    * @param axis - the axis to check the requirements for.
 115:    * @param r - the SizeRequirements. If null, one is created.
 116:    * @return the new SizeRequirements object.
 117:    */
 118:   protected SizeRequirements calculateMinorAxisRequirements(int axis,
 119:                                                             SizeRequirements r)
 120:   {
 121:     SizeRequirements sr = super.calculateMinorAxisRequirements(axis, r);
 122:     // FIXME: adjust it if the CSS width or height attribute is specified
 123:     // and applicable.
 124:     return sr;
 125:   }
 126:   
 127:   /**
 128:    * Lays out the box along the minor axis (the axis that is
 129:    * perpendicular to the axis that it represents). The results
 130:    * of the layout are placed in the given arrays which are
 131:    * the allocations to the children along the minor axis.
 132:    * 
 133:    * @param targetSpan - the total span given to the view, also 
 134:    * used to layout the children.
 135:    * @param axis - the minor axis
 136:    * @param offsets - the offsets from the origin of the view for
 137:    * all the child views. This is a return value and is filled in by this
 138:    * function.
 139:    * @param spans - the span of each child view. This is a return value and is 
 140:    * filled in by this function.
 141:    */
 142:   protected void layoutMinorAxis(int targetSpan, int axis,
 143:                                  int[] offsets, int[] spans)
 144:   {
 145:     // FIXME: Not implemented.
 146:     super.layoutMinorAxis(targetSpan, axis, offsets, spans);
 147:   }
 148:   
 149:   /**
 150:    * Paints using the given graphics configuration and shape.
 151:    * This delegates to the css box painter to paint the
 152:    * border and background prior to the interior.
 153:    * 
 154:    * @param g - Graphics configuration
 155:    * @param a - the Shape to render into.
 156:    */
 157:   public void paint(Graphics g, Shape a)
 158:   {
 159:     Rectangle rect = (Rectangle) a;
 160:     // FIXME: not fully implemented
 161:     getStyleSheet().getBoxPainter(getAttributes()).paint(g, rect.x, rect.y,
 162:                                                          rect.width,
 163:                                                          rect.height, this);
 164:     super.paint(g, a);
 165:   }
 166:   
 167:   /**
 168:    * Fetches the attributes to use when painting.
 169:    * 
 170:    * @return the attributes of this model.
 171:    */
 172:   public AttributeSet getAttributes()
 173:   {
 174:     return getStyleSheet().getViewAttributes(this);
 175:   }
 176:   
 177:   /**
 178:    * Gets the resize weight.
 179:    * 
 180:    * @param axis - the axis to get the resize weight for.
 181:    * @return the resize weight.
 182:    * @throws IllegalArgumentException - for an invalid axis
 183:    */
 184:   public int getResizeWeight(int axis) throws IllegalArgumentException
 185:   {
 186:     // Can't resize the Y_AXIS
 187:     if (axis == Y_AXIS)
 188:       return 0;
 189:     if (axis == X_AXIS)
 190:       return 1;
 191:     throw new IllegalArgumentException("Invalid Axis");
 192:   }
 193:   
 194:   /**
 195:    * Gets the alignment.
 196:    * 
 197:    * @param axis - the axis to get the alignment for.
 198:    * @return the alignment.
 199:    */
 200:   public float getAlignment(int axis)
 201:   {
 202:     if (axis == X_AXIS)
 203:       return 0.0F;
 204:     if (axis == Y_AXIS)
 205:       {
 206:         if (getViewCount() == 0)
 207:           return 0.0F;
 208:         float prefHeight = getPreferredSpan(Y_AXIS);
 209:         float firstRowHeight = getView(0).getPreferredSpan(Y_AXIS);
 210:         return (firstRowHeight / 2.F) / prefHeight;
 211:       }
 212:     throw new IllegalArgumentException("Invalid Axis");
 213:   }
 214:   
 215:   /**
 216:    * Gives notification from the document that attributes were
 217:    * changed in a location that this view is responsible for.
 218:    * 
 219:    * @param ev - the change information
 220:    * @param a - the current shape of the view
 221:    * @param f - the factory to use to rebuild if the view has children.
 222:    */
 223:   public void changedUpdate(DocumentEvent ev,
 224:                             Shape a, ViewFactory f)
 225:   {
 226:     super.changedUpdate(ev, a, f);
 227:     
 228:     // If more elements were added, then need to set the properties for them
 229:     int currPos = ev.getOffset();
 230:     if (currPos <= getStartOffset() && (currPos + ev.getLength()) >= getEndOffset())
 231:         setPropertiesFromAttributes();
 232:   }
 233: 
 234:   /**
 235:    * Determines the preferred span along the axis.
 236:    * 
 237:    * @param axis - the view to get the preferred span for.
 238:    * @return the span the view would like to be painted into >=0/
 239:    * The view is usually told to paint into the span that is returned, 
 240:    * although the parent may choose to resize or break the view.
 241:    * @throws IllegalArgumentException - for an invalid axis
 242:    */
 243:   public float getPreferredSpan(int axis) throws IllegalArgumentException
 244:   {
 245:     if (axis == X_AXIS || axis == Y_AXIS)
 246:       return super.getPreferredSpan(axis);
 247:     throw new IllegalArgumentException("Invalid Axis");
 248:   }
 249:   
 250:   /**
 251:    * Determines the minimum span along the axis.
 252:    * 
 253:    * @param axis - the axis to get the minimum span for.
 254:    * @return the span the view would like to be painted into >=0/
 255:    * The view is usually told to paint into the span that is returned, 
 256:    * although the parent may choose to resize or break the view.
 257:    * @throws IllegalArgumentException - for an invalid axis
 258:    */
 259:   public float getMinimumSpan(int axis) throws IllegalArgumentException
 260:   {
 261:     if (axis == X_AXIS || axis == Y_AXIS)
 262:       return super.getMinimumSpan(axis);
 263:     throw new IllegalArgumentException("Invalid Axis");
 264:   }
 265:   
 266:   /**
 267:    * Determines the maximum span along the axis.
 268:    * 
 269:    * @param axis - the axis to get the maximum span for.
 270:    * @return the span the view would like to be painted into >=0/
 271:    * The view is usually told to paint into the span that is returned, 
 272:    * although the parent may choose to resize or break the view.
 273:    * @throws IllegalArgumentException - for an invalid axis
 274:    */
 275:   public float getMaximumSpan(int axis) throws IllegalArgumentException
 276:   {
 277:     if (axis == X_AXIS || axis == Y_AXIS)
 278:       return super.getMaximumSpan(axis);
 279:     throw new IllegalArgumentException("Invalid Axis");
 280:   }
 281:   
 282:   /**
 283:    * Updates any cached values that come from attributes.
 284:    */
 285:   protected void setPropertiesFromAttributes()
 286:   {
 287:     // FIXME: Not implemented (need to use StyleSheet).
 288:   }
 289:   
 290:   /**
 291:    * Gets the default style sheet.
 292:    * 
 293:    * @return the style sheet
 294:    */
 295:   protected StyleSheet getStyleSheet()
 296:   {
 297:     StyleSheet styleSheet = new StyleSheet();
 298:     styleSheet.importStyleSheet(getClass().getResource(HTMLEditorKit.DEFAULT_CSS));
 299:     return styleSheet;
 300:   }
 301: }