KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > IconView


1 /*
2  * @(#)IconView.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.awt.*;
10 import javax.swing.Icon JavaDoc;
11 import javax.swing.event.*;
12
13 /**
14  * Icon decorator that implements the view interface. The
15  * entire element is used to represent the icon. This acts
16  * as a gateway from the display-only View implementations to
17  * interactive lightweight icons (that is, it allows icons
18  * to be embedded into the View hierarchy. The parent of the icon
19  * is the container that is handed out by the associated view
20  * factory.
21  *
22  * @author Timothy Prinzing
23  * @version 1.28 12/19/03
24  */

25 public class IconView extends View JavaDoc {
26
27     /**
28      * Creates a new icon view that represents an element.
29      *
30      * @param elem the element to create a view for
31      */

32     public IconView(Element JavaDoc elem) {
33     super(elem);
34     AttributeSet JavaDoc attr = elem.getAttributes();
35     c = StyleConstants.getIcon(attr);
36     }
37
38     // --- View methods ---------------------------------------------
39

40     /**
41      * Paints the icon.
42      * The real paint behavior occurs naturally from the association
43      * that the icon has with its parent container (the same
44      * container hosting this view), so this simply allows us to
45      * position the icon properly relative to the view. Since
46      * the coordinate system for the view is simply the parent
47      * containers, positioning the child icon is easy.
48      *
49      * @param g the rendering surface to use
50      * @param a the allocated region to render into
51      * @see View#paint
52      */

53     public void paint(Graphics g, Shape a) {
54     Rectangle alloc = a.getBounds();
55     c.paintIcon(getContainer(), g, alloc.x, alloc.y);
56     }
57
58     /**
59      * Determines the preferred span for this view along an
60      * axis.
61      *
62      * @param axis may be either View.X_AXIS or View.Y_AXIS
63      * @return the span the view would like to be rendered into
64      * Typically the view is told to render into the span
65      * that is returned, although there is no guarantee.
66      * The parent may choose to resize or break the view.
67      * @exception IllegalArgumentException for an invalid axis
68      */

69     public float getPreferredSpan(int axis) {
70     switch (axis) {
71     case View.X_AXIS:
72         return c.getIconWidth();
73     case View.Y_AXIS:
74         return c.getIconHeight();
75     default:
76         throw new IllegalArgumentException JavaDoc("Invalid axis: " + axis);
77     }
78     }
79
80     /**
81      * Determines the desired alignment for this view along an
82      * axis. This is implemented to give the alignment to the
83      * bottom of the icon along the y axis, and the default
84      * along the x axis.
85      *
86      * @param axis may be either View.X_AXIS or View.Y_AXIS
87      * @return the desired alignment >= 0.0f && <= 1.0f. This should be
88      * a value between 0.0 and 1.0 where 0 indicates alignment at the
89      * origin and 1.0 indicates alignment to the full span
90      * away from the origin. An alignment of 0.5 would be the
91      * center of the view.
92      */

93     public float getAlignment(int axis) {
94     switch (axis) {
95     case View.Y_AXIS:
96         return 1;
97     default:
98         return super.getAlignment(axis);
99     }
100     }
101
102     /**
103      * Provides a mapping from the document model coordinate space
104      * to the coordinate space of the view mapped to it.
105      *
106      * @param pos the position to convert >= 0
107      * @param a the allocated region to render into
108      * @return the bounding box of the given position
109      * @exception BadLocationException if the given position does not
110      * represent a valid location in the associated document
111      * @see View#modelToView
112      */

113     public Shape modelToView(int pos, Shape a, Position.Bias JavaDoc b) throws BadLocationException JavaDoc {
114     int p0 = getStartOffset();
115     int p1 = getEndOffset();
116     if ((pos >= p0) && (pos <= p1)) {
117         Rectangle r = a.getBounds();
118         if (pos == p1) {
119         r.x += r.width;
120         }
121         r.width = 0;
122         return r;
123     }
124     throw new BadLocationException JavaDoc(pos + " not in range " + p0 + "," + p1, pos);
125     }
126
127     /**
128      * Provides a mapping from the view coordinate space to the logical
129      * coordinate space of the model.
130      *
131      * @param x the X coordinate >= 0
132      * @param y the Y coordinate >= 0
133      * @param a the allocated region to render into
134      * @return the location within the model that best represents the
135      * given point of view >= 0
136      * @see View#viewToModel
137      */

138     public int viewToModel(float x, float y, Shape a, Position.Bias JavaDoc[] bias) {
139     Rectangle alloc = (Rectangle) a;
140     if (x < alloc.x + (alloc.width / 2)) {
141         bias[0] = Position.Bias.Forward;
142         return getStartOffset();
143     }
144     bias[0] = Position.Bias.Backward;
145     return getEndOffset();
146     }
147
148     // --- member variables ------------------------------------------------
149

150     private Icon JavaDoc c;
151 }
152
153
Popular Tags