/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. */
  24. package hudson.model;
  25. import hudson.util.ColorPalette;
  26. import org.jvnet.localizer.LocaleProvider;
  27. import org.jvnet.localizer.Localizable;
  28. import org.kohsuke.stapler.Stapler;
  29. import java.awt.*;
  30. import java.util.Locale;
  31. /**
  32. * Ball color used for the build status indication.
  33. *
  34. * <p>
  35. * There are four basic colors, plus their animated "bouncy" versions.
  36. * {@link #ordinal()} is the sort order.
  37. *
  38. * <p>
  39. * Note that multiple {@link BallColor} instances may map to the same
  40. * RGB color, to avoid the rainbow effect.
  41. *
  42. * <h2>Historical Note</h2>
  43. * <p>
  44. * Hudson started to overload colors &mdash; for example grey could mean
  45. * either disabled, aborted, or not yet built. As a result, {@link BallColor}
  46. * becomes more like a "logical" color, in the sense that different {@link BallColor}
  47. * values can map to the same RGB color. See issue #956.
  48. *
  49. * @author Kohsuke Kawaguchi
  50. */
  51. public enum BallColor implements StatusIcon {
  52. RED("red",Messages._BallColor_Failed(), ColorPalette.RED),
  53. RED_ANIME("red_anime",Messages._BallColor_InProgress(), ColorPalette.RED),
  54. YELLOW("yellow",Messages._BallColor_Unstable(), ColorPalette.YELLOW),
  55. YELLOW_ANIME("yellow_anime",Messages._BallColor_InProgress(), ColorPalette.YELLOW),
  56. BLUE("blue",Messages._BallColor_Success(), ColorPalette.BLUE),
  57. BLUE_ANIME("blue_anime",Messages._BallColor_InProgress(), ColorPalette.BLUE),
  58. // for historical reasons they are called grey.
  59. GREY("grey",Messages._BallColor_Pending(), ColorPalette.GREY),
  60. GREY_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
  61. DISABLED("grey",Messages._BallColor_Disabled(), ColorPalette.GREY),
  62. DISABLED_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
  63. ABORTED("grey",Messages._BallColor_Aborted(), ColorPalette.GREY),
  64. ABORTED_ANIME("grey_anime",Messages._BallColor_InProgress(), ColorPalette.GREY),
  65. ;
  66. private final Localizable description;
  67. private final String image;
  68. private final Color baseColor;
  69. BallColor(String image, Localizable description, Color baseColor) {
  70. this.baseColor = baseColor;
  71. // name() is not usable in the constructor, so I have to repeat the name twice
  72. // in the constants definition.
  73. this.image = image+ (image.endsWith("_anime")?".gif":".png");
  74. this.description = description;
  75. }
  76. /**
  77. * String like "red.gif" that represents the file name of the image.
  78. */
  79. public String getImage() {
  80. return image;
  81. }
  82. public String getImageOf(String size) {
  83. return Stapler.getCurrentRequest().getContextPath()+Hudson.RESOURCE_PATH+"/images/"+size+'/'+image;
  84. }
  85. /**
  86. * Gets the human-readable description used as img/@alt.
  87. */
  88. public String getDescription() {
  89. return description.toString(LocaleProvider.getLocale());
  90. }
  91. /**
  92. * Gets the RGB color of this color. Animation effect is not reflected to this value.
  93. */
  94. public Color getBaseColor() {
  95. return baseColor;
  96. }
  97. /**
  98. * Returns the {@link #getBaseColor()} in the "#RRGGBB" format.
  99. */
  100. public String getHtmlBaseColor() {
  101. return String.format("#%06X",baseColor.getRGB()&0xFFFFFF);
  102. }
  103. /**
  104. * Also used as a final name.
  105. */
  106. @Override
  107. public String toString() {
  108. return name().toLowerCase(Locale.ENGLISH);
  109. }
  110. /**
  111. * Gets the animated version.
  112. */
  113. public BallColor anime() {
  114. if(isAnimated()) return this;
  115. else return valueOf(name()+"_ANIME");
  116. }
  117. /**
  118. * Gets the unanimated version.
  119. */
  120. public BallColor noAnime() {
  121. if(isAnimated()) return valueOf(name().substring(0,name().length()-6));
  122. else return this;
  123. }
  124. /**
  125. * True if the icon is animated.
  126. */
  127. public boolean isAnimated() {
  128. return name().endsWith("_ANIME");
  129. }
  130. }