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.io.Serializable;
27
28 import net.sf.jasperreports.engine.JRConstants;
29 import net.sf.jasperreports.engine.JRField;
30 import net.sf.jasperreports.engine.JRPropertiesHolder;
31 import net.sf.jasperreports.engine.JRPropertiesMap;
32 import net.sf.jasperreports.engine.JRPropertyExpression;
33 import net.sf.jasperreports.engine.JRRuntimeException;
34 import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
35 import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;
36 import net.sf.jasperreports.engine.util.JRClassLoader;
37 import net.sf.jasperreports.engine.util.JRCloneUtils;
38
39
40 /**
41  * @author Teodor Danciu (teodord@users.sourceforge.net)
42  */

43 public class JRBaseField implements JRField, Serializable, JRChangeEventsSupport
44 {
45
46
47     /**
48      *
49      */

50     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
51     
52     public static final String PROPERTY_DESCRIPTION = "description";
53
54     /**
55      *
56      */

57     protected String name;
58     protected String description;
59     protected String valueClassName = java.lang.String.class.getName();
60     protected String valueClassRealName;
61
62     protected transient Class<?> valueClass;
63     
64     protected JRPropertiesMap propertiesMap;
65
66     private JRPropertyExpression[] propertyExpressions;
67
68
69     /**
70      *
71      */

72     protected JRBaseField()
73     {
74         this.propertiesMap = new JRPropertiesMap();
75     }
76     
77     
78     /**
79      *
80      */

81     protected JRBaseField(JRField field, JRBaseObjectFactory factory)
82     {
83         factory.put(field, this);
84         
85         name = field.getName();
86         description = field.getDescription();
87         valueClassName = field.getValueClassName();
88         
89         propertiesMap = field.getPropertiesMap().cloneProperties();
90         propertyExpressions = factory.getPropertyExpressions(field.getPropertyExpressions());
91     }
92
93
94     @Override
95     public String getName()
96     {
97         return this.name;
98     }
99         
100     @Override
101     public String getDescription()
102     {
103         return this.description;
104     }
105         
106     @Override
107     public void setDescription(String description)
108     {
109         Object old = this.description;
110         this.description = description;
111         getEventSupport().firePropertyChange(PROPERTY_DESCRIPTION, old, this.description);
112     }
113     
114     @Override
115     public Class<?> getValueClass()
116     {
117         if (valueClass == null)
118         {
119             String className = getValueClassRealName();
120             if (className != null)
121             {
122                 try
123                 {
124                     valueClass = JRClassLoader.loadClassForName(className);
125                 }
126                 catch(ClassNotFoundException e)
127                 {
128                     throw new JRRuntimeException(e);
129                 }
130             }
131         }
132         
133         return valueClass;
134     }
135     
136     @Override
137     public String getValueClassName()
138     {
139         return this.valueClassName;
140     }
141
142     /**
143      *
144      */

145     private String getValueClassRealName()
146     {
147         if (valueClassRealName == null)
148         {
149             valueClassRealName = JRClassLoader.getClassRealName(valueClassName);
150         }
151         
152         return valueClassRealName;
153     }
154
155     
156     @Override
157     public boolean hasProperties()
158     {
159         return propertiesMap != null && propertiesMap.hasProperties();
160     }
161
162
163     @Override
164     public JRPropertiesMap getPropertiesMap()
165     {
166         return propertiesMap;
167     }
168
169     
170     @Override
171     public JRPropertiesHolder getParentProperties()
172     {
173         return null;
174     }
175
176     
177     @Override
178     public JRPropertyExpression[] getPropertyExpressions()
179     {
180         return propertyExpressions;
181     }
182
183
184     @Override
185     public Object clone() 
186     {
187         JRBaseField clone = null;
188         
189         try
190         {
191             clone = (JRBaseField)super.clone();
192         }
193         catch (CloneNotSupportedException e)
194         {
195             throw new JRRuntimeException(e);
196         }
197
198         if (propertiesMap != null// attempt to use properties cloning utility results in clones having this field nullif it was non-null but empty
199         {
200             clone.propertiesMap = (JRPropertiesMap)propertiesMap.clone();
201         }
202         clone.propertyExpressions = JRCloneUtils.cloneArray(propertyExpressions);
203         clone.eventSupport = null;
204         
205         return clone;
206     }
207
208     
209     private transient JRPropertyChangeSupport eventSupport;
210     
211     @Override
212     public JRPropertyChangeSupport getEventSupport()
213     {
214         synchronized (this)
215         {
216             if (eventSupport == null)
217             {
218                 eventSupport = new JRPropertyChangeSupport(this);
219             }
220         }
221         
222         return eventSupport;
223     }
224
225 }
226