Coverage Report - org.eclipse.swtbot.swt.finder.utils.ImageFormatConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageFormatConverter
93%
15/16
75%
3/4
2
ImageFormatConverter$ImageType
100%
4/4
N/A
2
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *     Hans Schwaebli - http://swtbot.org/bugzilla/show_bug.cgi?id=112
 11  
  *******************************************************************************/
 12  
 package org.eclipse.swtbot.swt.finder.utils;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.Iterator;
 16  
 import java.util.List;
 17  
 
 18  
 import org.eclipse.swt.SWT;
 19  
 
 20  
 /**
 21  
  * Translates from strings to one of the constants in SWT#IMAGE_*
 22  
  *
 23  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 24  
  * @version $Id$
 25  
  * @since 1.3
 26  
  */
 27  
 public class ImageFormatConverter {
 28  
 
 29  9
         private List<ImageType>        imageTypes        = new ArrayList<ImageType>();
 30  
 
 31  
         /**
 32  
          * The default constructor.
 33  
          */
 34  9
         public ImageFormatConverter() {
 35  9
                 this.imageTypes.add(new ImageType("BMP", SWT.IMAGE_BMP)); //$NON-NLS-1$
 36  9
                 this.imageTypes.add(new ImageType("GIF", SWT.IMAGE_GIF)); //$NON-NLS-1$
 37  9
                 this.imageTypes.add(new ImageType("ICO", SWT.IMAGE_ICO)); //$NON-NLS-1$
 38  9
                 this.imageTypes.add(new ImageType("JPEG", SWT.IMAGE_JPEG)); //$NON-NLS-1$
 39  9
                 this.imageTypes.add(new ImageType("JPG", SWT.IMAGE_JPEG)); //$NON-NLS-1$
 40  9
                 this.imageTypes.add(new ImageType("PNG", SWT.IMAGE_PNG)); //$NON-NLS-1$
 41  9
                 this.imageTypes.add(new ImageType("TIFF", SWT.IMAGE_TIFF)); //$NON-NLS-1$
 42  9
         }
 43  
 
 44  
         /**
 45  
          * @param extension the image format
 46  
          * @return one of the constants defined in SWT#IMAGE*
 47  
          * @throws IllegalArgumentException if the type could not be resolved
 48  
          */
 49  
         public int imageTypeOf(String extension) {
 50  9
                 return typeFor(extension).type;
 51  
         }
 52  
 
 53  
         private ImageType typeFor(String format) {
 54  45
                 for (Iterator<ImageType> iterator = imageTypes.iterator(); iterator.hasNext();) {
 55  36
                         ImageType type = iterator.next();
 56  36
                         if (type.name.equalsIgnoreCase(format))
 57  9
                                 return type;
 58  
                 }
 59  0
                 throw new IllegalArgumentException("Did not understand format: " + format); //$NON-NLS-1$
 60  
         }
 61  
 
 62  
         /**
 63  
          * Mapping from strings to constants in SWT#IMAGE_*
 64  
          *
 65  
          * @author Ketan Padegaonkar &lt;KetanPadegaonkar [at] gmail [dot] com&gt;
 66  
          * @version $Id$
 67  
          */
 68  
         protected class ImageType {
 69  
                 /** The name of the image type */
 70  
                 public final String        name;
 71  
 
 72  
                 /** The type of the image, one of the constants in SWT.IMAGE_ */
 73  
                 public final int        type;
 74  
 
 75  
                 /**
 76  
                  * @param name the name of the image.
 77  
                  * @param type the type of the image.
 78  
                  */
 79  63
                 public ImageType(String name, int type) {
 80  63
                         this.name = name;
 81  63
                         this.type = type;
 82  63
                 }
 83  
         }
 84  
 
 85  
 }