~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/contrib/randelshofer/quaqua/MultiIcon.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)MultiIcon.java  1.0.1  2006-02-14
 
3
 *
 
4
 * Copyright (c) 2005 Werner Randelshofer
 
5
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 
6
 * All rights reserved.
 
7
 *
 
8
 * This software is the confidential and proprietary information of
 
9
 * Werner Randelshofer. ("Confidential Information").  You shall not
 
10
 * disclose such Confidential Information and shall use it only in
 
11
 * accordance with the terms of the license agreement you entered into
 
12
 * with Werner Randelshofer.
 
13
 */
 
14
 
 
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua;
 
16
 
 
17
import java.awt.*;
 
18
import javax.swing.*;
 
19
import javax.swing.plaf.*;
 
20
 
 
21
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util.*;
 
22
 
 
23
 
 
24
/**
 
25
 * An icon which paints one out of multiple icons depending on the state
 
26
 * of the component.
 
27
 * MultiIcon can lazily create the icons from a tiled image.
 
28
 *
 
29
 * @author  Werner Randelshofer
 
30
 * @version 1.0.1 2006-02-14 Created tileCount icons from image.
 
31
 * <br>1.0 October 17, 2005 Created.
 
32
 */
 
33
public abstract class MultiIcon implements Icon {
 
34
    /**
 
35
     * The icons from which we choose from.
 
36
     * This variable is null, if we are using a tiled image as our base.
 
37
     */
 
38
    protected Icon[] icons;
 
39
    
 
40
    /** Holds the icon pictures in a single image. This variable is used only
 
41
     *until we create the icons array. Then it is set to null.
 
42
     */
 
43
    private Image tiledImage;
 
44
    /**
 
45
     * The number of icons in the tiledImage.
 
46
     */
 
47
    private int tileCount;
 
48
    /**
 
49
     * Whether the tiledImage needs to be tiled horizontaly or vertically
 
50
     * to get the icons out of it.
 
51
     */
 
52
    private boolean isTiledHorizontaly;
 
53
    
 
54
    
 
55
    /**
 
56
     * Creates a new instance from an array of icons.
 
57
     * All icons must have the same dimensions.
 
58
     * If an icon is null, an icon is derived for the state from the
 
59
     * other icons.
 
60
     */
 
61
    public MultiIcon(Icon[] icons) {
 
62
        this.icons = icons;
 
63
        generateMissingIcons();
 
64
    }
 
65
 
 
66
    /**
 
67
     * Creates a new instance from an array of images.
 
68
     * All icons must have the same dimensions.
 
69
     * If an icon is null, an icon is derived for the state from the
 
70
     * other icons.
 
71
     */
 
72
    public MultiIcon(Image[] images) {
 
73
        this.icons = new Icon[images.length];
 
74
        for (int i=0, n = icons.length; i < n; i++) {
 
75
            if (images[i] != null) {
 
76
                icons[i] = new ImageIcon(images[i]);
 
77
            }
 
78
        }
 
79
        generateMissingIcons();
 
80
    }
 
81
    
 
82
    /**
 
83
     * Creates a new instance.
 
84
     * The icon representations are created lazily from the tiled image.
 
85
     */
 
86
    public MultiIcon(Image tiledImage, int tileCount, boolean isTiledHorizontaly) {
 
87
        this.tiledImage = tiledImage;
 
88
        this.tileCount = tileCount;
 
89
        this.isTiledHorizontaly = isTiledHorizontaly;
 
90
    }
 
91
    
 
92
    
 
93
    @Override
 
94
    public int getIconHeight() {
 
95
        generateIconsFromTiledImage();
 
96
        return icons[0].getIconHeight();
 
97
    }
 
98
    
 
99
    @Override
 
100
    public int getIconWidth() {
 
101
        generateIconsFromTiledImage();
 
102
        return icons[0].getIconWidth();
 
103
    }
 
104
    
 
105
    @Override
 
106
    public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y) {
 
107
        generateIconsFromTiledImage();
 
108
        Icon icon = getIcon(c);
 
109
        if (icon != null) {
 
110
            icon.paintIcon(c, g, x, y);
 
111
        }
 
112
    }
 
113
    
 
114
    private void generateIconsFromTiledImage() {
 
115
        if (icons == null) {
 
116
            icons = new Icon[tileCount];
 
117
            Image[] images = Images.split(tiledImage, tileCount, isTiledHorizontaly);
 
118
            for (int i=0, n = Math.min(images.length, icons.length); i < n; i++) {
 
119
                if (images[i] != null) {
 
120
                    icons[i] = new ImageIcon(images[i]);
 
121
                }
 
122
            }
 
123
            generateMissingIcons();
 
124
            tiledImage = null;
 
125
        }
 
126
    }
 
127
    
 
128
    protected abstract Icon getIcon(Component c);
 
129
    protected abstract void generateMissingIcons();
 
130
}