KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > CalipsoDecoratedReportLayout


1 package com.calipso.reportgenerator.common;
2
3 import net.sf.jasperreports.engine.design.*;
4 import net.sf.jasperreports.engine.*;
5 import net.sf.jasperreports.engine.base.JRBaseFont;
6
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.text.DateFormat JavaDoc;
9 import java.util.*;
10
11 /**
12  *
13  * User: jbassino
14  * Date: 18/10/2004
15  * Time: 17:26:19
16  *
17  */

18 public class CalipsoDecoratedReportLayout implements IJasperDefinition {
19   private IJasperDefinition jasperDefinition;
20   private static final int FIRSTCOLUMN_LABELS_X = 2;
21   private static final int FIRST_ROW_LABELS_Y = 2;
22   private static final int headerColumns = 3;
23   private static final int headerRows = 3;
24   private static final String JavaDoc countryParam = "countryparamVALUE";
25   private static final String JavaDoc zoneuserParam = "zoneuserVALUE";
26   private static final String JavaDoc moduleParam = "moduleVALUE";
27   private static final String JavaDoc endOfReportString = "*** FIN DE REPORTE ***";
28   //private int FIRSTCOLUMN_TITLE_LABELS_WIDTH = 70;
29

30   private CalipsoDecoratedReportLayout(){
31   }
32
33   public CalipsoDecoratedReportLayout(IJasperDefinition design) {
34     this.jasperDefinition = design;
35   }
36
37   public JasperDesign getJasperDefinition(boolean isLandscape) throws JRException {
38     JasperDesign design = jasperDefinition.getJasperDefinition(isLandscape);
39     addCalipsoParams(design);
40     addCalipsoHeader(design);
41     return design;
42   }
43
44   private void addCalipsoParams(JasperDesign design) throws JRException{
45     Collection paramNames = getParamsNames(design);
46     String JavaDoc[] names = new String JavaDoc[] {countryParam, zoneuserParam, moduleParam};
47     for (int i = 0; i < names.length; i++) {
48       String JavaDoc name = names[i];
49       if(!paramNames.contains(name)){
50         JRParameter param = getParameterFor(name);
51         design.addParameter(param);
52       }
53     }
54   }
55
56   private JRParameter getParameterFor(String JavaDoc name) {
57     JRDesignParameter parameter = new JRDesignParameter();
58     parameter.setValueClass(java.lang.String JavaDoc.class);
59     JRDesignExpression expression = new JRDesignExpression();
60     expression.setText("");
61     expression.setValueClass(java.lang.String JavaDoc.class);
62     parameter.setDefaultValueExpression(expression);
63     parameter.setName(name);
64     return parameter;
65   }
66
67   private Collection getParamsNames(JasperDesign design) {
68     Collection result = new Vector();
69     JRParameter[] params = design.getParameters();
70     for (int i = 0; i < params.length; i++) {
71       JRParameter param = params[i];
72       result.add(param.getName());
73     }
74     return result;
75   }
76
77   private void addCalipsoHeader(JasperDesign design) {
78     changeTitleBand(design);
79     addPageHeader(design);
80     addReportFooter(design);
81   }
82
83   private void addPageHeader(JasperDesign design) {
84     JRDesignBand pageHeader = (JRDesignBand)design.getPageHeader();
85     pageHeader.setHeight(30);
86     pageHeader.addElement(getBandRectangle(design, pageHeader));
87     addColumnHeaders(design, pageHeader);
88   }
89
90   private void addColumnHeaders(JasperDesign design, JRDesignBand pageHeader) {
91     int columnWidth = getColumnWidth(design);
92     addGroupsHeader(design, pageHeader, columnWidth);
93     addNonGroupsHeader(design, pageHeader, columnWidth);
94   }
95
96   private void addNonGroupsHeader(JasperDesign design, JRDesignBand pageHeader, int columnWidth) {
97     int groupCount = design.getGroups().length;
98     Collection elements = selectStaticTextElements(design.getDetail().getElements());
99     int i=0;
100     for (Iterator iterator = elements.iterator(); iterator.hasNext();i++) {
101       String JavaDoc name = (String JavaDoc) iterator.next();
102       pageHeader.addElement(getColumnHeader(pageHeader, name, columnWidth, i + groupCount));
103     }
104   }
105
106   private JRDesignElement getColumnHeader(JRDesignBand pageHeader, String JavaDoc name, int columnWidth, int i) {
107     JRDesignStaticText text = new JRDesignStaticText();
108     text.setFont(getLabelFont(getLabelHeightFromBand(pageHeader, 1) - 2));
109     text.setText(name);
110     text.setX(i * columnWidth + 1);
111     text.setY(FIRST_ROW_LABELS_Y);
112     text.setHeight(getLabelHeightFromBand(pageHeader, 1) - 2);
113     text.setWidth(columnWidth - 1);
114     text.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER);
115     return text;
116   }
117
118   private Collection selectStaticTextElements(JRElement[] elements) {
119     Collection collection = new Vector();
120     for (int i = 0; i < elements.length; i++) {
121       JRElement element = elements[i];
122       if(element instanceof JRDesignStaticText){
123         collection.add(((JRDesignStaticText)element).getText());
124       }else if(element instanceof JRDesignTextField){
125         String JavaDoc text = ((JRDesignTextField)element).getExpression().getChunks()[0].getText();
126         collection.add(text);
127       }
128     }
129     return collection;
130   }
131
132   private int getColumnWidth(JasperDesign design) {
133     int width = getPrintWidth(design);
134     int columnCount = design.getGroups().length + selectStaticTextElements(design.getDetail().getElements()).size();
135     return width / columnCount;
136   }
137
138   private void addGroupsHeader(JasperDesign design, JRDesignBand pageHeader, int columnWidth) {
139     JRGroup[] groups = design.getGroups();
140     for (int i = 0; i < groups.length; i++) {
141       JRGroup group = groups[i];
142       pageHeader.addElement(getGroupColumnHeader(pageHeader, group, columnWidth, i));
143     }
144   }
145
146   private JRDesignElement getGroupColumnHeader(JRDesignBand pageHeader, JRGroup group, int columnWidth, int i) {
147     String JavaDoc groupName = getTextForHeader(group);
148     JRDesignStaticText text = new JRDesignStaticText();
149     text.setFont(getLabelFont(getLabelHeightFromBand(pageHeader, 1) - 2));
150     text.setText(groupName);
151     text.setX(i * columnWidth + 1);
152     text.setY(FIRST_ROW_LABELS_Y);
153     text.setHeight(getLabelHeightFromBand(pageHeader, 1) - 2);
154     text.setWidth(columnWidth - 1);
155     text.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER);
156     return text;
157   }
158
159   private String JavaDoc getTextForHeader(JRGroup group) {
160     if(group.getName()!=null && group.getName().endsWith("_GROUP")){
161       int endIndex = group.getName().indexOf("_GROUP");
162       return group.getName().substring(0, endIndex);
163     }else{
164       return "";
165     }
166   }
167
168   private void addReportFooter(JasperDesign design) {
169     JRDesignBand endOfReport = (JRDesignBand)design.getSummary();
170     endOfReport.addElement(getEndOfReportLabel(endOfReport));
171   }
172
173   private JRDesignElement getEndOfReportLabel(JRDesignBand band) {
174     JRDesignStaticText text = new JRDesignStaticText();
175     text.setFont(getLabelFont(Math.min(20, band.getHeight())));
176     text.setText(endOfReportString);
177     text.setX(FIRSTCOLUMN_LABELS_X);
178     text.setY(FIRST_ROW_LABELS_Y);
179     text.setHeight(getLabelHeightFromBand(band, 1) - 2);
180     text.setWidth(getWidthForText(text));
181     text.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_LEFT);
182     return text;
183   }
184
185   /**
186    * Retorna un ancho aproximado o probable que podria ocupar un texto con una fuente determinada.
187    * No sirve para fuentes de mas de 18 puntos.
188    * @param text
189    * @return
190    */

191   private int getWidthForText(JRDesignStaticText text) {
192     if(text.getFont().getSize() < 12){
193       return text.getText().length() * 10;
194     }else{
195       return text.getText().length() * 20;
196     }
197   }
198
199   private void changeTitleBand(JasperDesign design) {
200     JRBand oldTitleBand = design.getTitle();
201     JRDesignBand titleBand = new JRDesignBand();
202     titleBand.setHeight(oldTitleBand.getHeight());
203     moveOldElements(design, oldTitleBand, titleBand);
204     JRDesignRectangle rectangle = getBandRectangle(design, oldTitleBand);
205     titleBand.addElement(rectangle);
206     titleBand.addElement(getCalipsoLabel(design, titleBand));
207     titleBand.addElement(getCountryZoneLabel(design, titleBand));
208     titleBand.addElement(getZoneUserLabel(design, titleBand));
209     titleBand.addElement(getModuleLabel(design, titleBand));
210     addPageNumberLabel(design, titleBand);
211     titleBand.addElement(getDateLabel(design, titleBand));
212     titleBand.addElement(getTimeLabel(design, titleBand));
213     design.setTitle(titleBand);
214   }
215
216   private JRDesignElement getTimeLabel(JasperDesign design, JRDesignBand titleBand) {
217     JRDesignTextField field = new JRDesignTextField();
218     field.setX(getLabelPosX(design, 3, 3));
219     field.setY(getLabelPosY(titleBand, headerRows, 3));
220     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
221     field.setWidth(getTitleLabelsWidth(design, headerColumns));
222     field.setFont(getLabelFont(field.getHeight()));
223     field.setBlankWhenNull(true);
224     JRDesignExpression expression = new JRDesignExpression();
225     expression.setValueClass(java.lang.String JavaDoc.class);
226     expression.setText("SimpleDateFormat.getTimeInstance().format(GregorianCalendar.getInstance().getTime())");
227     field.setExpression(expression);
228     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_RIGHT);
229     return field;
230   }
231
232   private JRDesignElement getDateLabel(JasperDesign design, JRDesignBand titleBand) {
233     JRDesignTextField field = new JRDesignTextField();
234     field.setX(getLabelPosX(design, 3, 3));
235     field.setY(getLabelPosY(titleBand, headerRows, 2));
236     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
237     field.setWidth(getTitleLabelsWidth(design, headerColumns));
238     field.setFont(getLabelFont(field.getHeight()));
239     field.setBlankWhenNull(true);
240     JRDesignExpression expression = new JRDesignExpression();
241     expression.setValueClass(java.lang.String JavaDoc.class);
242     expression.setText("new SimpleDateFormat(\"dd/MM/yyyy\").format(GregorianCalendar.getInstance().getTime())");
243     field.setExpression(expression);
244     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_RIGHT);
245     return field;
246   }
247
248   private void addPageNumberLabel(JasperDesign design, JRDesignBand titleBand) {
249     JRDesignStaticText textElement = new JRDesignStaticText();
250     textElement.setText("Pag. ");
251     int posX = design.getPageWidth() - design.getRightMargin();
252     textElement.setX(posX - 115);
253     textElement.setY(FIRST_ROW_LABELS_Y);
254     textElement.setHeight(getLabelHeightFromBand(titleBand, headerRows));
255     textElement.setWidth(30);
256     textElement.setFont(getLabelFont(textElement.getHeight()));
257     titleBand.addElement(textElement);
258
259     JRDesignTextField field = new JRDesignTextField();
260     field.setX(posX - 85);
261     field.setY(FIRST_ROW_LABELS_Y);
262     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
263     field.setWidth(22);
264     field.setFont(getLabelFont(textElement.getHeight()));
265     JRDesignExpression expression = new JRDesignExpression();
266     expression.setValueClass(java.lang.Integer JavaDoc.class);
267     expression.setText("$V{PAGE_NUMBER}");
268     field.setExpression(expression);
269     field.setPattern("###");
270     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER);
271     titleBand.addElement(field);
272
273     textElement = new JRDesignStaticText();
274     textElement.setText(" de ");
275     textElement.setX(posX - 63);
276     textElement.setY(FIRST_ROW_LABELS_Y);
277     textElement.setHeight(getLabelHeightFromBand(titleBand, headerRows));
278     textElement.setWidth(20);
279     textElement.setFont(getLabelFont(textElement.getHeight()));
280     titleBand.addElement(textElement);
281
282     field = new JRDesignTextField();
283     field.setX(posX - 43);
284     field.setY(FIRST_ROW_LABELS_Y);
285     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
286     field.setWidth(22);
287     field.setFont(getLabelFont(textElement.getHeight()));
288     expression = new JRDesignExpression();
289     expression.setValueClass(java.lang.Integer JavaDoc.class);
290     expression.setText("$V{PAGE_COUNT}");
291     field.setExpression(expression);
292     field.setPattern("###");
293     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER);
294     titleBand.addElement(field);
295   }
296
297   private JRDesignElement getModuleLabel(JasperDesign design, JRDesignBand titleBand) {
298     JRDesignTextField field = new JRDesignTextField();
299     field.setX(getLabelPosX(design, headerColumns, 2));
300     field.setY(FIRST_ROW_LABELS_Y);
301     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
302     field.setWidth(getTitleLabelsWidth(design, headerColumns));
303     field.setFont(getLabelFont(field.getHeight()));
304     field.setBlankWhenNull(true);
305     JRDesignExpression expression = new JRDesignExpression();
306     expression.setValueClass(java.lang.String JavaDoc.class);
307     expression.setText("$P{"+ moduleParam +"}");
308     field.setExpression(expression);
309     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER);
310     return field;
311   }
312
313   private void moveOldElements(JasperDesign design, JRBand oldTitleBand, JRDesignBand titleBand) {
314     JRElement[] elements = oldTitleBand.getElements();
315     if(elements.length > 1){
316       System.out.println(LanguageTraslator.traslate("468"));
317     }
318     JRDesignTextElement element = null;
319     try{
320       element = (JRDesignTextElement)elements[0];
321     }catch (ClassCastException JavaDoc e){
322       System.out.println(LanguageTraslator.traslate("469"));
323       e.printStackTrace();
324     }
325     element.setX(getLabelPosX(design, headerColumns, 2));
326     element.setY(getLabelPosY(titleBand, headerRows, 2));
327     element.setFont(getLabelFont(getLabelHeightFromBand(titleBand, headerRows)));
328     element.setHeight(getLabelHeightFromBand(titleBand, headerRows));
329     element.setWidth(getTitleLabelsWidth(design, headerColumns));
330     element.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER);
331     titleBand.addElement(element);
332   }
333
334   /**
335    * Retorna la posicion X donde debera ubicarse el elemento segun cuantas columnas halla, y su numero de columna
336    * @param design El design al que se agregara el elemento (tiene el width)
337    * @param columns Cantidad de columnas (cantidad de elementos que se agregaran seguidos en la misma linea)
338    * @param columnNumber
339    * @return
340    */

341   private int getLabelPosX(JasperDesign design, int columns, int columnNumber) {
342     int width = getTitleLabelsWidth(design, columns);
343     return (columnNumber - 1)* width;
344   }
345
346   /**
347    * Retorna el valor de la posicion Y donde se ubicara el elemento en el design.
348    * @param band La banda donde estara el elemento
349    * @param rows La cantidad de elementos que habrá uno sobre otro (como en filas)
350    * @param pos La posicion o numero de fila. Para la primera es uno.
351    * @return
352    */

353   private int getLabelPosY(JRDesignBand band, int rows, int pos){
354     int height = getLabelHeightFromBand(band, rows);
355     return ((pos - 1) * height) + ((pos - 1) * (height / rows));
356   }
357
358   private JRDesignElement getZoneUserLabel(JasperDesign design, JRDesignBand titleBand) {
359     JRDesignTextField field = new JRDesignTextField();
360     field.setX(FIRSTCOLUMN_LABELS_X);
361     field.setY(getLabelPosY(titleBand, headerRows, 3));
362     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
363     field.setWidth(getTitleLabelsWidth(design, headerColumns));
364     field.setFont(getLabelFont(field.getHeight()));
365     field.setBlankWhenNull(true);
366     JRDesignExpression expression = new JRDesignExpression();
367     expression.setValueClass(java.lang.String JavaDoc.class);
368     expression.setText("$P{"+ zoneuserParam +"}");
369     field.setExpression(expression);
370     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_LEFT);
371     return field;
372   }
373
374   private int getTitleLabelsWidth(JasperDesign design, int columns) {
375     return getPrintWidth(design) / columns;
376   }
377
378   private JRDesignElement getCountryZoneLabel(JasperDesign design, JRDesignBand titleBand) {
379     JRDesignTextField field = new JRDesignTextField();
380     field.setX(FIRSTCOLUMN_LABELS_X);
381     field.setY(getLabelPosY(titleBand, headerRows, 2));
382     field.setHeight(getLabelHeightFromBand(titleBand, headerRows));
383     field.setWidth(getTitleLabelsWidth(design, headerColumns));
384     field.setFont(getLabelFont(field.getHeight()));
385     field.setBlankWhenNull(true);
386     JRDesignExpression expression = new JRDesignExpression();
387     expression.setValueClass(java.lang.String JavaDoc.class);
388     expression.setText("$P{"+ countryParam +"}");
389     field.setExpression(expression);
390     field.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_LEFT);
391     return field;
392   }
393
394   private JRDesignElement getCalipsoLabel(JasperDesign design, JRDesignBand band) {
395     JRDesignStaticText staticText = new JRDesignStaticText();
396     staticText.setX(FIRSTCOLUMN_LABELS_X);
397     staticText.setY(FIRST_ROW_LABELS_Y);
398     staticText.setHeight(getLabelHeightFromBand(band, headerRows));
399     staticText.setWidth(getTitleLabelsWidth(design, headerColumns));
400     staticText.setFont(getLabelFont(staticText.getHeight()));
401     staticText.setText("CALIPSO");
402     staticText.setTextAlignment(JRTextElement.HORIZONTAL_ALIGN_LEFT);
403     return staticText;
404   }
405
406   private JRFont getLabelFont(int height) {
407     JRDesignFont font = new JRDesignFont();
408     font.setFontName("Arial");
409     font.setSize(getFontSize(height));
410     return font;
411   }
412
413   private int getFontSize(int labalHeight) {
414     if(labalHeight < 13){
415       return labalHeight - 4;
416     }else if(labalHeight < 18){
417       return labalHeight - 5;
418     }else{
419       return 12;
420     }
421   }
422
423   /*private int getCalipsoLabelWidth(JRDesignBand band) {
424     return
425   }*/

426
427   private int getLabelHeightFromBand(JRDesignBand band, int labels) {
428     return (band.getHeight() / labels + 1);
429   }
430
431   private void addElements(JRBand fromBand, JRDesignBand toBand) {
432     JRElement[] elements = fromBand.getElements();
433     for (int i = 0; i < elements.length; i++) {
434       JRElement element = elements[i];
435       toBand.addElement((JRDesignElement)element);
436     }
437   }
438
439   private int getPrintWidth(JasperDesign design){
440     return (design.getPageWidth() - design.getLeftMargin() - design.getRightMargin());
441   }
442
443   private JRDesignRectangle getBandRectangle(JasperDesign design, JRBand band) {
444     JRDesignRectangle rectangle = new JRDesignRectangle();
445     int width = getPrintWidth(design);
446     rectangle.setX(0);
447     rectangle.setY(0);
448     rectangle.setHeight(band.getHeight());
449     rectangle.setWidth(width);
450     rectangle.setPen(JRGraphicElement.PEN_2_POINT);
451     rectangle.setStretchType(JRGraphicElement.STRETCH_TYPE_RELATIVE_TO_BAND_HEIGHT);
452     rectangle.setMode(JRGraphicElement.MODE_TRANSPARENT);
453     return rectangle;
454   }
455 }
456
Popular Tags