KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > drawing > EscherContainer


1 /*********************************************************************
2 *
3 * Copyright (C) 2003 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff.drawing;
21
22 import common.Logger;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  * An escher container. This record may contain other escher containers or
29  * atoms
30  */

31 class EscherContainer extends EscherRecord
32 {
33   /**
34    * The logger
35    */

36   private static Logger logger = Logger.getLogger(EscherContainer.class);
37
38   /**
39    * Initialized flag
40    */

41   private boolean initialized;
42
43
44   /**
45    * The children of this container
46    */

47   private ArrayList JavaDoc children;
48
49   /**
50    * Constructor
51    *
52    * @param erd the raw data
53    */

54   public EscherContainer(EscherRecordData erd)
55   {
56     super(erd);
57     initialized = false;
58     children = new ArrayList JavaDoc();
59   }
60
61   /**
62    * Constructor used when writing out escher data
63    *
64    * @param type
65    */

66   protected EscherContainer(EscherRecordType type)
67   {
68     super(type);
69     setContainer(true);
70     children = new ArrayList JavaDoc();
71   }
72
73   /**
74    * Accessor for the children of this container
75    *
76    * @return the childre
77    */

78   public EscherRecord[] getChildren()
79   {
80     if (!initialized)
81     {
82       initialize();
83     }
84
85     Object JavaDoc[] ca = children.toArray(new EscherRecord[children.size()]);
86
87     return (EscherRecord[]) ca;
88   }
89
90   /**
91    * Adds a child to this container
92    *
93    * @param child the item to add
94    */

95   public void add(EscherRecord child)
96   {
97     children.add(child);
98   }
99
100   /**
101    * Removes a child from this container
102    *
103    * @param child the item to remove
104    */

105   public void remove(EscherRecord child)
106   {
107     children.remove(child);
108   }
109
110   /**
111    * Initialization
112    */

113   private void initialize()
114   {
115     int curpos = getPos() + HEADER_LENGTH;
116     int endpos = Math.min(getPos() + getLength(), getStreamLength());
117
118     EscherRecord newRecord = null;
119
120     while (curpos < endpos)
121     {
122       EscherRecordData erd = new EscherRecordData(getEscherStream(), curpos);
123
124       EscherRecordType type = erd.getType();
125       if (type == EscherRecordType.DGG)
126       {
127         newRecord = new Dgg(erd);
128       }
129       else if (type == EscherRecordType.DG)
130       {
131         newRecord = new Dg(erd);
132       }
133       else if (type == EscherRecordType.BSTORE_CONTAINER)
134       {
135         newRecord = new BStoreContainer(erd);
136       }
137       else if (type == EscherRecordType.SPGR_CONTAINER)
138       {
139         newRecord = new SpgrContainer(erd);
140       }
141       else if (type == EscherRecordType.SP_CONTAINER)
142       {
143         newRecord = new SpContainer(erd);
144       }
145       else if (type == EscherRecordType.SPGR)
146       {
147         newRecord = new Spgr(erd);
148       }
149       else if (type == EscherRecordType.SP)
150       {
151         newRecord = new Sp(erd);
152       }
153       else if (type == EscherRecordType.CLIENT_ANCHOR)
154       {
155         newRecord = new ClientAnchor(erd);
156       }
157       else if (type == EscherRecordType.CLIENT_DATA)
158       {
159         newRecord = new ClientData(erd);
160       }
161       else if (type == EscherRecordType.BSE)
162       {
163         newRecord = new BlipStoreEntry(erd);
164       }
165       else if (type == EscherRecordType.OPT)
166       {
167         newRecord = new Opt(erd);
168       }
169       else if (type == EscherRecordType.SPLIT_MENU_COLORS)
170       {
171         newRecord = new SplitMenuColors(erd);
172       }
173       else if (type == EscherRecordType.CLIENT_TEXT_BOX)
174       {
175         newRecord = new ClientTextBox(erd);
176       }
177       else
178       {
179         newRecord = new EscherAtom(erd);
180       }
181
182       children.add(newRecord);
183       curpos += newRecord.getLength();
184     }
185
186     initialized = true;
187   }
188
189   /**
190    * Gets the data for this container (and all of its children recursively
191    */

192   byte[] getData()
193   {
194     byte[] data = new byte[0];
195     for (Iterator JavaDoc i = children.iterator() ; i.hasNext() ; )
196     {
197       EscherRecord er = (EscherRecord) i.next();
198       byte[] childData = er.getData();
199
200       if (childData != null)
201       {
202         byte[] newData = new byte[data.length + childData.length];
203         System.arraycopy(data, 0, newData, 0, data.length);
204         System.arraycopy(childData, 0, newData, data.length, childData.length);
205         data = newData;
206       }
207     }
208
209     return setHeaderData(data);
210   }
211 }
212
Popular Tags