jdk/src/share/classes/javax/swing/colorchooser/ValueFormatter.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23697 e556a715949f
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     4
 *
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    10
 *
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    15
 * accompanied this code).
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    16
 *
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    23
 * questions.
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    24
 */
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    25
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    26
package javax.swing.colorchooser;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    27
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    28
import java.awt.event.FocusEvent;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    29
import java.awt.event.FocusListener;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    30
import java.text.ParseException;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    31
import static java.util.Locale.ENGLISH;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    32
import javax.swing.JFormattedTextField;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    33
import javax.swing.JFormattedTextField.AbstractFormatter;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    34
import javax.swing.SwingConstants;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    35
import javax.swing.SwingUtilities;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    36
import javax.swing.text.AttributeSet;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    37
import javax.swing.text.BadLocationException;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    38
import javax.swing.text.DefaultFormatterFactory;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    39
import javax.swing.text.DocumentFilter;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    40
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
    41
@SuppressWarnings("serial") // Same-version serialization only
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    42
final class ValueFormatter extends AbstractFormatter implements FocusListener, Runnable {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    43
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    44
    static void init(int length, boolean hex, JFormattedTextField text) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    45
        ValueFormatter formatter = new ValueFormatter(length, hex);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    46
        text.setColumns(length);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    47
        text.setFormatterFactory(new DefaultFormatterFactory(formatter));
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    48
        text.setHorizontalAlignment(SwingConstants.RIGHT);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    49
        text.setMinimumSize(text.getPreferredSize());
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    50
        text.addFocusListener(formatter);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    51
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    52
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    53
    private final DocumentFilter filter = new DocumentFilter() {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    54
        @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    55
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    56
            if (isValid(fb.getDocument().getLength() - length)) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    57
                fb.remove(offset, length);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    58
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    59
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    60
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    61
        @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    62
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet set) throws BadLocationException {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    63
            if (isValid(fb.getDocument().getLength() + text.length() - length) && isValid(text)) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    64
                fb.replace(offset, length, text.toUpperCase(ENGLISH), set);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    65
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    66
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    67
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    68
        @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    69
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet set) throws BadLocationException {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    70
            if (isValid(fb.getDocument().getLength() + text.length()) && isValid(text)) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    71
                fb.insertString(offset, text.toUpperCase(ENGLISH), set);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    72
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    73
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    74
    };
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    75
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    76
    private final int length;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    77
    private final int radix;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    78
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    79
    private JFormattedTextField text;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    80
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    81
    ValueFormatter(int length, boolean hex) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    82
        this.length = length;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    83
        this.radix = hex ? 16 : 10;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    84
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    85
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    86
    @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    87
    public Object stringToValue(String text) throws ParseException {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    88
        try {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    89
            return Integer.valueOf(text, this.radix);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    90
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    91
        catch (NumberFormatException nfe) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    92
            ParseException pe = new ParseException("illegal format", 0);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    93
            pe.initCause(nfe);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    94
            throw pe;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    95
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    96
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    97
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    98
    @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
    99
    public String valueToString(Object object) throws ParseException {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   100
        if (object instanceof Integer) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   101
            if (this.radix == 10) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   102
                return object.toString();
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   103
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   104
            int value = (Integer) object;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   105
            int index = this.length;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   106
            char[] array = new char[index];
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   107
            while (0 < index--) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   108
                array[index] = Character.forDigit(value & 0x0F, this.radix);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   109
                value >>= 4;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   110
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   111
            return new String(array).toUpperCase(ENGLISH);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   112
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   113
        throw new ParseException("illegal object", 0);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   114
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   115
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   116
    @Override
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   117
    protected DocumentFilter getDocumentFilter() {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   118
        return this.filter;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   119
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   120
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   121
    public void focusGained(FocusEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   122
        Object source = event.getSource();
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   123
        if (source instanceof JFormattedTextField) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   124
            this.text = (JFormattedTextField) source;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   125
            SwingUtilities.invokeLater(this);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   126
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   127
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   128
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   129
    public void focusLost(FocusEvent event) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   130
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   131
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   132
    public void run() {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   133
        if (this.text != null) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   134
            this.text.selectAll();
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   135
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   136
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   137
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   138
    private boolean isValid(int length) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   139
        return (0 <= length) && (length <= this.length);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   140
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   141
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   142
    private boolean isValid(String text) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   143
        int length = text.length();
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   144
        for (int i = 0; i < length; i++) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   145
            char ch = text.charAt(i);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   146
            if (Character.digit(ch, this.radix) < 0) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   147
                return false;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   148
            }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   149
        }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   150
        return true;
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   151
    }
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents:
diff changeset
   152
}