01: /*
02: * @(#)PasswordConverter.java 7/30/2005
03: *
04: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
05: */
06: package com.jidesoft.converter;
07:
08: import java.util.Arrays;
09:
10: /**
11: * Converter which converts String to String and converts it back.
12: */
13: public class PasswordConverter extends DefaultObjectConverter {
14: /**
15: * ConverterContext if the String is a file name.
16: */
17: public static ConverterContext CONTEXT = new ConverterContext(
18: "Password");
19: private char _echoChar = '*';
20:
21: public PasswordConverter() {
22: }
23:
24: /**
25: * Creates a PasswordConverter.
26: *
27: * @param echoChar The echo char. It is used to replace the real password so that other people can't see what user is typing.
28: */
29: public PasswordConverter(char echoChar) {
30: _echoChar = echoChar;
31: }
32:
33: @Override
34: public String toString(Object object, ConverterContext context) {
35: if (object instanceof char[]) {
36: int length = ((char[]) object).length;
37: char[] chars = new char[length];
38: Arrays.fill(chars, getEchoChar());
39: return new String(chars);
40: } else if (object != null) {
41: int length = object.toString().length();
42: char[] chars = new char[length];
43: Arrays.fill(chars, getEchoChar());
44: return new String(chars);
45: } else {
46: return "";
47: }
48: }
49:
50: public char getEchoChar() {
51: return _echoChar;
52: }
53:
54: public void setEchoChar(char echoChar) {
55: _echoChar = echoChar;
56: }
57:
58: @Override
59: public boolean supportFromString(String string,
60: ConverterContext context) {
61: return false;
62: }
63:
64: @Override
65: public Object fromString(String string, ConverterContext context) {
66: return null;
67: }
68: }
|