001 /* IconView.java -- A view to render icons 002 Copyright (C) 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package javax.swing.text; 040 041 import java.awt.Graphics; 042 import java.awt.Rectangle; 043 import java.awt.Shape; 044 045 import javax.swing.Icon; 046 import javax.swing.JTextPane; 047 048 /** 049 * A View that can render an icon. This view is created by the 050 * {@link StyledEditorKit}'s view factory for all elements that have name 051 * {@link StyleConstants#IconElementName}. This is usually created by 052 * inserting an icon into <code>JTextPane</code> using 053 * {@link JTextPane#insertIcon(Icon)} 054 * 055 * The icon is determined using the attribute 056 * {@link StyleConstants#IconAttribute}, which's value must be an {@link Icon}. 057 * 058 * @author Roman Kennke (kennke@aicas.com) 059 */ 060 public class IconView 061 extends View 062 { 063 064 /** 065 * Creates a new <code>IconView</code> for the given <code>Element</code>. 066 * 067 * @param element the element that is rendered by this IconView 068 */ 069 public IconView(Element element) 070 { 071 super(element); 072 } 073 074 /** 075 * Renders the <code>Element</code> that is associated with this 076 * <code>View</code>. 077 * 078 * @param g the <code>Graphics</code> context to render to 079 * @param a the allocated region for the <code>Element</code> 080 */ 081 public void paint(Graphics g, Shape a) 082 { 083 Icon icon = StyleConstants.getIcon(getElement().getAttributes()); 084 Rectangle b = a.getBounds(); 085 icon.paintIcon(getContainer(), g, b.x, b.y); 086 } 087 088 /** 089 * Returns the preferred span of the content managed by this 090 * <code>View</code> along the specified <code>axis</code>. 091 * 092 * @param axis the axis 093 * 094 * @return the preferred span of this <code>View</code>. 095 */ 096 public float getPreferredSpan(int axis) 097 { 098 Icon icon = StyleConstants.getIcon(getElement().getAttributes()); 099 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 }