KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > model > DrawingManager


1 /* ====================================================================
2    Copyright 2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

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 JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 /**
26  * Provides utilities to manage drawing groups.
27  *
28  * @author Glen Stampoultzis (glens at apache.org)
29  */

30 public class DrawingManager
31 {
32     EscherDggRecord dgg;
33     Map JavaDoc dgMap = new HashMap JavaDoc(); // key = Short(drawingId), value=EscherDgRecord
34

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 JavaDoc( dgId ), dg );
51         return dg;
52     }
53
54     /**
55      * Allocates new shape id for the new drawing group id.
56      *
57      * @return a new shape id.
58      */

59     public int allocateShapeId(short drawingGroupId)
60     {
61         // Get the last shape id for this drawing group.
62
EscherDgRecord dg = (EscherDgRecord) dgMap.get(new Short JavaDoc(drawingGroupId));
63         int lastShapeId = dg.getLastMSOSPID();
64
65
66         // Have we run out of shapes for this cluster?
67
int newShapeId = 0;
68         if (lastShapeId % 1024 == 1023)
69         {
70             // Yes:
71
// Find the starting shape id of the next free cluster
72
newShapeId = findFreeSPIDBlock();
73                 // Create a new cluster in the dgg record.
74
dgg.addCluster(drawingGroupId, 1);
75         }
76         else
77         {
78             // No:
79
// Find the cluster for this drawing group with free space.
80
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                         // Increment the number of shapes used for this cluster.
88
c.incrementShapeId();
89                     }
90                 }
91                 // If the last shape id = -1 then we know to find a free block;
92
if (dg.getLastMSOSPID() == -1)
93                 {
94                     newShapeId = findFreeSPIDBlock();
95                 }
96                 else
97                 {
98                     // The new shape id to be the last shapeid of this cluster + 1
99
newShapeId = dg.getLastMSOSPID() + 1;
100                 }
101             }
102         }
103         // Increment the total number of shapes used in the dgg.
104
dgg.setNumShapesSaved(dgg.getNumShapesSaved() + 1);
105         // Is the new shape id >= max shape id for dgg?
106
if (newShapeId >= dgg.getShapeIdMax())
107         {
108             // Yes:
109
// Set the max shape id = new shape id + 1
110
dgg.setShapeIdMax(newShapeId + 1);
111         }
112         // Set last shape id for this drawing group.
113
dg.setLastMSOSPID(newShapeId);
114         // Increased the number of shapes used for this drawing group.
115
dg.incrementShapeCount();
116
117
118         return newShapeId;
119     }
120
121     //////////// Non-public methods /////////////
122
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