KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > crosstab > CrosstabCell


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * CrosstabCell.java
28  *
29  * Created on 27 settembre 2005, 17.55
30  *
31  */

32
33 package it.businesslogic.ireport.crosstab;
34 import it.businesslogic.ireport.Box;
35 import it.businesslogic.ireport.BoxElement;
36 import it.businesslogic.ireport.CrosstabReportElement;
37 import it.businesslogic.ireport.ReportElement;
38 import it.businesslogic.ireport.Style;
39 import it.businesslogic.ireport.util.Java2DUtil;
40 import java.awt.Color JavaDoc;
41 import java.awt.Font JavaDoc;
42 import java.awt.Graphics2D JavaDoc;
43 import java.awt.Rectangle JavaDoc;
44 import java.awt.Stroke JavaDoc;
45
46 /**
47  *
48  * @author Administrator
49  */

50 public class CrosstabCell implements BoxElement {
51     
52     
53     public static final int HEADER_CELL = 1; // Total header, row header, group header
54
public static final int CT_HEADER_CELL = 3; // Crosstab header cell
55
public static final int DETAIL_CELL = 0; // Detail cell
56
public static final int NODATA_CELL = 2; // When no data cell
57

58     private int type = DETAIL_CELL;
59     private String JavaDoc name = null;
60     private int top = 0;
61     private int left = 0;
62     private int width = 0;
63     private int height = 0;
64     private String JavaDoc mode = "Transparent";
65     
66     private boolean paintSelection = false;
67     
68     private Style style = null;
69     
70     /**
71      * In the grid the line index of the top
72      **/

73     private int topIndex = 0;
74     
75     /**
76      * In the grid the line index of the bottom
77      **/

78     private int bottomIndex = 0;
79     
80     /**
81      * In the grid the line index of the left side
82      **/

83     private int leftIndex = 0;
84     
85     /**
86      * In the grid the line index of the right side
87      **/

88     private int rightIndex = 0;
89     
90     
91     private String JavaDoc rowTotalGroup = "";
92     private String JavaDoc columnTotalGroup = "";
93     
94     private CrosstabReportElement parent = null;
95     
96     private java.awt.Color JavaDoc backcolor = null;
97     
98     private Box box = new Box();
99     
100     /**
101      * The last zoomFactor used in a draw operation
102      **/

103     private double zoomFactor = 1.0;
104     
105     /** Creates a new instance of CrosstabCell */
106     public CrosstabCell() {
107     }
108
109     public String JavaDoc getName() {
110         
111         if (name == null)
112         {
113             if (getType() == NODATA_CELL)
114             {
115                 setName("When no data");
116             }
117             else if (getType() == DETAIL_CELL)
118             {
119                setName( ((getRowTotalGroup().equals("")) ? "Detail" : getRowTotalGroup()) + " / " + ((getColumnTotalGroup().equals("")) ? "Detail" : getColumnTotalGroup()) );
120             }
121            else if (getType() == CT_HEADER_CELL)
122            {
123                 setName("Crosstab header");
124            }
125         }
126         
127         return name;
128     }
129
130     public void setName(String JavaDoc name) {
131         this.name = name;
132     }
133
134     public int getTop() {
135         return top;
136     }
137
138     public void setTop(int top) {
139         this.top = top;
140     }
141
142     public int getLeft() {
143         return left;
144     }
145
146     public void setLeft(int left) {
147         this.left = left;
148     }
149
150     public int getWidth() {
151         return width;
152     }
153
154     public void setWidth(int width) {
155         this.width = width;
156     }
157
158     public int getHeight() {
159         return height;
160     }
161
162     public void setHeight(int height) {
163         this.height = height;
164     }
165
166     public Box getBox() {
167         return box;
168     }
169
170     public void setBox(Box box) {
171         this.box = box;
172     }
173
174     public int getType() {
175         return type;
176     }
177
178     public void setType(int type) {
179         this.type = type;
180     }
181
182     public String JavaDoc getRowTotalGroup() {
183         return rowTotalGroup;
184     }
185
186     public void setRowTotalGroup(String JavaDoc rowTotalGroup) {
187         this.rowTotalGroup = rowTotalGroup;
188     }
189
190     public String JavaDoc getColumnTotalGroup() {
191         return columnTotalGroup;
192     }
193
194     public void setColumnTotalGroup(String JavaDoc columnTotalGroup) {
195         this.columnTotalGroup = columnTotalGroup;
196     }
197
198     public java.awt.Color JavaDoc getBackcolor() {
199         return backcolor;
200     }
201
202     public void setBackcolor(java.awt.Color JavaDoc backcolor) {
203         this.backcolor = backcolor;
204     }
205
206     public CrosstabReportElement getParent() {
207         return parent;
208     }
209
210     public void setParent(CrosstabReportElement parent) {
211         this.parent = parent;
212     }
213     
214     public Rectangle JavaDoc getBounds()
215     {
216         return new Rectangle JavaDoc(left,top,width,height);
217     }
218     
219     
220     public void drawCell(Graphics2D JavaDoc g, double zoom_factor)
221     {
222         this.zoomFactor = zoom_factor;
223         
224         
225         Color JavaDoc bgcolor = (this.getBackcolor() == null || getMode().equals("Transparent")) ? Color.WHITE : this.getBackcolor();
226         
227         
228         if (getType() == NODATA_CELL)
229         {
230             g.setColor(new Color JavaDoc( bgcolor.getRed(), bgcolor.getGreen(), bgcolor.getBlue(), 200 ));
231         }
232         else
233         {
234             g.setColor( bgcolor );
235         }
236         g.fillRect(10 + getZoomedDim(this.getLeft()), 10 + getZoomedDim(this.getTop()),
237                 getZoomedDim(this.getWidth())-1, getZoomedDim(this.getHeight())-1);
238                 
239                 
240         
241         g.setFont( new Font JavaDoc( "Arial", Font.BOLD, 12)); //getZoomedDim(10)));
242
// We must center the text..
243
//int txt_width = g.getFontMetrics().stringWidth( this.getName() )/2;
244
int txt_height = g.getFontMetrics().getHeight();
245         txt_height -= g.getFontMetrics().getMaxDescent();
246         Color JavaDoc c = (getBackcolor() == null) ? Color.WHITE : getBackcolor();
247         g.setColor(new Color JavaDoc(255-c.getRed(),255 - c.getGreen(),255-c.getBlue(),32));
248
249         Java2DUtil.setClip(g,
250                 (Math.max(2,getZoomedDim(2))) + 10 + getZoomedDim(this.getLeft()),
251                 (Math.max(2,getZoomedDim(2))) + 10 + getZoomedDim(this.getTop()),
252                 getZoomedDim(this.getWidth())-1-(Math.max(2,getZoomedDim(2))),
253                 getZoomedDim(this.getHeight())-1-(Math.max(2,getZoomedDim(2))));
254
255         g.drawString( this.getName(),
256         (Math.max(2,getZoomedDim(2))) + 10 + getZoomedDim(this.getLeft()),
257         (Math.max(2,getZoomedDim(2))) + 10 + getZoomedDim(this.getTop()) + g.getFontMetrics().getAscent());
258         
259         
260         c = (getBackcolor() == null) ? Color.WHITE : getBackcolor();
261         g.setColor(new Color JavaDoc(255-c.getRed(),255 - c.getGreen(),255-c.getBlue(),200));
262
263         Java2DUtil.resetClip(g);
264         
265         if (isPaintSelection())
266         {
267             for (int j=1; j<=20; ++j)
268             {
269                  g.setColor(new Color JavaDoc(255,168,0,j));
270                  int i=10;
271                  g.fillRect(getZoomedDim(this.getLeft())-i+10, getZoomedDim(this.getTop())-i+10,
272                             getZoomedDim(this.getWidth())+(i*2), i);
273
274                  g.fillRect(getZoomedDim(this.getLeft())-i+10, getZoomedDim(this.getTop())+10,
275                            i, getZoomedDim(this.getHeight()));
276
277                  g.fillRect(getZoomedDim(this.getLeft())+10-i, getZoomedDim(this.getTop())+getZoomedDim(this.getHeight())+10,
278                            getZoomedDim(this.getWidth())+(i*2), i);
279
280                  g.fillRect(getZoomedDim(this.getLeft())+getZoomedDim(this.getWidth())+10, getZoomedDim(this.getTop())+10,
281                            i, getZoomedDim(this.getHeight()));
282             }
283         }
284
285     }
286     
287     
288     /**
289      * This method draw the box
290      * This method is called after draw elements.
291      *
292      */

293     public void drawCellBox(Graphics2D JavaDoc g, double zoom_factor, boolean isFirstInColumn, boolean isFirstInRow)
294     {
295        this.zoomFactor = zoom_factor;
296        // int correction = (zoom_factor <= 1) ? -1 : 0;
297

298         Stroke JavaDoc oldStroke = g.getStroke();
299         Color JavaDoc oldColor = g.getColor();
300
301         if (box != null)
302         {
303             // Left side
304
int ax = getZoomedDim(getLeft())+10;
305             int ay = getZoomedDim(getTop())+10;
306             int bx = ax + getZoomedDim(getWidth());
307             int by = ay + getZoomedDim(getHeight());
308
309             Stroke JavaDoc newBoxStroke = null;
310
311             if (box.getLeftBorderColor() != null && (newBoxStroke = ReportElement.getPenStroke(box.getLeftBorder(), zoom_factor)) != null)
312             {
313                 g.setColor(box.getLeftBorderColor());
314                 if ((newBoxStroke = ReportElement.getPenStroke(box.getLeftBorder(), zoom_factor)) != null)
315                 {
316                     g.setStroke(newBoxStroke);
317                     g.drawLine(ax, ay, ax, by);
318                 }
319                 
320                 newBoxStroke = null;
321             }
322             else if (isFirstInRow && box.getRightBorderColor() != null)
323             {
324                 g.setColor(box.getRightBorderColor());
325                 if ((newBoxStroke = ReportElement.getPenStroke(box.getRightBorder(), zoom_factor)) != null)
326                 {
327                     g.setStroke(newBoxStroke);
328                     g.drawLine(ax, ay, ax, by);
329                 }
330             }
331
332             if (box.getTopBorderColor() != null && (newBoxStroke = ReportElement.getPenStroke(box.getTopBorder(), zoom_factor)) != null)
333             {
334                 g.setColor(box.getTopBorderColor());
335                 g.setStroke(newBoxStroke);
336                 g.drawLine(ax, ay, bx, ay);
337             }
338             else if (isFirstInColumn && box.getBottomBorderColor() != null)
339             {
340                 g.setColor(box.getBottomBorderColor());
341                 if ((newBoxStroke = ReportElement.getPenStroke(box.getBottomBorder(), zoom_factor)) != null)
342                 {
343                     g.setStroke(newBoxStroke);
344                     g.drawLine(ax, ay, bx, ay);
345                 }
346             }
347             
348             if (box.getRightBorderColor() != null)
349             {
350                 g.setColor(box.getRightBorderColor());
351                 if ((newBoxStroke = ReportElement.getPenStroke(box.getRightBorder(), zoom_factor)) != null)
352                 {
353                     g.setStroke(newBoxStroke);
354                     g.drawLine(bx, ay, bx, by);
355                 }
356             }
357             
358             if (box.getBottomBorderColor() != null)
359             {
360                 g.setColor(box.getBottomBorderColor());
361                 if ((newBoxStroke = ReportElement.getPenStroke(box.getBottomBorder(), zoom_factor)) != null)
362                 {
363                     g.setStroke(newBoxStroke);
364                     g.drawLine(ax, by, bx, by);
365                 }
366             }
367         }
368         g.setStroke(oldStroke);
369     }
370     
371     /**
372      * Return an int. It performs zoomFactor*dim and a round.
373      */

374     public int getZoomedDim(int dim) {
375         if (getZoomFactor() == 1.0) return dim;
376         return (int)((double)dim*getZoomFactor());
377     }
378     
379     /**
380      * Cell duplication (elements are not duplicated!)
381      *
382      **/

383     public CrosstabCell cloneMe()
384     {
385         CrosstabCell cell = new CrosstabCell();
386         cell.setTop( this.getTop());
387         cell.setLeft( this.getLeft());
388         cell.setHeight( this.getHeight());
389         cell.setWidth( this.getWidth());
390         cell.setBox( this.getBox().cloneMe());
391         cell.setBackcolor( this.getBackcolor());
392         cell.setColumnTotalGroup( this.getColumnTotalGroup() );
393         cell.setRowTotalGroup( this.getRowTotalGroup() );
394         cell.setParent( this.getParent());
395         cell.setType( this.getType());
396         
397         return cell;
398     }
399
400     public double getZoomFactor() {
401         return zoomFactor;
402     }
403
404     public void setZoomFactor(double zoomFactor) {
405         this.zoomFactor = zoomFactor;
406     }
407
408     public int getTopIndex() {
409         return topIndex;
410     }
411
412     public void setTopIndex(int topIndex) {
413         this.topIndex = topIndex;
414     }
415
416     public int getBottomIndex() {
417         return bottomIndex;
418     }
419
420     public void setBottomIndex(int bottomIndex) {
421         this.bottomIndex = bottomIndex;
422     }
423
424     public int getLeftIndex() {
425         return leftIndex;
426     }
427
428     public void setLeftIndex(int leftIndex) {
429         this.leftIndex = leftIndex;
430     }
431
432     public int getRightIndex() {
433         return rightIndex;
434     }
435
436     public void setRightIndex(int rightIndex) {
437         this.rightIndex = rightIndex;
438     }
439     
440     public String JavaDoc toString()
441     {
442         return getName();
443     }
444
445     public boolean isPaintSelection() {
446         return paintSelection;
447     }
448
449     public void setPaintSelection(boolean paintSelection) {
450         this.paintSelection = paintSelection;
451     }
452
453     public String JavaDoc getMode() {
454         return mode;
455     }
456
457     public void setMode(String JavaDoc mode) {
458         this.mode = mode;
459     }
460
461     public Style getStyle() {
462         return style;
463     }
464
465     public void setStyle(Style style) {
466         if (this.style != style)
467         {
468             this.style = style;
469             if (style != null)
470             {
471                 this.setBackcolor( style.getAttributeColor( style.ATTRIBUTE_backcolor,getBackcolor(), true) );
472                 this.setMode( style.getAttributeString( style.ATTRIBUTE_mode, getMode(), true) );
473
474                 
475                 // BOX
476
if (style.getAttributeString(style.ATTRIBUTE_border, null, true) != null)
477                     this.getBox().setBorder( style.getAttributeString(style.ATTRIBUTE_border, null, true) );
478                 if (style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true) != null)
479                     this.getBox().setBorderColor( style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true));
480                 if (style.getAttributeString(style.ATTRIBUTE_padding, null, true) != null)
481                     this.getBox().setPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_padding, null, true) ));
482
483                 if (style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) != null)
484                     this.getBox().setTopBorder( style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) );
485                 if (style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true) != null)
486                     this.getBox().setTopBorderColor( style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true));
487                 if (style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) != null)
488                     this.getBox().setTopPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) ));
489
490                 if (style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) != null)
491                     this.getBox().setLeftBorder( style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) );
492                 if (style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true) != null)
493                     this.getBox().setLeftBorderColor( style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true));
494                 if (style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) != null)
495                     this.getBox().setLeftPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) ));
496
497                 if (style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) != null)
498                     this.getBox().setRightBorder( style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) );
499                 if (style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true) != null)
500                     this.getBox().setRightBorderColor( style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true));
501                 if (style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) != null)
502                     this.getBox().setRightPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) ));
503
504                 if (style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) != null)
505                     this.getBox().setBottomBorder( style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) );
506                 if (style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true) != null)
507                     this.getBox().setBottomBorderColor( style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true));
508                 if (style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) != null)
509                     this.getBox().setBottomPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) ));
510             }
511         }
512     }
513 }
514
Popular Tags