⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nimrodpasswordview.java

📁 一个java漂亮外观的源码
💻 JAVA
字号:
/*
 *                 (C) Copyright 2005 Nilo J. Gonzalez
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser Gereral Public Licence as published by the Free
 * Software Foundation; either version 2 of the Licence, or (at your opinion) any
 * later version.
 * 
 * This library is distributed in the hope that it will be usefull, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of merchantability or fitness for a
 * particular purpose. See the GNU Lesser General Public Licence for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public Licence along
 * with this library; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, Ma 02111-1307 USA.
 *
 * http://www.gnu.org/licenses/lgpl.html (English)
 * http://gugs.sindominio.net/gnu-gpl/lgpl-es.html (Espa駉l)
 *
 *
 * Original author: Nilo J. Gonz醠ez
 */

/**
 * Esta clase implementa los campos de password.
 * Esta clase cambia los asteriscos habituales por unos cuadrados con bordes redondeados 
 * @see NimRODPasswordFieldUI
 * @author Nilo J. Gonzalez
 */ 
 
package com.nilo.plaf.nimrod;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;


public class NimRODPasswordView extends PasswordView {
  protected static int ancho = 9;
  protected static int hueco = 3;
  
  public NimRODPasswordView( Element elem) {
    super( elem);
  }
  
  protected int drawEchoCharacter( Graphics g, int x, int y, char c) {
    int w = getFontMetrics().charWidth( c);
    w = ( w < ancho ? ancho : w);
    int h = getContainer().getHeight() / 2;
    
    g.fillRect( x+1,y-h, w-2,w);
    g.fillRect( x,y-h+1, w,w-2);
    
    return x + w + hueco;
  }
  
  public Shape modelToView( int pos, Shape a, Position.Bias b) throws BadLocationException {
  	Container c = getContainer();
  	if ( c instanceof JPasswordField ) {
  	  JPasswordField f = (JPasswordField)c;
  	  if ( !f.echoCharIsSet() ) {
  		  return super.modelToView( pos, a, b);
  	  }
  	    
      char echoChar = f.getEchoChar();
  	  int w = f.getFontMetrics( f.getFont()).charWidth( echoChar);
  	  w = ( w < ancho ? ancho : w) + hueco;
  	  
  	  Rectangle alloc = adjustAllocation( a).getBounds();
  	  int dx = (pos - getStartOffset()) * w;
  	  alloc.x += dx - 2;
      if ( alloc.x <= 1 ) {
        alloc.x = 2;
      }
  	  alloc.width = 1;
  	  
  	  return alloc;
  	}
  	
  	return null;
  }
  
  public int viewToModel( float fx, float fy, Shape a, Position.Bias[] bias) {
	  bias[0] = Position.Bias.Forward;
	  int n = 0;
	  Container c = getContainer();
	  if ( c instanceof JPasswordField ) {
	    JPasswordField f = (JPasswordField)c;
	    if ( !f.echoCharIsSet() ) {
		    return super.viewToModel( fx, fy, a, bias);
	    }
	    
	    char echoChar = f.getEchoChar();
	    int w = f.getFontMetrics( f.getFont()).charWidth( echoChar);
  	  w = ( w < ancho ? ancho : w) + hueco;
  	  
	    a = adjustAllocation( a);
	    Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
	    n = ((int)fx - alloc.x) / w;
	    if (n < 0) {
		    n = 0;
	    }
	    else if ( n > (getStartOffset() + getDocument().getLength()) ) {
		    n = getDocument().getLength() - getStartOffset();
	    }
	  }
	  
    return getStartOffset() + n;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -