[seam-commits] Seam SVN: r13476 - in sandbox/modules/spreadsheet/src: main/java/org/jboss/seam/spreadsheet/model and 3 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Jul 22 06:54:46 EDT 2010


Author: nickarls
Date: 2010-07-22 06:54:46 -0400 (Thu, 22 Jul 2010)
New Revision: 13476

Added:
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/JxlColumnAutoWidthCommand.java
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/ColumnAutoWidthCommand.java
   sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/ColumnAutoWidthCommandTest.java
   sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorksheetTest.java
Modified:
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/CommandFactory.java
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Range.java
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Worksheet.java
   sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/Command.java
Log:
auto column widths
max used ranges for worksheets

Modified: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/CommandFactory.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/CommandFactory.java	2010-07-22 10:43:47 UTC (rev 13475)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/CommandFactory.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -2,6 +2,7 @@
 
 import jxl.write.WritableSheet;
 
+import org.jboss.seam.spreadsheet.model.command.ColumnAutoWidthCommand;
 import org.jboss.seam.spreadsheet.model.command.ColumnWidthCommand;
 import org.jboss.seam.spreadsheet.model.command.Command;
 import org.jboss.seam.spreadsheet.model.command.MergeCellsCommand;
@@ -20,6 +21,8 @@
          return new JxlRowHeightCommand((RowHeightCommand) command, jxlWorksheet);
       case MERGE_CELLS:
          return new JxlMergeCellsCommand((MergeCellsCommand) command, jxlWorksheet);
+      case COLUMN_AUTOWIDTH:
+         return new JxlColumnAutoWidthCommand((ColumnAutoWidthCommand) command, jxlWorksheet);
       }
       return null;
    }

Added: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/JxlColumnAutoWidthCommand.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/JxlColumnAutoWidthCommand.java	                        (rev 0)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/command/JxlColumnAutoWidthCommand.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -0,0 +1,30 @@
+package org.jboss.seam.spreadsheet.jxl.command;
+
+import jxl.CellView;
+import jxl.write.WritableSheet;
+
+import org.jboss.seam.spreadsheet.model.command.ColumnAutoWidthCommand;
+import org.jboss.seam.spreadsheet.model.command.ColumnWidthCommand;
+
+public class JxlColumnAutoWidthCommand extends AbstractJxlCommand
+{
+   private ColumnAutoWidthCommand columnAutoWidthCommand;
+
+   public JxlColumnAutoWidthCommand(ColumnAutoWidthCommand columnAutoWidthCommand, WritableSheet jxlWorksheet)
+   {
+      super(jxlWorksheet);
+      this.columnAutoWidthCommand = columnAutoWidthCommand;
+   }
+
+   @Override
+   public void execute()
+   {
+      for (int column = columnAutoWidthCommand.getColumnStart(); column <= columnAutoWidthCommand.getColumnEnd(); column++)
+      {
+         CellView cellView = jxlWorksheet.getColumnView(column);
+         cellView.setAutosize(true);
+         jxlWorksheet.setColumnView(column, cellView);
+      }
+   }
+
+}

Modified: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Range.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Range.java	2010-07-22 10:43:47 UTC (rev 13475)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Range.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -1,5 +1,7 @@
 package org.jboss.seam.spreadsheet.model;
 
+import org.jboss.seam.spreadsheet.util.HashUtil;
+
 public class Range
 {
    private Coordinate topLeft;
@@ -36,4 +38,32 @@
    {
       return String.format("Range from %s to %s", topLeft, bottomRight);
    }
+
+   @Override
+   public boolean equals(Object other)
+   {
+      if (!(other instanceof Range))
+      {
+         return false;
+      }
+      Range otherRange = (Range) other;
+      if (!HashUtil.same(topLeft, otherRange.getTopLeft()))
+      {
+         return false;
+      }
+      if (!HashUtil.same(bottomRight, otherRange.getBottomRight()))
+      {
+         return false;
+      }
+      return true;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      int hashCode = 0;
+      hashCode += topLeft == null ? 0 : topLeft.hashCode();
+      hashCode += bottomRight == null ? 0 : bottomRight.hashCode();
+      return hashCode;
+   }
 }

Modified: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java	2010-07-22 10:43:47 UTC (rev 13475)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -59,4 +59,5 @@
    {
       this.settings = settings;
    }
+   
 }

Modified: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Worksheet.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Worksheet.java	2010-07-22 10:43:47 UTC (rev 13475)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Worksheet.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -79,4 +79,34 @@
    {
       this.settings = settings;
    }
+
+   public Range getUsedRange()
+   {
+      int minColumn = 0;
+      int minRow = 0;
+      int maxColumn = 0;
+      int maxRow = 0;
+
+      for (Cell cell : cells)
+      {
+         if (minColumn == 0 || cell.getCoordinate().getRow() < minRow)
+         {
+            minRow = cell.getCoordinate().getRow();
+         }
+         if (minRow == 0 || cell.getCoordinate().getRow() > maxRow)
+         {
+            maxRow = cell.getCoordinate().getRow();
+         }
+         if (maxColumn == 0 || cell.getCoordinate().getColumn() < minColumn)
+         {
+            minColumn = cell.getCoordinate().getColumn();
+         }
+         if (maxRow == 0 || cell.getCoordinate().getColumn() > maxColumn)
+         {
+            maxColumn = cell.getCoordinate().getColumn();
+         }
+      }
+      return Range.of(minColumn, minRow, maxColumn, maxRow);
+   }
+
 }

Added: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/ColumnAutoWidthCommand.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/ColumnAutoWidthCommand.java	                        (rev 0)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/ColumnAutoWidthCommand.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -0,0 +1,36 @@
+package org.jboss.seam.spreadsheet.model.command;
+
+public class ColumnAutoWidthCommand implements Command
+{
+   private int columnStart;
+   private int columnEnd;
+
+   public ColumnAutoWidthCommand(int columnStart, int columnEnd)
+   {
+      this.columnStart = columnStart;
+      this.columnEnd = columnEnd;
+   }
+
+   public ColumnAutoWidthCommand(int column)
+   {
+      columnStart = column;
+      columnEnd = column;
+   }
+
+   @Override
+   public CommandId getCommandId()
+   {
+      return CommandId.COLUMN_AUTOWIDTH;
+   }
+
+   public int getColumnStart()
+   {
+      return columnStart;
+   }
+
+   public int getColumnEnd()
+   {
+      return columnEnd;
+   }
+
+}

Modified: sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/Command.java
===================================================================
--- sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/Command.java	2010-07-22 10:43:47 UTC (rev 13475)
+++ sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/command/Command.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -4,7 +4,7 @@
 {
    public enum CommandId
    {
-      COLUMN_WIDTH, ROW_HEIGHT, MERGE_CELLS
+      COLUMN_WIDTH, ROW_HEIGHT, MERGE_CELLS, COLUMN_AUTOWIDTH
    };
 
    public abstract CommandId getCommandId();

Added: sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/ColumnAutoWidthCommandTest.java
===================================================================
--- sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/ColumnAutoWidthCommandTest.java	                        (rev 0)
+++ sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/ColumnAutoWidthCommandTest.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -0,0 +1,46 @@
+package org.jboss.seam.spreadsheet.jxl.command;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.spreadsheet.jxl.writer.JXLSpreadsheetWriter;
+import org.jboss.seam.spreadsheet.jxl.writer.event.Events;
+import org.jboss.seam.spreadsheet.model.Cell;
+import org.jboss.seam.spreadsheet.model.Coordinate;
+import org.jboss.seam.spreadsheet.model.Workbook;
+import org.jboss.seam.spreadsheet.model.Worksheet;
+import org.jboss.seam.spreadsheet.model.command.ColumnAutoWidthCommand;
+import org.jboss.seam.spreadsheet.model.formatting.SpreadsheetTest;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+ at RunWith(Arquillian.class)
+public class ColumnAutoWidthCommandTest extends SpreadsheetTest
+{
+
+   @Deployment
+   public static JavaArchive getArchive()
+   {
+      return ShrinkWrap.create("foo.jar", JavaArchive.class).addClasses(JXLSpreadsheetWriter.class, Events.class).addManifestResource(new ByteArrayAsset("<beans/>".getBytes()), ArchivePaths.create("beans.xml"));
+   }
+
+   @Test
+   public void mergeTest()
+   {
+      Cell cell = Cell.of("Nicklas Karlsson", Coordinate.ORIGO);
+      Worksheet ws = Worksheet.named("foo");
+      ws.getCells().add(cell);
+      ws.getCommands().add(new ColumnAutoWidthCommand(0));
+      Workbook wb = new Workbook();
+      wb.getWorksheets().add(ws);
+      jxl.Workbook jWb = getWorkbook(wb);
+      workbookToFile(wb, "c:/temp/foo.xls");
+      Assert.assertEquals(4096, jWb.getSheet(0).getColumnView(0).getSize());
+      Assert.assertEquals(2048, jWb.getSheet(0).getColumnView(1).getSize());
+   }
+}

Added: sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorksheetTest.java
===================================================================
--- sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorksheetTest.java	                        (rev 0)
+++ sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorksheetTest.java	2010-07-22 10:54:46 UTC (rev 13476)
@@ -0,0 +1,36 @@
+package org.jboss.seam.spreadsheet.model;
+
+import junit.framework.Assert;
+
+import org.jboss.seam.spreadsheet.model.formatting.SpreadsheetTest;
+import org.junit.Test;
+
+public class WorksheetTest extends SpreadsheetTest
+{
+   
+   @Test
+   public void freshWorksheetStateTest()
+   {
+      Worksheet worksheet = Worksheet.named("foo");
+      Assert.assertTrue(worksheet.getCellFormatRules().isEmpty());
+      Assert.assertTrue(worksheet.getCells().isEmpty());
+      Assert.assertTrue(worksheet.getCommands().isEmpty());
+      Assert.assertNull(worksheet.getSettings());
+   }
+   
+   @Test
+   public void usedRangeTest() {
+      Worksheet worksheet = Worksheet.named("foo");
+      Assert.assertEquals(Range.of(0, 0, 0, 0), worksheet.getUsedRange());
+   }
+   
+   @Test
+   public void usedRangeTest2() {
+      Worksheet worksheet = Worksheet.named("foo");
+      worksheet.getCells().add(Cell.of("foo", Coordinate.of(10, 10)));
+      worksheet.getCells().add(Cell.of("bar", Coordinate.of(20, 20)));
+      Assert.assertEquals(Range.of(10, 10, 20, 20), worksheet.getUsedRange());
+   }
+   
+   
+}



More information about the seam-commits mailing list