1 |
14 |
jlechner |
/* FieldView.java --
|
2 |
|
|
Copyright (C) 2004 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.Component;
|
42 |
|
|
import java.awt.ComponentOrientation;
|
43 |
|
|
import java.awt.FontMetrics;
|
44 |
|
|
import java.awt.Graphics;
|
45 |
|
|
import java.awt.Rectangle;
|
46 |
|
|
import java.awt.Shape;
|
47 |
|
|
|
48 |
|
|
import javax.swing.JTextField;
|
49 |
|
|
import javax.swing.event.DocumentEvent;
|
50 |
|
|
|
51 |
|
|
public class FieldView extends PlainView
|
52 |
|
|
{
|
53 |
|
|
public FieldView(Element elem)
|
54 |
|
|
{
|
55 |
|
|
super(elem);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
protected FontMetrics getFontMetrics()
|
59 |
|
|
{
|
60 |
|
|
Component container = getContainer();
|
61 |
|
|
return container.getFontMetrics(container.getFont());
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* Vertically centers the single line of text within the
|
66 |
|
|
* bounds of the input shape. The returned Rectangle is centered
|
67 |
|
|
* vertically within <code>shape</code> and has a height of the
|
68 |
|
|
* preferred span along the Y axis. Horizontal adjustment is done according
|
69 |
|
|
* to the horizontalAligment property of the component that is rendered.
|
70 |
|
|
*
|
71 |
|
|
* @param shape the shape within which the line is beeing centered
|
72 |
|
|
*/
|
73 |
|
|
protected Shape adjustAllocation(Shape shape)
|
74 |
|
|
{
|
75 |
|
|
Rectangle rectIn = shape.getBounds();
|
76 |
|
|
// vertical adjustment
|
77 |
|
|
int height = (int) getPreferredSpan(Y_AXIS);
|
78 |
|
|
int y = rectIn.y + (rectIn.height - height) / 2;
|
79 |
|
|
// horizontal adjustment
|
80 |
|
|
JTextField textField = (JTextField) getContainer();
|
81 |
|
|
int halign = textField.getHorizontalAlignment();
|
82 |
|
|
int width = (int) getPreferredSpan(X_AXIS);
|
83 |
|
|
int x;
|
84 |
|
|
ComponentOrientation orientation = textField.getComponentOrientation();
|
85 |
|
|
switch (halign)
|
86 |
|
|
{
|
87 |
|
|
case JTextField.CENTER:
|
88 |
|
|
x = rectIn.x + (rectIn.width - width) / 2;
|
89 |
|
|
break;
|
90 |
|
|
case JTextField.RIGHT:
|
91 |
|
|
x = rectIn.x + (rectIn.width - width);
|
92 |
|
|
break;
|
93 |
|
|
case JTextField.TRAILING:
|
94 |
|
|
if (orientation.isLeftToRight())
|
95 |
|
|
x = rectIn.x + (rectIn.width - width);
|
96 |
|
|
else
|
97 |
|
|
x = rectIn.x;
|
98 |
|
|
break;
|
99 |
|
|
case JTextField.LEADING:
|
100 |
|
|
if (orientation.isLeftToRight())
|
101 |
|
|
x = rectIn.x;
|
102 |
|
|
else
|
103 |
|
|
x = rectIn.x + (rectIn.width - width);
|
104 |
|
|
break;
|
105 |
|
|
case JTextField.LEFT:
|
106 |
|
|
default:
|
107 |
|
|
x = rectIn.x;
|
108 |
|
|
break;
|
109 |
|
|
}
|
110 |
|
|
return new Rectangle(x, y, width, height);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
public float getPreferredSpan(int axis)
|
114 |
|
|
{
|
115 |
|
|
if (axis != X_AXIS && axis != Y_AXIS)
|
116 |
|
|
throw new IllegalArgumentException();
|
117 |
|
|
|
118 |
|
|
FontMetrics fm = getFontMetrics();
|
119 |
|
|
|
120 |
|
|
if (axis == Y_AXIS)
|
121 |
|
|
return super.getPreferredSpan(axis);
|
122 |
|
|
|
123 |
|
|
String text;
|
124 |
|
|
Element elem = getElement();
|
125 |
|
|
|
126 |
|
|
try
|
127 |
|
|
{
|
128 |
|
|
text = elem.getDocument().getText(elem.getStartOffset(),
|
129 |
|
|
elem.getEndOffset());
|
130 |
|
|
}
|
131 |
|
|
catch (BadLocationException e)
|
132 |
|
|
{
|
133 |
|
|
// Should never happen
|
134 |
|
|
AssertionError ae = new AssertionError();
|
135 |
|
|
ae.initCause(e);
|
136 |
|
|
throw ae;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
return fm.stringWidth(text);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
public int getResizeWeight(int axis)
|
143 |
|
|
{
|
144 |
|
|
return axis = axis == X_AXIS ? 1 : 0;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
public Shape modelToView(int pos, Shape a, Position.Bias bias)
|
148 |
|
|
throws BadLocationException
|
149 |
|
|
{
|
150 |
|
|
Shape newAlloc = adjustAllocation(a);
|
151 |
|
|
return super.modelToView(pos, newAlloc, bias);
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
public void paint(Graphics g, Shape s)
|
155 |
|
|
{
|
156 |
|
|
Shape newAlloc = adjustAllocation(s);
|
157 |
|
|
super.paint(g, newAlloc);
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
|
161 |
|
|
{
|
162 |
|
|
Shape newAlloc = adjustAllocation(shape);
|
163 |
|
|
super.insertUpdate(ev, newAlloc, vf);
|
164 |
|
|
getContainer().repaint();
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
|
168 |
|
|
{
|
169 |
|
|
Shape newAlloc = adjustAllocation(shape);
|
170 |
|
|
super.removeUpdate(ev, newAlloc, vf);
|
171 |
|
|
getContainer().repaint();
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
|
175 |
|
|
{
|
176 |
|
|
Shape newAlloc = adjustAllocation(shape);
|
177 |
|
|
super.removeUpdate(ev, newAlloc, vf);
|
178 |
|
|
getContainer().repaint();
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
|
182 |
|
|
{
|
183 |
|
|
return super.viewToModel(fx, fy, a, bias);
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
}
|