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