001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jmeter.testbeans.gui;
018:
019: import java.awt.Component;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.FocusEvent;
023: import java.awt.event.FocusListener;
024: import java.beans.PropertyEditorSupport;
025:
026: import javax.swing.JPasswordField;
027:
028: /**
029: * This class implements a property editor for non-null String properties that
030: * supports custom editing (i.e.: provides a GUI component) based on a text
031: * field.
032: * <p>
033: * The provided GUI is a simple password field.
034: *
035: */
036: public class PasswordEditor extends PropertyEditorSupport implements
037: ActionListener, FocusListener {
038:
039: private JPasswordField textField;
040:
041: /**
042: * Value on which we started the editing. Used to avoid firing
043: * PropertyChanged events when there's not been such change.
044: */
045: private String initialValue = "";
046:
047: protected PasswordEditor() {
048: super ();
049:
050: textField = new JPasswordField();
051: textField.addActionListener(this );
052: textField.addFocusListener(this );
053: }
054:
055: public String getAsText() {
056: return new String(textField.getPassword());
057: }
058:
059: public void setAsText(String value) {
060: initialValue = value;
061: textField.setText(value);
062: }
063:
064: public Object getValue() {
065: return getAsText();
066: }
067:
068: public void setValue(Object value) {
069: if (value instanceof String)
070: setAsText((String) value);
071: else
072: throw new IllegalArgumentException();
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see java.beans.PropertyEditor#getCustomEditor()
079: */
080: public Component getCustomEditor() {
081: return textField;
082: }
083:
084: public boolean supportsCustomEditor() {
085: return true;
086: }
087:
088: /*
089: * (non-Javadoc) Avoid needlessly firing PropertyChanged events.
090: */
091: public void firePropertyChange() {
092: String newValue = getAsText();
093:
094: if (initialValue.equals(newValue))
095: return;
096: initialValue = newValue;
097:
098: super .firePropertyChange();
099: }
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
105: */
106: public void actionPerformed(ActionEvent e) {
107: firePropertyChange();
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
114: */
115: public void focusGained(FocusEvent e) {
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
122: */
123: public void focusLost(FocusEvent e) {
124: firePropertyChange();
125: }
126: }
|