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

nl.cloudfarming.client.util.swing.icon.BadgeIcon Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2008-2013 LimeTri. All rights reserved.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.util.swing.icon;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.EnumMap;
import java.util.Map;
import javax.swing.Icon;

/**
 * Icon, that allows you to set badge icon.
 * 
 * @author Frantisek Post
 */
public class BadgeIcon implements Icon {

    private Icon baseIcon;
    private Map badgeMap;

    public BadgeIcon(Icon baseIcon) {
        this();
        setBaseIcon(baseIcon);
    }
    
    public BadgeIcon() {
        badgeMap = new EnumMap<>(BadgePosition.class);
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {

        baseIcon.paintIcon(c, g, x, y);
        Dimension baseDimension = new Dimension(baseIcon.getIconWidth(), baseIcon.getIconHeight());

        for (Map.Entry entry : badgeMap.entrySet()) {
            Icon icon = entry.getValue();
            Point p = entry.getKey().getBadgePosition(baseDimension, new Dimension(icon.getIconWidth(), icon.getIconHeight()));
            icon.paintIcon(c, g, x + p.x, y + p.y);
        }
    }

    @Override
    public int getIconWidth() {
        return baseIcon.getIconWidth();
    }

    @Override
    public int getIconHeight() {
        return baseIcon.getIconHeight();
    }

    // API
    public void setBaseIcon(Icon baseIcon) {
        assert baseIcon != null;
        this.baseIcon = baseIcon;
    }

    public Icon getBaseIcon() {
        return baseIcon;
    }

    public void setBadge(Icon icon, BadgePosition badgePosition) {
        assert icon != null;
        assert badgePosition != null;
        badgeMap.put(badgePosition, icon);
    }

    public Icon getBadge(BadgePosition badgePosition) {
        return badgeMap.get(badgePosition);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy