~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to javax/swing/text/FieldView.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* FieldView.java -- 
2
 
   Copyright (C) 2004 Free Software Foundation, Inc.
 
2
   Copyright (C) 2004, 2006 Free Software Foundation, Inc.
3
3
 
4
4
This file is part of GNU Classpath.
5
5
 
39
39
package javax.swing.text;
40
40
 
41
41
import java.awt.Component;
42
 
import java.awt.ComponentOrientation;
 
42
import java.awt.Container;
43
43
import java.awt.FontMetrics;
44
44
import java.awt.Graphics;
 
45
import java.awt.Insets;
45
46
import java.awt.Rectangle;
46
47
import java.awt.Shape;
 
48
import java.awt.event.ActionEvent;
 
49
import java.awt.event.ActionListener;
47
50
 
 
51
import javax.swing.BoundedRangeModel;
48
52
import javax.swing.JTextField;
 
53
import javax.swing.event.ChangeEvent;
 
54
import javax.swing.event.ChangeListener;
49
55
import javax.swing.event.DocumentEvent;
50
56
 
51
57
public class FieldView extends PlainView
52
58
{
 
59
  BoundedRangeModel horizontalVisibility;
 
60
  
 
61
  /** Caches the preferred span of the X axis. It is invalidated by
 
62
   * setting it to -1f. This is done when text in the document
 
63
   * is inserted, removed or changed. The value is corrected as
 
64
   * soon as calculateHorizontalSpan() is called. 
 
65
   */
 
66
  float cachedSpan = -1f;
 
67
 
53
68
  public FieldView(Element elem)
54
69
  {
55
70
    super(elem);
 
71
    
 
72
  }
 
73
  
 
74
  /** Checks whether the given container is a JTextField. If so
 
75
   * it retrieves the textfield's horizontalVisibility instance.
 
76
   * 
 
77
   * <p>This method should be only called when the view's container
 
78
   * is valid. Naturally that would be the setParent() method however
 
79
   * that method is not overridden in the RI and that is why we chose
 
80
   * paint() instead.</p>
 
81
   */ 
 
82
  private void checkContainer()
 
83
  {
 
84
    Container c = getContainer();
 
85
    
 
86
    if (c instanceof JTextField)
 
87
      {
 
88
        horizontalVisibility = ((JTextField) c).getHorizontalVisibility();
 
89
        
 
90
        // Provokes a repaint when the BoundedRangeModel's values change
 
91
        // (which is what the RI does).
 
92
        horizontalVisibility.addChangeListener(new ChangeListener(){
 
93
          public void stateChanged(ChangeEvent event) {
 
94
            getContainer().repaint();
 
95
          };
 
96
        });
 
97
 
 
98
        // It turned out that the span calculated at this point is wrong
 
99
        // and needs to be recalculated (e.g. a different font setting is
 
100
        // not taken into account).
 
101
        calculateHorizontalSpan();
 
102
        
 
103
        // Initializes the BoundedRangeModel properly.
 
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);
56
122
  }
57
123
 
58
124
  protected FontMetrics getFontMetrics()
72
138
   */
73
139
  protected Shape adjustAllocation(Shape shape)
74
140
  {
 
141
    // Return null when the original allocation is null (like the RI).
 
142
    if (shape == null)
 
143
      return null;
 
144
    
75
145
    Rectangle rectIn = shape.getBounds();
76
146
    // vertical adjustment
77
147
    int height = (int) getPreferredSpan(Y_AXIS);
78
148
    int y = rectIn.y + (rectIn.height - height) / 2;
79
149
    // horizontal adjustment
80
150
    JTextField textField = (JTextField) getContainer();
81
 
    int halign = textField.getHorizontalAlignment();
82
 
    int width = (int) getPreferredSpan(X_AXIS);
 
151
    int width = (int) ((cachedSpan != -1f) ? cachedSpan : calculateHorizontalSpan());
83
152
    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
 
      }
 
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
    
110
182
    return new Rectangle(x, y, width, height);
111
183
  }
112
184
 
115
187
    if (axis != X_AXIS && axis != Y_AXIS)
116
188
      throw new IllegalArgumentException();
117
189
 
118
 
    FontMetrics fm = getFontMetrics();
119
190
 
120
191
    if (axis == Y_AXIS)
121
192
      return super.getPreferredSpan(axis);
122
193
 
123
 
    String text;
 
194
    if (cachedSpan != -1f)
 
195
      return cachedSpan;
 
196
    
 
197
    return calculateHorizontalSpan();
 
198
  }
 
199
  
 
200
  /** Calculates and sets the horizontal span and stores the value
 
201
   * in cachedSpan.
 
202
   */ 
 
203
  private float calculateHorizontalSpan()
 
204
  {
 
205
    Segment s = getLineBuffer();
124
206
    Element elem = getElement();
125
207
 
126
208
    try
127
209
      {
128
 
        text = elem.getDocument().getText(elem.getStartOffset(),
129
 
                                          elem.getEndOffset());
 
210
        elem.getDocument().getText(elem.getStartOffset(),
 
211
                                          elem.getEndOffset() - 1,
 
212
                                          s);
 
213
        
 
214
        return cachedSpan = Utilities.getTabbedTextWidth(s, getFontMetrics(), 0, this, s.offset);
130
215
      }
131
216
    catch (BadLocationException e)
132
217
      {
133
 
        // This should never happen.
134
 
        text = "";
 
218
        // Should never happen
 
219
        AssertionError ae = new AssertionError();
 
220
        ae.initCause(e);
 
221
        throw ae;
135
222
      }
136
 
    
137
 
    return fm.stringWidth(text) + 30;
138
223
  }
139
224
 
140
225
  public int getResizeWeight(int axis)
151
236
  
152
237
  public void paint(Graphics g, Shape s)
153
238
  {
 
239
    if (horizontalVisibility == null)
 
240
      checkContainer();
 
241
 
154
242
    Shape newAlloc = adjustAllocation(s);
 
243
    
 
244
    // Set a clip to prevent drawing outside of the allocation area.
 
245
    // TODO: Is there a better way to achieve this?
 
246
    Shape clip = g.getClip();
 
247
    g.setClip(s);
155
248
    super.paint(g, newAlloc);
 
249
    g.setClip(clip);
156
250
  }
157
251
 
158
252
  public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
159
253
  {
 
254
    cachedSpan = -1f;
 
255
    
 
256
    if (horizontalVisibility != null)
 
257
      updateVisibility();
 
258
    
160
259
    Shape newAlloc = adjustAllocation(shape);
 
260
    
161
261
    super.insertUpdate(ev, newAlloc, vf);
162
262
    getContainer().repaint();
163
263
  }
164
264
 
165
265
  public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
166
266
  {
 
267
    cachedSpan = -1f;
 
268
    
 
269
    if (horizontalVisibility != null)
 
270
      updateVisibility();
 
271
 
167
272
    Shape newAlloc = adjustAllocation(shape);
168
273
    super.removeUpdate(ev, newAlloc, vf);
169
274
    getContainer().repaint();
171
276
 
172
277
  public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
173
278
  {
 
279
    cachedSpan = -1f;
 
280
    
 
281
    if (horizontalVisibility != null)
 
282
      updateVisibility();
 
283
 
174
284
    Shape newAlloc = adjustAllocation(shape);
175
 
    super.removeUpdate(ev, newAlloc, vf);
 
285
    super.changedUpdate(ev, newAlloc, vf);
176
286
    getContainer().repaint();
177
287
  }
178
288
 
179
289
  public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
180
290
  {
181
 
    return super.viewToModel(fx, fy, a, bias);
 
291
    return super.viewToModel(fx, fy, adjustAllocation(a), bias);
182
292
  }
183
293
  
184
294
}