Logo Search packages:      
Sourcecode: jexcelapi version File versions  Download package

CheckBox.java

/*********************************************************************
*
*      Copyright (C) 2009 Andrew Khan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/

package jxl.biff.drawing;

import java.io.IOException;

import jxl.common.Assert;
import jxl.common.Logger;

import jxl.WorkbookSettings;
import jxl.biff.ContinueRecord;
import jxl.write.biff.File;

/**
 * Contains the various biff records used to copy a CheckBox (from the
 * Form toolbox) between workbook
 */
00035 public class CheckBox implements DrawingGroupObject
{
  /**
   * The logger
   */
00040   private static Logger logger = Logger.getLogger(CheckBox.class);

  /**
   * The spContainer that was read in
   */
00045   private EscherContainer readSpContainer;

  /**
   * The spContainer that was generated
   */
00050   private EscherContainer spContainer;

  /**
   * The MsoDrawingRecord associated with the drawing
   */
00055   private MsoDrawingRecord msoDrawingRecord;

  /**
   * The ObjRecord associated with the drawing
   */
00060   private ObjRecord objRecord;

  /**
   * Initialized flag
   */
00065   private boolean initialized = false;

  /**
   * The object id, assigned by the drawing group
   */
00070   private int objectId;

  /**
   * The blip id
   */
00075   private int blipId;

  /**
   * The shape id
   */
00080   private int shapeId;

  /**
   * The column
   */
00085   private int column;

  /**
   * The row position of the image
   */
00090   private int row;

  /**
   * The width of the image in cells
   */
00095   private double width;

  /**
   * The height of the image in cells
   */
00100   private double height;

  /**
   * The number of places this drawing is referenced
   */
00105   private int referenceCount;

  /**
   * The top level escher container
   */
00110   private EscherContainer escherData;

  /**
   * Where this image came from (read, written or a copy)
   */
00115   private Origin origin;

  /**
   * The drawing group for all the images
   */
00120   private DrawingGroup drawingGroup;

  /**
   * The drawing data
   */
00125   private DrawingData drawingData;

  /**
   * The type of this drawing object
   */
00130   private ShapeType type;

  /**
   * The drawing position on the sheet
   */
00135   private int drawingNumber;

  /**
   * An mso drawing record, which sometimes appears
   */
00140   private MsoDrawingRecord mso;

  /**
   * The text object record
   */
00145   private TextObjectRecord txo;

  /**
   * Text data from the first continue record
   */
00150   private ContinueRecord text;

  /**
   * Formatting data from the second continue record
   */
00155   private ContinueRecord formatting;

  /**
   * The workbook settings
   */
00160   private WorkbookSettings workbookSettings;

  /**
   * Constructor used when reading images
   *
   * @param mso the drawing record
   * @param obj the object record
   * @param dd the drawing data for all drawings on this sheet
   * @param dg the drawing group
   * @param ws the workbook settings
   */
00171   public CheckBox(MsoDrawingRecord mso, ObjRecord obj, DrawingData dd,
                  DrawingGroup dg, WorkbookSettings ws)
  {
    drawingGroup = dg;
    msoDrawingRecord = mso;
    drawingData = dd;
    objRecord = obj;
    initialized = false;
    workbookSettings = ws;
    origin = Origin.READ;
    drawingData.addData(msoDrawingRecord.getData());
    drawingNumber = drawingData.getNumDrawings() - 1;
    drawingGroup.addDrawing(this);

    Assert.verify(mso != null && obj != null);

    initialize();
  }

  /**
   * Copy constructor used to copy drawings from read to write
   *
   * @param dgo the drawing group object
   * @param dg the drawing group
   * @param ws the workbook settings
   */
00197   public CheckBox(DrawingGroupObject dgo,
                  DrawingGroup dg,
                  WorkbookSettings ws)
  {
    CheckBox d = (CheckBox) dgo;
    Assert.verify(d.origin == Origin.READ);
    msoDrawingRecord = d.msoDrawingRecord;
    objRecord = d.objRecord;
    initialized = false;
    origin = Origin.READ;
    drawingData = d.drawingData;
    drawingGroup = dg;
    drawingNumber = d.drawingNumber;
    drawingGroup.addDrawing(this);
    mso = d.mso;
    txo = d.txo;
    text = d.text;
    formatting = d.formatting;
    workbookSettings = ws;
  }

  /**
   * Constructor invoked when writing images
   */
00221   public CheckBox()
  {
    initialized = true;
    origin = Origin.WRITE;
    referenceCount = 1;
    type = ShapeType.HOST_CONTROL;
  }

  /**
   * Initializes the member variables from the Escher stream data
   */
00232   private void initialize()
  {
    readSpContainer = drawingData.getSpContainer(drawingNumber);
    Assert.verify(readSpContainer != null);

    EscherRecord[] children = readSpContainer.getChildren();

    Sp sp = (Sp) readSpContainer.getChildren()[0];
    objectId = objRecord.getObjectId();
    shapeId = sp.getShapeId();
    type = ShapeType.getType(sp.getShapeType());

    if (type == ShapeType.UNKNOWN)
    {
      logger.warn("Unknown shape type");
    }

    ClientAnchor clientAnchor = null;
    for (int i = 0; i < children.length && clientAnchor == null; i++)
    {
      if (children[i].getType() == EscherRecordType.CLIENT_ANCHOR)
      {
        clientAnchor = (ClientAnchor) children[i];
      }
    }

    if (clientAnchor == null)
    {
      logger.warn("Client anchor not found");
    }
    else
    {
      column = (int) clientAnchor.getX1();
      row = (int) clientAnchor.getY1();
    }

    initialized = true;
  }


  /**
   * Sets the object id.  Invoked by the drawing group when the object is
   * added to id
   *
   * @param objid the object id
   * @param bip the blip id
   * @param sid the shape id
   */
00280   public final void setObjectId(int objid, int bip, int sid)
  {
    objectId = objid;
    blipId = bip;
    shapeId = sid;

    if (origin == Origin.READ)
    {
      origin = Origin.READ_WRITE;
    }
  }

  /**
   * Accessor for the object id
   *
   * @return the object id
   */
00297   public final int getObjectId()
  {
    if (!initialized)
    {
      initialize();
    }

    return objectId;
  }

  /**
   * Accessor for the shape id
   *
   * @return the object id
   */
00312   public final int getShapeId()
  {
    if (!initialized)
    {
      initialize();
    }

    return shapeId;
  }

  /**
   * Accessor for the blip id
   *
   * @return the blip id
   */
00327   public final int getBlipId()
  {
    if (!initialized)
    {
      initialize();
    }

    return blipId;
  }

  /**
   * Gets the drawing record which was read in
   *
   * @return the drawing record
   */
00342   public MsoDrawingRecord  getMsoDrawingRecord()
  {
    return msoDrawingRecord;
  }

  /**
   * Creates the main Sp container for the drawing
   *
   * @return the SP container
   */
00352   public EscherContainer getSpContainer()
  {
    if (!initialized)
    {
      initialize();
    }

    if (origin == Origin.READ)
    {
      return getReadSpContainer();
    }

    SpContainer spc = new SpContainer();
    Sp sp = new Sp(type, shapeId, 2560);
    spc.add(sp);
    Opt opt = new Opt();
    opt.addProperty(127, false, false, 17039620);
    opt.addProperty(191, false, false, 524296);
    opt.addProperty(511, false, false, 524288);
    opt.addProperty(959, false, false, 131072);
    //    opt.addProperty(260, true, false, blipId);
    //    opt.addProperty(261, false, false, 36);
    spc.add(opt);

    ClientAnchor clientAnchor = new ClientAnchor(column,
                                                 row,
                                                 column + 1,
                                                 row + 1, 
                                                 0x1);
    spc.add(clientAnchor);
    ClientData clientData = new ClientData();
    spc.add(clientData);

    return spc;
  }

  /**
   * Sets the drawing group for this drawing.  Called by the drawing group
   * when this drawing is added to it
   *
   * @param dg the drawing group
   */
00394   public void setDrawingGroup(DrawingGroup dg)
  {
    drawingGroup = dg;
  }

  /**
   * Accessor for the drawing group
   *
   * @return the drawing group
   */
00404   public DrawingGroup getDrawingGroup()
  {
    return drawingGroup;
  }

  /**
   * Gets the origin of this drawing
   *
   * @return where this drawing came from
   */
00414   public Origin getOrigin()
  {
    return origin;
  }

  /**
   * Accessor for the reference count on this drawing
   *
   * @return the reference count
   */
00424   public int getReferenceCount()
  {
    return referenceCount;
  }

  /**
   * Sets the new reference count on the drawing
   *
   * @param r the new reference count
   */
00434   public void setReferenceCount(int r)
  {
    referenceCount = r;
  }

  /**
   * Accessor for the column of this drawing
   *
   * @return the column
   */
00444   public double getX()
  {
    if (!initialized)
    {
      initialize();
    }
    return column;
  }

  /**
   * Sets the column position of this drawing.  Used when inserting/removing
   * columns from the spreadsheet
   *
   * @param x the column
   */
00459   public void setX(double x)
  {
    if (origin == Origin.READ)
    {
      if (!initialized)
      {
        initialize();
      }
      origin = Origin.READ_WRITE;
    }

    column = (int) x;
  }

  /**
   * Accessor for the row of this drawing
   *
   * @return the row
   */
00478   public double getY()
  {
    if (!initialized)
    {
      initialize();
    }

    return row;
  }

  /**
   * Accessor for the row of the drawing
   *
   * @param y the row
   */
00493   public void setY(double y)
  {
    if (origin == Origin.READ)
    {
      if (!initialized)
      {
        initialize();
      }
      origin = Origin.READ_WRITE;
    }

    row = (int) y;
  }


  /**
   * Accessor for the width of this drawing
   *
   * @return the number of columns spanned by this image
   */
00513   public double getWidth()
  {
    if (!initialized)
    {
      initialize();
    }

    return width;
  }

  /**
   * Accessor for the width
   *
   * @param w the number of columns to span
   */
00528   public void setWidth(double w)
  {
    if (origin == Origin.READ)
    {
      if (!initialized)
      {
        initialize();
      }
      origin = Origin.READ_WRITE;
    }

    width = w;
  }

  /**
   * Accessor for the height of this drawing
   *
   * @return the number of rows spanned by this image
   */
00547   public double getHeight()
  {
    if (!initialized)
    {
      initialize();
    }

    return height;
  }

  /**
   * Accessor for the height of this drawing
   *
   * @param h the number of rows spanned by this image
   */
00562   public void setHeight(double h)
  {
    if (origin == Origin.READ)
    {
      if (!initialized)
      {
        initialize();
      }
      origin = Origin.READ_WRITE;
    }

    height = h;
  }


  /**
   * Gets the SpContainer that was read in
   *
   * @return the read sp container
   */
00582   private EscherContainer getReadSpContainer()
  {
    if (!initialized)
    {
      initialize();
    }

    return readSpContainer;
  }

  /**
   * Accessor for the image data
   *
   * @return the image data
   */
00597   public byte[] getImageData()
  {
    Assert.verify(origin == Origin.READ || origin == Origin.READ_WRITE);

    if (!initialized)
    {
      initialize();
    }

    return drawingGroup.getImageData(blipId);
  }

  /**
   * Accessor for the type
   *
   * @return the type
   */
00614   public ShapeType getType()
  {
    return type;
  }

  /**
   * Accessor for the image data
   *
   * @return the image data
   */
00624   public byte[] getImageBytes()
  {
    Assert.verify(false);
    return null;
  }

  /**
   * Accessor for the image file path.  Normally this is the absolute path
   * of a file on the directory system, but if this drawing was constructed
   * using an byte[] then the blip id is returned
   *
   * @return the image file path, or the blip id
   */
00637   public String getImageFilePath()
  {
    Assert.verify(false);
    return null;
  }

  /**
   * Writes out the additional records for a combo box
   *
   * @param outputFile the output file
   * @exception IOException
   */
00649   public void writeAdditionalRecords(File outputFile) throws IOException
  {
    if (origin == Origin.READ)
    {
      outputFile.write(objRecord);

      if (mso != null)
      {
        outputFile.write(mso);
      }
      outputFile.write(txo);
      outputFile.write(text);
      if (formatting != null)
      {
        outputFile.write(formatting);
      }
      return;
    }

    // Create the obj record
    ObjRecord objrec = new ObjRecord(objectId,
                                     ObjRecord.CHECKBOX);

    outputFile.write(objrec);

    logger.warn("Writing of additional records for checkboxes not " +
                "implemented");
  }

  /**
   * Writes any records that need to be written after all the drawing group
   * objects have been written
   * Writes out all the note records
   *
   * @param outputFile the output file
   */
00685   public void writeTailRecords(File outputFile)
  {
  }

  /**
   * Accessor for the row
   *
   * @return  the row
   */
00694   public int getRow()
  {
    return 0;
  }

  /**
   * Accessor for the column
   *
   * @return the column
   */
00704   public int getColumn()
  {
    return 0;
  }

  /**
   * Hashing algorithm
   *
   * @return  the hash code
   */
00714   public int hashCode()
  {
    return getClass().getName().hashCode();
  }

  /**
   * Accessor for the first drawing on the sheet.  This is used when
   * copying unmodified sheets to indicate that this drawing contains
   * the first time Escher gubbins
   *
   * @return TRUE if this MSORecord is the first drawing on the sheet
   */
00726   public boolean isFirst()
  {
    return msoDrawingRecord.isFirst();
  }

  /**
   * Queries whether this object is a form object.  Form objects have their
   * drawings records spread over TXO and CONTINUE records and
   * require special handling
   *
   * @return TRUE if this is a form object, FALSE otherwise
   */
00738   public boolean isFormObject()
  {
    return false;
  }

  /**
   * Sets the text object
   *
   * @param t the text object record
   */
00748   public void setTextObject(TextObjectRecord t)
  {
    txo = t;
  }

  /**
   * Sets the text data
   *
   * @param t continuation record
   */
00758   public void setText(ContinueRecord t)
  {
    text = t;
  }

  /**
   * Sets the formatting
   *
   * @param t continue record
   */
00768   public void setFormatting(ContinueRecord t)
  {
    formatting = t;
  }

  /**
   * The drawing record
   *
   * @param d the drawing record
   */
00778   public void addMso(MsoDrawingRecord d)
  {
    mso = d;
    drawingData.addRawData(mso.getData());
  }

}




Generated by  Doxygen 1.6.0   Back to index