View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.domain;
31  
32  import ar.com.fdvs.dj.domain.constants.*;
33  import ar.com.fdvs.dj.domain.constants.Font;
34  import ar.com.fdvs.dj.domain.constants.Transparency;
35  import ar.com.fdvs.dj.domain.entities.Entity;
36  import ar.com.fdvs.dj.util.LayoutUtils;
37  import net.sf.jasperreports.engine.base.JRBaseStyle;
38  import net.sf.jasperreports.engine.base.JRBoxPen;
39  import net.sf.jasperreports.engine.design.JRDesignConditionalStyle;
40  import net.sf.jasperreports.engine.design.JRDesignStyle;
41  import net.sf.jasperreports.engine.type.*;
42  
43  import java.awt.*;
44  import java.io.Serializable;
45  
46  /**
47   * Class that should be used to define the different styles in a friendly </br>
48   * and strict way.</br>
49   * </br>
50   * Usage example:</br>
51   * Style headerStyle = new Style();</br>
52   * headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);</br>
53   * headerStyle.setBorder(Border.PEN_2_POINT());</br>
54   * headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);</br>
55   * headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);</br>
56   */
57  public class Style implements Serializable, Cloneable {
58  
59  	private static final long serialVersionUID = Entity.SERIAL_VERSION_UID;
60  
61  	private String name;
62  	private String parentStyleName;
63  
64  	private Color backgroundColor = Color.WHITE;
65  	private Color textColor = Color.BLACK;
66  
67  	private Font font = Font.ARIAL_MEDIUM;
68  
69  	private Border border = Border.NO_BORDER();
70  
71  	private Border borderTop = null;
72  	private Border borderBottom = null;
73  	private Border borderLeft = null;
74  	private Border borderRight = null;
75  
76  	private Integer paddingBottom, paddingTop, paddingLeft, paddingRight;
77  
78  	private Integer padding = new Integer(2);
79  	private Integer radius = new Integer(0);
80  
81  	private Transparency transparency = Transparency.TRANSPARENT;
82  
83      private VerticalAlign verticalAlign = VerticalAlign.BOTTOM;
84      private HorizontalAlign horizontalAlign = HorizontalAlign.LEFT;
85      private Rotation rotation = Rotation.NONE;
86  
87      private Stretching streching = Stretching.RELATIVE_TO_TALLEST_OBJECT;
88  
89      private boolean stretchWithOverflow = true;
90      private boolean blankWhenNull = true;
91  
92      private String pattern;
93  
94      /**
95       * If true and another style exists in the design with the same name, this style overrides the existing one.
96       */
97      private boolean overridesExistingStyle = false;
98  
99      public boolean isOverridesExistingStyle() {
100 		return overridesExistingStyle;
101 	}
102 
103 	public void setOverridesExistingStyle(boolean overridesExistingStyle) {
104 		this.overridesExistingStyle = overridesExistingStyle;
105 	}
106 
107 	public Style(){}
108 
109     public Style(String name){
110     	this.name = name;
111     }
112     public Style(String name, String parentName){
113     	this.name = name;
114     	this.parentStyleName = parentName;
115     }
116 
117 	public boolean isBlankWhenNull() {
118 		return blankWhenNull;
119 	}
120 
121 	public void setBlankWhenNull(boolean blankWhenNull) {
122 		this.blankWhenNull = blankWhenNull;
123 	}
124 
125 	public Color getBackgroundColor() {
126 		return backgroundColor;
127 	}
128 
129 	public void setBackgroundColor(Color backgroundColor) {
130 		this.backgroundColor = backgroundColor;
131 	}
132 
133 	public Border getBorder() {
134 		return border;
135 	}
136 
137 	public void setBorder(Border border) {
138 		this.border = border;
139 	}
140 
141 	public Font getFont() {
142 		return font;
143 	}
144 
145 	public void setFont(Font font) {
146 		if (font != null)
147 			this.font = (Font) font.clone();
148 		else this.font = null;
149 	}
150 
151 	public HorizontalAlign getHorizontalAlign() {
152 		return horizontalAlign;
153 	}
154 
155 	public void setHorizontalAlign(HorizontalAlign horizontalAlign) {
156 		this.horizontalAlign = horizontalAlign;
157 	}
158 
159 	public Integer getPadding() {
160 		return padding;
161 	}
162 
163 	public void setPadding(Integer padding) {
164 		this.padding = padding;
165 	}
166 
167 	public Stretching getStreching() {
168 		return streching;
169 	}
170 
171 	public void setStreching(Stretching streching) {
172 		this.streching = streching;
173 	}
174 
175 	public boolean isStretchWithOverflow() {
176 		return stretchWithOverflow;
177 	}
178 
179 	public void setStretchWithOverflow(boolean stretchWithOverflow) {
180 		this.stretchWithOverflow = stretchWithOverflow;
181 	}
182 
183 	public Color getTextColor() {
184 		return textColor;
185 	}
186 
187 	public void setTextColor(Color textColor) {
188 		this.textColor = textColor;
189 	}
190 
191 	public Transparency getTransparency() {
192 		return transparency;
193 	}
194 
195 	public void setTransparency(Transparency transparency) {
196 		this.transparency = transparency;
197 	}
198 
199 	public void setTransparent(boolean transparent) {
200 		if (transparent)
201 			this.setTransparency(Transparency.TRANSPARENT);
202 		else
203 			this.setTransparency(Transparency.OPAQUE);
204 	}
205 
206 	public boolean isTransparent(){
207 		return this.transparency.equals(Transparency.TRANSPARENT);
208 	}
209 
210 	public VerticalAlign getVerticalAlign() {
211 		return verticalAlign;
212 	}
213 
214 	public void setVerticalAlign(VerticalAlign verticalAlign) {
215 		this.verticalAlign = verticalAlign;
216 	}
217 
218 	public JRDesignConditionalStyle transformAsConditinalStyle() {
219 		JRDesignConditionalStyle ret = new JRDesignConditionalStyle();
220 		setJRBaseStyleProperties(ret);
221 		return ret;
222 		
223 	}
224 
225 	public JRDesignStyle transform() {
226 		JRDesignStyle transformedStyle = new JRDesignStyle();
227 		transformedStyle.setName(this.name);
228 		transformedStyle.setParentStyleNameReference(this.parentStyleName);
229 		setJRBaseStyleProperties(transformedStyle);
230 		return transformedStyle;
231 	}
232 
233 	protected void setJRBaseStyleProperties(JRBaseStyle transformedStyle) {
234         JRBoxPen pen = transformedStyle.getLineBox().getPen();
235         if (getBorder()!=null){
236             LayoutUtils.convertBorderToPen(getBorder(),transformedStyle.getLineBox().getPen());
237         }
238 
239 		if (getBorderBottom()!= null)
240             LayoutUtils.convertBorderToPen(getBorderBottom(),transformedStyle.getLineBox().getBottomPen());
241 			//transformedStyle.setBottomBorder(getBorderBottom().getValue());
242 		if (getBorderTop()!= null)
243             LayoutUtils.convertBorderToPen(getBorderTop(),transformedStyle.getLineBox().getTopPen());
244 			//transformedStyle.setTopBorder(getBorderTop().getValue());
245 		if (getBorderLeft()!= null)
246             LayoutUtils.convertBorderToPen(getBorderLeft(),transformedStyle.getLineBox().getLeftPen());
247 			//transformedStyle.setLeftBorder(getBorderLeft().getValue());
248 		if (getBorderRight()!= null)
249             LayoutUtils.convertBorderToPen(getBorderRight(),transformedStyle.getLineBox().getRightPen());
250 			//transformedStyle.setRightBorder(getBorderRight().getValue());
251 
252 		//Padding
253 		transformedStyle.getLineBox().setPadding(getPadding());
254 
255 		if (paddingBottom != null)
256             transformedStyle.getLineBox().setBottomPadding(paddingBottom);
257 		if (paddingTop != null)
258             transformedStyle.getLineBox().setTopPadding(paddingTop);
259 		if (paddingLeft != null)
260             transformedStyle.getLineBox().setLeftPadding(paddingLeft);
261 		if (paddingRight != null)
262             transformedStyle.getLineBox().setRightPadding(paddingRight);
263 
264 		//Aligns
265 		if (getHorizontalAlign() != null)
266 			transformedStyle.setHorizontalAlignment(HorizontalAlignEnum.getByValue(getHorizontalAlign().getValue() ));
267 
268 		if (getVerticalAlign() != null)
269 			transformedStyle.setVerticalAlignment(VerticalAlignEnum.getByValue(getVerticalAlign().getValue()));
270 
271 		transformedStyle.setBlankWhenNull(blankWhenNull);
272 
273 		//Font
274 		if (font != null) {
275 			transformedStyle.setFontName(font.getFontName());
276 			transformedStyle.setFontSize(font.getFontSize());
277 			transformedStyle.setBold(font.isBold());
278 			transformedStyle.setItalic(font.isItalic());
279 			transformedStyle.setUnderline(font.isUnderline());
280 			transformedStyle.setPdfFontName(font.getPdfFontName());
281 			transformedStyle.setPdfEmbedded(font.isPdfFontEmbedded());
282 			transformedStyle.setPdfEncoding(font.getPdfFontEncoding());
283 		}
284 
285 		transformedStyle.setBackcolor(getBackgroundColor());
286 		transformedStyle.setForecolor(getTextColor());
287 
288 		if (getTransparency() != null)
289 			transformedStyle.setMode(ModeEnum.getByValue( getTransparency().getValue() ));
290 
291 		if (getRotation() != null)
292 			transformedStyle.setRotation(RotationEnum.getByValue( getRotation().getValue() ));
293 
294 		if (getRadius() != null)
295 			transformedStyle.setRadius(getRadius().intValue());
296 
297 		transformedStyle.setPattern(this.pattern);
298 
299 		/**
300 		 * This values are needed when exporting to JRXML
301 		 */
302         //TODO Check if this is still necessary
303         /*transformedStyle.setPen((byte)0);
304 		transformedStyle.setFill((byte)1);
305 		transformedStyle.setScaleImage(ImageScaleMode.NO_RESIZE.getValue());*/
306 	
307 	}
308 
309 	public Border getBorderBottom() {
310 		return borderBottom;
311 	}
312 
313 	public void setBorderBottom(Border borderBottom) {
314 		this.borderBottom = borderBottom;
315 	}
316 
317 	public Border getBorderLeft() {
318 		return borderLeft;
319 	}
320 
321 	public void setBorderLeft(Border borderLeft) {
322 		this.borderLeft = borderLeft;
323 	}
324 
325 	public Border getBorderRight() {
326 		return borderRight;
327 	}
328 
329 	public void setBorderRight(Border borderRight) {
330 		this.borderRight = borderRight;
331 	}
332 
333 	public Border getBorderTop() {
334 		return borderTop;
335 	}
336 
337 	public void setBorderTop(Border borderTop) {
338 		this.borderTop = borderTop;
339 	}
340 
341     /**
342      * use #Style.getBorder().getColor() instead
343      * @return
344      */
345     @Deprecated
346 	public Color getBorderColor() {
347         if (getBorder() == null)
348             return null;
349 		return getBorder().getColor();
350 	}
351 
352     /**
353      * Use #Style.setBorder(...) instead
354      * @param borderColor
355      */
356     @Deprecated
357 	public void setBorderColor(Color borderColor) {
358         if (getBorder() == null)
359             return;
360 
361         this.getBorder().setColor(borderColor);
362 	}
363 
364 	public Rotation getRotation() {
365 		return rotation;
366 	}
367 
368 	public void setRotation(Rotation rotation) {
369 		this.rotation = rotation;
370 	}
371 
372 	public Integer getRadius() {
373 		return radius;
374 	}
375 
376 	public void setRadius(Integer radius) {
377 		this.radius = radius;
378 	}
379 
380 	public Integer getPaddingBottom() {
381 		return paddingBottom;
382 	}
383 
384 	public void setPaddingBottom(Integer paddingBottom) {
385 		this.paddingBottom = paddingBottom;
386 	}
387 
388     public Integer getPaddingTop() {
389 		return paddingTop;
390 	}
391 
392 	public void setPaddingTop(Integer paddingTop) {
393 		this.paddingTop = paddingTop;
394 	}
395 
396 	public Integer getPaddingLeft() {
397 		return paddingLeft;
398 	}
399 
400 	public void setPaddingLeft(Integer paddingLeft) {
401 		this.paddingLeft = paddingLeft;
402 	}
403 
404 	public Integer getPaddingRight() {
405 		return paddingRight;
406 	}
407 
408 	public void setPaddingRight(Integer paddingRight) {
409 		this.paddingRight = paddingRight;
410 	}
411 
412 	public String getName() {
413 		return name;
414 	}
415 
416 	public void setName(String name) {
417 		this.name = name;
418 	}
419 
420 	public String getParentStyleName() {
421 		return parentStyleName;
422 	}
423 
424 	public void setParentStyleName(String parentStyleName) {
425 		this.parentStyleName = parentStyleName;
426 	}
427 
428 	/**
429 	 * Creates a blank style (no default values).
430 	 * Useful when we need a style with a parent style, not defined properties (null ones) will be inherited
431 	 * from parent style
432 	 *
433 	 * @param name  style name
434 	 * @return  Style
435 	 */
436 	public static Style createBlankStyle(String name){
437 		Style style = new Style(name);
438 
439 		style.setBackgroundColor(null);
440 		style.setBorderColor(null);
441 		style.setTransparency(null);
442 		style.setTextColor(null);
443 		style.setBorder(null);
444 		style.setFont(null);
445 		style.setPadding(null);
446 		style.setRadius(null);
447 		style.setVerticalAlign(null);
448 		style.setHorizontalAlign(null);
449 		style.setRotation(null);
450 		style.setStreching(null);
451 
452 		return style;
453 
454 	}
455 
456 	public static Style createBlankStyle(String name, String parent){
457 		Style s = createBlankStyle(name);
458 		s.setParentStyleName(parent);
459 		return s;
460 	}
461 
462 	public String getPattern() {
463 		return pattern;
464 	}
465 
466 	public void setPattern(String pattern) {
467 		this.pattern = pattern;
468 	}
469 
470 	public Object clone() throws CloneNotSupportedException {
471 		Style style = (Style) super.clone();
472 		style.setFont(this.font);
473 		return style;
474 	}
475 }