Source for javax.swing.text.IconView

   1: /* IconView.java -- A view to render icons
   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.Graphics;
  42: import java.awt.Rectangle;
  43: import java.awt.Shape;
  44: 
  45: import javax.swing.Icon;
  46: import javax.swing.JTextPane;
  47: import javax.swing.SwingConstants;
  48: 
  49: /**
  50:  * A View that can render an icon. This view is created by the
  51:  * {@link StyledEditorKit}'s view factory for all elements that have name
  52:  * {@link StyleConstants#IconElementName}. This is usually created by
  53:  * inserting an icon into <code>JTextPane</code> using
  54:  * {@link JTextPane#insertIcon(Icon)}
  55:  *
  56:  * The icon is determined using the attribute
  57:  * {@link StyleConstants#IconAttribute}, which's value must be an {@link Icon}.
  58:  *
  59:  * @author Roman Kennke (kennke@aicas.com)
  60:  */
  61: public class IconView
  62:   extends View
  63: {
  64: 
  65:   /**
  66:    * Creates a new <code>IconView</code> for the given <code>Element</code>.
  67:    *
  68:    * @param element the element that is rendered by this IconView
  69:    */
  70:   public IconView(Element element)
  71:   {
  72:     super(element);
  73:   }
  74: 
  75:   /**
  76:    * Renders the <code>Element</code> that is associated with this
  77:    * <code>View</code>.
  78:    *
  79:    * @param g the <code>Graphics</code> context to render to
  80:    * @param a the allocated region for the <code>Element</code>
  81:    */
  82:   public void paint(Graphics g, Shape a)
  83:   {
  84:     Icon icon = StyleConstants.getIcon(getElement().getAttributes());
  85:     Rectangle b = a.getBounds();
  86:     icon.paintIcon(getContainer(), g, b.x, b.y);
  87:   }
  88: 
  89:   /**
  90:    * Returns the preferred span of the content managed by this
  91:    * <code>View</code> along the specified <code>axis</code>.
  92:    *
  93:    * @param axis the axis
  94:    *
  95:    * @return the preferred span of this <code>View</code>.
  96:    */
  97:   public float getPreferredSpan(int axis)
  98:   {
  99:     Icon icon = StyleConstants.getIcon(getElement().getAttributes());
 100:     float span;
 101:     if (axis == X_AXIS)
 102:       span = icon.getIconWidth();
 103:     else if (axis == Y_AXIS)
 104:       span = icon.getIconHeight();
 105:     else
 106:       throw new IllegalArgumentException();
 107:     return span;
 108:   }
 109: 
 110:   /**
 111:    * Maps a position in the document into the coordinate space of the View.
 112:    * The output rectangle usually reflects the font height but has a width
 113:    * of zero.
 114:    *
 115:    * @param pos the position of the character in the model
 116:    * @param a the area that is occupied by the view
 117:    * @param b either {@link Position.Bias#Forward} or
 118:    *        {@link Position.Bias#Backward} depending on the preferred
 119:    *        direction bias. If <code>null</code> this defaults to
 120:    *        <code>Position.Bias.Forward</code>
 121:    *
 122:    * @return a rectangle that gives the location of the document position
 123:    *         inside the view coordinate space
 124:    *
 125:    * @throws BadLocationException if <code>pos</code> is invalid
 126:    * @throws IllegalArgumentException if b is not one of the above listed
 127:    *         valid values
 128:    */
 129:   public Shape modelToView(int pos, Shape a, Position.Bias b)
 130:     throws BadLocationException
 131:   {
 132:     Element el = getElement();
 133:     if (pos < el.getStartOffset() || pos >= el.getEndOffset())
 134:       throw new BadLocationException("Illegal offset for this view", pos);
 135:     Rectangle r = a.getBounds();
 136:     Icon icon = StyleConstants.getIcon(el.getAttributes());
 137:     return new Rectangle(r.x, r.y, icon.getIconWidth(), icon.getIconHeight());
 138:   }
 139: 
 140:   /**
 141:    * Maps coordinates from the <code>View</code>'s space into a position
 142:    * in the document model.
 143:    *
 144:    * @param x the x coordinate in the view space
 145:    * @param y the y coordinate in the view space
 146:    * @param a the allocation of this <code>View</code>
 147:    * @param b the bias to use
 148:    *
 149:    * @return the position in the document that corresponds to the screen
 150:    *         coordinates <code>x, y</code>
 151:    */
 152:   public int viewToModel(float x, float y, Shape a, Position.Bias[] b)
 153:   {
 154:     // The element should only have one character position and it is clear
 155:     // that this position is the position that best matches the given screen
 156:     // coordinates, simply because this view has only this one position.
 157:     Element el = getElement();
 158:     return el.getStartOffset();
 159:   }
 160: 
 161: }