KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > PrintDataElement


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.math.*;
17 import java.sql.*;
18
19 import org.compiere.model.MLocation;
20 import org.compiere.util.*;
21
22 /**
23  * Print Data Element
24  *
25  * @author Jorg Janke
26  * @version $Id: PrintDataElement.java,v 1.11 2003/10/10 01:03:10 jjanke Exp $
27  */

28 public class PrintDataElement
29 {
30     /**
31      * Print Data Element Constructor
32      * @param columnName name
33      * @param value display value
34      * @param displayType optional displayType
35      * @param isPKey is primary key
36      * @param isPageBreak if true force page break
37      */

38     public PrintDataElement (String JavaDoc columnName, Object JavaDoc value, int displayType, boolean isPKey, boolean isPageBreak)
39     {
40         if (columnName == null)
41             throw new IllegalArgumentException JavaDoc("PrintDataElement - Name cannot be null");
42         m_columnName = columnName;
43         m_value = value;
44         m_displayType = displayType;
45         m_isPKey = isPKey;
46         m_isPageBreak = isPageBreak;
47     } // PrintDataElement
48

49     /**
50      * Print Data Element Constructor
51      * @param columnName name
52      * @param value display value
53      * @param displayType optional displayType
54      */

55     public PrintDataElement(String JavaDoc columnName, Object JavaDoc value, int displayType)
56     {
57         this (columnName, value, displayType, false, false);
58     } // PrintDataElement
59

60     /** Data Name */
61     private String JavaDoc m_columnName;
62     /** Data Value */
63     private Object JavaDoc m_value;
64     /** Display Type */
65     private int m_displayType;
66     /** Is Primary Key */
67     private boolean m_isPKey;
68     /** Is Page Break */
69     private boolean m_isPageBreak;
70
71
72     /** XML Element Name */
73     public static final String JavaDoc XML_TAG = "element";
74     /** XML Attribute Name */
75     public static final String JavaDoc XML_ATTRIBUTE_NAME = "name";
76     /** XML Attribute Key */
77     public static final String JavaDoc XML_ATTRIBUTE_KEY = "key";
78
79
80     /**
81      * Get Name
82      * @return name
83      */

84     public String JavaDoc getColumnName()
85     {
86         return m_columnName;
87     } // getName
88

89     /**
90      * Get Node Value
91      * @return value
92      */

93     public Object JavaDoc getValue()
94     {
95         return m_value;
96     } // getValue
97

98     /**
99      * Get Function Value
100      * @return length or numeric value
101      */

102     public BigDecimal getFunctionValue()
103     {
104         if (m_value == null)
105             return Env.ZERO;
106
107         // Numbers - return number value
108
if (m_value instanceof BigDecimal)
109             return (BigDecimal)m_value;
110         if (m_value instanceof Number JavaDoc)
111             return new BigDecimal(((Number JavaDoc)m_value).doubleValue());
112
113         // Boolean - return 1 for true 0 for false
114
if (m_value instanceof Boolean JavaDoc)
115         {
116             if (((Boolean JavaDoc)m_value).booleanValue())
117                 return Env.ONE;
118             else
119                 return Env.ZERO;
120         }
121
122         // Return Length
123
String JavaDoc s = m_value.toString();
124         return new BigDecimal(s.length());
125     } // getFunctionValue
126

127     /**
128      * Get Node Value Display
129      * @param language optional language - if null nubers/dates are not formatted
130      * @return display value optionally formatted
131      */

132     public String JavaDoc getValueDisplay (Language language)
133     {
134         if (m_value == null)
135             return "";
136         String JavaDoc retValue = m_value.toString();
137         if (m_displayType == DisplayType.Location)
138             return getValueDisplay_Location();
139         else if (m_columnName.equals("C_BPartner_Location_ID") || m_columnName.equals("BillTo_ID"))
140             return getValueDisplay_BPLocation();
141         else if (m_displayType == 0 || m_value instanceof String JavaDoc || m_value instanceof NamePair)
142             ;
143         else if (language != null) // Optional formatting of Numbers and Dates
144
{
145             if (DisplayType.isNumeric(m_displayType))
146                 retValue = DisplayType.getNumberFormat(m_displayType, language).format(m_value);
147             else if (DisplayType.isDate(m_displayType))
148                 retValue = DisplayType.getDateFormat(m_displayType, language).format(m_value);
149         }
150         return retValue;
151     } // getValueDisplay
152

153     /**
154      * Return Address String not just name
155      * @return Address String
156      */

157     private String JavaDoc getValueDisplay_BPLocation ()
158     {
159         try
160         {
161             int C_BPartner_Location_ID = Integer.parseInt (getValueKey ());
162             if (C_BPartner_Location_ID == 0)
163                 return m_value.toString();
164             //
165
MLocation loc = new MLocation (Env.getCtx(), 0);
166             loc.loadBPLocation (C_BPartner_Location_ID);
167             return loc.toStringCR();
168         }
169         catch (Exception JavaDoc ex)
170         {
171             return m_value.toString();
172         }
173     } // getValueDisplay_BPLocation
174

175
176     /**
177      * Return Address String not just City
178      * @return Address String
179      */

180     private String JavaDoc getValueDisplay_Location ()
181     {
182         try
183         {
184             int C_Location_ID = Integer.parseInt (getValueKey ());
185             if (C_Location_ID == 0)
186                 return m_value.toString();
187             //
188
MLocation loc = new MLocation (Env.getCtx(), 0);
189             loc.load(C_Location_ID);
190             return loc.toStringCR();
191         }
192         catch (Exception JavaDoc ex)
193         {
194             return m_value.toString();
195         }
196     } // getValueDisplay_Location
197

198
199     /**
200      * Get Node Value Key
201      * @return key
202      */

203     public String JavaDoc getValueKey()
204     {
205         if (m_value == null)
206             return "";
207         if (m_value instanceof NamePair)
208             return ((NamePair)m_value).getID();
209         return "";
210     } // getValueKey
211

212     /**
213      * Is Value Null
214      * @return true if value is null
215      */

216     public boolean isNull()
217     {
218         return m_value == null;
219     } // isNull
220

221     /*************************************************************************/
222
223     /**
224      * Get Display Type
225      * @return Display Type
226      */

227     public int getDisplayType()
228     {
229         return m_displayType;
230     } // getDisplayType
231

232     /**
233      * Is Value numeric
234      * @return true if value is a numeric
235      */

236     public boolean isNumeric()
237     {
238         if (m_displayType == 0)
239             return m_value instanceof BigDecimal;
240         return DisplayType.isNumeric(m_displayType);
241     } // isNumeric
242

243     /**
244      * Is Value a date
245      * @return true if value is a date
246      */

247     public boolean isDate()
248     {
249         if (m_displayType == 0)
250             return m_value instanceof Timestamp;
251         return DisplayType.isDate(m_displayType);
252     } // isDate
253

254     /**
255      * Is Value an ID
256      * @return true if value is an ID
257      */

258     public boolean isID()
259     {
260         return DisplayType.isID(m_displayType);
261     } // isID
262

263     /**
264      * Is Value boolean
265      * @return true if value is a boolean
266      */

267     public boolean isYesNo()
268     {
269         if (m_displayType == 0)
270             return m_value instanceof Boolean JavaDoc;
271         return DisplayType.YesNo == m_displayType;
272     } // isYesNo
273

274     /**
275      * Is Value the primary key of row
276      * @return true if value is the PK
277      */

278     public boolean isPKey()
279     {
280         return m_isPKey;
281     } // isPKey
282

283     /**
284      * Column value forces page break
285      * @return true if page break
286      */

287     public boolean isPageBreak()
288     {
289         return m_isPageBreak;
290     } // isPageBreak
291

292     /*************************************************************************/
293
294     /**
295      * HashCode
296      * @return hash code
297      */

298     public int hashCode()
299     {
300         if (m_value == null)
301             return m_columnName.hashCode();
302         return m_columnName.hashCode() + m_value.hashCode();
303     } // hashCode
304

305     /**
306      * Equals
307      * @param compare compare object
308      * @return true if equals
309      */

310     public boolean equals (Object JavaDoc compare)
311     {
312         if (compare instanceof PrintDataElement)
313         {
314             PrintDataElement pde = (PrintDataElement)compare;
315             if (pde.getColumnName().equals(m_columnName))
316             {
317                 if (pde.getValue() != null && pde.getValue().equals(m_value))
318                     return true;
319                 if (pde.getValue() == null && m_value == null)
320                     return true;
321             }
322         }
323         return false;
324     } // equals
325

326     /**
327      * String representation
328      * @return info
329      */

330     public String JavaDoc toString()
331     {
332         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(m_columnName).append("=").append(m_value);
333         if (m_isPKey)
334             sb.append("(PK)");
335         return sb.toString();
336     } // toString
337

338     /**
339      * Value Has Key
340      * @return true if value has a key
341      */

342     public boolean hasKey()
343     {
344         return m_value instanceof NamePair;
345     } // hasKey
346

347     /**
348      * String representation
349      * @return info
350      */

351     public String JavaDoc dump()
352     {
353         if (m_value instanceof NamePair)
354         {
355             NamePair pp = (NamePair)m_value;
356             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(m_columnName);
357             sb.append("(").append(pp.getID()).append(")")
358                 .append("=").append(pp.getName());
359             if (m_isPKey)
360                 sb.append("(PK)");
361             return sb.toString();
362         }
363         else
364             return toString();
365     } // toString
366

367 } // PrintDataElement
368
Popular Tags