1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: public class FieldView extends PlainView
57: {
58: BoundedRangeModel horizontalVisibility;
59:
60:
65: float cachedSpan = -1f;
66:
67: public FieldView(Element elem)
68: {
69: super(elem);
70:
71: }
72:
73:
81: private void checkContainer()
82: {
83: Container c = getContainer();
84:
85: if (c instanceof JTextField)
86: {
87: horizontalVisibility = ((JTextField) c).getHorizontalVisibility();
88:
89:
90:
91: horizontalVisibility.addChangeListener(new ChangeListener(){
92: public void stateChanged(ChangeEvent event) {
93: getContainer().repaint();
94: }
95: });
96:
97:
98:
99:
100: calculateHorizontalSpan();
101:
102:
103: updateVisibility();
104: }
105:
106: }
107:
108: private void updateVisibility()
109: {
110: JTextField tf = (JTextField) getContainer();
111: Insets insets = tf.getInsets();
112:
113: int width = tf.getWidth() - insets.left - insets.right;
114:
115: horizontalVisibility.setMaximum(Math.max((int) ((cachedSpan != -1f)
116: ? cachedSpan
117: : calculateHorizontalSpan()),
118: width));
119:
120: horizontalVisibility.setExtent(width - 1);
121: }
122:
123: protected FontMetrics getFontMetrics()
124: {
125: Component container = getContainer();
126: return container.getFontMetrics(container.getFont());
127: }
128:
129:
138: protected Shape adjustAllocation(Shape shape)
139: {
140:
141: if (shape == null)
142: return null;
143:
144: Rectangle rectIn = shape.getBounds();
145:
146: int height = (int) getPreferredSpan(Y_AXIS);
147: int y = rectIn.y + (rectIn.height - height) / 2;
148:
149: JTextField textField = (JTextField) getContainer();
150: int width = (int) ((cachedSpan != -1f) ? cachedSpan : calculateHorizontalSpan());
151: int x;
152: if (horizontalVisibility != null && horizontalVisibility.getExtent() < width)
153: x = rectIn.x - horizontalVisibility.getValue();
154: else
155: switch (textField.getHorizontalAlignment())
156: {
157: case JTextField.CENTER:
158: x = rectIn.x + (rectIn.width - width) / 2;
159: break;
160: case JTextField.RIGHT:
161: x = rectIn.x + (rectIn.width - width - 1);
162: break;
163: case JTextField.TRAILING:
164: if (textField.getComponentOrientation().isLeftToRight())
165: x = rectIn.x + (rectIn.width - width - 1);
166: else
167: x = rectIn.x;
168: break;
169: case JTextField.LEADING:
170: if (textField.getComponentOrientation().isLeftToRight())
171: x = rectIn.x;
172: else
173: x = rectIn.x + (rectIn.width - width - 1);
174: break;
175: case JTextField.LEFT:
176: default:
177: x = rectIn.x;
178: break;
179: }
180:
181: return new Rectangle(x, y, width, height);
182: }
183:
184: public float getPreferredSpan(int axis)
185: {
186: if (axis != X_AXIS && axis != Y_AXIS)
187: throw new IllegalArgumentException();
188:
189:
190: if (axis == Y_AXIS)
191: return super.getPreferredSpan(axis);
192:
193: if (cachedSpan != -1f)
194: return cachedSpan;
195:
196: return calculateHorizontalSpan();
197: }
198:
199:
202: private float calculateHorizontalSpan()
203: {
204: Segment s = getLineBuffer();
205: Element elem = getElement();
206:
207: try
208: {
209: elem.getDocument().getText(elem.getStartOffset(),
210: elem.getEndOffset() - 1,
211: s);
212:
213: return cachedSpan = Utilities.getTabbedTextWidth(s, getFontMetrics(), 0, this, s.offset);
214: }
215: catch (BadLocationException e)
216: {
217:
218: AssertionError ae = new AssertionError();
219: ae.initCause(e);
220: throw ae;
221: }
222: }
223:
224: public int getResizeWeight(int axis)
225: {
226: return axis == X_AXIS ? 1 : 0;
227: }
228:
229: public Shape modelToView(int pos, Shape a, Position.Bias bias)
230: throws BadLocationException
231: {
232: Shape newAlloc = adjustAllocation(a);
233: return super.modelToView(pos, newAlloc, bias);
234: }
235:
236: public void paint(Graphics g, Shape s)
237: {
238: if (horizontalVisibility == null)
239: checkContainer();
240:
241: Shape newAlloc = adjustAllocation(s);
242:
243: Shape clip = g.getClip();
244: if (clip != null)
245: {
246:
247:
248:
249:
250:
251:
252:
253:
254: Rectangle r = s.getBounds();
255: Rectangle cb = clip.getBounds();
256: SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, cb);
257:
258: g.setClip(cb);
259: }
260: else
261: g.setClip(s);
262:
263: super.paint(g, newAlloc);
264: g.setClip(clip);
265:
266: }
267:
268: public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
269: {
270: cachedSpan = -1f;
271:
272: if (horizontalVisibility != null)
273: updateVisibility();
274:
275: Shape newAlloc = adjustAllocation(shape);
276:
277: super.insertUpdate(ev, newAlloc, vf);
278: getContainer().repaint();
279: }
280:
281: public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
282: {
283: cachedSpan = -1f;
284:
285: if (horizontalVisibility != null)
286: updateVisibility();
287:
288: Shape newAlloc = adjustAllocation(shape);
289: super.removeUpdate(ev, newAlloc, vf);
290: getContainer().repaint();
291: }
292:
293: public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
294: {
295: cachedSpan = -1f;
296:
297: if (horizontalVisibility != null)
298: updateVisibility();
299:
300: Shape newAlloc = adjustAllocation(shape);
301: super.changedUpdate(ev, newAlloc, vf);
302: getContainer().repaint();
303: }
304:
305: public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
306: {
307: return super.viewToModel(fx, fy, adjustAllocation(a), bias);
308: }
309:
310: }