2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
package javax.swing.text;
|
|
26 |
|
|
27 |
import java.awt.*;
|
|
28 |
import javax.swing.Icon;
|
|
29 |
import javax.swing.event.*;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Icon decorator that implements the view interface. The
|
|
33 |
* entire element is used to represent the icon. This acts
|
|
34 |
* as a gateway from the display-only View implementations to
|
|
35 |
* interactive lightweight icons (that is, it allows icons
|
|
36 |
* to be embedded into the View hierarchy. The parent of the icon
|
|
37 |
* is the container that is handed out by the associated view
|
|
38 |
* factory.
|
|
39 |
*
|
|
40 |
* @author Timothy Prinzing
|
|
41 |
*/
|
|
42 |
public class IconView extends View {
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Creates a new icon view that represents an element.
|
|
46 |
*
|
|
47 |
* @param elem the element to create a view for
|
|
48 |
*/
|
|
49 |
public IconView(Element elem) {
|
|
50 |
super(elem);
|
|
51 |
AttributeSet attr = elem.getAttributes();
|
|
52 |
c = StyleConstants.getIcon(attr);
|
|
53 |
}
|
|
54 |
|
|
55 |
// --- View methods ---------------------------------------------
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Paints the icon.
|
|
59 |
* The real paint behavior occurs naturally from the association
|
|
60 |
* that the icon has with its parent container (the same
|
|
61 |
* container hosting this view), so this simply allows us to
|
|
62 |
* position the icon properly relative to the view. Since
|
|
63 |
* the coordinate system for the view is simply the parent
|
|
64 |
* containers, positioning the child icon is easy.
|
|
65 |
*
|
|
66 |
* @param g the rendering surface to use
|
|
67 |
* @param a the allocated region to render into
|
|
68 |
* @see View#paint
|
|
69 |
*/
|
|
70 |
public void paint(Graphics g, Shape a) {
|
|
71 |
Rectangle alloc = a.getBounds();
|
|
72 |
c.paintIcon(getContainer(), g, alloc.x, alloc.y);
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Determines the preferred span for this view along an
|
|
77 |
* axis.
|
|
78 |
*
|
|
79 |
* @param axis may be either View.X_AXIS or View.Y_AXIS
|
|
80 |
* @return the span the view would like to be rendered into
|
|
81 |
* Typically the view is told to render into the span
|
|
82 |
* that is returned, although there is no guarantee.
|
|
83 |
* The parent may choose to resize or break the view.
|
|
84 |
* @exception IllegalArgumentException for an invalid axis
|
|
85 |
*/
|
|
86 |
public float getPreferredSpan(int axis) {
|
|
87 |
switch (axis) {
|
|
88 |
case View.X_AXIS:
|
|
89 |
return c.getIconWidth();
|
|
90 |
case View.Y_AXIS:
|
|
91 |
return c.getIconHeight();
|
|
92 |
default:
|
|
93 |
throw new IllegalArgumentException("Invalid axis: " + axis);
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Determines the desired alignment for this view along an
|
|
99 |
* axis. This is implemented to give the alignment to the
|
|
100 |
* bottom of the icon along the y axis, and the default
|
|
101 |
* along the x axis.
|
|
102 |
*
|
|
103 |
* @param axis may be either View.X_AXIS or View.Y_AXIS
|
|
104 |
* @return the desired alignment >= 0.0f && <= 1.0f. This should be
|
|
105 |
* a value between 0.0 and 1.0 where 0 indicates alignment at the
|
|
106 |
* origin and 1.0 indicates alignment to the full span
|
|
107 |
* away from the origin. An alignment of 0.5 would be the
|
|
108 |
* center of the view.
|
|
109 |
*/
|
|
110 |
public float getAlignment(int axis) {
|
|
111 |
switch (axis) {
|
|
112 |
case View.Y_AXIS:
|
|
113 |
return 1;
|
|
114 |
default:
|
|
115 |
return super.getAlignment(axis);
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Provides a mapping from the document model coordinate space
|
|
121 |
* to the coordinate space of the view mapped to it.
|
|
122 |
*
|
|
123 |
* @param pos the position to convert >= 0
|
|
124 |
* @param a the allocated region to render into
|
|
125 |
* @return the bounding box of the given position
|
|
126 |
* @exception BadLocationException if the given position does not
|
|
127 |
* represent a valid location in the associated document
|
|
128 |
* @see View#modelToView
|
|
129 |
*/
|
|
130 |
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
|
|
131 |
int p0 = getStartOffset();
|
|
132 |
int p1 = getEndOffset();
|
|
133 |
if ((pos >= p0) && (pos <= p1)) {
|
|
134 |
Rectangle r = a.getBounds();
|
|
135 |
if (pos == p1) {
|
|
136 |
r.x += r.width;
|
|
137 |
}
|
|
138 |
r.width = 0;
|
|
139 |
return r;
|
|
140 |
}
|
|
141 |
throw new BadLocationException(pos + " not in range " + p0 + "," + p1, pos);
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Provides a mapping from the view coordinate space to the logical
|
|
146 |
* coordinate space of the model.
|
|
147 |
*
|
|
148 |
* @param x the X coordinate >= 0
|
|
149 |
* @param y the Y coordinate >= 0
|
|
150 |
* @param a the allocated region to render into
|
|
151 |
* @return the location within the model that best represents the
|
|
152 |
* given point of view >= 0
|
|
153 |
* @see View#viewToModel
|
|
154 |
*/
|
|
155 |
public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
|
|
156 |
Rectangle alloc = (Rectangle) a;
|
|
157 |
if (x < alloc.x + (alloc.width / 2)) {
|
|
158 |
bias[0] = Position.Bias.Forward;
|
|
159 |
return getStartOffset();
|
|
160 |
}
|
|
161 |
bias[0] = Position.Bias.Backward;
|
|
162 |
return getEndOffset();
|
|
163 |
}
|
|
164 |
|
|
165 |
// --- member variables ------------------------------------------------
|
|
166 |
|
|
167 |
private Icon c;
|
|
168 |
}
|