001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020/**
021 * Getted from
022 * http://www.javalobby.org/java/forums/t48652.html
023 */
024
025package pt.ua.dicoogle.rGUI.client.UIHelper;
026
027import java.text.ParseException;
028
029import javax.swing.text.MaskFormatter;
030
031/**
032 * A special version of the {@link javax.swing.text.MaskFormatter} for
033 * {@link javax.swing.JFormattedTextField formatted text fields} that supports
034 * the field being emptied/left blank.
035 *
036 * @author R.J. Lorimer
037 * @author Luis Silva
038 */
039@Deprecated
040public class AllowBlankMaskFormatter extends MaskFormatter {
041
042        private boolean allowBlankField = true;
043        private String blankRepresentation;
044
045        public AllowBlankMaskFormatter() {
046                super();
047        }
048        public AllowBlankMaskFormatter(String mask) throws ParseException {
049                super(mask);
050        }
051
052        public void setAllowBlankField(boolean allowBlankField) {
053                this.allowBlankField = allowBlankField;
054        }
055
056        public boolean isAllowBlankField() {
057                return allowBlankField;
058        }
059
060        /**
061         * Update our blank representation whenever the mask is updated.
062         */
063        @Override public void setMask(String mask) throws ParseException {
064                super.setMask(mask);
065                updateBlankRepresentation();
066        }
067
068        /**
069         * Update our blank representation whenever the mask is updated.
070         */
071        @Override public void setPlaceholderCharacter(char placeholder) {
072                super.setPlaceholderCharacter(placeholder);
073                updateBlankRepresentation();
074        }
075
076        /**
077         * Override the stringToValue method to check the blank representation.
078         */
079        @Override public Object stringToValue(String value) throws ParseException {
080                Object result = value;
081                if(isAllowBlankField() && blankRepresentation != null && blankRepresentation.equals(value)) {
082                        // an empty field should have a 'null' value.
083                        result = null;
084                }
085                else {
086                        result = super.stringToValue(value);
087                }
088                return result;
089        }
090
091        private void updateBlankRepresentation() {
092                try {
093                        // calling valueToString on the parent class with a null attribute will get the 'blank'
094                        // representation.
095                        blankRepresentation = valueToString(null);
096                }
097                catch(ParseException e) {
098                        blankRepresentation = null;
099                }
100        }
101}