1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.base;
25
26 import java.awt.Color;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.io.Serializable;
30
31 import net.sf.jasperreports.engine.Deduplicable;
32 import net.sf.jasperreports.engine.JRConstants;
33 import net.sf.jasperreports.engine.JRPen;
34 import net.sf.jasperreports.engine.JRPenContainer;
35 import net.sf.jasperreports.engine.JRRuntimeException;
36 import net.sf.jasperreports.engine.JRStyleContainer;
37 import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
38 import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;
39 import net.sf.jasperreports.engine.type.LineStyleEnum;
40 import net.sf.jasperreports.engine.util.ObjectUtils;
41 import net.sf.jasperreports.engine.util.StyleResolver;
42
43
44 /**
45  * This is useful for drawing borders around text elements and images. Boxes can have borders and paddings, which can
46  * have different width and colour on each side of the element.
47  * @author Teodor Danciu (teodord@users.sourceforge.net)
48  */

49 public class JRBasePen implements JRPen, Serializable, Cloneable, JRChangeEventsSupport, Deduplicable
50 {
51
52
53     /**
54      *
55      */

56     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
57     
58     public static final String PROPERTY_LINE_WIDTH = "lineWidth";
59     
60     public static final String PROPERTY_LINE_STYLE = "lineStyle";
61     
62     public static final String PROPERTY_LINE_COLOR = "lineColor";
63     
64
65     protected JRPenContainer penContainer;
66
67     /**
68      *
69      */

70     protected Float lineWidth;
71     protected LineStyleEnum lineStyleValue;
72     protected Color lineColor;
73
74     
75     /**
76      *
77      */

78     public JRBasePen(JRPenContainer penContainer)
79     {
80         super();
81         
82         this.penContainer = penContainer;
83     }
84     
85     
86     /**
87      * @deprecated Replaced by {@link #getPenContainer()}.
88      */

89     @Override
90     public JRStyleContainer getStyleContainer()
91     {
92         return penContainer;
93     }
94
95     @Override
96     public JRPenContainer getPenContainer()
97     {
98         return penContainer;
99     }
100
101     /**
102      *
103      */

104     protected StyleResolver getStyleResolver() 
105     {
106         if (getStyleContainer().getDefaultStyleProvider() != null)
107         {
108             return getStyleContainer().getDefaultStyleProvider().getStyleResolver();
109         }
110         return StyleResolver.getInstance();
111     }
112     
113     @Override
114     public Float getLineWidth()
115     {
116         return getStyleResolver().getLineWidth(this, penContainer.getDefaultLineWidth());
117     }
118
119     @Override
120     public Float getOwnLineWidth()
121     {
122         return lineWidth;
123     }
124
125     /**
126      * @deprecated Replaced by {@link #setLineWidth(Float)}.
127      */

128     @Override
129     public void setLineWidth(float lineWidth)
130     {
131         setLineWidth((Float)lineWidth);
132     }
133
134     @Override
135     public void setLineWidth(Float lineWidth)
136     {
137         Object old = this.lineWidth;
138         this.lineWidth = lineWidth;
139         getEventSupport().firePropertyChange(PROPERTY_LINE_WIDTH, old, this.lineWidth);
140     }
141
142     @Override
143     public LineStyleEnum getLineStyleValue()
144     {
145         return getStyleResolver().getLineStyleValue(this);
146     }
147
148     @Override
149     public LineStyleEnum getOwnLineStyleValue()
150     {
151         return lineStyleValue;
152     }
153
154     @Override
155     public void setLineStyle(LineStyleEnum lineStyleValue)
156     {
157         Object old = this.lineStyleValue;
158         this.lineStyleValue = lineStyleValue;
159         getEventSupport().firePropertyChange(PROPERTY_LINE_STYLE, old, this.lineStyleValue);
160     }
161
162     @Override
163     public Color getLineColor()
164     {
165         return getStyleResolver().getLineColor(this, penContainer.getDefaultLineColor());
166     }
167
168     @Override
169     public Color getOwnLineColor()
170     {
171         return lineColor;
172     }
173
174     @Override
175     public void setLineColor(Color lineColor)
176     {
177         Object old = this.lineColor;
178         this.lineColor = lineColor;
179         getEventSupport().firePropertyChange(PROPERTY_LINE_COLOR, old, this.lineColor);
180     }
181
182
183     public String getStyleNameReference()
184     {
185         return null;
186     }
187
188     @Override
189     public JRPen clone(JRPenContainer penContainer)
190     {
191         JRBasePen clone = null;
192         try
193         {
194             clone = (JRBasePen)super.clone();
195         }
196         catch(CloneNotSupportedException e)
197         {
198             throw new JRRuntimeException(e);
199         }
200         
201         clone.penContainer = penContainer;
202         clone.eventSupport = null;
203         
204         return clone;
205     }
206     
207     private transient JRPropertyChangeSupport eventSupport;
208     
209     @Override
210     public JRPropertyChangeSupport getEventSupport()
211     {
212         synchronized (this)
213         {
214             if (eventSupport == null)
215             {
216                 eventSupport = new JRPropertyChangeSupport(this);
217             }
218         }
219         
220         return eventSupport;
221     }
222
223     
224     /*
225      * These fields are only for serialization backward compatibility.
226      */

227     private int PSEUDO_SERIAL_VERSION_UID = JRConstants.PSEUDO_SERIAL_VERSION_UID; //NOPMD
228     /**
229      * @deprecated
230      */

231     private Byte lineStyle;
232     
233     @SuppressWarnings("deprecation")
234     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
235     {
236         in.defaultReadObject();
237         
238         if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_3_7_2)
239         {
240             lineStyleValue = LineStyleEnum.getByValue(lineStyle);
241             
242             lineStyle = null;
243         }
244     }
245
246
247     @Override
248     public int getHashCode()
249     {
250         ObjectUtils.HashCode hash = ObjectUtils.hash();
251         hash.add(lineWidth);
252         hash.add(lineStyleValue);
253         hash.add(lineColor);
254         return hash.getHashCode();
255     }
256
257
258     @Override
259     public boolean isIdentical(Object object)
260     {
261         if (this == object)
262         {
263             return true;
264         }
265         
266         if (!(object instanceof JRBasePen))
267         {
268             return false;
269         }
270         
271         JRBasePen pen = (JRBasePen) object;
272
273         return 
274                 ObjectUtils.equals(lineWidth, pen.lineWidth)
275                 && ObjectUtils.equals(lineStyleValue, pen.lineStyleValue)
276                 && ObjectUtils.equals(lineColor, pen.lineColor);
277     }
278
279 }
280