001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: /**
021: * A client anchor is attached to an excel worksheet. It anchors against a
022: * top-left and buttom-right cell.
023: *
024: * @author Glen Stampoultzis (glens at apache.org)
025: */
026: public class HSSFClientAnchor extends HSSFAnchor {
027: short col1;
028: int row1;
029: short col2;
030: int row2;
031: int anchorType;
032:
033: /**
034: * Creates a new client anchor and defaults all the anchor positions to 0.
035: */
036: public HSSFClientAnchor() {
037: }
038:
039: /**
040: * Creates a new client anchor and sets the top-left and bottom-right
041: * coordinates of the anchor.
042: *
043: * @param dx1 the x coordinate within the first cell.
044: * @param dy1 the y coordinate within the first cell.
045: * @param dx2 the x coordinate within the second cell.
046: * @param dy2 the y coordinate within the second cell.
047: * @param col1 the column (0 based) of the first cell.
048: * @param row1 the row (0 based) of the first cell.
049: * @param col2 the column (0 based) of the second cell.
050: * @param row2 the row (0 based) of the second cell.
051: */
052: public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2,
053: short col1, int row1, short col2, int row2) {
054: super (dx1, dy1, dx2, dy2);
055:
056: checkRange(dx1, 0, 1023, "dx1");
057: checkRange(dx2, 0, 1023, "dx2");
058: checkRange(dy1, 0, 255, "dy1");
059: checkRange(dy2, 0, 255, "dy2");
060: checkRange(col1, 0, 255, "col1");
061: checkRange(col2, 0, 255, "col2");
062: checkRange(row1, 0, 255 * 256, "row1");
063: checkRange(row2, 0, 255 * 256, "row2");
064:
065: this .col1 = col1;
066: this .row1 = row1;
067: this .col2 = col2;
068: this .row2 = row2;
069: }
070:
071: /**
072: * Calculates the height of a client anchor in points.
073: *
074: * @param sheet the sheet the anchor will be attached to
075: * @return the shape height.
076: */
077: public float getAnchorHeightInPoints(HSSFSheet sheet) {
078: int y1 = getDy1();
079: int y2 = getDy2();
080: int row1 = Math.min(getRow1(), getRow2());
081: int row2 = Math.max(getRow1(), getRow2());
082:
083: float points = 0;
084: if (row1 == row2) {
085: points = ((y2 - y1) / 256.0f)
086: * getRowHeightInPoints(sheet, row2);
087: } else {
088: points += ((256.0f - y1) / 256.0f)
089: * getRowHeightInPoints(sheet, row1);
090: for (int i = row1 + 1; i < row2; i++) {
091: points += getRowHeightInPoints(sheet, i);
092: }
093: points += (y2 / 256.0f) * getRowHeightInPoints(sheet, row2);
094: }
095:
096: return points;
097: }
098:
099: private float getRowHeightInPoints(HSSFSheet sheet, int rowNum) {
100: HSSFRow row = sheet.getRow(rowNum);
101: if (row == null)
102: return sheet.getDefaultRowHeightInPoints();
103: else
104: return row.getHeightInPoints();
105: }
106:
107: public short getCol1() {
108: return col1;
109: }
110:
111: public void setCol1(short col1) {
112: checkRange(col1, 0, 255, "col1");
113: this .col1 = col1;
114: }
115:
116: public short getCol2() {
117: return col2;
118: }
119:
120: public void setCol2(short col2) {
121: checkRange(col2, 0, 255, "col2");
122: this .col2 = col2;
123: }
124:
125: public int getRow1() {
126: return row1;
127: }
128:
129: public void setRow1(int row1) {
130: checkRange(row1, 0, 256 * 256, "row1");
131: this .row1 = row1;
132: }
133:
134: public int getRow2() {
135: return row2;
136: }
137:
138: public void setRow2(int row2) {
139: checkRange(row2, 0, 256 * 256, "row2");
140: this .row2 = row2;
141: }
142:
143: /**
144: * Dets the top-left and bottom-right
145: * coordinates of the anchor.
146: *
147: * @param x1 the x coordinate within the first cell.
148: * @param y1 the y coordinate within the first cell.
149: * @param x2 the x coordinate within the second cell.
150: * @param y2 the y coordinate within the second cell.
151: * @param col1 the column (0 based) of the first cell.
152: * @param row1 the row (0 based) of the first cell.
153: * @param col2 the column (0 based) of the second cell.
154: * @param row2 the row (0 based) of the second cell.
155: */
156: public void setAnchor(short col1, int row1, int x1, int y1,
157: short col2, int row2, int x2, int y2) {
158: checkRange(dx1, 0, 1023, "dx1");
159: checkRange(dx2, 0, 1023, "dx2");
160: checkRange(dy1, 0, 255, "dy1");
161: checkRange(dy2, 0, 255, "dy2");
162: checkRange(col1, 0, 255, "col1");
163: checkRange(col2, 0, 255, "col2");
164: checkRange(row1, 0, 255 * 256, "row1");
165: checkRange(row2, 0, 255 * 256, "row2");
166:
167: this .col1 = col1;
168: this .row1 = row1;
169: this .dx1 = x1;
170: this .dy1 = y1;
171: this .col2 = col2;
172: this .row2 = row2;
173: this .dx2 = x2;
174: this .dy2 = y2;
175: }
176:
177: /**
178: * @return true if the anchor goes from right to left.
179: */
180: public boolean isHorizontallyFlipped() {
181: if (col1 == col2)
182: return dx1 > dx2;
183: else
184: return col1 > col2;
185: }
186:
187: /**
188: * @return true if the anchor goes from bottom to top.
189: */
190: public boolean isVerticallyFlipped() {
191: if (row1 == row2)
192: return dy1 > dy2;
193: else
194: return row1 > row2;
195: }
196:
197: /**
198: * Gets the anchor type
199: * <p>
200: * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
201: */
202: public int getAnchorType() {
203: return anchorType;
204: }
205:
206: /**
207: * Sets the anchor type
208: * <p>
209: * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
210: */
211: public void setAnchorType(int anchorType) {
212: this .anchorType = anchorType;
213: }
214:
215: private void checkRange(int value, int minRange, int maxRange,
216: String varName) {
217: if (value < minRange || value > maxRange)
218: throw new IllegalArgumentException(varName
219: + " must be between " + minRange + " and "
220: + maxRange);
221: }
222:
223: }
|