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 
35 
36 /**
37  * Represents a BIFF Record that describes the selected area of a worksheet
38  */
39 public class Selection implements BIFFRecord {
40 
41     private byte[] rwTop		= new byte[2];
42     private byte   colLeft;
43     private byte[] rwBottom		= new byte[2];
44     private byte   colRight;
45     private byte[] rwActive		= new byte[2];
46     private byte   colActive;
47 
48 	/**
49 	 * Default Constructor
50 	 */
Selection()51     public Selection() {
52     	this.rwTop			= EndianConverter.writeShort((short) 0);
53     	this.colLeft		= 0;
54     	this.rwBottom		= EndianConverter.writeShort((short) 0);
55     	this.colRight		= 0;
56     	this.rwActive		= EndianConverter.writeShort((short) 0);
57     	this.colActive		= 0;
58 
59     }
60 
61 	/**
62 	 * Constructs a Selection Record from the <code>InputStream</code>
63 	 *
64 	 * @param	is InputStream containing a Pocket Excel Data file.
65 	 */
Selection(InputStream is)66     public Selection(InputStream is) throws IOException {
67 		read(is);
68     }
69 
70     /**
71 	 * Get the hex code for this particular <code>BIFFRecord</code>
72 	 *
73 	 * @return the hex code for <code>Selection</code>
74 	 */
getBiffType()75     public short getBiffType() {
76         return PocketExcelConstants.CURRENT_SELECTION;
77     }
78 
getActiveCell()79     public Point getActiveCell() {
80 		Point p = new Point(colActive, EndianConverter.readShort(rwActive));
81         return p;
82     }
83 
setActiveCell(Point p)84     public void setActiveCell(Point p) {
85 
86 		colActive = (byte) p.getX();
87 		rwActive = EndianConverter.writeShort((short) p.getY());
88     }
89 
90 	/**
91 	 * Reads a Selection Record from the <code>InputStream</code>
92 	 *
93 	 * @param	input InputStream containing a Pocket Excel Data file.
94 	 */
read(InputStream input)95     public int read(InputStream input) throws IOException {
96 
97         int numOfBytesRead	= input.read(rwTop);
98         colLeft				+= (byte) input.read();
99         numOfBytesRead		+= input.read(rwBottom);
100         colRight			+= (byte) input.read();
101         numOfBytesRead		+= input.read(rwActive);
102         colActive			+= (byte) input.read();
103 		numOfBytesRead += 3;
104 
105         Debug.log(Debug.TRACE,"\trwTop : "+ EndianConverter.readShort(rwTop) +
106                             " colLeft : " + colLeft +
107                             " rwBottom : " + EndianConverter.readShort(rwBottom) +
108                             " colRight : "+ colRight +
109                             " rwActive : " + EndianConverter.readShort(rwActive) +
110                             " colActive : " + colActive);
111 
112         return numOfBytesRead;
113     }
114 
write(OutputStream output)115     public void write(OutputStream output) throws IOException {
116 
117 		output.write(getBiffType());
118 		output.write(rwTop);
119 		output.write(colLeft);
120 		output.write(rwBottom);
121 		output.write(colRight);
122 		output.write(rwActive);
123 		output.write(colActive);
124 
125 		Debug.log(Debug.TRACE,"Writing Selection record");
126     }
127 
128 }
129