1    /* ====================================================================
2     * The Apache Software License, Version 1.1
3     *
4     * Copyright (c) 2002 The Apache Software Foundation.  All rights
5     * reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without
8     * modification, are permitted provided that the following conditions
9     * are met:
10    *
11    * 1. Redistributions of source code must retain the above copyright
12    *    notice, this list of conditions and the following disclaimer.
13    *
14    * 2. Redistributions in binary form must reproduce the above copyright
15    *    notice, this list of conditions and the following disclaimer in
16    *    the documentation and/or other materials provided with the
17    *    distribution.
18    *
19    * 3. The end-user documentation included with the redistribution,
20    *    if any, must include the following acknowledgment:
21    *       "This product includes software developed by the
22    *        Apache Software Foundation (http://www.apache.org/)."
23    *    Alternately, this acknowledgment may appear in the software itself,
24    *    if and wherever such third-party acknowledgments normally appear.
25    *
26    * 4. The names "Apache" and "Apache Software Foundation" and
27    *    "Apache POI" must not be used to endorse or promote products
28    *    derived from this software without prior written permission. For
29    *    written permission, please contact apache@apache.org.
30    *
31    * 5. Products derived from this software may not be called "Apache",
32    *    "Apache POI", nor may "Apache" appear in their name, without
33    *    prior written permission of the Apache Software Foundation.
34    *
35    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46    * SUCH DAMAGE.
47    * ====================================================================
48    *
49    * This software consists of voluntary contributions made by many
50    * individuals on behalf of the Apache Software Foundation.  For more
51    * information on the Apache Software Foundation, please see
52    * <http://www.apache.org/>.
53    */
54   
55   package org.apache.poi.hssf.usermodel;
56   
57   import org.apache.poi.hssf.model.Workbook;
58   import org.apache.poi.hssf.record.NameRecord;
59   import org.apache.poi.hssf.util.RangeAddress;
60   import org.apache.poi.hssf.util.SheetReferences;
61   
62   /**
63    * Title:        High Level Represantion of Named Range <P>
64    * REFERENCE:  <P>
65    * @author Libin Roman (Vista Portal LDT. Developer)
66    */
67   
68   public class HSSFName {
69       private Workbook         book;
70       private NameRecord       name;
71       
72       /** Creates new HSSFName   - called by HSSFWorkbook to create a sheet from
73        * scratch.
74        *
75        * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createName()
76        * @param name the Name Record
77        * @param book - lowlevel Workbook object associated with the sheet.
78        * @param book the Workbook */
79       
80       protected HSSFName(Workbook book, NameRecord name) {
81           this.book = book;
82           this.name = name;
83       }
84       
85       /** Get the sheets name which this named range is referenced to
86        * @return sheet name, which this named range refered to
87        */    
88   
89       public String getSheetName() {
90           String result ;
91           short indexToExternSheet = name.getExternSheetNumber();
92           
93           result = book.findSheetNameFromExternSheet(indexToExternSheet);
94           
95           return result;
96       }
97       
98       /** 
99        * gets the name of the named range
100       * @return named range name
101       */    
102  
103      public String getNameName(){
104          String result = name.getNameText();
105          
106          return result;
107      }
108      
109      /** 
110       * sets the name of the named range
111       * @param nameName named range name to set
112       */    
113  
114      public void setNameName(String nameName){
115          name.setNameText(nameName);
116          name.setNameTextLength((byte)nameName.length());
117      }
118  
119      /** 
120       * gets the reference of the named range
121       * @return reference of the named range
122       */    
123  
124      public String getReference() {
125          String result;
126          SheetReferences refs = book.getSheetReferences();
127          result = name.getAreaReference(refs);
128  
129          return result;
130      }
131  
132      
133  
134      /** 
135       * sets the sheet name which this named range referenced to
136       * @param sheetName the sheet name of the reference
137       */    
138  
139      private void setSheetName(String sheetName){
140          int sheetNumber = book.getSheetIndex(sheetName);
141  
142          short externSheetNumber = book.checkExternSheet(sheetNumber);
143          name.setExternSheetNumber(externSheetNumber);
144  //        name.setIndexToSheet(externSheetNumber);
145  
146      }
147  
148    
149      /** 
150       * sets the reference of this named range
151       * @param ref the reference to set
152       */    
153  
154      public void setReference(String ref){
155  
156          RangeAddress ra = new RangeAddress(ref);
157  
158          String sheetName = ra.getSheetName();
159  
160          if (ra.hasSheetName()) {
161              setSheetName(sheetName);
162          }
163  
164          if (ra.getFromCell().equals(ra.getToCell()) == false) {
165              name.setAreaReference(ra.getFromCell() + ":" + ra.getToCell());
166          } else {
167              name.setAreaReference(ra.getFromCell());            
168          }
169  
170      }
171  
172  }
173  
174