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: import org.apache.poi.hssf.model.Workbook;
021: import org.apache.poi.hssf.record.BoundSheetRecord;
022: import org.apache.poi.hssf.record.NameRecord;
023: import org.apache.poi.hssf.util.RangeAddress;
024:
025: /**
026: * Title: High Level Represantion of Named Range <P>
027: * REFERENCE: <P>
028: * @author Libin Roman (Vista Portal LDT. Developer)
029: */
030:
031: public class HSSFName {
032: private Workbook book;
033: private NameRecord name;
034:
035: /** Creates new HSSFName - called by HSSFWorkbook to create a sheet from
036: * scratch.
037: *
038: * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createName()
039: * @param name the Name Record
040: * @param book lowlevel Workbook object associated with the sheet.
041: */
042:
043: protected HSSFName(Workbook book, NameRecord name) {
044: this .book = book;
045: this .name = name;
046: }
047:
048: /** Get the sheets name which this named range is referenced to
049: * @return sheet name, which this named range refered to
050: */
051:
052: public String getSheetName() {
053: String result;
054: short indexToExternSheet = name.getExternSheetNumber();
055:
056: result = book.findSheetNameFromExternSheet(indexToExternSheet);
057:
058: return result;
059: }
060:
061: /**
062: * gets the name of the named range
063: * @return named range name
064: */
065:
066: public String getNameName() {
067: String result = name.getNameText();
068:
069: return result;
070: }
071:
072: /**
073: * sets the name of the named range
074: * @param nameName named range name to set
075: */
076:
077: public void setNameName(String nameName) {
078: name.setNameText(nameName);
079: name.setNameTextLength((byte) nameName.length());
080:
081: //Check to ensure no other names have the same case-insensitive name
082: for (int i = book.getNumNames() - 1; i >= 0; i--) {
083: NameRecord rec = book.getNameRecord(i);
084: if (rec != name) {
085: if (rec.getNameText().equalsIgnoreCase(getNameName()))
086: throw new IllegalArgumentException(
087: "The workbook already contains this name (case-insensitive)");
088: }
089: }
090: }
091:
092: /**
093: * gets the reference of the named range
094: * @return reference of the named range
095: */
096:
097: public String getReference() {
098: String result;
099: result = name.getAreaReference(book);
100:
101: return result;
102: }
103:
104: /**
105: * sets the sheet name which this named range referenced to
106: * @param sheetName the sheet name of the reference
107: */
108:
109: private void setSheetName(String sheetName) {
110: int sheetNumber = book.getSheetIndex(sheetName);
111:
112: short externSheetNumber = book.checkExternSheet(sheetNumber);
113: name.setExternSheetNumber(externSheetNumber);
114: // name.setIndexToSheet(externSheetNumber);
115:
116: }
117:
118: /**
119: * sets the reference of this named range
120: * @param ref the reference to set
121: */
122:
123: public void setReference(String ref) {
124:
125: RangeAddress ra = new RangeAddress(ref);
126:
127: String sheetName = ra.getSheetName();
128:
129: if (ra.hasSheetName()) {
130: setSheetName(sheetName);
131: }
132:
133: //allow the poi utilities to parse it out
134: name.setAreaReference(ref);
135:
136: }
137:
138: }
|