001    /*
002     * Copyright (C) 2010 Evgeny Mandrikov
003     *
004     * Sonar-IDE is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Lesser General Public
006     * License as published by the Free Software Foundation; either
007     * version 3 of the License, or (at your option) any later version.
008     *
009     * Sonar-IDE is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with Sonar-IDE; if not, write to the Free Software
016     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
017     */
018    
019    package org.sonar.ide.ui;
020    
021    import org.sonar.ide.shared.violations.ViolationUtils;
022    import org.sonar.wsclient.services.Measure;
023    import org.sonar.wsclient.services.Violation;
024    
025    import java.awt.*;
026    
027    /**
028     * @author Evgeny Mandrikov
029     */
030    public final class IconsUtils {
031      private static final Color[] VIOLATION_COLOR = new Color[]{
032          Color.RED, // Blocker
033          Color.RED, // Critical
034          Color.YELLOW, // Major
035          Color.YELLOW, // Minor
036          Color.YELLOW // Info
037      };
038    
039      protected static final String IMAGES_PATH = "/org/sonar/ide/images/";
040    
041      /**
042       * Returns icon for specified violation.
043       *
044       * @param violation violation
045       * @return icon
046       */
047      public static String getIconPath(Violation violation) {
048        return IMAGES_PATH + "violation.png";
049      }
050    
051      /**
052       * Returns priority icon for specified violation.
053       *
054       * @param violation violation
055       * @return priority icon
056       */
057      public static String getPriorityIconPath(Violation violation) {
058        String priority = violation.getPriority();
059        return IMAGES_PATH + "priority/" + priority.toLowerCase() + ".gif";
060      }
061    
062      public static String getTendencyIconPath(Measure measure, boolean small) {
063        Integer trend = measure.getTrend();
064        Integer var = measure.getVar();
065        if (trend == null || var == null) {
066          return null;
067        }
068        if (var == 0) {
069          return null;
070        }
071        // trend = color
072        // var = value
073        StringBuilder sb = new StringBuilder(IMAGES_PATH);
074        sb.append("tendency/");
075        sb.append(var);
076        switch (trend) {
077          case 0:
078            sb.append("-black");
079            break;
080          case -1:
081            sb.append("-red");
082            break;
083          case 1:
084            sb.append("-green");
085            break;
086          default:
087            // WTF?
088            break;
089        }
090        if (small) {
091          sb.append("-small");
092        }
093        sb.append(".png");
094        return sb.toString();
095      }
096    
097      /**
098       * Returns tendency icon for specified measure.
099       *
100       * @param measure measure
101       * @return tendency icon
102       */
103      public static String getTendencyIconPath(Measure measure) {
104        return getTendencyIconPath(measure, false);
105      }
106    
107      /**
108       * Returns color for specified violation.
109       *
110       * @param violation violation
111       * @return color
112       */
113      public static Color getColor(Violation violation) {
114        String priority = violation.getPriority();
115        return VIOLATION_COLOR[ViolationUtils.convertPriority(priority)];
116      }
117    
118      /**
119       * Hide utility-class constructor.
120       */
121      private IconsUtils() {
122      }
123    }