OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.ui.list;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;

public class HighLightableJLabel extends JLabel {
    public static final Color DEFAULT_COLOR = new Color(252, 252, 180);
    private List<String> texts = new ArrayList<>(1);
    private Color highLightColor = DEFAULT_COLOR;
    private boolean ignoreCase = true;

    public HighLightableJLabel() {
        super();
    }

    public HighLightableJLabel(String label) {
        super(label);
    }

    public void setHightlight(List<String> parts) {
        this.texts.clear();
        this.texts.addAll(parts);
    }

    public void setHightlight(String text) {
        this.texts.clear();
        this.texts.add(text);
    }

    public void setHighLightColor(Color highLightColor) {
        this.highLightColor = highLightColor;
    }

    public void setIgnoreCase(boolean ignoreCase) {
        this.ignoreCase = ignoreCase;
    }

    @Override
    public void paint(Graphics g) {
        if (this.texts != null && this.highLightColor != null && !this.texts.isEmpty()) {
            String currentText = this.getText();
            if (this.ignoreCase) {
                currentText = currentText.toLowerCase();
            }
            for (String text : this.texts) {
                String textToSearch = text;
                if (this.ignoreCase) {
                    textToSearch = textToSearch.toLowerCase();
                }
                int currentIndex = 0;
                int offset = currentText.indexOf(textToSearch, currentIndex);
                while (offset >= 0) {
                    final FontMetrics metrics = g.getFontMetrics();
                    final String start = getText().substring(0, offset);
                    final int startX = metrics.stringWidth(start);
                    final int length = metrics.stringWidth(text);
                    final int height = metrics.getHeight();
                    // Draw the highlighted region before drawing the rest of the label:
                    g.setColor(this.highLightColor);
                    g.fillRect(startX, 0, length, height);
                    // Next!
                    currentIndex = offset + textToSearch.length();
                    offset = currentText.indexOf(textToSearch, currentIndex);
                }
            }
        }
        super.paint(g);
    }

}