You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/05/21 21:57:41 UTC

svn commit: r658833 - in /poi/trunk/src/java/org/apache/poi/hssf: record/formula/Area3DPtg.java record/formula/AreaPtg.java util/AreaReference.java

Author: josh
Date: Wed May 21 12:57:40 2008
New Revision: 658833

URL: http://svn.apache.org/viewvc?rev=658833&view=rev
Log:
improved toString and refactored toFormulaString on Area(3D)Ptg

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java
    poi/trunk/src/java/org/apache/poi/hssf/util/AreaReference.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java?rev=658833&r1=658832&r2=658833&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java Wed May 21 12:57:40 2008
@@ -27,7 +27,7 @@
 
 
 /**
- * Title:        Area 3D Ptg - 3D referecnce (Sheet + Area)<P>
+ * Title:        Area 3D Ptg - 3D reference (Sheet + Area)<P>
  * Description:  Defined a area in Extern Sheet. <P>
  * REFERENCE:  <P>
  * @author Libin Roman (Vista Portal LDT. Developer)
@@ -35,7 +35,6 @@
  * @author Jason Height (jheight at chariot dot net dot au)
  * @version 1.0-pre
  */
-
 public class Area3DPtg extends Ptg implements AreaI
 {
 	public final static byte sid = 0x3b;
@@ -84,23 +83,15 @@
 		  setExternSheetIndex(externalSheetIndex);
 	}
 
-	public String toString()
-	{
-		StringBuffer buffer = new StringBuffer();
-
-		buffer.append( "AreaPtg\n" );
-		buffer.append( "Index to Extern Sheet = " + getExternSheetIndex() ).append( "\n" );
-		buffer.append( "firstRow = " + getFirstRow() ).append( "\n" );
-		buffer.append( "lastRow  = " + getLastRow() ).append( "\n" );
-		buffer.append( "firstCol = " + getFirstColumn() ).append( "\n" );
-		buffer.append( "lastCol  = " + getLastColumn() ).append( "\n" );
-		buffer.append( "firstColRel= "
-				+ isFirstRowRelative() ).append( "\n" );
-		buffer.append( "lastColRowRel = "
-				+ isLastRowRelative() ).append( "\n" );
-		buffer.append( "firstColRel   = " + isFirstColRelative() ).append( "\n" );
-		buffer.append( "lastColRel	= " + isLastColRelative() ).append( "\n" );
-		return buffer.toString();
+	public String toString() {
+		StringBuffer sb = new StringBuffer();
+		sb.append(getClass().getName());
+		sb.append(" [");
+		sb.append("sheetIx=").append(getExternSheetIndex());
+		sb.append(" ! ");
+		sb.append(AreaReference.formatAsString(this));
+		sb.append("]");
+		return sb.toString();
 	}
 
 	public void writeBytes( byte[] array, int offset )
@@ -284,7 +275,7 @@
 		}
 		
 		// Now the normal area bit
-		retval.append( AreaPtg.toFormulaString(this, book) );
+		retval.append(AreaReference.formatAsString(this));
 		
 		// All done
 		return retval.toString();
@@ -326,6 +317,7 @@
 
 	public int hashCode()
 	{
+		// TODO - hashCode seems to be unused
 		int result;
 		result = (int) field_1_index_extern_sheet;
 		result = 29 * result + (int) field_2_first_row;

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java?rev=658833&r1=658832&r2=658833&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/AreaPtg.java Wed May 21 12:57:40 2008
@@ -114,23 +114,13 @@
       return "AreaPtg";
     }    
 
-    public String toString()
-    {
-        StringBuffer buffer = new StringBuffer();
-
-        buffer.append(getAreaPtgName());
-        buffer.append("\n");
-        buffer.append("firstRow = " + getFirstRow()).append("\n");
-        buffer.append("lastRow  = " + getLastRow()).append("\n");
-        buffer.append("firstCol = " + getFirstColumn()).append("\n");
-        buffer.append("lastCol  = " + getLastColumn()).append("\n");
-        buffer.append("firstColRowRel= "
-                      + isFirstRowRelative()).append("\n");
-        buffer.append("lastColRowRel = "
-                      + isLastRowRelative()).append("\n");
-        buffer.append("firstColRel   = " + isFirstColRelative()).append("\n");
-        buffer.append("lastColRel    = " + isLastColRelative()).append("\n");
-        return buffer.toString();
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append(getClass().getName());
+        sb.append(" [");
+        sb.append(AreaReference.formatAsString(this));
+        sb.append("]");
+        return sb.toString();
     }
 
     public void writeBytes(byte [] array, int offset) {
@@ -307,19 +297,8 @@
         field_4_last_column = column;
     }
     
-    public String toFormulaString(HSSFWorkbook book)
-    {
-    	return toFormulaString(this, book);
-    }
-    protected static String toFormulaString(AreaI area, HSSFWorkbook book) {
-    	CellReference topLeft = new CellReference(area.getFirstRow(),area.getFirstColumn(),!area.isFirstRowRelative(),!area.isFirstColRelative());
-    	CellReference botRight = new CellReference(area.getLastRow(),area.getLastColumn(),!area.isLastRowRelative(),!area.isLastColRelative());
-    	
-    	if(AreaReference.isWholeColumnReference(topLeft, botRight)) {
-    		return (new AreaReference(topLeft, botRight)).formatAsString();
-    	} else {
-    		return topLeft.formatAsString() + ":" + botRight.formatAsString(); 
-    	}
+    public String toFormulaString(HSSFWorkbook book) {
+        return AreaReference.formatAsString(this);
     }
 
     public byte getDefaultOperandClass() {

Modified: poi/trunk/src/java/org/apache/poi/hssf/util/AreaReference.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/util/AreaReference.java?rev=658833&r1=658832&r2=658833&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/util/AreaReference.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/util/AreaReference.java Wed May 21 12:57:40 2008
@@ -15,12 +15,13 @@
    limitations under the License.
 ==================================================================== */
 
-
 package org.apache.poi.hssf.util;
 
 import java.util.ArrayList;
 import java.util.StringTokenizer;
 
+import org.apache.poi.hssf.record.formula.AreaI;
+
 public final class AreaReference {
 
     /** The character (!) that separates sheet names from cell references */ 
@@ -50,13 +51,13 @@
         
         // Special handling for whole-column references
         if(parts.length == 2 && parts[0].length() == 1 &&
-        		parts[1].length() == 1 && 
-        		parts[0].charAt(0) >= 'A' && parts[0].charAt(0) <= 'Z' &&
-        		parts[1].charAt(0) >= 'A' && parts[1].charAt(0) <= 'Z') {
-        	// Represented internally as x$1 to x$65536
-        	//  which is the maximum range of rows
-        	parts[0] = parts[0] + "$1";
-        	parts[1] = parts[1] + "$65536";
+                parts[1].length() == 1 && 
+                parts[0].charAt(0) >= 'A' && parts[0].charAt(0) <= 'Z' &&
+                parts[1].charAt(0) >= 'A' && parts[1].charAt(0) <= 'Z') {
+            // Represented internally as x$1 to x$65536
+            //  which is the maximum range of rows
+            parts[0] = parts[0] + "$1";
+            parts[1] = parts[1] + "$65536";
         }
         
         _firstCell = new CellReference(parts[0]);
@@ -74,9 +75,9 @@
      * Creates an area ref from a pair of Cell References.
      */
     public AreaReference(CellReference topLeft, CellReference botRight) {
-    	_firstCell = topLeft;
-    	_lastCell = botRight;
-    	_isSingleCell = false;
+        _firstCell = topLeft;
+        _lastCell = botRight;
+        _isSingleCell = false;
     }
 
     /**
@@ -98,17 +99,17 @@
      *  such as C:C or D:G ?
      */
     public static boolean isWholeColumnReference(CellReference topLeft, CellReference botRight) {
-    	// These are represented as something like
-    	//   C$1:C$65535 or D$1:F$0
-    	// i.e. absolute from 1st row to 0th one
-    	if(topLeft.getRow() == 0 && topLeft.isRowAbsolute() &&
-    		botRight.getRow() == 65535 && botRight.isRowAbsolute()) {
-    		return true;
-    	}
-    	return false;
+        // These are represented as something like
+        //   C$1:C$65535 or D$1:F$0
+        // i.e. absolute from 1st row to 0th one
+        if(topLeft.getRow() == 0 && topLeft.isRowAbsolute() &&
+            botRight.getRow() == 65535 && botRight.isRowAbsolute()) {
+            return true;
+        }
+        return false;
     }
     public boolean isWholeColumnReference() {
-    	return isWholeColumnReference(_firstCell, _lastCell);
+        return isWholeColumnReference(_firstCell, _lastCell);
     }
 
     /**
@@ -155,26 +156,26 @@
      * Returns a reference to every cell covered by this area
      */
     public CellReference[] getAllReferencedCells() {
-    	// Special case for single cell reference
-    	if(_isSingleCell) {
-    		return  new CellReference[] { _firstCell, };
-    	}
+        // Special case for single cell reference
+        if(_isSingleCell) {
+            return  new CellReference[] { _firstCell, };
+        }
  
-    	// Interpolate between the two
+        // Interpolate between the two
         int minRow = Math.min(_firstCell.getRow(), _lastCell.getRow());
-    	int maxRow = Math.max(_firstCell.getRow(), _lastCell.getRow());
-    	int minCol = Math.min(_firstCell.getCol(), _lastCell.getCol());
-    	int maxCol = Math.max(_firstCell.getCol(), _lastCell.getCol());
+        int maxRow = Math.max(_firstCell.getRow(), _lastCell.getRow());
+        int minCol = Math.min(_firstCell.getCol(), _lastCell.getCol());
+        int maxCol = Math.max(_firstCell.getCol(), _lastCell.getCol());
         String sheetName = _firstCell.getSheetName();
-    	
-    	ArrayList refs = new ArrayList();
-    	for(int row=minRow; row<=maxRow; row++) {
-    		for(int col=minCol; col<=maxCol; col++) {
-    			CellReference ref = new CellReference(sheetName, row, col, _firstCell.isRowAbsolute(), _firstCell.isColAbsolute());
-    			refs.add(ref);
-    		}
-    	}
-    	return (CellReference[])refs.toArray(new CellReference[refs.size()]);
+        
+        ArrayList refs = new ArrayList();
+        for(int row=minRow; row<=maxRow; row++) {
+            for(int col=minCol; col<=maxCol; col++) {
+                CellReference ref = new CellReference(sheetName, row, col, _firstCell.isRowAbsolute(), _firstCell.isColAbsolute());
+                refs.add(ref);
+            }
+        }
+        return (CellReference[])refs.toArray(new CellReference[refs.size()]);
     }
 
     /**
@@ -189,14 +190,14 @@
      * @return the text representation of this area reference as it would appear in a formula.
      */
     public String formatAsString() {
-    	// Special handling for whole-column references
-    	if(isWholeColumnReference()) {
-    		return
-    			CellReference.convertNumToColString(_firstCell.getCol())
-    			+ ":" +
-    			CellReference.convertNumToColString(_lastCell.getCol());
-    	}
-    	
+        // Special handling for whole-column references
+        if(isWholeColumnReference()) {
+            return
+                CellReference.convertNumToColString(_firstCell.getCol())
+                + ":" +
+                CellReference.convertNumToColString(_lastCell.getCol());
+        }
+        
         StringBuffer sb = new StringBuffer(32);
         sb.append(_firstCell.formatAsString());
         if(!_isSingleCell) {
@@ -210,6 +211,18 @@
         }
         return sb.toString();
     }
+    /**
+     * Formats a 2-D area as it would appear in a formula.  See formatAsString() (no-arg)
+     */
+    public static String formatAsString(AreaI area) {
+        CellReference topLeft = new CellReference(area.getFirstRow(),area.getFirstColumn(),!area.isFirstRowRelative(),!area.isFirstColRelative());
+        CellReference botRight = new CellReference(area.getLastRow(),area.getLastColumn(),!area.isLastRowRelative(),!area.isLastColRelative());
+        
+        if(isWholeColumnReference(topLeft, botRight)) {
+            return (new AreaReference(topLeft, botRight)).formatAsString();
+        }
+        return topLeft.formatAsString() + ":" + botRight.formatAsString(); 
+    }
     public String toString() {
         StringBuffer sb = new StringBuffer(64);
         sb.append(getClass().getName()).append(" [");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org