/*
* SwingML Copyright (C) 2002 SwingML Team
*
* This library 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 2 of the License, or (at your option) any
* later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Ezequiel Cuellar <ecuellar@crosslogic.com>
* Farid Ibrahim <faridibrahim@lycos.com>
*
*/
package org.swingml.component;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import org.swingml.*;
import org.swingml.event.*;
import org.swingml.model.*;
public class JTextFieldComponent extends JTextField implements XMLTranslatable, DocumentListener, FocusListener, MouseListener, KeyListener, ISwingMLTextContainer {
private class ContextMenu extends JPopupMenu implements ActionListener {
private JTextFieldComponent m_textField = null;
private final String MENU_COPY = "Copy";
private final String MENU_CUT = "Cut";
private final String MENU_DELETE = "Delete";
private final String MENU_PASTE = "Paste";
private final String MENU_SELECT_ALL = "Select All";
private final String MENU_SEPARATOR = "-";
public ContextMenu (final JTextFieldComponent textField) {
this.m_textField = textField;
String items[] = { MENU_CUT, MENU_COPY, MENU_PASTE, MENU_DELETE, MENU_SEPARATOR, MENU_SELECT_ALL};
for (int i = 0; i < items.length; i++) {
if (items[i].equalsIgnoreCase(MENU_SEPARATOR)) {
javax.swing.JSeparator separator = new JSeparator();
this.add(separator);
} else {
JMenuItem menuItem = new JMenuItem(items[i]);
menuItem.addActionListener(this);
boolean isEnabled = ((items[i].equalsIgnoreCase(MENU_CUT) || items[i].equalsIgnoreCase(MENU_COPY) || items[i].equalsIgnoreCase(MENU_DELETE))
&& (this.m_textField.getSelectedText() == null || this.m_textField.getSelectedText().length() == 0) ? false : true);
// This entire block is used just to determine whether or
// not the
// data in the clipboard is available for pasting into a
// text field.
if (items[i].equalsIgnoreCase(MENU_PASTE)) {
try {
Clipboard clipBoard = this.m_textField.getToolkit().getSystemClipboard();
if (clipBoard != null) {
Transferable clipObject = clipBoard.getContents(null);
Object realData = clipObject.getTransferData(DataFlavor.stringFlavor);
isEnabled = (realData != null);
}
} catch (UnsupportedFlavorException e) {
isEnabled = false;
} catch (Exception e) {
// Swallow the exception.
}
}
// Finally set the enabled status of the menu item.
menuItem.setEnabled(isEnabled);
this.add(menuItem);
}
}
}
public void actionPerformed (final ActionEvent event) {
if (event.getActionCommand().equalsIgnoreCase(MENU_CUT)) {
this.m_textField.cut();
} else if (event.getActionCommand().equalsIgnoreCase(MENU_COPY)) {
this.m_textField.copy();
} else if (event.getActionCommand().equalsIgnoreCase(MENU_PASTE)) {
this.m_textField.paste();
} else if (event.getActionCommand().equalsIgnoreCase(MENU_DELETE)) {
if (this.m_textField.getSelectedText() != null && this.m_textField.getSelectedText().length() > 0) {
StringBuffer text = new StringBuffer(this.m_textField.getText());
text.replace(this.m_textField.getSelectionStart(), this.m_textField.getSelectionEnd(), "");
this.m_textField.setText(text.toString());
}
} else if (event.getActionCommand().equalsIgnoreCase(MENU_SELECT_ALL)) {
this.m_textField.requestFocus();
this.m_textField.selectAll();
}
}
}
private EventHandler eventHandler = EventHandler.getInstance();
private JTextFieldModel model;
public JTextFieldComponent (final JTextFieldModel theModel) {
this.model = theModel;
setVisible(theModel.isVisible());
setName(theModel.getName());
setText(theModel.getText());
setColumns(Integer.parseInt(theModel.getCols()));
setEditable(theModel.isEditable());
getDocument().addDocumentListener(this);
addFocusListener(this);
setToolTipText(theModel.getTooltip());
addMouseListener(this);
List listeners = theModel.getListeners();
for (int i = 0; i < listeners.size(); i++) {
ListenerModel lm = (ListenerModel) listeners.get(i);
if (lm.getEvent().indexOf("KeyListener") != -1) {
addKeyListener(this);
break;
}
}
if (theModel.getForeground() != null) {
setForeground(theModel.getForeground());
}
if (theModel.getBackground() != null) {
setBackground(theModel.getBackground());
}
setEnabled(theModel.isEnabled());
if (null != theModel.getReturnButton()) {
addKeyListener(new ReturnKeyAdapter(theModel.getReturnButton()));
}
}
public void changedUpdate (final DocumentEvent aEvt) {
this.eventHandler.handleEvent(this.getModel(), Constants.CHANGED_UPDATE, this);
}
public void focusGained (final FocusEvent aEvt) {
this.eventHandler.handleEvent(this.getModel(), Constants.FOCUS_GAINED, this);
if (!aEvt.isTemporary() && aEvt.getID() == FocusEvent.FOCUS_GAINED) {
internalFocusGained();
}
}
public void focusLost (final FocusEvent aEvt) {
this.eventHandler.handleEvent(this.getModel(), Constants.FOCUS_LOST, this);
}
public JTextFieldModel getModel () {
return this.model;
}
public String getXMLValue () {
return super.getText();
}
public void insertUpdate (final DocumentEvent aEvt) {
this.eventHandler.handleEvent(this.getModel(), Constants.INSERT_UPDATE, this);
}
private void internalFocusGained () {
if (model.shouldAutoSelectOnFocus()) {
selectAll();
}
}
/**
* Don't allow focus if the field is not editable
*/
public boolean isFocusable () {
return isEditable();
}
public void keyPressed (KeyEvent e) {}
public void keyReleased (KeyEvent e) {
this.eventHandler.handleEvent(this.getModel(), Constants.KEY_RELEASED, this);
}
public void keyTyped (KeyEvent e) {}
public void mouseClicked (final MouseEvent aEvt) {
possiblePopup(aEvt);
if (!hasFocus()) {
internalFocusGained();
}
}
public void mouseEntered (final MouseEvent aEvt) {}
public void mouseExited (final MouseEvent aEvt) {}
public void mousePressed (final MouseEvent aEvt) {
possiblePopup(aEvt);
}
public void mouseReleased (final MouseEvent aEvt) {
possiblePopup(aEvt);
}
public void possiblePopup (final MouseEvent aEvt) {
if (aEvt.isPopupTrigger()) {
ContextMenu thePopup = new ContextMenu(this);
thePopup.show(aEvt.getComponent(), aEvt.getX(), aEvt.getY());
}
}
public void removeUpdate (final DocumentEvent aEvt) {
this.eventHandler.handleEvent(this.getModel(), Constants.REMOVE_UPDATE, this);
}
}
|