All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.validator.swing.ui.IconValidationUI Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-6
Show newest version
/*
 * *##% 
 * JAXX Runtime
 * Copyright (C) 2008 - 2009 CodeLutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * ##%*
 */
package jaxx.runtime.validator.swing.ui;

import org.jdesktop.jxlayer.JXLayer;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import jaxx.runtime.validator.BeanValidatorField;
import jaxx.runtime.validator.BeanValidatorScope;

/** @author chemit */
public class IconValidationUI extends AbstractBeanValidatorUI {

    // The icon to be shown at the layer's corner
    protected static BufferedImage errorIcon;
    protected static BufferedImage warningIcon;
    protected static BufferedImage infoIcon;

    public IconValidationUI(BeanValidatorField field) {
        super(field);
        if (errorIcon == null) {
            errorIcon = prepareIcon(Color.RED);
        }
        if (warningIcon == null) {
            warningIcon = prepareIcon(Color.ORANGE);
        }
        if (infoIcon == null) {
            infoIcon = prepareIcon(Color.GREEN);
        }
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        c.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 3));
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        c.setBorder(null);
    }

    @Override
    protected void paintLayer(Graphics2D g2, JXLayer l) {
        super.paintLayer(g2, l);
        // There is no need to take insets into account for this painter
        BeanValidatorScope scope = field.getScope();
        if (scope != null) {
            BufferedImage icon = null;
            switch (scope) {
                case ERROR:
                    icon = errorIcon;
                    break;
                case WARNING:
                    icon = warningIcon;
                    break;
                case INFO:
                    icon = infoIcon;
                    break;
            }
            if (icon != null) {
                g2.drawImage(icon, l.getWidth() - icon.getWidth() - 1, 0, null);
            }
        }
    }

    protected static BufferedImage prepareIcon(Color color) {
        int width = 7;
        int height = 8;
        BufferedImage icon = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) icon.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g2.setColor(color);
        g2.fillRect(0, 0, width, height);
        g2.setColor(Color.WHITE);
        g2.drawLine(0, 0, width, height);
        g2.drawLine(0, height, width, 0);
        g2.dispose();
        return icon;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy