Source for javax.swing.text.PasswordView

   1: /* PasswordView.java -- 
   2:  Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4:  This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing.text;
  40: 
  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;
  46: 
  47: import javax.swing.JPasswordField;
  48: 
  49: public class PasswordView
  50:   extends FieldView
  51: {
  52:   /**
  53:    * Buffer for putting the echo char into it and
  54:    * then using it to draw it into the view.
  55:    */
  56:   private char[] oneCharBuffer = new char[1];
  57: 
  58:   public PasswordView(Element elem)
  59:   {
  60:     super(elem);
  61:   }
  62: 
  63:   /**
  64:    * Draws one echo character at a given position.
  65:    *
  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
  70:    *
  71:    * @return the next x position right of the drawn character
  72:    */
  73:   protected int drawEchoCharacter(Graphics g, int x, int y, char ch)
  74:   {
  75:     // Update font metrics.
  76:     updateMetrics();
  77: 
  78:     // Draw character.
  79:     oneCharBuffer[0] = ch;
  80:     g.drawChars(oneCharBuffer, 0, 1, x, y);
  81: 
  82:     // Return new x position right of drawn character.
  83:     return x + metrics.charWidth(ch);
  84:   }
  85: 
  86:   private char getEchoChar()
  87:   {
  88:     char ch = ((JPasswordField) getContainer()).getEchoChar();
  89: 
  90:     if (ch == 0)
  91:       ch = '*';
  92: 
  93:     return ch;
  94:   }
  95: 
  96:   /**
  97:    * Draws selected text at a given position.
  98:    *
  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
 104:    *
 105:    * @return the next x position right of the drawn character
 106:    */
 107:   protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
 108:     throws BadLocationException
 109:   {
 110:     // FIXME: Throw BadLocationException somehow.
 111: 
 112:     // Update font metrics.
 113:     updateMetrics();
 114: 
 115:     // Get echo character.
 116:     char ch = getEchoChar();
 117: 
 118:     // Set color for selected text.
 119:     g.setColor(selectedColor);
 120:     g.setColor(Color.BLACK);
 121: 
 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;
 127: 
 128:     // Draw echo charaters.
 129:     g.drawChars(buffer, 0, len, x, y);
 130: 
 131:     // Return new x position right of all drawn characters.
 132:     return x + len * metrics.charWidth(ch);
 133:   }
 134: 
 135:   /**
 136:    * Draws unselected text at a given position.
 137:    *
 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
 143:    *
 144:    * @return the next x position right of the drawn character
 145:    */
 146:   protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
 147:     throws BadLocationException
 148:   {
 149:     // FIXME: Throw BadLocationException somehow.
 150: 
 151:     // Update font metrics.
 152:     updateMetrics();
 153: 
 154:     // Get echo character.
 155:     char ch = getEchoChar();
 156:     Segment segment = new Segment();
 157: 
 158:     // Set color for unselected text.
 159:     g.setColor(unselectedColor);
 160:     g.setColor(Color.BLACK);
 161: 
 162:     // Initialize buffer for faster drawing of all characters.
 163:     p1--;
 164:     getDocument().getText(p0, p1 - p0, segment);
 165:     int len = segment.toString().length();
 166:     
 167:     char[] buffer = new char[len];
 168:     for (int index = 0; index < len; ++index)
 169:       buffer[index] = ch;
 170:     
 171:     y += getPreferredSpan(Y_AXIS)/2;
 172:     
 173:     // Draw echo charaters.
 174:     g.drawChars(buffer, 0, len, x, y);
 175:     
 176:     // Return new x position right of all drawn characters.
 177:     return x + (len * metrics.charWidth(ch));
 178:   }
 179: 
 180:   /**
 181:    * Determines the preferred span for this view along an axis.
 182:    * 
 183:    * @param axis to get the preferred span of
 184:    * @return the preferred span of the axis
 185:    */
 186:   public float getPreferredSpan(int axis)
 187:   {
 188:     if (axis != X_AXIS && axis != Y_AXIS)
 189:       throw new IllegalArgumentException();
 190: 
 191:     FontMetrics fm = getFontMetrics();
 192: 
 193:     if (axis == Y_AXIS)
 194:         return fm.getHeight();
 195: 
 196:     String text;
 197:     Element elem = getElement();
 198: 
 199:     try
 200:       {
 201:         text = elem.getDocument().getText(elem.getStartOffset(),
 202:                                           elem.getEndOffset());
 203:       }
 204:     catch (BadLocationException e)
 205:       {
 206:         // This should never happen.
 207:         text = "";
 208:       }
 209:     return fm.stringWidth(text);
 210:   }
 211: 
 212:   /**
 213:    * Provides a mapping from the document model coordinate space to the
 214:    * coordinate space of the view mapped to it.
 215:    *
 216:    * This method is overridden to provide a correct mapping with respect to the
 217:    * echo char and not to the real content.
 218:    *
 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
 225:    */
 226:   public Shape modelToView(int pos, Shape a, Position.Bias b)
 227:     throws BadLocationException
 228:   {
 229:     Shape newAlloc = adjustAllocation(a);
 230: 
 231:     // Ensure metrics are up-to-date.
 232:     updateMetrics();
 233:     
 234:     // Get rectangle of the line containing position.
 235:     int lineIndex = getElement().getElementIndex(pos);
 236:     Rectangle rect = lineToRect(newAlloc, lineIndex);
 237: 
 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;
 248: 
 249:     int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x,
 250:                            this, lineStart);
 251: 
 252:     // Calc the real rectangle.
 253:     rect.x += xoffset;
 254:     rect.width = 1;
 255:     rect.height = metrics.getHeight();
 256: 
 257:     return rect;
 258:   }
 259: 
 260:   /**
 261:    * Provides a mapping from the view coordinate space to the logical 
 262:    * coordinate space of the model.
 263:    * 
 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
 270:    * 
 271:    */
 272:   public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias)
 273:   {
 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);
 277:   }
 278: }