⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawinggroup.java

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
*
*      Copyright (C) 2004 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import common.Assert;
import common.Logger;

import jxl.read.biff.Record;
import jxl.write.biff.File;

/**
 * This class contains the Excel picture data in Escher format for the
 * entire workbook
 */
public class DrawingGroup implements EscherStream
{
  /**
   * The logger
   */
  private static Logger logger = Logger.getLogger(DrawingGroup.class);

  /**
   * The escher data read in from file
   */
  private byte[] drawingData;

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

  /**
   * The Bstore container, which contains all the drawing data
   */
  private BStoreContainer bstoreContainer;

  /**
   * The initialized flag
   */
  private boolean initialized;

  /**
   * The list of user added drawings
   */
  private ArrayList drawings;

  /**
   * The number of blips
   */
  private int numBlips;

  /**
   * The number of charts
   */
  private int numCharts;

  /**
   * The number of shape ids used on the second Dgg cluster
   */
  private int drawingGroupId;

  /**
   * Flag which indicates that at least one of the drawings has been omitted
   * from the worksheet
   */
  private boolean drawingsOmitted;

  /**
   * The origin of this drawing group
   */
  private Origin origin;

  /**
   * A hash map of images keyed on the file path, containing the
   * reference count
   */
  private HashMap imageFiles;

  /**
   * A count of the next available object id
   */
  private int maxObjectId;

  /**
   * The maximum shape id so far encountered
   */
  private int maxShapeId;

  /**
   * Constructor
   *
   * @param o the origin of this drawing group
   */
  public DrawingGroup(Origin o)
  {
    origin = o;
    initialized = o == Origin.WRITE ? true : false;
    drawings = new ArrayList();
    imageFiles = new HashMap();
    drawingsOmitted = false;
    maxObjectId = 1;
    maxShapeId = 1024;
  }

  /**
   * Copy constructor
   * Uses a shallow copy for most things, since as soon as anything
   * is changed, the drawing group is invalidated and all the data blocks
   * regenerated
   *
   * @param dg the drawing group to copy
   */
  public DrawingGroup(DrawingGroup dg)
  {
    drawingData = dg.drawingData;
    escherData = dg.escherData;
    bstoreContainer = dg.bstoreContainer;
    initialized = dg.initialized;
    drawingData = dg.drawingData;
    escherData = dg.escherData;
    bstoreContainer = dg.bstoreContainer;
    numBlips = dg.numBlips;
    numCharts = dg.numCharts;
    drawingGroupId = dg.drawingGroupId;
    drawingsOmitted = dg.drawingsOmitted;
    origin = dg.origin;
    imageFiles = (HashMap) dg.imageFiles.clone();
    maxObjectId = dg.maxObjectId;
    maxShapeId = dg.maxShapeId;

    // Create this as empty, because all drawings will get added later
    // as part of the sheet copy process
    drawings = new ArrayList();
  }

  /**

  /**
   * Adds in a drawing group record to this drawing group.  The binary
   * data is extracted from the drawing group and added to a single
   * byte array
   *
   * @param mso the drawing group record to add
   */
  public void add(MsoDrawingGroupRecord mso)
  {
    addData(mso.getData());
  }

  /**
   * Adds a continue record to this drawing group.  the binary data is
   * extracted and appended to the byte array
   *
   * @param cont the continue record
   */
  public void add(Record cont)
  {
    addData(cont.getData());
  }

  /**
   * Adds the mso record data to the drawing data
   *
   * @param msodata the raw mso data
   */
  private void addData(byte[] msodata)
  {
    if (drawingData == null)
    {
      drawingData = new byte[msodata.length];
      System.arraycopy(msodata, 0, drawingData, 0, msodata.length);
      return;
    }

    // Grow the array
    byte[] newdata = new byte[drawingData.length + msodata.length];
    System.arraycopy(drawingData, 0, newdata, 0, drawingData.length);
    System.arraycopy(msodata, 0, newdata, drawingData.length, msodata.length);
    drawingData = newdata;
  }

  /**
   * Adds a drawing to the drawing group
   *
   * @param d the drawing to add
   */
  final void addDrawing(DrawingGroupObject d)
  {
    drawings.add(d);
    maxObjectId = Math.max(maxObjectId, d.getObjectId());
    maxShapeId = Math.max(maxShapeId, d.getShapeId());
  }

  /**
   * Adds a  chart to the drawing group
   *
   * @param c the chart
   */
  public void add(Chart c)
  {
    numCharts++;
  }

  /**
   * Adds a drawing from the public, writable interface
   *
   * @param d the drawing to add
   */
  public void add(DrawingGroupObject d)
  {
    if (origin == Origin.READ)
    {
      origin = Origin.READ_WRITE;
      BStoreContainer bsc = getBStoreContainer(); // force initialization
      Dgg dgg = (Dgg) escherData.getChildren()[0];
      drawingGroupId = dgg.getCluster(1).drawingGroupId - numBlips - 1;
      numBlips = bsc != null ? bsc.getNumBlips() : 0;

      if (bsc != null)
      {
        Assert.verify(numBlips == bsc.getNumBlips());
      }
    }

    if (!(d instanceof Drawing))
    {
      // Assign a new object id and add it to the list
      //      drawings.add(d);
      maxObjectId++;
      maxShapeId++;
      d.setDrawingGroup(this);
      d.setObjectId(maxObjectId, numBlips + 1, maxShapeId);
      if (drawings.size() > maxObjectId)
      {
        logger.warn("drawings length " + drawings.size() +
                    " exceeds the max object id " + maxObjectId);
      }
      //      numBlips++;
      return;
    }

    Drawing drawing = (Drawing) d;

    // See if this is referenced elsewhere
    Drawing refImage =
      (Drawing) imageFiles.get(d.getImageFilePath());

    if (refImage == null)
    {
      // There are no other references to this drawing, so assign
      // a new object id and put it on the hash map
      maxObjectId++;
      maxShapeId++;
      drawings.add(drawing);
      drawing.setDrawingGroup(this);
      drawing.setObjectId(maxObjectId, numBlips + 1, maxShapeId);
      numBlips++;
      imageFiles.put(drawing.getImageFilePath(), drawing);
    }
    else
    {
      // This drawing is used elsewhere in the workbook.  Increment the
      // reference count on the drawing, and set the object id of the drawing
      // passed in
      refImage.setReferenceCount(refImage.getReferenceCount() + 1);
      drawing.setDrawingGroup(this);
      drawing.setObjectId(refImage.getObjectId(),
                          refImage.getBlipId(),
                          refImage.getShapeId());
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -