Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / TitledSeparator.java @ 2035

History | View | Annotate | Download (3.37 KB)

1
package org.gvsig.tools.swing.impl;
2

    
3
import java.awt.Color;
4
import java.awt.Component;
5
import java.awt.Dimension;
6
import java.awt.Graphics;
7
import java.awt.Graphics2D;
8
import java.awt.LinearGradientPaint;
9
import java.awt.Paint;
10
import java.awt.geom.Point2D;
11
import javax.swing.BorderFactory;
12
import javax.swing.Icon;
13
import javax.swing.JLabel;
14
import javax.swing.UIManager;
15

    
16
/*
17
 * Based on portions of code from java.awt.geom.TitledSeparator of the
18
 * aterai/java-swing-tips project (Copyright (c) 2015 TERAI Atsuhiro)
19
 * https://github.com/aterai/java-swing-tips/tree/master/TitledSeparator
20
 */
21
public class TitledSeparator extends JLabel {
22

    
23
    private Color color;
24
    private final String title;
25
    private final Color target;
26
    private final int height;
27
    private final int titlePosition;
28
    private final int titleJustification;
29

    
30
    public TitledSeparator(String title, int height, int titlePosition, int titleJustification) {
31
        this(title, null, height, titlePosition, titleJustification);
32
    }
33

    
34
    public TitledSeparator(
35
            String _title, Color _target, int _height, int _titlePosition, int _titleJustification) {
36
        super();
37
        this.title = _title;
38
        this.target = _target;
39
        this.height = _height;
40
        this.titlePosition = _titlePosition;
41
        this.titleJustification = _titleJustification;
42
        Icon icon = new Icon() {
43
            private int width = -1;
44
            private Paint painter1, painter2;
45

    
46
            @Override
47
            public void paintIcon(Component c, Graphics g, int x, int y) {
48
                int w = c.getWidth();
49
                if (w != width || painter1 == null || painter2 == null || color == null) {
50
                    width = w;
51
                    Point2D start = new Point2D.Float(0f, 0f);
52
                    Point2D end = new Point2D.Float((float) width, 0f);
53
                    float[] dist = {0.0f, 1.0f};
54
                    color = getBackground();
55
                    color = color == null ? UIManager.getColor("Panel.background") : color;
56
                    Color tc = target == null ? color : target;
57
                    painter1 = new LinearGradientPaint(
58
                            start, end, dist, new Color[]{tc.darker(), color});
59
                    painter2 = new LinearGradientPaint(
60
                            start, end, dist, new Color[]{tc.brighter(), color});
61
                }
62
                int h = getIconHeight() / 2;
63
                Graphics2D g2 = (Graphics2D) g.create();
64
                g2.setPaint(painter1);
65
                g2.fillRect(x, y, width, getIconHeight());
66
                g2.setPaint(painter2);
67
                g2.fillRect(x, y + h, width, getIconHeight() - h);
68
                g2.dispose();
69
            }
70

    
71
            @Override
72
            public int getIconWidth() {
73
                return 200;
74
            } //dummy width
75

    
76
            @Override
77
            public int getIconHeight() {
78
                return height;
79
            }
80
        };
81
        this.setBorder(BorderFactory.createTitledBorder(
82
                BorderFactory.createMatteBorder(height, 0, 0, 0, icon), title,
83
                titleJustification, titlePosition));
84
    }
85

    
86
    @Override
87
    public Dimension getMaximumSize() {
88
        Dimension d = super.getPreferredSize();
89
        d.width = Short.MAX_VALUE;
90
        return d;
91
    }
92

    
93
    @Override
94
    public void updateUI() {
95
        super.updateUI();
96
        color = null;
97
    }
98
}