1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records;
25 
26 import java.io.OutputStream;
27 import java.io.InputStream;
28 import java.io.IOException;
29 import java.awt.Point;
30 
31 import org.openoffice.xmerge.util.Debug;
32 import org.openoffice.xmerge.util.EndianConverter;
33 import org.openoffice.xmerge.converter.xml.sxc.pexcel.PocketExcelConstants;
34 import org.openoffice.xmerge.converter.xml.sxc.SheetSettings;
35 
36 /**
37  * Represents a BIFF Record that describes the number and position of unfrozen
38  * panes.
39  */
40 public class Pane implements BIFFRecord {
41 
42 	private byte[] x		= new byte[2];
43     private byte[] y		= new byte[2];
44     private byte[] rwTop	= new byte[2];
45     private byte[] colLeft	= new byte[2];
46     private byte   pnnAcct;
47 
48    	/**
49 	 * Default Constructor
50 	 */
Pane()51 	public Pane() {
52     	pnnAcct = (byte) 0x02;	// Default setting
53 	}
54 
55 	/**
56  	 * Constructs a Pane Record from the <code>InputStream</code>
57  	 *
58  	 * @param	is InputStream containing a Pane record
59  	 */
Pane(InputStream is)60     public Pane(InputStream is) throws IOException {
61 		read(is);
62     }
63 
64     /**
65 	 * Get the hex code for this particular <code>BIFFRecord</code>
66 	 *
67 	 * @return the hex code for <code>Pane</code>
68 	 */
getBiffType()69     public short getBiffType() {
70         return PocketExcelConstants.PANE_INFO;
71     }
72 
73     /**
74 	 * Gets the split point for this pane, in the case of splits this will be
75 	 * in twips.
76 	 *
77 	 * @return the split point
78 	 */
getSplitPoint()79     public Point getSplitPoint() {
80 
81 		int xTwips = EndianConverter.readShort(x)/11;
82 		int yTwips = EndianConverter.readShort(y)/15;
83         return (new Point(xTwips, yTwips));
84     }
85 
86     /**
87 	 * Gets the freeze point for this pane, in the case of freezes this will
88 	 * be a zero-based index to either the column or row.
89 	 *
90 	 * @return the freeze point
91 	 */
getFreezePoint()92     public Point getFreezePoint() {
93 
94         return (new Point(EndianConverter.readShort(x),
95 		EndianConverter.readShort(y)));
96     }
97 
98     /**
99 	 * Sets the split point for this pane, coordinates are in column row units
100 	 * if the split type is freeze or twips if split type is split.
101 	 *
102 	 * @param splitType contains the X and Y split types (freeze or split)
103 	 * @param p the split point
104 	 */
setSplitPoint(Point splitType, Point p)105     public void setSplitPoint(Point splitType, Point p) {
106 
107 		if(splitType.getX()==SheetSettings.SPLIT
108 			|| splitType.getY()==SheetSettings.SPLIT) {
109 			int yTwips = (int) p.getY();
110 			short yPxl = (short) (yTwips * 15);
111 			y = EndianConverter.writeShort(yPxl);
112 			int xTwips = (int) p.getX();
113 			short xPxl = (short) (xTwips * 11);
114 			x = EndianConverter.writeShort(xPxl);
115 		} else {
116 			y = EndianConverter.writeShort((short) p.getY());
117 			x = EndianConverter.writeShort((short) p.getX());
118 		}
119 
120     }
121 
122     /**
123 	 * Get the hex code for this particular <code>BIFFRecord</code>
124 	 *
125 	 * @return the hex code for <code>Pane</code>
126 	 */
setPaneNumber(int paneNumber)127     public void setPaneNumber(int paneNumber) {
128         pnnAcct = (byte) paneNumber;
129     }
130 
131     /**
132 	 * Get the pane number of the active pane
133 	 * 0 - bottom right, 1 - top right
134 	 * 2 - bottom left, 3 - top left
135 	 *
136 	 * @return the hex code for <code>Pane</code>
137 	 */
getPaneNumber()138     public int getPaneNumber() {
139         return pnnAcct;
140     }
141 
142     /**
143 	 * Set the top row visible in the lower pane
144 	 *
145 	 * @param top 0-based inex of the top row
146 	 */
setTop(int top)147     public void setTop(int top) {
148         rwTop = EndianConverter.writeShort((short)top);
149     }
150 
151     /**
152 	 * Set leftmost column visible in the right pane
153 	 *
154 	 * @param left 0-based index of the leftmost column
155 	 */
setLeft(int left)156     public void setLeft(int left) {
157         colLeft = EndianConverter.writeShort((short)left);
158     }
159 
160     /**
161 	 * Get the top row visible in the lower pane
162 	 *
163 	 * @return the hex code for <code>Pane</code>
164 	 */
getTop()165     public int getTop() {
166         return EndianConverter.readShort(rwTop);
167     }
168 
169     /**
170 	 * Get leftmost column visible in the right pane
171 	 *
172 	 * @return 0-based index of the column
173 	 */
getLeft()174     public int getLeft() {
175         return EndianConverter.readShort(colLeft);
176     }
177 
178 
179 	/**
180 	 * Reads a <code>Pane</code> record from the <code>InputStream</code>
181 	 *
182 	 * @param input <code>InputStream</code> to read from
183 	 * @return the total number of bytes read
184 	 */
read(InputStream input)185     public int read(InputStream input) throws IOException {
186 
187         int numOfBytesRead	= input.read(x);
188         numOfBytesRead		+= input.read(y);
189         numOfBytesRead		+= input.read(rwTop);
190         numOfBytesRead		+= input.read(colLeft);
191 		pnnAcct				= (byte) input.read();
192         numOfBytesRead++;
193 
194         Debug.log(Debug.TRACE, "\tx : "+ EndianConverter.readShort(x) +
195                             " y : " + EndianConverter.readShort(y) +
196                             " rwTop : " + EndianConverter.readShort(rwTop) +
197                             " colLeft : " + EndianConverter.readShort(colLeft) +
198                             " pnnAcct : " + pnnAcct);
199 
200         return numOfBytesRead;
201     }
202 
write(OutputStream output)203     public void write(OutputStream output) throws IOException {
204 
205     	output.write(getBiffType());
206 		output.write(x);
207     	output.write(y);
208 	    output.write(rwTop);
209 	    output.write(colLeft);
210 	    output.write(pnnAcct);
211 
212 		Debug.log(Debug.TRACE,"Writing Pane record");
213     }
214 }
215