001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Ezequiel Cuellar <ecuellar@crosslogic.com>
019: * Farid Ibrahim <faridibrahim@lycos.com>
020: *
021: */
022:
023: package org.swingml.component;
024:
025: import java.awt.datatransfer.*;
026: import java.awt.event.*;
027: import java.util.*;
028:
029: import javax.swing.*;
030: import javax.swing.event.*;
031:
032: import org.swingml.*;
033: import org.swingml.event.*;
034: import org.swingml.model.*;
035:
036: public class JTextFieldComponent extends JTextField implements
037: XMLTranslatable, DocumentListener, FocusListener,
038: MouseListener, KeyListener, ISwingMLTextContainer {
039:
040: private class ContextMenu extends JPopupMenu implements
041: ActionListener {
042:
043: private JTextFieldComponent m_textField = null;
044: private final String MENU_COPY = "Copy";
045: private final String MENU_CUT = "Cut";
046: private final String MENU_DELETE = "Delete";
047: private final String MENU_PASTE = "Paste";
048: private final String MENU_SELECT_ALL = "Select All";
049:
050: private final String MENU_SEPARATOR = "-";
051:
052: public ContextMenu(final JTextFieldComponent textField) {
053: this .m_textField = textField;
054:
055: String items[] = { MENU_CUT, MENU_COPY, MENU_PASTE,
056: MENU_DELETE, MENU_SEPARATOR, MENU_SELECT_ALL };
057:
058: for (int i = 0; i < items.length; i++) {
059: if (items[i].equalsIgnoreCase(MENU_SEPARATOR)) {
060: javax.swing.JSeparator separator = new JSeparator();
061: this .add(separator);
062: } else {
063: JMenuItem menuItem = new JMenuItem(items[i]);
064: menuItem.addActionListener(this );
065:
066: boolean isEnabled = ((items[i]
067: .equalsIgnoreCase(MENU_CUT)
068: || items[i].equalsIgnoreCase(MENU_COPY) || items[i]
069: .equalsIgnoreCase(MENU_DELETE))
070: && (this .m_textField.getSelectedText() == null || this .m_textField
071: .getSelectedText().length() == 0) ? false
072: : true);
073:
074: // This entire block is used just to determine whether or
075: // not the
076: // data in the clipboard is available for pasting into a
077: // text field.
078: if (items[i].equalsIgnoreCase(MENU_PASTE)) {
079: try {
080: Clipboard clipBoard = this .m_textField
081: .getToolkit().getSystemClipboard();
082:
083: if (clipBoard != null) {
084: Transferable clipObject = clipBoard
085: .getContents(null);
086: Object realData = clipObject
087: .getTransferData(DataFlavor.stringFlavor);
088: isEnabled = (realData != null);
089: }
090: } catch (UnsupportedFlavorException e) {
091: isEnabled = false;
092: } catch (Exception e) {
093: // Swallow the exception.
094: }
095: }
096: // Finally set the enabled status of the menu item.
097: menuItem.setEnabled(isEnabled);
098: this .add(menuItem);
099: }
100: }
101: }
102:
103: public void actionPerformed(final ActionEvent event) {
104: if (event.getActionCommand().equalsIgnoreCase(MENU_CUT)) {
105: this .m_textField.cut();
106: } else if (event.getActionCommand().equalsIgnoreCase(
107: MENU_COPY)) {
108: this .m_textField.copy();
109: } else if (event.getActionCommand().equalsIgnoreCase(
110: MENU_PASTE)) {
111: this .m_textField.paste();
112:
113: } else if (event.getActionCommand().equalsIgnoreCase(
114: MENU_DELETE)) {
115: if (this .m_textField.getSelectedText() != null
116: && this .m_textField.getSelectedText().length() > 0) {
117: StringBuffer text = new StringBuffer(
118: this .m_textField.getText());
119:
120: text.replace(this .m_textField.getSelectionStart(),
121: this .m_textField.getSelectionEnd(), "");
122: this .m_textField.setText(text.toString());
123: }
124: } else if (event.getActionCommand().equalsIgnoreCase(
125: MENU_SELECT_ALL)) {
126: this .m_textField.requestFocus();
127: this .m_textField.selectAll();
128: }
129: }
130: }
131:
132: private EventHandler eventHandler = EventHandler.getInstance();
133: private JTextFieldModel model;
134:
135: public JTextFieldComponent(final JTextFieldModel theModel) {
136: this .model = theModel;
137: setVisible(theModel.isVisible());
138: setName(theModel.getName());
139: setText(theModel.getText());
140: setColumns(Integer.parseInt(theModel.getCols()));
141: setEditable(theModel.isEditable());
142: getDocument().addDocumentListener(this );
143: addFocusListener(this );
144: setToolTipText(theModel.getTooltip());
145: addMouseListener(this );
146: List listeners = theModel.getListeners();
147: for (int i = 0; i < listeners.size(); i++) {
148: ListenerModel lm = (ListenerModel) listeners.get(i);
149: if (lm.getEvent().indexOf("KeyListener") != -1) {
150: addKeyListener(this );
151: break;
152: }
153: }
154: if (theModel.getForeground() != null) {
155: setForeground(theModel.getForeground());
156: }
157:
158: if (theModel.getBackground() != null) {
159: setBackground(theModel.getBackground());
160: }
161:
162: setEnabled(theModel.isEnabled());
163:
164: if (null != theModel.getReturnButton()) {
165: addKeyListener(new ReturnKeyAdapter(theModel
166: .getReturnButton()));
167: }
168: }
169:
170: public void changedUpdate(final DocumentEvent aEvt) {
171: this .eventHandler.handleEvent(this .getModel(),
172: Constants.CHANGED_UPDATE, this );
173: }
174:
175: public void focusGained(final FocusEvent aEvt) {
176: this .eventHandler.handleEvent(this .getModel(),
177: Constants.FOCUS_GAINED, this );
178: if (!aEvt.isTemporary()
179: && aEvt.getID() == FocusEvent.FOCUS_GAINED) {
180: internalFocusGained();
181: }
182: }
183:
184: public void focusLost(final FocusEvent aEvt) {
185: this .eventHandler.handleEvent(this .getModel(),
186: Constants.FOCUS_LOST, this );
187: }
188:
189: public JTextFieldModel getModel() {
190: return this .model;
191: }
192:
193: public String getXMLValue() {
194: return super .getText();
195: }
196:
197: public void insertUpdate(final DocumentEvent aEvt) {
198: this .eventHandler.handleEvent(this .getModel(),
199: Constants.INSERT_UPDATE, this );
200: }
201:
202: private void internalFocusGained() {
203: if (model.shouldAutoSelectOnFocus()) {
204: selectAll();
205: }
206: }
207:
208: /**
209: * Don't allow focus if the field is not editable
210: */
211: public boolean isFocusable() {
212: return isEditable();
213: }
214:
215: public void keyPressed(KeyEvent e) {
216: }
217:
218: public void keyReleased(KeyEvent e) {
219: this .eventHandler.handleEvent(this .getModel(),
220: Constants.KEY_RELEASED, this );
221: }
222:
223: public void keyTyped(KeyEvent e) {
224: }
225:
226: public void mouseClicked(final MouseEvent aEvt) {
227: possiblePopup(aEvt);
228: if (!hasFocus()) {
229: internalFocusGained();
230: }
231: }
232:
233: public void mouseEntered(final MouseEvent aEvt) {
234: }
235:
236: public void mouseExited(final MouseEvent aEvt) {
237: }
238:
239: public void mousePressed(final MouseEvent aEvt) {
240: possiblePopup(aEvt);
241: }
242:
243: public void mouseReleased(final MouseEvent aEvt) {
244: possiblePopup(aEvt);
245: }
246:
247: public void possiblePopup(final MouseEvent aEvt) {
248: if (aEvt.isPopupTrigger()) {
249: ContextMenu thePopup = new ContextMenu(this );
250: thePopup
251: .show(aEvt.getComponent(), aEvt.getX(), aEvt.getY());
252: }
253: }
254:
255: public void removeUpdate(final DocumentEvent aEvt) {
256: this.eventHandler.handleEvent(this.getModel(),
257: Constants.REMOVE_UPDATE, this);
258: }
259:
260: }
|