xref: /AOO41X/main/xmerge/source/pexcel/java/org/openoffice/xmerge/converter/xml/sxc/pexcel/records/Selection.java (revision 0c0e82a55dc5b7baa849907647dcb88a54f5f573)
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 
79     /**
80      * Get the hex code for this particular <code>BIFFRecord</code>
81      *
82      * @return the hex code for <code>Selection</code>
83      */
getActiveCell()84     public Point getActiveCell() {
85         Point p = new Point(colActive, EndianConverter.readShort(rwActive));
86         return p;
87     }
88 
89     /**
90      * Get the hex code for this particular <code>BIFFRecord</code>
91      *
92      * @return the hex code for <code>Selection</code>
93      */
setActiveCell(Point p)94     public void setActiveCell(Point p) {
95 
96         colActive = (byte) p.getX();
97         rwActive = EndianConverter.writeShort((short) p.getY());
98     }
99 
100     /**
101      * Reads a Selection Record from the <code>InputStream</code>
102      *
103      * @param   input InputStream containing a Pocket Excel Data file.
104      */
read(InputStream input)105     public int read(InputStream input) throws IOException {
106 
107         int numOfBytesRead  = input.read(rwTop);
108         colLeft             += (byte) input.read();
109         numOfBytesRead      += input.read(rwBottom);
110         colRight            += (byte) input.read();
111         numOfBytesRead      += input.read(rwActive);
112         colActive           += (byte) input.read();
113         numOfBytesRead += 3;
114 
115         Debug.log(Debug.TRACE,"\trwTop : "+ EndianConverter.readShort(rwTop) +
116                             " colLeft : " + colLeft +
117                             " rwBottom : " + EndianConverter.readShort(rwBottom) +
118                             " colRight : "+ colRight +
119                             " rwActive : " + EndianConverter.readShort(rwActive) +
120                             " colActive : " + colActive);
121 
122         return numOfBytesRead;
123     }
124 
write(OutputStream output)125     public void write(OutputStream output) throws IOException {
126 
127         output.write(getBiffType());
128         output.write(rwTop);
129         output.write(colLeft);
130         output.write(rwBottom);
131         output.write(colRight);
132         output.write(rwActive);
133         output.write(colActive);
134 
135         Debug.log(Debug.TRACE,"Writing Selection record");
136     }
137 
138 }
139