KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import common.Logger;
26
27 import jxl.biff.IntegerHelper;
28 import jxl.biff.StringHelper;
29
30 /**
31  * An options record in the escher stream
32  */

33 class Opt extends EscherAtom
34 {
35   /**
36    * The logger
37    */

38   private static Logger logger = Logger.getLogger(Opt.class);
39
40   private byte[] data;
41   private int numProperties;
42
43   /**
44    * The list of properties
45    */

46   private ArrayList JavaDoc properties;
47
48   final static class Property
49   {
50     int id;
51     boolean blipId;
52     boolean complex;
53     int value;
54     String JavaDoc stringValue;
55     
56     public Property(int i, boolean bl, boolean co, int v)
57     {
58       id = i;
59       blipId = bl;
60       complex = co;
61       value = v;
62       // logger.debug("property id: " + id +
63
// " blipId " + blipId + " complex " + co + " value " + v);
64
}
65
66     public Property(int i, boolean bl, boolean co, int v, String JavaDoc s)
67     {
68       id = i;
69       blipId = bl;
70       complex = co;
71       value = v;
72       stringValue = s;
73     }
74   }
75
76   public Opt(EscherRecordData erd)
77   {
78     super(erd);
79     numProperties = getInstance();
80     readProperties();
81   }
82
83   private void readProperties()
84   {
85     properties = new ArrayList JavaDoc();
86     int pos = 0;
87     byte[] bytes = getBytes();
88     
89     for (int i = 0 ; i < numProperties ; i++)
90     {
91       int val = IntegerHelper.getInt(bytes[pos], bytes[pos+1]);
92       int id = val & 0x3fff;
93       int value = IntegerHelper.getInt(bytes[pos+2], bytes[pos+3],
94                                        bytes[pos+4], bytes[pos+5]);
95       Property p = new Property(id,
96                                 (val & 0x4000) != 0,
97                                 (val & 0x8000) != 0,
98                                 value);
99       pos += 6;
100       properties.add(p);
101     }
102
103     for (Iterator JavaDoc i = properties.iterator(); i.hasNext() ; )
104     {
105       Property p = (Property) i.next();
106       if (p.complex)
107       {
108         p.stringValue = StringHelper.getUnicodeString(bytes, p.value/2,
109                                                       pos);
110         pos += p.value;
111       }
112     }
113   }
114
115   public Opt()
116   {
117     super(EscherRecordType.OPT);
118     properties = new ArrayList JavaDoc();
119     setVersion(3);
120   }
121
122   byte[] getData()
123   {
124     numProperties = properties.size();
125     setInstance(numProperties);
126
127     data = new byte[numProperties * 6];
128     int pos = 0;
129
130     // Add in the root data
131
for (Iterator JavaDoc i = properties.iterator() ; i.hasNext() ; )
132     {
133       Property p = (Property) i.next();
134       int val = p.id & 0x3fff;
135       
136       if (p.blipId)
137       {
138         val |= 0x4000;
139       }
140       
141       if (p.complex)
142       {
143         val |= 0x8000;
144       }
145
146       IntegerHelper.getTwoBytes(val, data, pos);
147       IntegerHelper.getFourBytes(p.value, data, pos+2);
148       pos += 6 ;
149     }
150
151     // Add in any complex data
152
for (Iterator JavaDoc i = properties.iterator() ; i.hasNext() ; )
153     {
154       Property p = (Property) i.next();
155
156       if (p.complex && p.stringValue != null)
157       {
158         byte[] newData =
159           new byte[data.length + p.stringValue.length() * 2];
160         System.arraycopy(data, 0, newData, 0, data.length);
161         StringHelper.getUnicodeBytes(p.stringValue, newData, data.length);
162         data = newData;
163       }
164     }
165
166     return setHeaderData(data);
167   }
168
169   void addProperty(int id, boolean blip, boolean complex, int val)
170   {
171     Property p = new Property(id, blip, complex, val);
172     properties.add(p);
173   }
174
175   void addProperty(int id, boolean blip, boolean complex, int val, String JavaDoc s)
176   {
177     Property p = new Property(id, blip, complex, val, s);
178     properties.add(p);
179   }
180
181   Property getProperty(int id)
182   {
183     boolean found = false;
184     Property p = null;
185     for (Iterator JavaDoc i = properties.iterator() ; i.hasNext() && !found ; )
186     {
187       p = (Property) i.next();
188       if (p.id == id)
189       {
190         found = true;
191       }
192     }
193     return found ? p : null;
194   }
195
196 }
197
Popular Tags