39 |
package javax.swing.text; |
package javax.swing.text; |
40 |
|
|
41 |
import java.awt.Component; |
import java.awt.Component; |
42 |
|
import java.awt.ComponentOrientation; |
43 |
import java.awt.FontMetrics; |
import java.awt.FontMetrics; |
44 |
import java.awt.Graphics; |
import java.awt.Graphics; |
45 |
|
import java.awt.Rectangle; |
46 |
import java.awt.Shape; |
import java.awt.Shape; |
47 |
|
|
48 |
|
import javax.swing.JTextField; |
49 |
|
import javax.swing.event.DocumentEvent; |
50 |
|
|
51 |
public class FieldView extends PlainView |
public class FieldView extends PlainView |
52 |
{ |
{ |
53 |
public FieldView(Element elem) |
public FieldView(Element elem) |
61 |
return container.getFontMetrics(container.getFont()); |
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) |
public float getPreferredSpan(int axis) |
114 |
{ |
{ |
115 |
if (axis != X_AXIS && axis != Y_AXIS) |
if (axis != X_AXIS && axis != Y_AXIS) |
145 |
public Shape modelToView(int pos, Shape a, Position.Bias bias) |
public Shape modelToView(int pos, Shape a, Position.Bias bias) |
146 |
throws BadLocationException |
throws BadLocationException |
147 |
{ |
{ |
148 |
return super.modelToView(pos, a, bias); |
Shape newAlloc = adjustAllocation(a); |
149 |
|
return super.modelToView(pos, newAlloc, bias); |
150 |
} |
} |
151 |
|
|
152 |
public void paint(Graphics g, Shape s) |
public void paint(Graphics g, Shape s) |
153 |
{ |
{ |
154 |
super.paint(g, s); |
Shape newAlloc = adjustAllocation(s); |
155 |
|
super.paint(g, newAlloc); |
156 |
|
} |
157 |
|
|
158 |
|
public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf) |
159 |
|
{ |
160 |
|
Shape newAlloc = adjustAllocation(shape); |
161 |
|
super.insertUpdate(ev, newAlloc, vf); |
162 |
} |
} |
163 |
|
|
164 |
|
public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf) |
165 |
|
{ |
166 |
|
Shape newAlloc = adjustAllocation(shape); |
167 |
|
super.removeUpdate(ev, newAlloc, vf); |
168 |
|
} |
169 |
|
|
170 |
|
public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf) |
171 |
|
{ |
172 |
|
Shape newAlloc = adjustAllocation(shape); |
173 |
|
super.removeUpdate(ev, newAlloc, vf); |
174 |
|
} |
175 |
|
|
176 |
} |
} |