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