001 /*
002 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.text;
026
027 import java.awt.*;
028 import javax.swing.Icon;
029 import javax.swing.event.*;
030
031 /**
032 * Icon decorator that implements the view interface. The
033 * entire element is used to represent the icon. This acts
034 * as a gateway from the display-only View implementations to
035 * interactive lightweight icons (that is, it allows icons
036 * to be embedded into the View hierarchy. The parent of the icon
037 * is the container that is handed out by the associated view
038 * factory.
039 *
040 * @author Timothy Prinzing
041 * @version 1.35 05/05/07
042 */
043 public class IconView extends View {
044
045 /**
046 * Creates a new icon view that represents an element.
047 *
048 * @param elem the element to create a view for
049 */
050 public IconView(Element elem) {
051 super (elem);
052 AttributeSet attr = elem.getAttributes();
053 c = StyleConstants.getIcon(attr);
054 }
055
056 // --- View methods ---------------------------------------------
057
058 /**
059 * Paints the icon.
060 * The real paint behavior occurs naturally from the association
061 * that the icon has with its parent container (the same
062 * container hosting this view), so this simply allows us to
063 * position the icon properly relative to the view. Since
064 * the coordinate system for the view is simply the parent
065 * containers, positioning the child icon is easy.
066 *
067 * @param g the rendering surface to use
068 * @param a the allocated region to render into
069 * @see View#paint
070 */
071 public void paint(Graphics g, Shape a) {
072 Rectangle alloc = a.getBounds();
073 c.paintIcon(getContainer(), g, alloc.x, alloc.y);
074 }
075
076 /**
077 * Determines the preferred span for this view along an
078 * axis.
079 *
080 * @param axis may be either View.X_AXIS or View.Y_AXIS
081 * @return the span the view would like to be rendered into
082 * Typically the view is told to render into the span
083 * that is returned, although there is no guarantee.
084 * The parent may choose to resize or break the view.
085 * @exception IllegalArgumentException for an invalid axis
086 */
087 public float getPreferredSpan(int axis) {
088 switch (axis) {
089 case View.X_AXIS:
090 return c.getIconWidth();
091 case View.Y_AXIS:
092 return c.getIconHeight();
093 default:
094 throw new IllegalArgumentException("Invalid axis: " + axis);
095 }
096 }
097
098 /**
099 * Determines the desired alignment for this view along an
100 * axis. This is implemented to give the alignment to the
101 * bottom of the icon along the y axis, and the default
102 * along the x axis.
103 *
104 * @param axis may be either View.X_AXIS or View.Y_AXIS
105 * @return the desired alignment >= 0.0f && <= 1.0f. This should be
106 * a value between 0.0 and 1.0 where 0 indicates alignment at the
107 * origin and 1.0 indicates alignment to the full span
108 * away from the origin. An alignment of 0.5 would be the
109 * center of the view.
110 */
111 public float getAlignment(int axis) {
112 switch (axis) {
113 case View.Y_AXIS:
114 return 1;
115 default:
116 return super .getAlignment(axis);
117 }
118 }
119
120 /**
121 * Provides a mapping from the document model coordinate space
122 * to the coordinate space of the view mapped to it.
123 *
124 * @param pos the position to convert >= 0
125 * @param a the allocated region to render into
126 * @return the bounding box of the given position
127 * @exception BadLocationException if the given position does not
128 * represent a valid location in the associated document
129 * @see View#modelToView
130 */
131 public Shape modelToView(int pos, Shape a, Position.Bias b)
132 throws BadLocationException {
133 int p0 = getStartOffset();
134 int p1 = getEndOffset();
135 if ((pos >= p0) && (pos <= p1)) {
136 Rectangle r = a.getBounds();
137 if (pos == p1) {
138 r.x += r.width;
139 }
140 r.width = 0;
141 return r;
142 }
143 throw new BadLocationException(pos + " not in range " + p0
144 + "," + p1, pos);
145 }
146
147 /**
148 * Provides a mapping from the view coordinate space to the logical
149 * coordinate space of the model.
150 *
151 * @param x the X coordinate >= 0
152 * @param y the Y coordinate >= 0
153 * @param a the allocated region to render into
154 * @return the location within the model that best represents the
155 * given point of view >= 0
156 * @see View#viewToModel
157 */
158 public int viewToModel(float x, float y, Shape a,
159 Position.Bias[] bias) {
160 Rectangle alloc = (Rectangle) a;
161 if (x < alloc.x + (alloc.width / 2)) {
162 bias[0] = Position.Bias.Forward;
163 return getStartOffset();
164 }
165 bias[0] = Position.Bias.Backward;
166 return getEndOffset();
167 }
168
169 // --- member variables ------------------------------------------------
170
171 private Icon c;
172 }
|