/[classpath]/classpath/javax/swing/text/FieldView.java
ViewVC logotype

Diff of /classpath/javax/swing/text/FieldView.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by mkoch, Sun Nov 21 12:56:30 2004 UTC revision 1.4 by rabbit78, Wed May 11 10:36:26 2005 UTC
# Line 39  exception statement from your version. * Line 39  exception statement from your version. *
39  package javax.swing.text;  package javax.swing.text;
40    
41  import java.awt.Component;  import java.awt.Component;
42    import java.awt.ComponentOrientation;
43  import java.awt.FontMetrics;  import java.awt.FontMetrics;
44  import java.awt.Graphics;  import java.awt.Graphics;
45    import java.awt.Rectangle;
46  import java.awt.Shape;  import java.awt.Shape;
47    
48    import javax.swing.JTextField;
49    import javax.swing.event.DocumentEvent;
50    
51  public class FieldView extends PlainView  public class FieldView extends PlainView
52  {  {
53    public FieldView(Element elem)    public FieldView(Element elem)
# Line 56  public class FieldView extends PlainView Line 61  public class FieldView extends PlainView
61      return container.getFontMetrics(container.getFont());      return container.getFontMetrics(container.getFont());
62    }    }
63    
64      /**
65       * Vertically centers the single line of text within the
66       * bounds of the input shape. The returned Rectangle is centered
67       * vertically within <code>shape</code> and has a height of the
68       * preferred span along the Y axis. Horizontal adjustment is done according
69       * to the horizontalAligment property of the component that is rendered.
70       *
71       * @param shape the shape within which the line is beeing centered
72       */
73      protected Shape adjustAllocation(Shape shape)
74      {
75        Rectangle rectIn = shape.getBounds();
76        // vertical adjustment
77        int height = (int) getPreferredSpan(Y_AXIS);
78        int y = rectIn.y + (rectIn.height - height) / 2;
79        // horizontal adjustment
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)    public float getPreferredSpan(int axis)
114    {    {
115      if (axis != X_AXIS && axis != Y_AXIS)      if (axis != X_AXIS && axis != Y_AXIS)
# Line 91  public class FieldView extends PlainView Line 145  public class FieldView extends PlainView
145    public Shape modelToView(int pos, Shape a, Position.Bias bias)    public Shape modelToView(int pos, Shape a, Position.Bias bias)
146      throws BadLocationException      throws BadLocationException
147    {    {
148      return super.modelToView(pos, a, bias);      Shape newAlloc = adjustAllocation(a);
149        return super.modelToView(pos, newAlloc, bias);
150    }    }
151        
152    public void paint(Graphics g, Shape s)    public void paint(Graphics g, Shape s)
153    {    {
154      super.paint(g, s);      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    }    }
163    
164      public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
165      {
166        Shape newAlloc = adjustAllocation(shape);
167        super.removeUpdate(ev, newAlloc, vf);
168      }
169    
170      public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
171      {
172        Shape newAlloc = adjustAllocation(shape);
173        super.removeUpdate(ev, newAlloc, vf);
174      }
175    
176  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26