Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / text / PasswordView.java
blob9d4c86a8388617bd12c3babd02c370928a589944
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 // Update font metrics.
111 updateMetrics();
113 // Get echo character.
114 char ch = getEchoChar();
116 // Set color for selected text.
117 g.setColor(selectedColor);
118 g.setColor(Color.BLACK);
120 // Draw echo character using drawEchoCharacter() method.
121 for (int index = p0; index < p1; ++index)
122 x = drawEchoCharacter(g, x, y, ch);
123 return x;
127 * Draws unselected text at a given position.
129 * @param g the <code>Graphics</code> object to draw to
130 * @param x the x-position of the start of the baseline
131 * @param y the y-position of the start of the baseline
132 * @param p0 the position of the first character to draw
133 * @param p1 the position of the first character not to draw
135 * @return the next x position right of the drawn character
137 protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
138 throws BadLocationException
140 // Update font metrics.
141 updateMetrics();
143 // Get echo character.
144 char ch = getEchoChar();
146 // Set color for unselected text.
147 g.setColor(unselectedColor);
148 g.setColor(Color.BLACK);
150 // Draw echo character using drawEchoCharacter() method.
151 for (int index = p0; index < p1; ++index)
152 x = drawEchoCharacter(g, x, y, ch);
153 return x;
157 * Determines the preferred span for this view along an axis.
159 * @param axis to get the preferred span of
160 * @return the preferred span of the axis
162 public float getPreferredSpan(int axis)
164 if (axis != X_AXIS && axis != Y_AXIS)
165 throw new IllegalArgumentException();
167 FontMetrics fm = getFontMetrics();
169 if (axis == Y_AXIS)
170 return fm.getHeight();
172 String text;
173 Element elem = getElement();
177 text = elem.getDocument().getText(elem.getStartOffset(),
178 elem.getEndOffset());
180 catch (BadLocationException e)
182 // This should never happen.
183 text = "";
185 return fm.stringWidth(text);
189 * Provides a mapping from the document model coordinate space to the
190 * coordinate space of the view mapped to it.
192 * This method is overridden to provide a correct mapping with respect to the
193 * echo char and not to the real content.
195 * @param pos - the position to convert >= 0
196 * @param a - the allocated region to render into
197 * @param b - typesafe enumeration to indicate bias to a position in the model.
198 * @return the bounding box of the given position
199 * @throws BadLocationException if the given position does not
200 * represent a valid location in the associated document
202 public Shape modelToView(int pos, Shape a, Position.Bias b)
203 throws BadLocationException
205 Shape newAlloc = adjustAllocation(a);
207 // Ensure metrics are up-to-date.
208 updateMetrics();
210 // Get rectangle of the line containing position.
211 int lineIndex = getElement().getElementIndex(pos);
212 Rectangle rect = lineToRect(newAlloc, lineIndex);
214 // Get the rectangle for position.
215 Element line = getElement().getElement(lineIndex);
216 int lineStart = line.getStartOffset();
217 Segment segment = getLineBuffer();
218 segment.array = new char[pos - lineStart];
219 char echoChar = getEchoChar();
220 for (int i = 0; i < segment.array.length; ++i)
221 segment.array[i] = echoChar;
222 segment.offset = 0;
223 segment.count = segment.array.length;
225 int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x,
226 this, lineStart);
228 // Calc the real rectangle.
229 rect.x += xoffset;
230 rect.width = 1;
231 rect.height = metrics.getHeight();
233 return rect;
237 * Provides a mapping from the view coordinate space to the logical
238 * coordinate space of the model.
240 * @param fx - the X coordinate >= 0.0f
241 * @param fy - the Y coordinate >= 0.0f
242 * @param a - the allocated region to render into
243 * @param bias - typesafe enumeration to indicate bias to a position in the model.
244 * @return the location within the model that best represents
245 * the given point in the view
248 public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
250 // FIXME: This only provides a view->model mapping for the real text
251 // content and does not respect the echo char.
252 return super.viewToModel(fx, fy, a, bias);