1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ddmuilib; 18 19 import com.android.ddmlib.Log; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.Color; 23 import org.eclipse.swt.graphics.GC; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.widgets.Display; 26 27 public class ImageHelper { 28 29 /** 30 * Loads an image from a resource. This method used a class to locate the 31 * resources, and then load the filename from /images inside the resources.<br> 32 * Extra parameters allows for creation of a replacement image of the 33 * loading failed. 34 * 35 * @param loader the image loader used. 36 * @param display the Display object 37 * @param fileName the file name 38 * @param width optional width to create replacement Image. If -1, null be 39 * be returned if the loading fails. 40 * @param height optional height to create replacement Image. If -1, null be 41 * be returned if the loading fails. 42 * @param phColor optional color to create replacement Image. If null, Blue 43 * color will be used. 44 * @return a new Image or null if the loading failed and the optional 45 * replacement size was -1 46 */ loadImage(IImageLoader loader, Display display, String fileName, int width, int height, Color phColor)47 public static Image loadImage(IImageLoader loader, Display display, 48 String fileName, int width, int height, Color phColor) { 49 50 Image img = null; 51 if (loader != null) { 52 img = loader.loadImage(fileName, display); 53 } 54 55 if (img == null) { 56 Log.w("ddms", "Couldn't load " + fileName); 57 // if we had the extra parameter to create replacement image then we 58 // create and return it. 59 if (width != -1 && height != -1) { 60 return createPlaceHolderArt(display, width, height, 61 phColor != null ? phColor : display 62 .getSystemColor(SWT.COLOR_BLUE)); 63 } 64 65 // otherwise, just return null 66 return null; 67 } 68 69 return img; 70 } 71 72 /** 73 * Create place-holder art with the specified color. 74 */ createPlaceHolderArt(Display display, int width, int height, Color color)75 public static Image createPlaceHolderArt(Display display, int width, 76 int height, Color color) { 77 Image img = new Image(display, width, height); 78 GC gc = new GC(img); 79 gc.setForeground(color); 80 gc.drawLine(0, 0, width, height); 81 gc.drawLine(0, height - 1, width, -1); 82 gc.dispose(); 83 return img; 84 } 85 86 } 87