View Javadoc

1   /*
2    * JXLStyleCreator.java
3    *
4    * Created on November 17, 2006, 5:18 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 jxl.format.Alignment;
13  import jxl.format.Border;
14  import jxl.format.BorderLineStyle;
15  import jxl.format.Colour;
16  import jxl.format.Font;
17  import jxl.format.VerticalAlignment;
18  import jxl.write.WritableCell;
19  import jxl.write.WritableCellFormat;
20  import jxl.write.WritableFont;
21  import jxl.write.WriteException;
22  import org.wiztools.xml2spreadsheet.exception.OperationException;
23  import org.wiztools.xml2spreadsheet.util.StyleHashCreator;
24  import org.wiztools.xml2spreadsheet.util.StyleRepository;
25  
26  /***
27   *
28   * @author subhash
29   */
30  public class JXLStyleCreator {
31      
32      private static JXLStyleCreator jsc = new JXLStyleCreator();
33      
34      /*** Creates a new instance of JXLStyleCreator */
35      private JXLStyleCreator() {
36      }
37      
38      public static JXLStyleCreator getInstance(){
39          return jsc;
40      }
41      
42      public void setBackground(WritableCellFormat wcf, Colour color){
43          
44      }
45      
46      public WritableCellFormat getStyle(final String cellStyleVal,
47              final StyleRepository styleRepo)
48                      throws OperationException{
49          
50          String hash = StyleHashCreator.getHash(cellStyleVal);
51          // System.out.println("style hash: "+hash);
52          WritableCellFormat style = (WritableCellFormat)styleRepo.get(hash);
53          
54          if(style != null){
55              // System.out.println("Got style from repository for hash: "+hash);
56              return style;
57          }
58          
59          style = new WritableCellFormat();
60          
61          String[] arr = cellStyleVal.split("//s*;//s*");
62          
63          try{
64          
65              for(int i=0; i<arr.length; i++){
66                  String[] tarr = arr[i].split("//s*://s*");
67                  if(tarr.length != 2){
68                      continue;
69                  }
70                  String key = tarr[0];
71                  String val = tarr[1];
72                  if("background".equals(key)){
73                      // style.setFillBackgroundColor(HSSFColor.BLACK.index);
74                      style.setBackground(JXLColor.getInstance().getColor(val));
75                  }
76                  else if("color".equals(key)){
77                      Font font = style.getFont();
78                      // font.setColor(HSSFFont.COLOR_RED);
79                      //font.setColor(getColor(val));
80                      //style.setFont(font);
81                  }
82                  else if("align".equals(key)){
83                      if("center".equals(val)){
84                          style.setAlignment(Alignment.CENTRE);
85                      }
86                      else if("justify".equals(val)){
87                          style.setAlignment(Alignment.JUSTIFY);
88                      }
89                      else if("left".equals(val)){
90                          style.setAlignment(Alignment.LEFT);
91                      }
92                      else if("right".equals(val)){
93                          style.setAlignment(Alignment.RIGHT);
94                      }
95                      else if("fill".equals(val)){
96                          style.setAlignment(Alignment.FILL);
97                      }
98                      else if("general".equals(val)){
99                          style.setAlignment(Alignment.GENERAL);
100                     }
101                     else if("center-selection".equals(val)){
102                         style.setAlignment(Alignment.CENTRE);
103                     }
104                     else{
105                         throw new OperationException("Invalid value for style align: "+val);
106                     }
107                 }
108                 else if("valign".equals(key)){
109                     if("center".equals(val)){
110                         style.setVerticalAlignment(VerticalAlignment.CENTRE);
111                     }
112                     else if("bottom".equals(val)){
113                         style.setVerticalAlignment(VerticalAlignment.BOTTOM);
114                     }
115                     else if("top".equals(val)){
116                         style.setVerticalAlignment(VerticalAlignment.TOP);
117                     }
118                     else if("justify".equals(val)){
119                         style.setVerticalAlignment(VerticalAlignment.JUSTIFY);
120                     }
121                     else{
122                         throw new OperationException("Invalid value for style valign: "+val);
123                     }
124                 }
125                 else if("text-decoration".equals(key)){
126                     if("bold".equals(val)){
127                         Font font = style.getFont();
128                         //HSSFFont font = workBook.createFont();
129                         //font.setBoldweight(font.BOLDWEIGHT_BOLD);
130                         //style.setFont(font);
131                     }
132                     else if("normal".equals(val)){
133                         //HSSFFont font = workBook.createFont();
134                         //font.setBoldweight(font.BOLDWEIGHT_NORMAL);
135                         //style.setFont(font);
136                     }
137                     else{
138                         throw new OperationException(
139                                 "Invalid value for style text-decoration: "+val);
140                     }
141                 }
142                 else if("border".equals(key)){
143                     if("dashed".equals(val)){
144                         style.setBorder(Border.ALL, BorderLineStyle.DASHED);
145                     }
146                     else if("dotted".equals(val)){
147                         style.setBorder(Border.ALL, BorderLineStyle.DOTTED);
148                     }
149                     else if("double".equals(val)){
150                         style.setBorder(Border.ALL, BorderLineStyle.DOUBLE);
151                     }
152                     else if("hair".equals(val)){
153                         style.setBorder(Border.ALL, BorderLineStyle.HAIR);
154                     }
155                     else if("medium".equals(val)){
156                         style.setBorder(Border.ALL, BorderLineStyle.MEDIUM);
157                     }
158                     else if("thick".equals(val)){
159                         style.setBorder(Border.ALL, BorderLineStyle.THICK);
160                     }
161                     else{
162                         throw new OperationException(
163                                 "Invalid value for style border: "+val);
164                     }
165                 }
166             }
167         }
168         catch(WriteException we){
169             throw new OperationException("" + we.getMessage());
170         }
171         
172         styleRepo.put(hash, style);
173         
174         return style;
175     }
176     
177 }