View Javadoc

1   /*
2    * POIColor.java
3    *
4    * Created on April 11, 2005, 4:32 PM
5    */
6   
7   package org.wiztools.xml2spreadsheet.poiimpl;
8   
9   import org.wiztools.xml2spreadsheet.exception.OperationException;
10  import java.util.ArrayList;
11  import java.util.Collection;
12  import java.util.HashMap;
13  import java.util.Map;
14  import java.util.Set;
15  import org.apache.poi.hssf.util.HSSFColor;
16  
17  /***
18   *
19   * @author subhash
20   */
21  public final class POIColor {
22      
23      private static POIColor poiColor;
24      private Map<String, Integer> colorMap;
25      private Collection<String> colorNames;
26      
27      /*** Creates a new instance of POIColor */
28      private POIColor() {
29      }
30      
31      public static synchronized POIColor getInstance(){
32          if(poiColor == null){
33              poiColor = new POIColor();
34              poiColor.populateColor();
35          }
36          return poiColor;
37      }
38      
39      private void populateColor(){
40          colorMap = new HashMap<String, Integer>();
41          colorNames = new ArrayList<String>();
42          Map m = HSSFColor.getIndexHash();
43          Set s = m.keySet();
44          for(Object o: s){
45              String className = m.get(o).getClass().getName();
46              // Example className: org.apache.poi.hssf.util.HSSFColor$DARK_RED
47              // For the above example, the color name would be: dark_red
48              className = className.split("//$")[1].toLowerCase();
49              colorNames.add(className);
50              colorMap.put(className ,(Integer)o);
51          }
52      }
53      
54      public Collection<String> getSupportedColors(){
55          return colorNames;
56      }
57      
58      public short getColor(final String str) throws OperationException{
59          Integer i = colorMap.get(str);
60          if(i == null){
61              throw new OperationException("Color value not found: "+str);
62          }
63          return (short)i.intValue();
64      }
65  }
66