PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/BallColor.java

http://github.com/hudson/hudson
Java | 145 lines | 60 code | 15 blank | 70 comment | 4 complexity | 514556f0ef655f2518bdbfd9d7207a56 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Simon Wiest
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.model;
 25
 26import hudson.util.ColorPalette;
 27import org.jvnet.localizer.LocaleProvider;
 28import org.jvnet.localizer.Localizable;
 29import org.kohsuke.stapler.Stapler;
 30
 31import java.awt.*;
 32import java.util.Locale;
 33
 34/**
 35 * Ball color used for the build status indication.
 36 *
 37 * <p>
 38 * There are four basic colors, plus their animated "bouncy" versions.
 39 * {@link #ordinal()} is the sort order. 
 40 *
 41 * <p>
 42 * Note that multiple {@link BallColor} instances may map to the same
 43 * RGB color, to avoid the rainbow effect.
 44 *
 45 * <h2>Historical Note</h2>
 46 * <p>
 47 * Hudson started to overload colors &mdash; for example grey could mean
 48 * either disabled, aborted, or not yet built. As a result, {@link BallColor}
 49 * becomes more like a "logical" color, in the sense that different {@link BallColor}
 50 * values can map to the same RGB color. See issue #956.
 51 *
 52 * @author Kohsuke Kawaguchi
 53 */
 54public enum BallColor implements StatusIcon {
 55    RED("red",Messages._BallColor_Failed(), ColorPalette.RED),
 56    RED_ANIME("red_anime",Messages._BallColor_InProgress(), ColorPalette.RED),
 57    YELLOW("yellow",Messages._BallColor_Unstable(), ColorPalette.YELLOW),
 58    YELLOW_ANIME("yellow_anime",Messages._BallColor_InProgress(), ColorPalette.YELLOW),
 59    BLUE("blue",Messages._BallColor_Success(), ColorPalette.BLUE),
 60    BLUE_ANIME("blue_anime",Messages._BallColor_InProgress(), ColorPalette.BLUE),
 61    // for historical reasons they are called grey.
 62    GREY("grey",Messages._BallColor_Pending(), ColorPalette.GREY),
 63    GREY_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
 64
 65    DISABLED("grey",Messages._BallColor_Disabled(), ColorPalette.GREY),
 66    DISABLED_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
 67    ABORTED("grey",Messages._BallColor_Aborted(), ColorPalette.GREY),
 68    ABORTED_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
 69    ;
 70
 71    private final Localizable description;
 72    private final String image;
 73    private final Color baseColor;
 74
 75    BallColor(String image, Localizable description, Color baseColor) {
 76        this.baseColor = baseColor;
 77        // name() is not usable in the constructor, so I have to repeat the name twice
 78        // in the constants definition.
 79        this.image = image+ (image.endsWith("_anime")?".gif":".png");
 80        this.description = description;
 81    }
 82
 83    /**
 84     * String like "red.gif" that represents the file name of the image.
 85     */
 86    public String getImage() {
 87        return image;
 88    }
 89
 90    public String getImageOf(String size) {
 91        return Stapler.getCurrentRequest().getContextPath()+Hudson.RESOURCE_PATH+"/images/"+size+'/'+image;
 92    }
 93
 94    /**
 95     * Gets the human-readable description used as img/@alt.
 96     */
 97    public String getDescription() {
 98        return description.toString(LocaleProvider.getLocale());
 99    }
100
101    /**
102     * Gets the RGB color of this color. Animation effect is not reflected to this value.
103     */
104    public Color getBaseColor() {
105        return baseColor;
106    }
107
108    /**
109     * Returns the {@link #getBaseColor()} in the "#RRGGBB" format.
110     */
111    public String getHtmlBaseColor() {
112        return String.format("#%06X",baseColor.getRGB()&0xFFFFFF);
113    }
114
115    /**
116     * Also used as a final name.
117     */
118    @Override
119    public String toString() {
120        return name().toLowerCase(Locale.ENGLISH);
121    }
122
123    /**
124     * Gets the animated version.
125     */
126    public BallColor anime() {
127        if(isAnimated())   return this;
128        else               return valueOf(name()+"_ANIME");
129    }
130
131    /**
132     * Gets the unanimated version.
133     */
134    public BallColor noAnime() {
135        if(isAnimated())   return valueOf(name().substring(0,name().length()-6));
136        else               return this;
137    }
138
139    /**
140     * True if the icon is animated.
141     */
142    public boolean isAnimated() {
143        return name().endsWith("_ANIME");
144    }
145}