Merge from the pain train
[official-gcc.git] / libjava / javax / swing / text / PasswordView.java
blobad18350e947a4f0a5efbb85c9b70c8d6e43ce633
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.Graphics;
44 import javax.swing.JPasswordField;
46 public class PasswordView extends FieldView
48 /**
49 * Buffer for putting the echo char into it and
50 * then using it to draw it into the view.
52 private char[] oneCharBuffer = new char[1];
54 public PasswordView(Element elem)
56 super(elem);
59 /**
60 * Draws one echo character at a given position.
62 * @param g the <code>Graphics</code> object to draw to
63 * @param x the x-position
64 * @param y the y-position
65 * @param ch the echo character
67 * @return the next x position right of the drawn character
69 protected int drawEchoCharacter(Graphics g, int x, int y, char ch)
71 // Update font metrics.
72 updateMetrics();
74 // Draw character.
75 oneCharBuffer[0] = ch;
76 g.drawChars(oneCharBuffer, 0, 1, x, y);
78 // Return new x position right of drawn character.
79 return x + metrics.charWidth(ch);
82 private char getEchoChar()
84 char ch = ((JPasswordField) getContainer()).getEchoChar();
86 if (ch == 0)
87 ch = '*';
89 return ch;
92 /**
93 * Draws selected text at a given position.
95 * @param g the <code>Graphics</code> object to draw to
96 * @param x the x-position
97 * @param y the y-position
98 * @param p0 the position of the first character to draw
99 * @param p1 the position of the first character not to draw
101 * @return the next x position right of the drawn character
103 protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
104 throws BadLocationException
106 // FIXME: Throw BadLocationException somehow.
108 // Update font metrics.
109 updateMetrics();
111 // Get echo character.
112 char ch = getEchoChar();
114 // Set color for selected text.
115 g.setColor(selectedColor);
116 g.setColor(Color.BLACK);
118 // Initialize buffer for faster drawing of all characters.
119 int len = p1 - p0;
120 char[] buffer = new char[len];
121 for (int index = 0; index < len; ++index)
122 buffer[index] = ch;
124 // Draw echo charaters.
125 g.drawChars(buffer, 0, len, x, y);
127 // Return new x position right of all drawn characters.
128 return x + len * metrics.charWidth(ch);
132 * Draws unselected text at a given position.
134 * @param g the <code>Graphics</code> object to draw to
135 * @param x the x-position
136 * @param y the y-position
137 * @param p0 the position of the first character to draw
138 * @param p1 the position of the first character not to draw
140 * @return the next x position right of the drawn character
142 protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
143 throws BadLocationException
145 // FIXME: Throw BadLocationException somehow.
147 // Update font metrics.
148 updateMetrics();
150 // Get echo character.
151 char ch = getEchoChar();
153 // Set color for unselected text.
154 g.setColor(unselectedColor);
155 g.setColor(Color.BLACK);
157 // Initialize buffer for faster drawing of all characters.
158 int len = p1 - p0;
159 char[] buffer = new char[len];
160 for (int index = 0; index < len; ++index)
161 buffer[index] = ch;
163 // Draw echo charaters.
164 g.drawChars(buffer, 0, len, x, y);
166 // Return new x position right of all drawn characters.
167 return x + len * metrics.charWidth(ch);