Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / text / FieldView.java
blob2496418444ee6f76a5a7ea37cab30e9af1b5e3e9
1 /* FieldView.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.awt.Component;
42 import java.awt.ComponentOrientation;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Rectangle;
46 import java.awt.Shape;
48 import javax.swing.JTextField;
49 import javax.swing.event.DocumentEvent;
51 public class FieldView extends PlainView
53 public FieldView(Element elem)
55 super(elem);
58 protected FontMetrics getFontMetrics()
60 Component container = getContainer();
61 return container.getFontMetrics(container.getFont());
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.
71 * @param shape the shape within which the line is beeing centered
73 protected Shape adjustAllocation(Shape shape)
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)
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;
110 return new Rectangle(x, y, width, height);
113 public float getPreferredSpan(int axis)
115 if (axis != X_AXIS && axis != Y_AXIS)
116 throw new IllegalArgumentException();
118 FontMetrics fm = getFontMetrics();
120 if (axis == Y_AXIS)
121 return super.getPreferredSpan(axis);
123 String text;
124 Element elem = getElement();
128 text = elem.getDocument().getText(elem.getStartOffset(),
129 elem.getEndOffset());
131 catch (BadLocationException e)
133 // Should never happen
134 AssertionError ae = new AssertionError();
135 ae.initCause(e);
136 throw ae;
139 return fm.stringWidth(text);
142 public int getResizeWeight(int axis)
144 return axis = axis == X_AXIS ? 1 : 0;
147 public Shape modelToView(int pos, Shape a, Position.Bias bias)
148 throws BadLocationException
150 Shape newAlloc = adjustAllocation(a);
151 return super.modelToView(pos, newAlloc, bias);
154 public void paint(Graphics g, Shape s)
156 Shape newAlloc = adjustAllocation(s);
157 super.paint(g, newAlloc);
160 public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
162 Shape newAlloc = adjustAllocation(shape);
163 super.insertUpdate(ev, newAlloc, vf);
164 getContainer().repaint();
167 public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
169 Shape newAlloc = adjustAllocation(shape);
170 super.removeUpdate(ev, newAlloc, vf);
171 getContainer().repaint();
174 public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
176 Shape newAlloc = adjustAllocation(shape);
177 super.removeUpdate(ev, newAlloc, vf);
178 getContainer().repaint();
181 public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
183 return super.viewToModel(fx, fy, a, bias);