1 16 17 package org.apache.poi.hssf.model; 18 19 import org.apache.poi.ddf.EscherDggRecord; 20 import org.apache.poi.ddf.EscherDgRecord; 21 22 import java.util.Map ; 23 import java.util.HashMap ; 24 25 30 public class DrawingManager 31 { 32 EscherDggRecord dgg; 33 Map dgMap = new HashMap (); 35 public DrawingManager( EscherDggRecord dgg ) 36 { 37 this.dgg = dgg; 38 } 39 40 public EscherDgRecord createDgRecord() 41 { 42 EscherDgRecord dg = new EscherDgRecord(); 43 dg.setRecordId( EscherDgRecord.RECORD_ID ); 44 short dgId = findNewDrawingGroupId(); 45 dg.setOptions( (short) ( dgId << 4 ) ); 46 dg.setNumShapes( 0 ); 47 dg.setLastMSOSPID( -1 ); 48 dgg.addCluster( dgId, 0 ); 49 dgg.setDrawingsSaved( dgg.getDrawingsSaved() + 1 ); 50 dgMap.put( new Short ( dgId ), dg ); 51 return dg; 52 } 53 54 59 public int allocateShapeId(short drawingGroupId) 60 { 61 EscherDgRecord dg = (EscherDgRecord) dgMap.get(new Short (drawingGroupId)); 63 int lastShapeId = dg.getLastMSOSPID(); 64 65 66 int newShapeId = 0; 68 if (lastShapeId % 1024 == 1023) 69 { 70 newShapeId = findFreeSPIDBlock(); 73 dgg.addCluster(drawingGroupId, 1); 75 } 76 else 77 { 78 for (int i = 0; i < dgg.getFileIdClusters().length; i++) 81 { 82 EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i]; 83 if (c.getDrawingGroupId() == drawingGroupId) 84 { 85 if (c.getNumShapeIdsUsed() != 1024) 86 { 87 c.incrementShapeId(); 89 } 90 } 91 if (dg.getLastMSOSPID() == -1) 93 { 94 newShapeId = findFreeSPIDBlock(); 95 } 96 else 97 { 98 newShapeId = dg.getLastMSOSPID() + 1; 100 } 101 } 102 } 103 dgg.setNumShapesSaved(dgg.getNumShapesSaved() + 1); 105 if (newShapeId >= dgg.getShapeIdMax()) 107 { 108 dgg.setShapeIdMax(newShapeId + 1); 111 } 112 dg.setLastMSOSPID(newShapeId); 114 dg.incrementShapeCount(); 116 117 118 return newShapeId; 119 } 120 121 short findNewDrawingGroupId() 123 { 124 short dgId = 1; 125 while ( drawingGroupExists( dgId ) ) 126 dgId++; 127 return dgId; 128 } 129 130 boolean drawingGroupExists( short dgId ) 131 { 132 for ( int i = 0; i < dgg.getFileIdClusters().length; i++ ) 133 { 134 if ( dgg.getFileIdClusters()[i].getDrawingGroupId() == dgId ) 135 return true; 136 } 137 return false; 138 } 139 140 int findFreeSPIDBlock() 141 { 142 int max = dgg.getShapeIdMax(); 143 int next = ( ( max / 1024 ) + 1 ) * 1024; 144 return next; 145 } 146 147 public EscherDggRecord getDgg() 148 { 149 return dgg; 150 } 151 152 } 153 | Popular Tags |