Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / text / PasswordView.java
blobc3aa66cbe17c73f579874006351ca5ed18e59e56
1 /* PasswordView.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.Color;
42 import java.awt.FontMetrics;
43 import java.awt.Graphics;
44 import java.awt.Shape;
46 import javax.swing.JPasswordField;
48 public class PasswordView
49 extends FieldView
51 /**
52 * Buffer for putting the echo char into it and
53 * then using it to draw it into the view.
55 private char[] oneCharBuffer = new char[1];
57 public PasswordView(Element elem)
59 super(elem);
62 /**
63 * Draws one echo character at a given position.
65 * @param g the <code>Graphics</code> object to draw to
66 * @param x the x-position
67 * @param y the y-position
68 * @param ch the echo character
70 * @return the next x position right of the drawn character
72 protected int drawEchoCharacter(Graphics g, int x, int y, char ch)
74 // Update font metrics.
75 updateMetrics();
77 // Draw character.
78 oneCharBuffer[0] = ch;
79 g.drawChars(oneCharBuffer, 0, 1, x, y);
81 // Return new x position right of drawn character.
82 return x + metrics.charWidth(ch);
85 private char getEchoChar()
87 char ch = ((JPasswordField) getContainer()).getEchoChar();
89 if (ch == 0)
90 ch = '*';
92 return ch;
95 /**
96 * Draws selected text at a given position.
98 * @param g the <code>Graphics</code> object to draw to
99 * @param x the x-position
100 * @param y the y-position
101 * @param p0 the position of the first character to draw
102 * @param p1 the position of the first character not to draw
104 * @return the next x position right of the drawn character
106 protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
107 throws BadLocationException
109 // FIXME: Throw BadLocationException somehow.
111 // Update font metrics.
112 updateMetrics();
114 // Get echo character.
115 char ch = getEchoChar();
117 // Set color for selected text.
118 g.setColor(selectedColor);
119 g.setColor(Color.BLACK);
121 // Initialize buffer for faster drawing of all characters.
122 int len = p1 - p0;
123 char[] buffer = new char[len];
124 for (int index = 0; index < len; ++index)
125 buffer[index] = ch;
127 // Draw echo charaters.
128 g.drawChars(buffer, 0, len, x, y);
130 // Return new x position right of all drawn characters.
131 return x + len * metrics.charWidth(ch);
135 * Draws unselected text at a given position.
137 * @param g the <code>Graphics</code> object to draw to
138 * @param x the x-position
139 * @param y the y-position
140 * @param p0 the position of the first character to draw
141 * @param p1 the position of the first character not to draw
143 * @return the next x position right of the drawn character
145 protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
146 throws BadLocationException
148 // FIXME: Throw BadLocationException somehow.
150 // Update font metrics.
151 updateMetrics();
153 // Get echo character.
154 char ch = getEchoChar();
155 Segment segment = new Segment();
157 // Set color for unselected text.
158 g.setColor(unselectedColor);
159 g.setColor(Color.BLACK);
161 // Initialize buffer for faster drawing of all characters.
162 p1--;
163 getDocument().getText(p0, p1 - p0, segment);
164 int len = segment.toString().length();
166 char[] buffer = new char[len];
167 for (int index = 0; index < len; ++index)
168 buffer[index] = ch;
170 y += getPreferredSpan(Y_AXIS)/2;
172 // Draw echo charaters.
173 g.drawChars(buffer, 0, len, x, y);
175 // Return new x position right of all drawn characters.
176 return x + (len * metrics.charWidth(ch));
180 * Determines the preferred span for this view along an axis.
182 * @param axis to get the preferred span of
183 * @return the preferred span of the axis
185 public float getPreferredSpan(int axis)
187 if (axis != X_AXIS && axis != Y_AXIS)
188 throw new IllegalArgumentException();
190 FontMetrics fm = getFontMetrics();
192 if (axis == Y_AXIS)
193 return fm.getHeight();
195 String text;
196 Element elem = getElement();
200 text = elem.getDocument().getText(elem.getStartOffset(),
201 elem.getEndOffset());
203 catch (BadLocationException e)
205 // This should never happen.
206 text = "";
208 return fm.stringWidth(text);
212 * Provides a mapping from the document model coordinate space to the
213 * coordinate space of the view mapped to it.
215 * @param pos - the position to convert >= 0
216 * @param a - the allocated region to render into
217 * @param b - typesafe enumeration to indicate bias to a position in the model.
218 * @return the bounding box of the given position
219 * @throws BadLocationException if the given position does not
220 * represent a valid location in the associated document
222 public Shape modelToView(int pos, Shape a, Position.Bias b)
223 throws BadLocationException
225 return super.modelToView(pos, a, b);
229 * Provides a mapping from the view coordinate space to the logical
230 * coordinate space of the model.
232 * @param fx - the X coordinate >= 0.0f
233 * @param fy - the Y coordinate >= 0.0f
234 * @param a - the allocated region to render into
235 * @param bias - typesafe enumeration to indicate bias to a position in the model.
236 * @return the location within the model that best represents
237 * the given point in the view
240 public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
242 return super.viewToModel(fx, fy, a, bias);