KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFClientAnchor


1 /* ====================================================================
2    Copyright 2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.usermodel;
18
19 import org.apache.poi.ddf.EscherClientAnchorRecord;
20 import org.apache.poi.ddf.EscherRecord;
21
22
23 /**
24  * A client anchor is attached to an excel worksheet. It anchors against a
25  * top-left and buttom-right cell.
26  *
27  * @author Glen Stampoultzis (glens at apache.org)
28  */

29 public class HSSFClientAnchor
30         extends HSSFAnchor
31 {
32     short col1;
33     int row1;
34     short col2;
35     int row2;
36
37     /**
38      * Creates a new client anchor and defaults all the anchor positions to 0.
39      */

40     public HSSFClientAnchor()
41     {
42     }
43
44     /**
45      * Creates a new client anchor and sets the top-left and bottom-right
46      * coordinates of the anchor.
47      *
48      * @param dx1 the x coordinate within the first cell.
49      * @param dy1 the y coordinate within the first cell.
50      * @param dx2 the x coordinate within the second cell.
51      * @param dy2 the y coordinate within the second cell.
52      * @param col1 the column (0 based) of the first cell.
53      * @param row1 the row (0 based) of the first cell.
54      * @param col2 the column (0 based) of the second cell.
55      * @param row2 the row (0 based) of the second cell.
56      */

57     public HSSFClientAnchor( int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2 )
58     {
59         super( dx1, dy1, dx2, dy2 );
60
61         checkRange(dx1, 0, 1023, "dx1");
62         checkRange(dx2, 0, 1023, "dx2");
63         checkRange(dy1, 0, 255, "dy1");
64         checkRange(dy2, 0, 255, "dy2");
65         checkRange(col1, 0, 255, "col1");
66         checkRange(col2, 0, 255, "col2");
67         checkRange(row1, 0, 255 * 256, "row1");
68         checkRange(row2, 0, 255 * 256, "row2");
69
70         this.col1 = col1;
71         this.row1 = row1;
72         this.col2 = col2;
73         this.row2 = row2;
74     }
75
76     /**
77      * Calculates the height of a client anchor in points.
78      *
79      * @param sheet the sheet the anchor will be attached to
80      * @return the shape height.
81      */

82     public float getAnchorHeightInPoints(HSSFSheet sheet )
83     {
84         int y1 = Math.min( getDy1(), getDy2() );
85         int y2 = Math.max( getDy1(), getDy2() );
86         int row1 = Math.min( getRow1(), getRow2() );
87         int row2 = Math.max( getRow1(), getRow2() );
88
89         float points = 0;
90         if (row1 == row2)
91         {
92             points = ((y2 - y1) / 256.0f) * getRowHeightInPoints(sheet, row2);
93         }
94         else
95         {
96             points += ((256.0f - y1) / 256.0f) * getRowHeightInPoints(sheet, row1);
97             for (int i = row1 + 1; i < row2; i++)
98             {
99                 points += getRowHeightInPoints(sheet, i);
100             }
101             points += (y2 / 256.0f) * getRowHeightInPoints(sheet, row2);
102         }
103
104         return points;
105     }
106
107     private float getRowHeightInPoints(HSSFSheet sheet, int rowNum)
108     {
109         HSSFRow row = sheet.getRow(rowNum);
110         if (row == null)
111             return sheet.getDefaultRowHeightInPoints();
112         else
113             return row.getHeightInPoints();
114     }
115
116     public short getCol1()
117     {
118         return col1;
119     }
120
121     public void setCol1( short col1 )
122     {
123         checkRange(col1, 0, 255, "col1");
124         this.col1 = col1;
125     }
126
127     public short getCol2()
128     {
129         return col2;
130     }
131
132     public void setCol2( short col2 )
133     {
134         checkRange(col2, 0, 255, "col2");
135         this.col2 = col2;
136     }
137
138     public int getRow1()
139     {
140         return row1;
141     }
142
143     public void setRow1( int row1 )
144     {
145         checkRange(row1, 0, 256 * 256, "row1");
146         this.row1 = row1;
147     }
148
149     public int getRow2()
150     {
151         return row2;
152     }
153
154     public void setRow2( int row2 )
155     {
156         checkRange(row2, 0, 256 * 256, "row2");
157         this.row2 = row2;
158     }
159
160     /**
161      * Dets the top-left and bottom-right
162      * coordinates of the anchor.
163      *
164      * @param x1 the x coordinate within the first cell.
165      * @param y1 the y coordinate within the first cell.
166      * @param x2 the x coordinate within the second cell.
167      * @param y2 the y coordinate within the second cell.
168      * @param col1 the column (0 based) of the first cell.
169      * @param row1 the row (0 based) of the first cell.
170      * @param col2 the column (0 based) of the second cell.
171      * @param row2 the row (0 based) of the second cell.
172      */

173     public void setAnchor( short col1, int row1, int x1, int y1, short col2, int row2, int x2, int y2 )
174     {
175         checkRange(dx1, 0, 1023, "dx1");
176         checkRange(dx2, 0, 1023, "dx2");
177         checkRange(dy1, 0, 255, "dy1");
178         checkRange(dy2, 0, 255, "dy2");
179         checkRange(col1, 0, 255, "col1");
180         checkRange(col2, 0, 255, "col2");
181         checkRange(row1, 0, 255 * 256, "row1");
182         checkRange(row2, 0, 255 * 256, "row2");
183
184         this.col1 = col1;
185         this.row1 = row1;
186         this.dx1 = x1;
187         this.dy1 = y1;
188         this.col2 = col2;
189         this.row2 = row2;
190         this.dx2 = x2;
191         this.dy2 = y2;
192     }
193
194     /**
195      * @return true if the anchor goes from right to left.
196      */

197     public boolean isHorizontallyFlipped()
198     {
199         if (col1 == col2)
200             return dx1 > dx2;
201         else
202             return col1 > col2;
203     }
204
205     /**
206      * @return true if the anchor goes from bottom to top.
207      */

208     public boolean isVerticallyFlipped()
209     {
210         if (row1 == row2)
211             return dy1 > dy2;
212         else
213             return row1 > row2;
214     }
215
216     private void checkRange( int value, int minRange, int maxRange, String JavaDoc varName )
217     {
218         if (value < minRange || value > maxRange)
219             throw new IllegalArgumentException JavaDoc(varName + " must be between " + minRange + " and " + maxRange);
220     }
221
222
223 }
224
Popular Tags