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: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.io.Serializable;
023: import javax.swing.JFormattedTextField;
024:
025: public class DefaultFormatterFactory extends
026: JFormattedTextField.AbstractFormatterFactory implements
027: Serializable {
028:
029: private JFormattedTextField.AbstractFormatter nullFormatter;
030: private JFormattedTextField.AbstractFormatter editFormatter;
031: private JFormattedTextField.AbstractFormatter displayFormatter;
032: private JFormattedTextField.AbstractFormatter defaultFormatter;
033:
034: public DefaultFormatterFactory() {
035: }
036:
037: public DefaultFormatterFactory(
038: final JFormattedTextField.AbstractFormatter defaultFormatter,
039: final JFormattedTextField.AbstractFormatter displayFormatter,
040: final JFormattedTextField.AbstractFormatter editFormatter) {
041: this (defaultFormatter, displayFormatter, editFormatter, null);
042: }
043:
044: public DefaultFormatterFactory(
045: final JFormattedTextField.AbstractFormatter defaultFormatter,
046: final JFormattedTextField.AbstractFormatter displayFormatter) {
047: this (defaultFormatter, displayFormatter, null, null);
048: }
049:
050: public DefaultFormatterFactory(
051: final JFormattedTextField.AbstractFormatter defaultFormatter,
052: final JFormattedTextField.AbstractFormatter displayFormatter,
053: final JFormattedTextField.AbstractFormatter editFormatter,
054: final JFormattedTextField.AbstractFormatter nullFormatter) {
055: this .defaultFormatter = defaultFormatter;
056: this .displayFormatter = displayFormatter;
057: this .editFormatter = editFormatter;
058: this .nullFormatter = nullFormatter;
059: }
060:
061: public DefaultFormatterFactory(
062: final JFormattedTextField.AbstractFormatter defaultFormatter) {
063: this (defaultFormatter, null, null, null);
064: }
065:
066: public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
067: return defaultFormatter;
068: }
069:
070: public JFormattedTextField.AbstractFormatter getDisplayFormatter() {
071: return displayFormatter;
072: }
073:
074: public JFormattedTextField.AbstractFormatter getEditFormatter() {
075: return editFormatter;
076: }
077:
078: public JFormattedTextField.AbstractFormatter getFormatter(
079: final JFormattedTextField ftf) {
080: if (ftf.getValue() == null && nullFormatter != null) {
081: return nullFormatter;
082: }
083: boolean hasFocus = ftf.hasFocus();
084: JFormattedTextField.AbstractFormatter formatter = hasFocus ? editFormatter
085: : displayFormatter;
086:
087: return (formatter != null) ? formatter : defaultFormatter;
088: }
089:
090: public JFormattedTextField.AbstractFormatter getNullFormatter() {
091: return nullFormatter;
092: }
093:
094: public void setDefaultFormatter(
095: final JFormattedTextField.AbstractFormatter formatter) {
096: defaultFormatter = formatter;
097: }
098:
099: public void setDisplayFormatter(
100: final JFormattedTextField.AbstractFormatter formatter) {
101: displayFormatter = formatter;
102: }
103:
104: public void setEditFormatter(
105: final JFormattedTextField.AbstractFormatter formatter) {
106: editFormatter = formatter;
107: }
108:
109: public void setNullFormatter(
110: final JFormattedTextField.AbstractFormatter formatter) {
111: nullFormatter = formatter;
112: }
113: }
|