001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.model;
019:
020: import org.apache.poi.ddf.EscherDgRecord;
021: import org.apache.poi.ddf.EscherDggRecord;
022:
023: import java.util.List;
024: import java.util.ArrayList;
025:
026: /**
027: * Provides utilities to manage drawing groups.
028: *
029: * @author Glen Stampoultzis (glens at apache.org)
030: */
031: public class DrawingManager2 {
032: EscherDggRecord dgg;
033: List drawingGroups = new ArrayList();
034:
035: public DrawingManager2(EscherDggRecord dgg) {
036: this .dgg = dgg;
037: }
038:
039: public EscherDgRecord createDgRecord() {
040: EscherDgRecord dg = new EscherDgRecord();
041: dg.setRecordId(EscherDgRecord.RECORD_ID);
042: short dgId = findNewDrawingGroupId();
043: dg.setOptions((short) (dgId << 4));
044: dg.setNumShapes(0);
045: dg.setLastMSOSPID(-1);
046: drawingGroups.add(dg);
047: dgg.addCluster(dgId, 0);
048: dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
049: return dg;
050: }
051:
052: /**
053: * Allocates new shape id for the new drawing group id.
054: *
055: * @return a new shape id.
056: */
057: public int allocateShapeId(short drawingGroupId) {
058: dgg.setNumShapesSaved(dgg.getNumShapesSaved() + 1);
059:
060: // Add to existing cluster if space available
061: for (int i = 0; i < dgg.getFileIdClusters().length; i++) {
062: EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i];
063: if (c.getDrawingGroupId() == drawingGroupId
064: && c.getNumShapeIdsUsed() != 1024) {
065: int result = c.getNumShapeIdsUsed() + (1024 * (i + 1));
066: c.incrementShapeId();
067: EscherDgRecord dg = getDrawingGroup(drawingGroupId);
068: dg.setNumShapes(dg.getNumShapes() + 1);
069: dg.setLastMSOSPID(result);
070: if (result >= dgg.getShapeIdMax())
071: dgg.setShapeIdMax(result + 1);
072: return result;
073: }
074: }
075:
076: // Create new cluster
077: dgg.addCluster(drawingGroupId, 0);
078: dgg.getFileIdClusters()[dgg.getFileIdClusters().length - 1]
079: .incrementShapeId();
080: EscherDgRecord dg = getDrawingGroup(drawingGroupId);
081: dg.setNumShapes(dg.getNumShapes() + 1);
082: int result = (1024 * dgg.getFileIdClusters().length);
083: dg.setLastMSOSPID(result);
084: if (result >= dgg.getShapeIdMax())
085: dgg.setShapeIdMax(result + 1);
086: return result;
087: }
088:
089: //////////// Non-public methods /////////////
090: short findNewDrawingGroupId() {
091: short dgId = 1;
092: while (drawingGroupExists(dgId))
093: dgId++;
094: return dgId;
095: }
096:
097: EscherDgRecord getDrawingGroup(int drawingGroupId) {
098: return (EscherDgRecord) drawingGroups.get(drawingGroupId - 1);
099: }
100:
101: boolean drawingGroupExists(short dgId) {
102: for (int i = 0; i < dgg.getFileIdClusters().length; i++) {
103: if (dgg.getFileIdClusters()[i].getDrawingGroupId() == dgId)
104: return true;
105: }
106: return false;
107: }
108:
109: int findFreeSPIDBlock() {
110: int max = dgg.getShapeIdMax();
111: int next = ((max / 1024) + 1) * 1024;
112: return next;
113: }
114:
115: public EscherDggRecord getDgg() {
116: return dgg;
117: }
118:
119: }
|