Merge from mainline
[official-gcc.git] / libjava / classpath / javax / swing / text / PasswordView.java
blobe54331c5a8eeb1c2a27eff795b02e8555f137348
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.Rectangle;
45 import java.awt.Shape;
47 import javax.swing.JPasswordField;
49 public class PasswordView
50 extends FieldView
52 /**
53 * Buffer for putting the echo char into it and
54 * then using it to draw it into the view.
56 private char[] oneCharBuffer = new char[1];
58 public PasswordView(Element elem)
60 super(elem);
63 /**
64 * Draws one echo character at a given position.
66 * @param g the <code>Graphics</code> object to draw to
67 * @param x the x-position
68 * @param y the y-position
69 * @param ch the echo character
71 * @return the next x position right of the drawn character
73 protected int drawEchoCharacter(Graphics g, int x, int y, char ch)
75 // Update font metrics.
76 updateMetrics();
78 // Draw character.
79 oneCharBuffer[0] = ch;
80 g.drawChars(oneCharBuffer, 0, 1, x, y);
82 // Return new x position right of drawn character.
83 return x + metrics.charWidth(ch);
86 private char getEchoChar()
88 char ch = ((JPasswordField) getContainer()).getEchoChar();
90 if (ch == 0)
91 ch = '*';
93 return ch;
96 /**
97 * Draws selected text at a given position.
99 * @param g the <code>Graphics</code> object to draw to
100 * @param x the x-position
101 * @param y the y-position
102 * @param p0 the position of the first character to draw
103 * @param p1 the position of the first character not to draw
105 * @return the next x position right of the drawn character
107 protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
108 throws BadLocationException
110 // FIXME: Throw BadLocationException somehow.
112 // Update font metrics.
113 updateMetrics();
115 // Get echo character.
116 char ch = getEchoChar();
118 // Set color for selected text.
119 g.setColor(selectedColor);
120 g.setColor(Color.BLACK);
122 // Initialize buffer for faster drawing of all characters.
123 int len = p1 - p0;
124 char[] buffer = new char[len];
125 for (int index = 0; index < len; ++index)
126 buffer[index] = ch;
128 // Draw echo charaters.
129 g.drawChars(buffer, 0, len, x, y);
131 // Return new x position right of all drawn characters.
132 return x + len * metrics.charWidth(ch);
136 * Draws unselected text at a given position.
138 * @param g the <code>Graphics</code> object to draw to
139 * @param x the x-position
140 * @param y the y-position
141 * @param p0 the position of the first character to draw
142 * @param p1 the position of the first character not to draw
144 * @return the next x position right of the drawn character
146 protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
147 throws BadLocationException
149 // FIXME: Throw BadLocationException somehow.
151 // Update font metrics.
152 updateMetrics();
154 // Get echo character.
155 char ch = getEchoChar();
156 Segment segment = new Segment();
158 // Set color for unselected text.
159 g.setColor(unselectedColor);
160 g.setColor(Color.BLACK);
162 // Initialize buffer for faster drawing of all characters.
163 p1--;
164 getDocument().getText(p0, p1 - p0, segment);
165 int len = segment.toString().length();
167 char[] buffer = new char[len];
168 for (int index = 0; index < len; ++index)
169 buffer[index] = ch;
171 y += getPreferredSpan(Y_AXIS)/2;
173 // Draw echo charaters.
174 g.drawChars(buffer, 0, len, x, y);
176 // Return new x position right of all drawn characters.
177 return x + (len * metrics.charWidth(ch));
181 * Determines the preferred span for this view along an axis.
183 * @param axis to get the preferred span of
184 * @return the preferred span of the axis
186 public float getPreferredSpan(int axis)
188 if (axis != X_AXIS && axis != Y_AXIS)
189 throw new IllegalArgumentException();
191 FontMetrics fm = getFontMetrics();
193 if (axis == Y_AXIS)
194 return fm.getHeight();
196 String text;
197 Element elem = getElement();
201 text = elem.getDocument().getText(elem.getStartOffset(),
202 elem.getEndOffset());
204 catch (BadLocationException e)
206 // This should never happen.
207 text = "";
209 return fm.stringWidth(text);
213 * Provides a mapping from the document model coordinate space to the
214 * coordinate space of the view mapped to it.
216 * This method is overridden to provide a correct mapping with respect to the
217 * echo char and not to the real content.
219 * @param pos - the position to convert >= 0
220 * @param a - the allocated region to render into
221 * @param b - typesafe enumeration to indicate bias to a position in the model.
222 * @return the bounding box of the given position
223 * @throws BadLocationException if the given position does not
224 * represent a valid location in the associated document
226 public Shape modelToView(int pos, Shape a, Position.Bias b)
227 throws BadLocationException
229 Shape newAlloc = adjustAllocation(a);
231 // Ensure metrics are up-to-date.
232 updateMetrics();
234 // Get rectangle of the line containing position.
235 int lineIndex = getElement().getElementIndex(pos);
236 Rectangle rect = lineToRect(newAlloc, lineIndex);
238 // Get the rectangle for position.
239 Element line = getElement().getElement(lineIndex);
240 int lineStart = line.getStartOffset();
241 Segment segment = getLineBuffer();
242 segment.array = new char[pos - lineStart];
243 char echoChar = getEchoChar();
244 for (int i = 0; i < segment.array.length; ++i)
245 segment.array[i] = echoChar;
246 segment.offset = 0;
247 segment.count = segment.array.length;
249 int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x,
250 this, lineStart);
252 // Calc the real rectangle.
253 rect.x += xoffset;
254 rect.width = 1;
255 rect.height = metrics.getHeight();
257 return rect;
261 * Provides a mapping from the view coordinate space to the logical
262 * coordinate space of the model.
264 * @param fx - the X coordinate >= 0.0f
265 * @param fy - the Y coordinate >= 0.0f
266 * @param a - the allocated region to render into
267 * @param bias - typesafe enumeration to indicate bias to a position in the model.
268 * @return the location within the model that best represents
269 * the given point in the view
272 public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
274 // FIXME: This only provides a view->model mapping for the real text
275 // content and does not respect the echo char.
276 return super.viewToModel(fx, fy, a, bias);