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