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: text = "";
135: }
136:
137: return fm.stringWidth(text) + 30;
138: }
139:
140: public int getResizeWeight(int axis)
141: {
142: return axis = axis == X_AXIS ? 1 : 0;
143: }
144:
145: public Shape modelToView(int pos, Shape a, Position.Bias bias)
146: throws BadLocationException
147: {
148: Shape newAlloc = adjustAllocation(a);
149: return super.modelToView(pos, newAlloc, bias);
150: }
151:
152: public void paint(Graphics g, Shape s)
153: {
154: 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: getContainer().repaint();
163: }
164:
165: public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
166: {
167: Shape newAlloc = adjustAllocation(shape);
168: super.removeUpdate(ev, newAlloc, vf);
169: getContainer().repaint();
170: }
171:
172: public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
173: {
174: Shape newAlloc = adjustAllocation(shape);
175: super.removeUpdate(ev, newAlloc, vf);
176: getContainer().repaint();
177: }
178:
179: public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
180: {
181: return super.viewToModel(fx, fy, a, bias);
182: }
183:
184: }