package com.sonatype.buildserver.eclipse.ui;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sonatype.buildserver.monitor.HudsonJob;
import com.sonatype.buildserver.monitor.ErrorJob;
import org.hudsonci.rest.model.build.BuildDTO;
import org.hudsonci.rest.model.build.BuildResultDTO;
import org.hudsonci.rest.model.build.BuildStateDTO;
import org.hudsonci.rest.model.project.HealthDTO;
import org.hudsonci.rest.model.project.ProjectDTO;
{
private static Logger log = LoggerFactory.getLogger( HudsonImages.class );
private static final String PATH = "icons/";
public static final String EMPTY = PATH + "empty.gif";
public static final String JUNIT = PATH + "junit.gif";
public static final String CONSOLE = PATH + "console.gif";
public static final String SHOW_FULL_CONSOLE = PATH + "showFullConsole.gif";
public static final String ABORTED = PATH + "grey.gif";
public static final Image ABORTED_IMAGE = getImage( ABORTED );
public static final String LOCKED_BADGE = PATH + "lockedBadge.gif";
public static final String ADD_JOB = PATH + "blue_add.gif";
public static final ImageDescriptor ADD_JOB_DESCRIPTOR = getImageDescriptor( ADD_JOB );
public static final String BUILD = PATH + "build.png";
public static final ImageDescriptor BUILD_DESCRIPTOR = getImageDescriptor( BUILD );
public static final String FAILURE = PATH + "red.gif";
public static final Image FAILURE_IMAGE = getImage( FAILURE );
public static final String FAILURE_ANIME = PATH + "red_anime.gif";
public static final String NOT_BUILT = PATH + "grey.gif";
public static final Image NOT_BUILT_IMAGE = getImage( NOT_BUILT );
public static final String REFRESH = PATH + "refresh.gif";
public static final Image REFRESH_IMAGE = getImage( REFRESH );
public static final String RUNNING = PATH + "running.gif";
public static final Image RUNNING_IMAGE = getImage( RUNNING );
public static final String RUNNING_BADGE = PATH + "running_badge.gif";
public static final String SUCCESS = PATH + "blue.gif";
public static final Image SUCCESS_IMAGE = getImage( SUCCESS );
public static final String SUCCESS_ANIME = PATH + "blue_anime.gif";
public static final String UNSTABLE = PATH + "yellow.gif";
public static final Image UNSTABLE_IMAGE = getImage( UNSTABLE );
public static final String UNSTABLE_ANIME = PATH + "yellow_anime.gif";
public static final String WARNING = PATH + "warning.gif";
public static final Image WARNING_IMAGE = getImage( WARNING );
public static final String HEALTH_00_TO_19 = PATH + "health-00to19.gif";
public static final Image HEALTH_00_TO_20_IMAGE = getImage( HEALTH_00_TO_19 );
public static final String HEALTH_20_TO_39 = PATH + "health-20to39.gif";
public static final Image HEALTH_21_TO_40_IMAGE = getImage( HEALTH_20_TO_39 );
public static final String HEALTH_40_TO_59 = PATH + "health-40to59.gif";
public static final Image HEALTH_41_TO_60_IMAGE = getImage( HEALTH_40_TO_59 );
public static final String HEALTH_60_TO_79 = PATH + "health-60to79.gif";
public static final Image HEALTH_61_TO_80_IMAGE = getImage( HEALTH_60_TO_79 );
public static final String HEALTH_80_PLUS = PATH + "health-80plus.gif";
public static final Image HEALTH_81_PLUS_IMAGE = getImage( HEALTH_80_PLUS );
public static final String WEB = PATH + "web.gif";
public static final ImageDescriptor WEB_DESCRIPTOR = getImageDescriptor( WEB );
public static final String HUDSON = PATH + "hudson.gif";
public static final ImageDescriptor HUDSON_DESCRIPTOR = getImageDescriptor( HUDSON );
public static final Image ADD_CHANGE = getImage( PATH + "document_add.gif" );
public static final Image DELETE_CHANGE = getImage( PATH + "document_delete.gif" );
public static final Image EDIT_CHANGE = getImage( PATH + "document_edit.gif" );
public static final Image CHANGESET = getImage( PATH + "changeset.gif" );
public static final Image JUNIT_IMAGE = getImage( JUNIT );
public static final String OVERLAY_CHANGED = PATH + "overlay_delta.gif";
public static final String OVERLAY_PENDING = PATH + "overlay_pending.gif";
public static Image
getImage( String image )
{
return HudsonUIActivator.getDefault().getImage( image );
}
{
return HudsonUIActivator.getDefault().getImageDescriptor( image );
}
public static Image
getOverlayImage( String base, String overlay1, String overlay2)
{
getOverlayImageDescriptor( base, overlay1,overlay2);
ImageRegistry imageRegistry = getImageRegistry();
return imageRegistry == null ? null : imageRegistry.get( base + overlay1 + overlay2 );
}
{
HudsonUIActivator plugin = HudsonUIActivator.getDefault();
return plugin == null ? null : plugin.getImageRegistry();
}
{
String key = basekey + overlaykey1 + overlaykey2;
try
{
ImageRegistry imageRegistry = getImageRegistry();
if ( imageRegistry != null )
{
ImageDescriptor imageDescriptor = imageRegistry.getDescriptor( key );
if ( imageDescriptor == null )
{
ImageDescriptor base = getImageDescriptor( basekey );
ImageDescriptor overlay1 = overlaykey1 != null ? getImageDescriptor( overlaykey1 ) : null;
ImageDescriptor overlay2 = overlaykey2 != null ? getImageDescriptor( overlaykey2 ) : null;
if ( base == null)
{
log.error( "cannot construct overlay image descriptor for " + basekey + " " + overlaykey1 + " " + overlaykey2 );
return null;
}
imageDescriptor = new DecorationOverlayIcon( base.createImage(),
new ImageDescriptor[] {null, overlay1, null, overlay2, null} );
imageRegistry.put( key, imageDescriptor );
}
return imageDescriptor;
}
}
catch ( Exception ex )
{
log.error( key, ex );
}
return null;
}
{
try
{
ImageRegistry imageRegistry = getImageRegistry();
if ( imageRegistry != null )
{
ImageDescriptor imageDescriptor = imageRegistry.getDescriptor( key );
if ( imageDescriptor != null )
{
imageRegistry.remove( key );
}
{
imageDescriptor = ImageDescriptor.createFromImageData( imageData );
imageRegistry.put( key, imageDescriptor );
}
return imageDescriptor;
}
}
catch ( Exception ex )
{
log.error( key, ex );
}
return null;
}
{
BuildResultDTO buildResult = null;
ProjectDTO jobDetails = job.getJobDetails();
if ( jobDetails != null )
{
if ( jobDetails instanceof ErrorJob )
return HudsonImages.WARNING_IMAGE;
if ( !jobDetails.isEnabled() )
{
return NOT_BUILT_IMAGE;
}
String overlay1 = null;
if (jobDetails.isQueued()) {
overlay1 = OVERLAY_PENDING;
}
String overlay2 = null;
BuildDTO build = jobDetails.getLastBuild();
if ( build != null )
{
buildResult = build.getResult();
}
String base = getResultImagePath( buildResult );
if (base == null) {
if ( build != null && build.getState() == BuildStateDTO.BUILDING ) {
base = RUNNING;
}
}
if (base == null && overlay1 != null) {
return REFRESH_IMAGE;
}
if (base == null) {
return null;
}
return getOverlayImage( base, overlay1, overlay2 );
}
return null;
}
{
if ( result == BuildResultDTO.ABORTED )
{
return ABORTED;
}
else if ( result == BuildResultDTO.FAILURE )
{
return FAILURE;
}
else if ( result == BuildResultDTO.NOT_BUILT )
{
return NOT_BUILT;
}
else if ( result == BuildResultDTO.SUCCESS )
{
return SUCCESS;
}
else if ( result == BuildResultDTO.UNSTABLE )
{
return UNSTABLE;
}
return null;
}
{
String path = getResultImagePath( result );
if (path != null) {
return getImage( path );
}
return null;
}
{
int score = -1;
ProjectDTO jobDetails = job.getJobDetails();
if ( jobDetails != null )
{
if ( jobDetails instanceof ErrorJob )
return null;
HealthDTO health = jobDetails.getHealth();
if ( health != null )
{
score = health.getScore();
}
}
return getHealthImage( score );
}
{
if ( score > 80 )
{
return HEALTH_81_PLUS_IMAGE;
}
else if ( score > 60 )
{
return HEALTH_61_TO_80_IMAGE;
}
else if ( score > 40 )
{
return HEALTH_41_TO_60_IMAGE;
}
else if ( score > 20 )
{
return HEALTH_21_TO_40_IMAGE;
}
else if ( score >= 0 )
{
return HEALTH_00_TO_20_IMAGE;
}
else
return null;
}
}