001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * CrosstabCell.java
028: *
029: * Created on 27 settembre 2005, 17.55
030: *
031: */
032:
033: package it.businesslogic.ireport.crosstab;
034:
035: import it.businesslogic.ireport.Box;
036: import it.businesslogic.ireport.BoxElement;
037: import it.businesslogic.ireport.CrosstabReportElement;
038: import it.businesslogic.ireport.ReportElement;
039: import it.businesslogic.ireport.Style;
040: import it.businesslogic.ireport.util.Java2DUtil;
041: import java.awt.Color;
042: import java.awt.Font;
043: import java.awt.Graphics2D;
044: import java.awt.Rectangle;
045: import java.awt.Stroke;
046:
047: /**
048: *
049: * @author Administrator
050: */
051: public class CrosstabCell implements BoxElement {
052:
053: public static final int HEADER_CELL = 1; // Total header, row header, group header
054: public static final int CT_HEADER_CELL = 3; // Crosstab header cell
055: public static final int DETAIL_CELL = 0; // Detail cell
056: public static final int NODATA_CELL = 2; // When no data cell
057:
058: private int type = DETAIL_CELL;
059: private String name = null;
060: private int top = 0;
061: private int left = 0;
062: private int width = 0;
063: private int height = 0;
064: private String mode = "Transparent";
065:
066: private boolean paintSelection = false;
067:
068: private Style style = null;
069:
070: /**
071: * In the grid the line index of the top
072: **/
073: private int topIndex = 0;
074:
075: /**
076: * In the grid the line index of the bottom
077: **/
078: private int bottomIndex = 0;
079:
080: /**
081: * In the grid the line index of the left side
082: **/
083: private int leftIndex = 0;
084:
085: /**
086: * In the grid the line index of the right side
087: **/
088: private int rightIndex = 0;
089:
090: private String rowTotalGroup = "";
091: private String columnTotalGroup = "";
092:
093: private CrosstabReportElement parent = null;
094:
095: private java.awt.Color backcolor = null;
096:
097: private Box box = new Box();
098:
099: /**
100: * The last zoomFactor used in a draw operation
101: **/
102: private double zoomFactor = 1.0;
103:
104: /** Creates a new instance of CrosstabCell */
105: public CrosstabCell() {
106: }
107:
108: public String getName() {
109:
110: if (name == null) {
111: if (getType() == NODATA_CELL) {
112: setName("When no data");
113: } else if (getType() == DETAIL_CELL) {
114: setName(((getRowTotalGroup().equals("")) ? "Detail"
115: : getRowTotalGroup())
116: + " / "
117: + ((getColumnTotalGroup().equals("")) ? "Detail"
118: : getColumnTotalGroup()));
119: } else if (getType() == CT_HEADER_CELL) {
120: setName("Crosstab header");
121: }
122: }
123:
124: return name;
125: }
126:
127: public void setName(String name) {
128: this .name = name;
129: }
130:
131: public int getTop() {
132: return top;
133: }
134:
135: public void setTop(int top) {
136: this .top = top;
137: }
138:
139: public int getLeft() {
140: return left;
141: }
142:
143: public void setLeft(int left) {
144: this .left = left;
145: }
146:
147: public int getWidth() {
148: return width;
149: }
150:
151: public void setWidth(int width) {
152: this .width = width;
153: }
154:
155: public int getHeight() {
156: return height;
157: }
158:
159: public void setHeight(int height) {
160: this .height = height;
161: }
162:
163: public Box getBox() {
164: return box;
165: }
166:
167: public void setBox(Box box) {
168: this .box = box;
169: }
170:
171: public int getType() {
172: return type;
173: }
174:
175: public void setType(int type) {
176: this .type = type;
177: }
178:
179: public String getRowTotalGroup() {
180: return rowTotalGroup;
181: }
182:
183: public void setRowTotalGroup(String rowTotalGroup) {
184: this .rowTotalGroup = rowTotalGroup;
185: }
186:
187: public String getColumnTotalGroup() {
188: return columnTotalGroup;
189: }
190:
191: public void setColumnTotalGroup(String columnTotalGroup) {
192: this .columnTotalGroup = columnTotalGroup;
193: }
194:
195: public java.awt.Color getBackcolor() {
196: return backcolor;
197: }
198:
199: public void setBackcolor(java.awt.Color backcolor) {
200: this .backcolor = backcolor;
201: }
202:
203: public CrosstabReportElement getParent() {
204: return parent;
205: }
206:
207: public void setParent(CrosstabReportElement parent) {
208: this .parent = parent;
209: }
210:
211: public Rectangle getBounds() {
212: return new Rectangle(left, top, width, height);
213: }
214:
215: public void drawCell(Graphics2D g, double zoom_factor) {
216: this .zoomFactor = zoom_factor;
217:
218: Color bgcolor = (this .getBackcolor() == null || getMode()
219: .equals("Transparent")) ? Color.WHITE : this
220: .getBackcolor();
221:
222: if (getType() == NODATA_CELL) {
223: g.setColor(new Color(bgcolor.getRed(), bgcolor.getGreen(),
224: bgcolor.getBlue(), 200));
225: } else {
226: g.setColor(bgcolor);
227: }
228: g.fillRect(10 + getZoomedDim(this .getLeft()),
229: 10 + getZoomedDim(this .getTop()), getZoomedDim(this
230: .getWidth()) - 1,
231: getZoomedDim(this .getHeight()) - 1);
232:
233: g.setFont(new Font("Arial", Font.BOLD, 12)); //getZoomedDim(10)));
234: // We must center the text..
235: //int txt_width = g.getFontMetrics().stringWidth( this.getName() )/2;
236: int txt_height = g.getFontMetrics().getHeight();
237: txt_height -= g.getFontMetrics().getMaxDescent();
238: Color c = (getBackcolor() == null) ? Color.WHITE
239: : getBackcolor();
240: g.setColor(new Color(255 - c.getRed(), 255 - c.getGreen(),
241: 255 - c.getBlue(), 32));
242:
243: try {
244: Java2DUtil.setClip(g, (Math.max(2, getZoomedDim(2))) + 10
245: + getZoomedDim(this .getLeft()), (Math.max(2,
246: getZoomedDim(2)))
247: + 10 + getZoomedDim(this .getTop()),
248: getZoomedDim(this .getWidth()) - 1
249: - (Math.max(2, getZoomedDim(2))),
250: getZoomedDim(this .getHeight()) - 1
251: - (Math.max(2, getZoomedDim(2))));
252:
253: g.drawString(this .getName(), (Math.max(2, getZoomedDim(2)))
254: + 10 + getZoomedDim(this .getLeft()), (Math.max(2,
255: getZoomedDim(2)))
256: + 10
257: + getZoomedDim(this .getTop())
258: + g.getFontMetrics().getAscent());
259:
260: c = (getBackcolor() == null) ? Color.WHITE : getBackcolor();
261: g.setColor(new Color(255 - c.getRed(), 255 - c.getGreen(),
262: 255 - c.getBlue(), 200));
263:
264: } catch (Exception ex) {
265: ex.printStackTrace();
266: } finally {
267: Java2DUtil.resetClip(g);
268: }
269:
270: if (isPaintSelection()) {
271: for (int j = 1; j <= 20; ++j) {
272: g.setColor(new Color(255, 168, 0, j));
273: int i = 10;
274: g.fillRect(getZoomedDim(this .getLeft()) - i + 10,
275: getZoomedDim(this .getTop()) - i + 10,
276: getZoomedDim(this .getWidth()) + (i * 2), i);
277:
278: g.fillRect(getZoomedDim(this .getLeft()) - i + 10,
279: getZoomedDim(this .getTop()) + 10, i,
280: getZoomedDim(this .getHeight()));
281:
282: g.fillRect(getZoomedDim(this .getLeft()) + 10 - i,
283: getZoomedDim(this .getTop())
284: + getZoomedDim(this .getHeight()) + 10,
285: getZoomedDim(this .getWidth()) + (i * 2), i);
286:
287: g.fillRect(getZoomedDim(this .getLeft())
288: + getZoomedDim(this .getWidth()) + 10,
289: getZoomedDim(this .getTop()) + 10, i,
290: getZoomedDim(this .getHeight()));
291: }
292: }
293:
294: }
295:
296: /**
297: * This method draw the box
298: * This method is called after draw elements.
299: *
300: */
301: public void drawCellBox(Graphics2D g, double zoom_factor,
302: boolean isFirstInColumn, boolean isFirstInRow) {
303: this .zoomFactor = zoom_factor;
304: // int correction = (zoom_factor <= 1) ? -1 : 0;
305:
306: Stroke oldStroke = g.getStroke();
307: Color oldColor = g.getColor();
308:
309: if (box != null) {
310: // Left side
311: int ax = getZoomedDim(getLeft()) + 10;
312: int ay = getZoomedDim(getTop()) + 10;
313: int bx = ax + getZoomedDim(getWidth());
314: int by = ay + getZoomedDim(getHeight());
315:
316: Stroke newBoxStroke = null;
317:
318: if (box.getLeftBorderColor() != null
319: && (newBoxStroke = ReportElement.getPenStroke(box
320: .getLeftBorder(), box.getLeftPen(),
321: zoom_factor)) != null) {
322: g.setColor(box.getLeftBorderColor());
323: if ((newBoxStroke = ReportElement
324: .getPenStroke(box.getLeftBorder(), box
325: .getLeftPen(), zoom_factor)) != null) {
326: g.setStroke(newBoxStroke);
327: g.drawLine(ax, ay, ax, by);
328: }
329:
330: newBoxStroke = null;
331: } else if (isFirstInRow
332: && box.getRightBorderColor() != null) {
333: g.setColor(box.getRightBorderColor());
334: if ((newBoxStroke = ReportElement.getPenStroke(box
335: .getRightBorder(), box.getRightPen(),
336: zoom_factor)) != null) {
337: g.setStroke(newBoxStroke);
338: g.drawLine(ax, ay, ax, by);
339: }
340: }
341:
342: if (box.getTopBorderColor() != null
343: && (newBoxStroke = ReportElement.getPenStroke(box
344: .getTopBorder(), box.getTopPen(),
345: zoom_factor)) != null) {
346: g.setColor(box.getTopBorderColor());
347: g.setStroke(newBoxStroke);
348: g.drawLine(ax, ay, bx, ay);
349: } else if (isFirstInColumn
350: && box.getBottomBorderColor() != null) {
351: g.setColor(box.getBottomBorderColor());
352: if ((newBoxStroke = ReportElement.getPenStroke(box
353: .getBottomBorder(), box.getBottomPen(),
354: zoom_factor)) != null) {
355: g.setStroke(newBoxStroke);
356: g.drawLine(ax, ay, bx, ay);
357: }
358: }
359:
360: if (box.getRightBorderColor() != null) {
361: g.setColor(box.getRightBorderColor());
362: if ((newBoxStroke = ReportElement.getPenStroke(box
363: .getRightBorder(), box.getRightPen(),
364: zoom_factor)) != null) {
365: g.setStroke(newBoxStroke);
366: g.drawLine(bx, ay, bx, by);
367: }
368: }
369:
370: if (box.getBottomBorderColor() != null) {
371: g.setColor(box.getBottomBorderColor());
372: if ((newBoxStroke = ReportElement.getPenStroke(box
373: .getBottomBorder(), box.getBottomPen(),
374: zoom_factor)) != null) {
375: g.setStroke(newBoxStroke);
376: g.drawLine(ax, by, bx, by);
377: }
378: }
379: }
380: g.setStroke(oldStroke);
381: }
382:
383: /**
384: * Return an int. It performs zoomFactor*dim and a round.
385: */
386: public int getZoomedDim(int dim) {
387: if (getZoomFactor() == 1.0)
388: return dim;
389: return (int) ((double) dim * getZoomFactor());
390: }
391:
392: /**
393: * Cell duplication (elements are not duplicated!)
394: *
395: **/
396: public CrosstabCell cloneMe() {
397: CrosstabCell cell = new CrosstabCell();
398: cell.setTop(this .getTop());
399: cell.setLeft(this .getLeft());
400: cell.setHeight(this .getHeight());
401: cell.setWidth(this .getWidth());
402: cell.setBox(this .getBox().cloneMe());
403: cell.setBackcolor(this .getBackcolor());
404: cell.setColumnTotalGroup(this .getColumnTotalGroup());
405: cell.setRowTotalGroup(this .getRowTotalGroup());
406: cell.setParent(this .getParent());
407: cell.setType(this .getType());
408:
409: return cell;
410: }
411:
412: public double getZoomFactor() {
413: return zoomFactor;
414: }
415:
416: public void setZoomFactor(double zoomFactor) {
417: this .zoomFactor = zoomFactor;
418: }
419:
420: public int getTopIndex() {
421: return topIndex;
422: }
423:
424: public void setTopIndex(int topIndex) {
425: this .topIndex = topIndex;
426: }
427:
428: public int getBottomIndex() {
429: return bottomIndex;
430: }
431:
432: public void setBottomIndex(int bottomIndex) {
433: this .bottomIndex = bottomIndex;
434: }
435:
436: public int getLeftIndex() {
437: return leftIndex;
438: }
439:
440: public void setLeftIndex(int leftIndex) {
441: this .leftIndex = leftIndex;
442: }
443:
444: public int getRightIndex() {
445: return rightIndex;
446: }
447:
448: public void setRightIndex(int rightIndex) {
449: this .rightIndex = rightIndex;
450: }
451:
452: public String toString() {
453: return getName();
454: }
455:
456: public boolean isPaintSelection() {
457: return paintSelection;
458: }
459:
460: public void setPaintSelection(boolean paintSelection) {
461: this .paintSelection = paintSelection;
462: }
463:
464: public String getMode() {
465: return mode;
466: }
467:
468: public void setMode(String mode) {
469: this .mode = mode;
470: }
471:
472: public Style getStyle() {
473: return style;
474: }
475:
476: public void setStyle(Style style) {
477: if (this .style != style) {
478: this .style = style;
479: if (style != null) {
480: this .setBackcolor(style
481: .getAttributeColor(style.ATTRIBUTE_backcolor,
482: getBackcolor(), true));
483: this .setMode(style.getAttributeString(
484: style.ATTRIBUTE_mode, getMode(), true));
485:
486: // BOX
487: if (style.getAttributeString(style.ATTRIBUTE_border,
488: null, true) != null)
489: this .getBox()
490: .setBorder(
491: style.getAttributeString(
492: style.ATTRIBUTE_border,
493: null, true));
494: if (style.getAttributeColor(
495: style.ATTRIBUTE_borderColor, null, true) != null)
496: this .getBox().setBorderColor(
497: style.getAttributeColor(
498: style.ATTRIBUTE_borderColor, null,
499: true));
500: if (style.getAttributeString(style.ATTRIBUTE_padding,
501: null, true) != null)
502: this .getBox().setPadding(
503: Integer.parseInt(style
504: .getAttributeString(
505: style.ATTRIBUTE_padding,
506: null, true)));
507:
508: if (style.getAttributeString(style.ATTRIBUTE_topBorder,
509: null, true) != null)
510: this .getBox().setTopBorder(
511: style.getAttributeString(
512: style.ATTRIBUTE_topBorder, null,
513: true));
514: if (style.getAttributeColor(
515: style.ATTRIBUTE_topBorderColor, null, true) != null)
516: this .getBox().setTopBorderColor(
517: style.getAttributeColor(
518: style.ATTRIBUTE_topBorderColor,
519: null, true));
520: if (style.getAttributeString(
521: style.ATTRIBUTE_topPadding, null, true) != null)
522: this .getBox().setTopPadding(
523: Integer.parseInt(style.getAttributeString(
524: style.ATTRIBUTE_topPadding, null,
525: true)));
526:
527: if (style.getAttributeString(
528: style.ATTRIBUTE_leftBorder, null, true) != null)
529: this .getBox().setLeftBorder(
530: style.getAttributeString(
531: style.ATTRIBUTE_leftBorder, null,
532: true));
533: if (style.getAttributeColor(
534: style.ATTRIBUTE_leftBorderColor, null, true) != null)
535: this .getBox().setLeftBorderColor(
536: style.getAttributeColor(
537: style.ATTRIBUTE_leftBorderColor,
538: null, true));
539: if (style.getAttributeString(
540: style.ATTRIBUTE_leftPadding, null, true) != null)
541: this .getBox().setLeftPadding(
542: Integer.parseInt(style.getAttributeString(
543: style.ATTRIBUTE_leftPadding, null,
544: true)));
545:
546: if (style.getAttributeString(
547: style.ATTRIBUTE_rightBorder, null, true) != null)
548: this .getBox().setRightBorder(
549: style.getAttributeString(
550: style.ATTRIBUTE_rightBorder, null,
551: true));
552: if (style.getAttributeColor(
553: style.ATTRIBUTE_rightBorderColor, null, true) != null)
554: this .getBox().setRightBorderColor(
555: style.getAttributeColor(
556: style.ATTRIBUTE_rightBorderColor,
557: null, true));
558: if (style.getAttributeString(
559: style.ATTRIBUTE_rightPadding, null, true) != null)
560: this .getBox().setRightPadding(
561: Integer.parseInt(style.getAttributeString(
562: style.ATTRIBUTE_rightPadding, null,
563: true)));
564:
565: if (style.getAttributeString(
566: style.ATTRIBUTE_bottomBorder, null, true) != null)
567: this .getBox().setBottomBorder(
568: style.getAttributeString(
569: style.ATTRIBUTE_bottomBorder, null,
570: true));
571: if (style.getAttributeColor(
572: style.ATTRIBUTE_bottomBorderColor, null, true) != null)
573: this .getBox().setBottomBorderColor(
574: style.getAttributeColor(
575: style.ATTRIBUTE_bottomBorderColor,
576: null, true));
577: if (style.getAttributeString(
578: style.ATTRIBUTE_bottomPadding, null, true) != null)
579: this .getBox().setBottomPadding(
580: Integer.parseInt(style.getAttributeString(
581: style.ATTRIBUTE_bottomPadding,
582: null, true)));
583: }
584: }
585: }
586: }
|