/**
* This file is part of JSurveyLib.
*
* JSurveyLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JSurveyLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JSurveyLib. If not, see <http://www.gnu.org/licenses/>.
**/
/*
* Modulname: TextField
* Autor: Eyer Leander
* Datum: 03.03.2005
*
* (c) Copyright 2005 by
* Eyer IT Services, Naters
* All rights reserved
*/
package org.jsurveylib.gui.swing.widget;
import org.jsurveylib.gui.swing.renderer.TextChangeListener;
import org.jsurveylib.gui.swing.renderer.TextChangeEvent;
import javax.swing.JTextField;
import javax.swing.text.Document;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
/**
* <u><b><font color="red">FOR INTERNAL USE ONLY.</font></b></u>
* <p/>
* A customized version of the JTextField.
* The primary advantage is the ability to install a "ChangeListener", who will
* be called when the content of the TextField changes (quite similar to the change
* event with JavaScript). Additionally, it automatically preselects the whole content
* when activated (to simplify changing).
*/
public class ETextField extends JTextField implements ActionListener, FocusListener, KeyListener {
/**
* Content Cache (so that we actually know when something was changed)
*/
private String cache = "";
/**
* Event listeners
*/
private Vector listeners = new Vector();
/**
* Default Constructor
*/
public ETextField() {
super();
init();
}
/**
* Constructor with fixed number of columns
* @param columns The number of columns
*/
public ETextField(int columns) {
super(columns);
init();
}
/**
* This method is called by all constructors
*/
private void init() {
//config GUI
setMargin(new Insets(3, 2, 3, 2));
//register listeners
addActionListener(this);
addFocusListener(this);
addKeyListener(this);
}
/**
* Update the internal cache if the text was changed through this api call (instead of changed by the user)
*/
public void setText(String newText) {
if (!super.getText().equals(newText)) {
super.setText(newText);
cache = newText;
if (cache == null) {
cache = "";
}
}
}
/**
* Add a TextChangeListener to the notify list. The listener will be informed
* about changes of the text field
*
* @param listener Listener to be added
*/
public void addChangeListener(TextChangeListener listener) {
listeners.add(listener);
}
/**
* Remove a TextChangeListener from the notify list. The listener will no longer be informed
* about changes in the text field
*
* @param listener Listener to be removed
*/
public void removeChangeListener(TextChangeListener listener) {
listeners.remove(listener);
}
/**
* Notify all listeners about a change in the text field
*
* @param evt Event describing the change
*/
private void fireTextChangeEvent(TextChangeEvent evt) {
for (int i = 0; i < listeners.size(); i++) {
((TextChangeListener) listeners.get(i)).textChanged(evt);
}
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
fireEventIfRequired(TextChangeEvent.Trigger.FOCUS_LOST);
}
/**
* Invoked when a component gains the keyboard focus.
*/
public void focusGained(FocusEvent e) {
selectAll();
}
/**
* Invoked when a component loses the keyboard focus.
*/
public void focusLost(FocusEvent e) {
fireEventIfRequired(TextChangeEvent.Trigger.FOCUS_LOST);
}
/**
* Compare the current text with the cached one and fire an event if a change occured
*
* @param trigger Trigger who initialized the examination
*/
private void fireEventIfRequired(TextChangeEvent.Trigger trigger) {
String newText = getText();
if (!newText.equals(cache)) {
TextChangeEvent evt = new TextChangeEvent(this, cache, newText, trigger);
fireTextChangeEvent(evt);
cache = newText;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
fireEventIfRequired(TextChangeEvent.Trigger.ACTION);
}
}