View Javadoc

1   /*
2    * JXLColor.java
3    *
4    * Created on November 17, 2006, 5:36 PM
5    *
6    * To change this template, choose Tools | Template Manager
7    * and open the template in the editor.
8    */
9   
10  package org.wiztools.xml2spreadsheet.jxlimpl;
11  
12  import java.lang.reflect.Field;
13  import java.util.HashMap;
14  import java.util.Map;
15  import jxl.format.Colour;
16  import org.wiztools.xml2spreadsheet.exception.OperationException;
17  import org.wiztools.xml2spreadsheet.exception.XML2XLSFatalException;
18  
19  /***
20   *
21   * @author subhash
22   */
23  public class JXLColor {
24      
25      private static JXLColor jxlc;
26      
27      private Map<String, Colour> colorMap;
28      
29      /*** Creates a new instance of JXLColor */
30      private JXLColor() {
31          colorMap = new HashMap<String, Colour>();
32          Field[] arr = Colour.class.getFields();
33          try{
34              for(Field f: arr){
35                  //System.out.println("=>>>>"+f.getType().getName()+" :: "+Colour.class.getName());
36                  if(f.getType().getName().equals(Colour.class.getName())){
37                      Colour color = (Colour)f.get(null);
38                      //System.out.println("Putting in Map colors: "+f.getName().toLowerCase());
39                      colorMap.put(f.getName().toLowerCase(), color);
40                  }
41              }
42          }
43          catch(IllegalAccessException iae){
44              //throw new XML2XLSFatalException("Access permission for reflection need to be provided for this application to work.");
45              iae.printStackTrace();
46          }
47      }
48      
49      public static JXLColor getInstance(){
50          if(jxlc == null){
51              jxlc = new JXLColor();
52          }
53          return jxlc;
54      }
55      
56      public Colour getColor(String colorStr) throws OperationException{
57          colorStr = colorStr.toLowerCase();
58          Colour c = colorMap.get(colorStr);
59          if(c == null){
60              throw new OperationException("Color not found in the map: "+colorStr);
61          }
62          return c;
63      }
64  }