BallColor.java 5.0 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Simon Wiest
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.model;

26
import hudson.Functions;
K
kohsuke 已提交
27
import hudson.util.ColorPalette;
28
import org.jvnet.localizer.LocaleProvider;
K
i18n  
kohsuke 已提交
29
import org.jvnet.localizer.Localizable;
30 31
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
K
i18n  
kohsuke 已提交
32

K
kohsuke 已提交
33
import java.awt.*;
34 35
import java.util.Locale;

36 37 38 39 40 41 42
/**
 * Ball color used for the build status indication.
 *
 * <p>
 * There are four basic colors, plus their animated "bouncy" versions.
 * {@link #ordinal()} is the sort order. 
 *
43
 * <p>
K
kohsuke 已提交
44
 * Note that multiple {@link BallColor} instances may map to the same
45 46 47 48 49 50 51 52 53
 * RGB color, to avoid the rainbow effect.
 *
 * <h2>Historical Note</h2>
 * <p>
 * Hudson started to overload colors &mdash; for example grey could mean
 * either disabled, aborted, or not yet built. As a result, {@link BallColor}
 * becomes more like a "logical" color, in the sense that different {@link BallColor}
 * values can map to the same RGB color. See issue #956.
 *
54 55
 * @author Kohsuke Kawaguchi
 */
56
public enum BallColor implements StatusIcon {
K
kohsuke 已提交
57 58 59 60 61 62
    RED("red",Messages._BallColor_Failed(), ColorPalette.RED),
    RED_ANIME("red_anime",Messages._BallColor_InProgress(), ColorPalette.RED),
    YELLOW("yellow",Messages._BallColor_Unstable(), ColorPalette.YELLOW),
    YELLOW_ANIME("yellow_anime",Messages._BallColor_InProgress(), ColorPalette.YELLOW),
    BLUE("blue",Messages._BallColor_Success(), ColorPalette.BLUE),
    BLUE_ANIME("blue_anime",Messages._BallColor_InProgress(), ColorPalette.BLUE),
63
    // for historical reasons they are called grey.
K
kohsuke 已提交
64 65
    GREY("grey",Messages._BallColor_Pending(), ColorPalette.GREY),
    GREY_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
66

K
kohsuke 已提交
67 68 69 70
    DISABLED("grey",Messages._BallColor_Disabled(), ColorPalette.GREY),
    DISABLED_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
    ABORTED("grey",Messages._BallColor_Aborted(), ColorPalette.GREY),
    ABORTED_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
71
    ;
72

K
i18n  
kohsuke 已提交
73
    private final Localizable description;
74
    private final String image;
K
kohsuke 已提交
75
    private final Color baseColor;
76

K
kohsuke 已提交
77 78
    BallColor(String image, Localizable description, Color baseColor) {
        this.baseColor = baseColor;
79 80 81
        // name() is not usable in the constructor, so I have to repeat the name twice
        // in the constants definition.
        this.image = image+".gif";
82 83 84
        this.description = description;
    }

85 86 87 88 89 90 91
    /**
     * String like "red.gif" that represents the file name of the image.
     */
    public String getImage() {
        return image;
    }

92
    public String getImageOf(String size) {
K
oops  
Kohsuke Kawaguchi 已提交
93
        return Stapler.getCurrentRequest().getContextPath()+Hudson.RESOURCE_PATH+"/images/"+size+'/'+image;
94 95
    }

96 97 98 99
    /**
     * Gets the human-readable description used as img/@alt.
     */
    public String getDescription() {
100
        return description.toString(LocaleProvider.getLocale());
101
    }
102

K
kohsuke 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116
    /**
     * Gets the RGB color of this color. Animation effect is not reflected to this value.
     */
    public Color getBaseColor() {
        return baseColor;
    }

    /**
     * Returns the {@link #getBaseColor()} in the "#RRGGBB" format.
     */
    public String getHtmlBaseColor() {
        return String.format("#%06X",baseColor.getRGB()&0xFFFFFF);
    }

117 118 119
    /**
     * Also used as a final name.
     */
120
    @Override
121
    public String toString() {
122
        return name().toLowerCase(Locale.ENGLISH);
123 124 125 126 127 128
    }

    /**
     * Gets the animated version.
     */
    public BallColor anime() {
K
kohsuke 已提交
129 130
        if(isAnimated())   return this;
        else               return valueOf(name()+"_ANIME");
131
    }
K
kohsuke 已提交
132 133 134 135 136

    /**
     * Gets the unanimated version.
     */
    public BallColor noAnime() {
K
kohsuke 已提交
137 138 139 140 141 142 143 144 145
        if(isAnimated())   return valueOf(name().substring(0,name().length()-6));
        else               return this;
    }

    /**
     * True if the icon is animated.
     */
    public boolean isAnimated() {
        return name().endsWith("_ANIME");
K
kohsuke 已提交
146
    }
147
}