]> source.dussan.org Git - poi.git/commitdiff
Add protect record to sheet. Sync from REL_2_BRANCH ..Thanks Rick Berman
authorAvik Sengupta <avik@apache.org>
Sat, 23 Aug 2003 19:01:52 +0000 (19:01 +0000)
committerAvik Sengupta <avik@apache.org>
Sat, 23 Aug 2003 19:01:52 +0000 (19:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353311 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/Sheet.java
src/java/org/apache/poi/hssf/record/ProtectRecord.java
src/java/org/apache/poi/hssf/record/ProtectionRev4Record.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java

index a4dc8ffdc186fb8089e8b3ec7266a2dbe7c2fcfb..0a94563c6c1aa8f626ef3c2be26636e645f331a8 100644 (file)
@@ -122,6 +122,7 @@ public class Sheet implements Model
     private Iterator                    valueRecIterator = null;
     private Iterator                    rowRecIterator   = null;
     protected int                       eofLoc           = 0;
+       protected ProtectRecord             protect          = null;
 
     public static final byte PANE_LOWER_RIGHT = (byte)0;
     public static final byte PANE_UPPER_RIGHT = (byte)1;
@@ -265,6 +266,10 @@ public class Sheet implements Model
             {
                 retval.printSetup = (PrintSetupRecord) rec;
             }
+            else if ( rec.getSid() == ProtectRecord.sid )
+            {
+                 retval.protect = (ProtectRecord) rec;
+            }
            else if ( rec.getSid() == LeftMarginRecord.sid)
            {
                retval.getMargins()[LeftMargin] = (LeftMarginRecord) rec;
@@ -416,6 +421,8 @@ public class Sheet implements Model
         retval.selection = 
                 (SelectionRecord) retval.createSelection();
         records.add(retval.selection);
+               retval.protect = (ProtectRecord) retval.createProtect();
+               records.add(retval.protect);
         records.add(retval.createEOF());
         retval.records = records;
         log.log(log.DEBUG, "Sheet createsheet from scratch exit");
@@ -2644,4 +2651,26 @@ public class Sheet implements Model
             margins = new Margin[4];
        return margins;
     }
+
+       /**
+        * creates a Protect record with protect set to false.
+        * @see org.apache.poi.hssf.record.ProtectRecord
+        * @see org.apache.poi.hssf.record.Record
+        * @return a ProtectRecord
+        */
+
+       protected Record createProtect()
+       {
+               log.log(log.DEBUG, "create protect record with protection disabled");
+               ProtectRecord retval = new ProtectRecord();
+
+               retval.setProtect(false);
+               // by default even when we support encryption we won't
+               return retval; // want to default to be protected
+       }
+       
+       public ProtectRecord getProtect()
+       {
+               return protect;
+    }
 }
index 66978f9bf432071d58c2327157ad46b78277308d..a633756d51658242077d1f17ec136c4ba090f180 100644 (file)
@@ -139,9 +139,9 @@ public class ProtectRecord
      * @return whether to protect the sheet or not
      */
 
-    public short getProtect()
+    public boolean getProtect()
     {
-        return field_1_protect;
+        return (field_1_protect == 1);
     }
 
     public String toString()
@@ -149,8 +149,8 @@ public class ProtectRecord
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[PROTECT]\n");
-        buffer.append("    .protected      = ")
-            .append(Integer.toHexString(getProtect())).append("\n");
+           buffer.append("    .protect         = ").append(getProtect())
+            .append("\n");
         buffer.append("[/PROTECT]\n");
         return buffer.toString();
     }
@@ -160,7 +160,7 @@ public class ProtectRecord
         LittleEndian.putShort(data, 0 + offset, sid);
         LittleEndian.putShort(data, 2 + offset,
                               (( short ) 0x02));   // 2 bytes (6 total)
-        LittleEndian.putShort(data, 4 + offset, getProtect());
+        LittleEndian.putShort(data, 4 + offset, field_1_protect);
         return getRecordSize();
     }
 
index e385a4adfcc741bb2bd8d688585545970a142c6a..be6493812660d1fb55b021c159c4e5c75c74a6c4 100644 (file)
@@ -139,18 +139,18 @@ public class ProtectionRev4Record
      * @return whether to protect the workbook or not
      */
 
-    public short getProtect()
-    {
-        return field_1_protect;
-    }
+       public boolean getProtect()
+       {
+               return (field_1_protect == 1);
+       }
 
     public String toString()
     {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[PROT4REV]\n");
-        buffer.append("    .rowheight      = ")
-            .append(Integer.toHexString(getProtect())).append("\n");
+           buffer.append("    .protect         = ").append(getProtect())
+            .append("\n");
         buffer.append("[/PROT4REV]\n");
         return buffer.toString();
     }
@@ -160,7 +160,7 @@ public class ProtectionRev4Record
         LittleEndian.putShort(data, 0 + offset, sid);
         LittleEndian.putShort(data, 2 + offset,
                               (( short ) 0x02));   // 2 bytes (6 total)
-        LittleEndian.putShort(data, 4 + offset, getProtect());
+        LittleEndian.putShort(data, 4 + offset, field_1_protect);
         return getRecordSize();
     }
 
index 803a469a9286ab2d6e5a98f75d7cc2de03a76e22..c0fcfe0aa8dd2261e2360435a8de704cadeaab2a 100644 (file)
@@ -892,6 +892,22 @@ public class HSSFSheet
         sclRecord.setDenominator((short)denominator);
         getSheet().setSCLRecord(sclRecord);
     }
+    
+       /**
+        * Answer whether protection is enabled or disabled
+        * @return true => protection enabled; false => protection disabled
+        */
+       public boolean getProtect() {
+               return getSheet().getProtect().getProtect();            
+       }
+
+       /**
+        * Sets the protection on enabled or disabled
+        * @param protect true => protection enabled; false => protection disabled
+        */
+       public void setProtect(boolean protect) {
+               getSheet().getProtect().setProtect(protect);            
+       }
 
        /**
         * Shifts the merged regions left or right depending on mode
@@ -914,7 +930,7 @@ public class HSSFSheet
                         //dont check if it's not within the shifted area
                         if (! (inStart && inEnd)) continue;
                        
-                        //only shift if the region outside the shifted rows is not merged too                                  
+                        //only shift if the region outside the shifted rows is not merged too                                  
                         if (!merged.contains(startRow-1, (short)0) && !merged.contains(endRow+1, (short)0)){
                                 merged.setRowFrom(merged.getRowFrom()+n);                                      
                                 merged.setRowTo(merged.getRowTo()+n);
index e2e30f73f82ac34a60a72c5613d62d1f8f81c580..3779f3a781eba53de3cec80161c3fcb6f6151076 100644 (file)
@@ -62,6 +62,7 @@ import junit.framework.TestCase;
 
 import org.apache.poi.hssf.model.Sheet;
 import org.apache.poi.hssf.record.HCenterRecord;
+import org.apache.poi.hssf.record.ProtectRecord;
 import org.apache.poi.hssf.record.SCLRecord;
 import org.apache.poi.hssf.record.VCenterRecord;
 import org.apache.poi.hssf.record.WSBoolRecord;
@@ -238,6 +239,27 @@ public class TestHSSFSheet
         cell.setCellValue("Difference Check");
         assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test");
     }
+    
+       /**
+        * Test that the ProtectRecord is included when creating or cloning a sheet
+        */
+       public void testProtect() {
+               HSSFWorkbook workbook = new HSSFWorkbook();
+               HSSFSheet hssfSheet = workbook.createSheet();
+               Sheet sheet = hssfSheet.getSheet();
+               ProtectRecord protect = sheet.getProtect();
+       
+               assertFalse(protect.getProtect());
+
+               // This will tell us that cloneSheet, and by extension,
+               // the list forms of createSheet leave us with an accessible
+               // ProtectRecord.
+               hssfSheet.setProtect(true);
+               Sheet cloned = sheet.cloneSheet();
+               assertNotNull(cloned.getProtect());
+               assertTrue(hssfSheet.getProtect());
+       }
+
 
     public void testZoom()
             throws Exception