KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > JRPrintElementIndex


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine;
29
30 import java.util.Arrays JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35
36 /**
37  * @author Teodor Danciu (teodord@users.sourceforge.net)
38  * @version $Id: JRPrintElementIndex.java 1364 2006-08-31 18:13:20 +0300 (Thu, 31 Aug 2006) lucianc $
39  */

40 public class JRPrintElementIndex
41 {
42
43
44     /**
45      *
46      */

47     private int reportIndex = 0;
48     private int pageIndex = 0;
49     private List JavaDoc elementIndexes = null;
50
51
52     /**
53      *
54      */

55     public JRPrintElementIndex(
56         int reportIndex,
57         int pageIndex,
58         Integer JavaDoc[] elementIndexes
59         )
60     {
61         this.reportIndex = reportIndex;
62         this.pageIndex = pageIndex;
63         this.elementIndexes = Arrays.asList(elementIndexes);
64     }
65
66
67     /**
68      *
69      */

70     public int getReportIndex()
71     {
72         return this.reportIndex;
73     }
74         
75
76     /**
77      *
78      */

79     public int getPageIndex()
80     {
81         return this.pageIndex;
82     }
83         
84
85     /**
86      *
87      */

88     public Integer JavaDoc[] getElementIndexes()
89     {
90         return (Integer JavaDoc[]) elementIndexes.toArray(new Integer JavaDoc[elementIndexes.size()]);
91     }
92
93
94     /**
95      * Returns a String representation of this element index.
96      * <p>
97      * The representation is obtained by appending all the indexes that compose this instance.
98      * The result is compatible with {@link #parsePrintElementIndex(String) parsePrintElementIndex(String)},
99      * which can be used to recreate the elemetn index instance from a String representation.
100      * </p>
101      */

102     public String JavaDoc toString()
103     {
104         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
105         str.append(reportIndex);
106         str.append('_');
107         str.append(pageIndex);
108         for (Iterator JavaDoc it = elementIndexes.iterator(); it.hasNext();)
109         {
110             Integer JavaDoc idx = (Integer JavaDoc) it.next();
111             str.append('_');
112             str.append(idx);
113         }
114         return str.toString();
115     }
116     
117     
118     /**
119      * Parses a String representation as obtained by {@link #toString() toString()} back
120      * into an element index instance.
121      *
122      * @param indexStr the String representation of an element index
123      * @return an element index instance corresponding to the String representation
124      */

125     public static JRPrintElementIndex parsePrintElementIndex(String JavaDoc indexStr)
126     {
127         StringTokenizer JavaDoc tkzer = new StringTokenizer JavaDoc(indexStr, "_");
128         
129         int reportIndex = Integer.parseInt(tkzer.nextToken());
130         int pageIndex = Integer.parseInt(tkzer.nextToken());
131
132         Integer JavaDoc[] elementIndexes = new Integer JavaDoc[tkzer.countTokens()];
133         int c = 0;
134         while (tkzer.hasMoreTokens())
135         {
136             elementIndexes[c++] = Integer.valueOf(tkzer.nextToken());
137         }
138
139         return
140             new JRPrintElementIndex(
141                 reportIndex,
142                 pageIndex,
143                 elementIndexes
144                 );
145     }
146 }
147
Popular Tags