001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.components;
017
018 import java.awt.event.ActionEvent;
019 import java.awt.event.KeyEvent;
020 import java.text.Format;
021
022 import javax.swing.AbstractAction;
023 import javax.swing.JFormattedTextField;
024 import javax.swing.KeyStroke;
025 import javax.swing.text.DefaultFormatter;
026
027 public class PatchedJFormattedTextField extends JFormattedTextField {
028
029 private static final String TOGGLE_OVERWRITE_MODE_ACTION = "toggleOverwriteModeAction";
030
031 public PatchedJFormattedTextField() {
032 super();
033 customInit();
034 }
035
036 public PatchedJFormattedTextField(Object value) {
037 super(value);
038 customInit();
039 }
040
041 public PatchedJFormattedTextField(Format format) {
042 super(format);
043 customInit();
044 }
045
046 public PatchedJFormattedTextField(AbstractFormatter formatter) {
047 super(formatter);
048 customInit();
049 }
050
051 public PatchedJFormattedTextField(AbstractFormatterFactory factory) {
052 super(factory);
053 customInit();
054 }
055
056 public PatchedJFormattedTextField(AbstractFormatterFactory factory, Object currentValue) {
057 super(factory, currentValue);
058 customInit();
059 }
060
061 private void customInit() {
062 setFocusLostBehavior(COMMIT);
063 if (getFormatter() instanceof DefaultFormatter) {
064 final DefaultFormatter d = (DefaultFormatter)getFormatter();
065 AbstractAction toggleOverwrite = new AbstractAction() {
066 public void actionPerformed(ActionEvent e) {
067 d.setOverwriteMode(!(d.getOverwriteMode()));
068 }
069 };
070 getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0), TOGGLE_OVERWRITE_MODE_ACTION);
071 getActionMap().put(TOGGLE_OVERWRITE_MODE_ACTION, toggleOverwrite);
072 }
073 }
074
075 /**
076 * Overiding this method prevents the TextField from intercepting the Enter
077 * Key when focus is not on it. This allows default buttons to function.
078 * This should be removed when Swing fixes their bug.
079 *
080 * @see javax.swing.JComponent#processKeyBinding(javax.swing.KeyStroke,
081 * java.awt.event.KeyEvent, int, boolean)
082 */
083 public boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
084 if (ks == KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0) || ks == KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)) {
085 return false;
086 }
087 return super.processKeyBinding(ks, e, condition, pressed);
088 }
089
090 }