1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
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:
73: protected Shape adjustAllocation(Shape shape)
74: {
75: Rectangle rectIn = shape.getBounds();
76:
77: int height = (int) getPreferredSpan(Y_AXIS);
78: int y = rectIn.y + (rectIn.height - height) / 2;
79:
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:
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: }