OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 80 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
Line 14... Line 14...
14
 package org.openconcerto.ui.list;
14
 package org.openconcerto.ui.list;
15
 
15
 
16
import java.awt.Color;
16
import java.awt.Color;
17
import java.awt.FontMetrics;
17
import java.awt.FontMetrics;
18
import java.awt.Graphics;
18
import java.awt.Graphics;
-
 
19
import java.util.ArrayList;
-
 
20
import java.util.List;
19
 
21
 
20
import javax.swing.JLabel;
22
import javax.swing.JLabel;
21
 
23
 
22
public class HighLightableJLabel extends JLabel {
24
public class HighLightableJLabel extends JLabel {
23
    public static Color DEFAULT_COLOR = new Color(252, 252, 180);
25
    public static final Color DEFAULT_COLOR = new Color(252, 252, 180);
24
    private String text;
26
    private List<String> texts = new ArrayList<>(1);
25
    private Color highLightColor = DEFAULT_COLOR;
27
    private Color highLightColor = DEFAULT_COLOR;
-
 
28
    private boolean ignoreCase = true;
-
 
29
 
-
 
30
    public HighLightableJLabel() {
-
 
31
        super();
-
 
32
    }
-
 
33
 
-
 
34
    public HighLightableJLabel(String label) {
-
 
35
        super(label);
-
 
36
    }
-
 
37
 
-
 
38
    public void setHightlight(List<String> parts) {
-
 
39
        this.texts.clear();
-
 
40
        this.texts.addAll(parts);
-
 
41
    }
26
 
42
 
27
    public void setHightlight(String text) {
43
    public void setHightlight(String text) {
-
 
44
        this.texts.clear();
28
        this.text = text;
45
        this.texts.add(text);
29
    }
46
    }
30
 
47
 
31
    public void setHighLightColor(Color highLightColor) {
48
    public void setHighLightColor(Color highLightColor) {
32
        this.highLightColor = highLightColor;
49
        this.highLightColor = highLightColor;
33
    }
50
    }
34
 
51
 
-
 
52
    public void setIgnoreCase(boolean ignoreCase) {
-
 
53
        this.ignoreCase = ignoreCase;
-
 
54
    }
-
 
55
 
35
    @Override
56
    @Override
36
    public void paint(Graphics g) {
57
    public void paint(Graphics g) {
37
        if (highLightColor != null && text != null) {
58
        if (this.texts != null && this.highLightColor != null && !this.texts.isEmpty()) {
38
            String currentText = this.getText();
59
            String currentText = this.getText();
-
 
60
            if (this.ignoreCase) {
39
            int offset = currentText.indexOf(text);
61
                currentText = currentText.toLowerCase();
-
 
62
            }
-
 
63
            for (String text : this.texts) {
-
 
64
                String textToSearch = text;
40
            if (offset >= 0) {
65
                if (this.ignoreCase) {
41
                FontMetrics metrics = g.getFontMetrics();
66
                    textToSearch = textToSearch.toLowerCase();
-
 
67
                }
-
 
68
                int currentIndex = 0;
42
                String start = getText().substring(0, offset);
69
                int offset = currentText.indexOf(textToSearch, currentIndex);
-
 
70
                while (offset >= 0) {
43
                // You may also need to account for some offsets here:
71
                    final FontMetrics metrics = g.getFontMetrics();
44
                int startX = metrics.stringWidth(start);
72
                    final String start = getText().substring(0, offset);
45
                int startY = 0; // You probably have some vertical offset to add here.
73
                    final int startX = metrics.stringWidth(start);
46
                int length = metrics.stringWidth(text);
74
                    final int length = metrics.stringWidth(text);
47
                int height = metrics.getHeight();
75
                    final int height = metrics.getHeight();
48
                // Now you can draw the highlighted region before drawing the rest of the label:
76
                    // Draw the highlighted region before drawing the rest of the label:
49
                g.setColor(highLightColor);
77
                    g.setColor(this.highLightColor);
50
                g.fillRect(startX, startY, length, height);
78
                    g.fillRect(startX, 0, length, height);
-
 
79
                    // Next!
-
 
80
                    currentIndex = offset + textToSearch.length();
-
 
81
                    offset = currentText.indexOf(textToSearch, currentIndex);
-
 
82
                }
51
            }
83
            }
52
        }
84
        }
53
        super.paint(g);
85
        super.paint(g);
54
    }
86
    }
-
 
87
 
55
}
88
}