1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
17
18 package org.apache.poi.hssf.usermodel;
19
20
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 int anchorType;
37
38 /**
39 * Creates a new client anchor and defaults all the anchor positions to 0.
40 */
41 public HSSFClientAnchor()
42 {
43 }
44
45 /**
46 * Creates a new client anchor and sets the top-left and bottom-right
47 * coordinates of the anchor.
48 *
49 * @param dx1 the x coordinate within the first cell.
50 * @param dy1 the y coordinate within the first cell.
51 * @param dx2 the x coordinate within the second cell.
52 * @param dy2 the y coordinate within the second cell.
53 * @param col1 the column (0 based) of the first cell.
54 * @param row1 the row (0 based) of the first cell.
55 * @param col2 the column (0 based) of the second cell.
56 * @param row2 the row (0 based) of the second cell.
57 */
58 public HSSFClientAnchor( int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2 )
59 {
60 super( dx1, dy1, dx2, dy2 );
61
62 checkRange(dx1, 0, 1023, "dx1");
63 checkRange(dx2, 0, 1023, "dx2");
64 checkRange(dy1, 0, 255, "dy1");
65 checkRange(dy2, 0, 255, "dy2");
66 checkRange(col1, 0, 255, "col1");
67 checkRange(col2, 0, 255, "col2");
68 checkRange(row1, 0, 255 * 256, "row1");
69 checkRange(row2, 0, 255 * 256, "row2");
70
71 this.col1 = col1;
72 this.row1 = row1;
73 this.col2 = col2;
74 this.row2 = row2;
75 }
76
77 /**
78 * Calculates the height of a client anchor in points.
79 *
80 * @param sheet the sheet the anchor will be attached to
81 * @return the shape height.
82 */
83 public float getAnchorHeightInPoints(HSSFSheet sheet )
84 {
85 int y1 = getDy1();
86 int y2 = getDy2();
87 int row1 = Math.min( getRow1(), getRow2() );
88 int row2 = Math.max( getRow1(), getRow2() );
89
90 float points = 0;
91 if (row1 == row2)
92 {
93 points = ((y2 - y1) / 256.0f) * getRowHeightInPoints(sheet, row2);
94 }
95 else
96 {
97 points += ((256.0f - y1) / 256.0f) * getRowHeightInPoints(sheet, row1);
98 for (int i = row1 + 1; i < row2; i++)
99 {
100 points += getRowHeightInPoints(sheet, i);
101 }
102 points += (y2 / 256.0f) * getRowHeightInPoints(sheet, row2);
103 }
104
105 return points;
106 }
107
108 private float getRowHeightInPoints(HSSFSheet sheet, int rowNum)
109 {
110 HSSFRow row = sheet.getRow(rowNum);
111 if (row == null)
112 return sheet.getDefaultRowHeightInPoints();
113 else
114 return row.getHeightInPoints();
115 }
116
117 public short getCol1()
118 {
119 return col1;
120 }
121
122 public void setCol1( short col1 )
123 {
124 checkRange(col1, 0, 255, "col1");
125 this.col1 = col1;
126 }
127
128 public short getCol2()
129 {
130 return col2;
131 }
132
133 public void setCol2( short col2 )
134 {
135 checkRange(col2, 0, 255, "col2");
136 this.col2 = col2;
137 }
138
139 public int getRow1()
140 {
141 return row1;
142 }
143
144 public void setRow1( int row1 )
145 {
146 checkRange(row1, 0, 256 * 256, "row1");
147 this.row1 = row1;
148 }
149
150 public int getRow2()
151 {
152 return row2;
153 }
154
155 public void setRow2( int row2 )
156 {
157 checkRange(row2, 0, 256 * 256, "row2");
158 this.row2 = row2;
159 }
160
161 /**
162 * Dets the top-left and bottom-right
163 * coordinates of the anchor.
164 *
165 * @param x1 the x coordinate within the first cell.
166 * @param y1 the y coordinate within the first cell.
167 * @param x2 the x coordinate within the second cell.
168 * @param y2 the y coordinate within the second cell.
169 * @param col1 the column (0 based) of the first cell.
170 * @param row1 the row (0 based) of the first cell.
171 * @param col2 the column (0 based) of the second cell.
172 * @param row2 the row (0 based) of the second cell.
173 */
174 public void setAnchor( short col1, int row1, int x1, int y1, short col2, int row2, int x2, int y2 )
175 {
176 checkRange(dx1, 0, 1023, "dx1");
177 checkRange(dx2, 0, 1023, "dx2");
178 checkRange(dy1, 0, 255, "dy1");
179 checkRange(dy2, 0, 255, "dy2");
180 checkRange(col1, 0, 255, "col1");
181 checkRange(col2, 0, 255, "col2");
182 checkRange(row1, 0, 255 * 256, "row1");
183 checkRange(row2, 0, 255 * 256, "row2");
184
185 this.col1 = col1;
186 this.row1 = row1;
187 this.dx1 = x1;
188 this.dy1 = y1;
189 this.col2 = col2;
190 this.row2 = row2;
191 this.dx2 = x2;
192 this.dy2 = y2;
193 }
194
195 /**
196 * @return true if the anchor goes from right to left.
197 */
198 public boolean isHorizontallyFlipped()
199 {
200 if (col1 == col2)
201 return dx1 > dx2;
202 else
203 return col1 > col2;
204 }
205
206 /**
207 * @return true if the anchor goes from bottom to top.
208 */
209 public boolean isVerticallyFlipped()
210 {
211 if (row1 == row2)
212 return dy1 > dy2;
213 else
214 return row1 > row2;
215 }
216
217 /**
218 * Gets the anchor type
219 * <p>
220 * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
221 */
222 public int getAnchorType()
223 {
224 return anchorType;
225 }
226
227 /**
228 * Sets the anchor type
229 * <p>
230 * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
231 */
232 public void setAnchorType( int anchorType )
233 {
234 this.anchorType = anchorType;
235 }
236
237 private void checkRange( int value, int minRange, int maxRange, String varName )
238 {
239 if (value < minRange || value > maxRange)
240 throw new IllegalArgumentException(varName + " must be between " + minRange + " and " + maxRange);
241 }
242
243
244 }