OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
80 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
80 ilm 5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.list;
15
 
16
import java.awt.Color;
17
import java.awt.FontMetrics;
18
import java.awt.Graphics;
182 ilm 19
import java.util.ArrayList;
20
import java.util.List;
80 ilm 21
 
22
import javax.swing.JLabel;
23
 
24
public class HighLightableJLabel extends JLabel {
182 ilm 25
    public static final Color DEFAULT_COLOR = new Color(252, 252, 180);
26
    private List<String> texts = new ArrayList<>(1);
80 ilm 27
    private Color highLightColor = DEFAULT_COLOR;
182 ilm 28
    private boolean ignoreCase = true;
80 ilm 29
 
182 ilm 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
    }
42
 
80 ilm 43
    public void setHightlight(String text) {
182 ilm 44
        this.texts.clear();
45
        this.texts.add(text);
80 ilm 46
    }
47
 
48
    public void setHighLightColor(Color highLightColor) {
49
        this.highLightColor = highLightColor;
50
    }
51
 
182 ilm 52
    public void setIgnoreCase(boolean ignoreCase) {
53
        this.ignoreCase = ignoreCase;
54
    }
55
 
80 ilm 56
    @Override
57
    public void paint(Graphics g) {
182 ilm 58
        if (this.texts != null && this.highLightColor != null && !this.texts.isEmpty()) {
80 ilm 59
            String currentText = this.getText();
182 ilm 60
            if (this.ignoreCase) {
61
                currentText = currentText.toLowerCase();
80 ilm 62
            }
182 ilm 63
            for (String text : this.texts) {
64
                String textToSearch = text;
65
                if (this.ignoreCase) {
66
                    textToSearch = textToSearch.toLowerCase();
67
                }
68
                int currentIndex = 0;
69
                int offset = currentText.indexOf(textToSearch, currentIndex);
70
                while (offset >= 0) {
71
                    final FontMetrics metrics = g.getFontMetrics();
72
                    final String start = getText().substring(0, offset);
73
                    final int startX = metrics.stringWidth(start);
74
                    final int length = metrics.stringWidth(text);
75
                    final int height = metrics.getHeight();
76
                    // Draw the highlighted region before drawing the rest of the label:
77
                    g.setColor(this.highLightColor);
78
                    g.fillRect(startX, 0, length, height);
79
                    // Next!
80
                    currentIndex = offset + textToSearch.length();
81
                    offset = currentText.indexOf(textToSearch, currentIndex);
82
                }
83
            }
80 ilm 84
        }
85
        super.paint(g);
86
    }
182 ilm 87
 
80 ilm 88
}