File: NullLabel.java

package info (click to toggle)
libjide-oss-java 3.7.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,264 kB
  • sloc: java: 89,463; xml: 268; makefile: 35
file content (129 lines) | stat: -rw-r--r-- 4,095 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * @(#)HollowLabel.java 7/12/2005
 *
 * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
 */
package com.jidesoft.swing;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;

/**
 * This is part of the null-components. A null component doesn't have foreground, background or font value set. In the
 * other words, the foreground, background and font value of null-component are null. But this doesn't mean
 * getBackground(), getForeground() or getFont() will return null. According to {@link
 * java.awt.Component#getBackground()}, {@link java.awt.Component#getForeground()} and {@link
 * java.awt.Component#getFont()}, if the value is null, it will get the value from its parent. In the other words, if
 * you add a null-component to JPanel, you can use JPanel to control the background, foreground and font of this
 * null-component. The feature is very helpful if you want to make sure all components in a JPanel has the same
 * background, foreground or font.
 * <p/>
 * Even in null-components, you can still change the foreground, background or font value if you do want. However, you'll
 * have to use a font which is not an instance of FontUIResource or a color which is not an instance of ColorUIResource.
 * <p/>
 * We creates a few null-components. It doesn't cover all components. You can always create your own. All you need to do
 * is this
 * <pre><code>
 * public class NullXxxComponent extends XxxComponent {
 *     // invoke clearAttribute() in all the constructors
 * <p/>
 * public void setFont(Font font) {
 *     if (font instanceof FontUIResource) {
 *         return;
 *     }
 *     super.setFont(font);
 * }
 * <p/>
 * public void setBackground(Color bg) {
 *     if (bg instanceof ColorUIResource) {
 *         return;
 *     }
 *     super.setBackground(bg);
 * }
 * <p/>
 * public void setForeground(Color fg) {
 *     if (fg instanceof ColorUIResource) {
 *         return;
 *     }
 *     super.setForeground(fg);
 * }
 * <p/>
 *     private void clearAttribute() {
 *         setFont(null);
 *         setBackground(null);
 *         // do not do this for JButton since JButton always paints button
 *         // content background. So it'd better to leave the foreground alone
 *         setForeground(null);
 *     }
 * }
 * </code></pre>
 *
 * @see com.jidesoft.swing.NullButton
 * @see com.jidesoft.swing.NullCheckBox
 * @see com.jidesoft.swing.NullJideButton
 * @see com.jidesoft.swing.NullPanel
 * @see com.jidesoft.swing.NullRadioButton
 * @see com.jidesoft.swing.NullTristateCheckBox
 */
public class NullLabel extends JLabel {
    public NullLabel() {
        clearAttribute();
    }

    public NullLabel(String text, Icon icon, int horizontalAlignment) {
        super(text, icon, horizontalAlignment);
        clearAttribute();
    }

    public NullLabel(String text, int horizontalAlignment) {
        super(text, horizontalAlignment);
        clearAttribute();
    }

    public NullLabel(String text) {
        super(text);
        clearAttribute();
    }

    public NullLabel(Icon image, int horizontalAlignment) {
        super(image, horizontalAlignment);
        clearAttribute();
    }

    public NullLabel(Icon image) {
        super(image);
        clearAttribute();
    }

    private void clearAttribute() {
        super.setFont(null);
        super.setBackground(null);
        super.setForeground(null);
    }

    @Override
    public void setFont(Font font) {
        if (font instanceof FontUIResource) {
            return;
        }
        super.setFont(font);
    }

    @Override
    public void setBackground(Color bg) {
        if (bg instanceof ColorUIResource) {
            return;
        }
        super.setBackground(bg);
    }

    @Override
    public void setForeground(Color fg) {
        if (fg instanceof ColorUIResource) {
            return;
        }
        super.setForeground(fg);
    }
}