PageRenderTime 501ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/model/DrawingManager.java

http://github.com/openmicroscopy/bioformats
Java | 176 lines | 94 code | 17 blank | 65 comment | 14 complexity | af6e616851edfd46b96138b7817d24d2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.model;
  38. import loci.poi.ddf.EscherDggRecord;
  39. import loci.poi.ddf.EscherDgRecord;
  40. import java.util.Map;
  41. import java.util.HashMap;
  42. /**
  43. * Provides utilities to manage drawing groups.
  44. *
  45. * @author Glen Stampoultzis (glens at apache.org)
  46. */
  47. public class DrawingManager
  48. {
  49. EscherDggRecord dgg;
  50. Map dgMap = new HashMap(); // key = Short(drawingId), value=EscherDgRecord
  51. public DrawingManager( EscherDggRecord dgg )
  52. {
  53. this.dgg = dgg;
  54. }
  55. public EscherDgRecord createDgRecord()
  56. {
  57. EscherDgRecord dg = new EscherDgRecord();
  58. dg.setRecordId( EscherDgRecord.RECORD_ID );
  59. short dgId = findNewDrawingGroupId();
  60. dg.setOptions( (short) ( dgId << 4 ) );
  61. dg.setNumShapes( 0 );
  62. dg.setLastMSOSPID( -1 );
  63. dgg.addCluster( dgId, 0 );
  64. dgg.setDrawingsSaved( dgg.getDrawingsSaved() + 1 );
  65. dgMap.put( new Short( dgId ), dg );
  66. return dg;
  67. }
  68. /**
  69. * Allocates new shape id for the new drawing group id.
  70. *
  71. * @return a new shape id.
  72. */
  73. public int allocateShapeId(short drawingGroupId)
  74. {
  75. // Get the last shape id for this drawing group.
  76. EscherDgRecord dg = (EscherDgRecord) dgMap.get(new Short(drawingGroupId));
  77. int lastShapeId = dg.getLastMSOSPID();
  78. // Have we run out of shapes for this cluster?
  79. int newShapeId = 0;
  80. if (lastShapeId % 1024 == 1023)
  81. {
  82. // Yes:
  83. // Find the starting shape id of the next free cluster
  84. newShapeId = findFreeSPIDBlock();
  85. // Create a new cluster in the dgg record.
  86. dgg.addCluster(drawingGroupId, 1);
  87. }
  88. else
  89. {
  90. // No:
  91. // Find the cluster for this drawing group with free space.
  92. for (int i = 0; i < dgg.getFileIdClusters().length; i++)
  93. {
  94. EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i];
  95. if (c.getDrawingGroupId() == drawingGroupId)
  96. {
  97. if (c.getNumShapeIdsUsed() != 1024)
  98. {
  99. // Increment the number of shapes used for this cluster.
  100. c.incrementShapeId();
  101. }
  102. }
  103. // If the last shape id = -1 then we know to find a free block;
  104. if (dg.getLastMSOSPID() == -1)
  105. {
  106. newShapeId = findFreeSPIDBlock();
  107. }
  108. else
  109. {
  110. // The new shape id to be the last shapeid of this cluster + 1
  111. newShapeId = dg.getLastMSOSPID() + 1;
  112. }
  113. }
  114. }
  115. // Increment the total number of shapes used in the dgg.
  116. dgg.setNumShapesSaved(dgg.getNumShapesSaved() + 1);
  117. // Is the new shape id >= max shape id for dgg?
  118. if (newShapeId >= dgg.getShapeIdMax())
  119. {
  120. // Yes:
  121. // Set the max shape id = new shape id + 1
  122. dgg.setShapeIdMax(newShapeId + 1);
  123. }
  124. // Set last shape id for this drawing group.
  125. dg.setLastMSOSPID(newShapeId);
  126. // Increased the number of shapes used for this drawing group.
  127. dg.incrementShapeCount();
  128. return newShapeId;
  129. }
  130. //////////// Non-public methods /////////////
  131. short findNewDrawingGroupId()
  132. {
  133. short dgId = 1;
  134. while ( drawingGroupExists( dgId ) )
  135. dgId++;
  136. return dgId;
  137. }
  138. boolean drawingGroupExists( short dgId )
  139. {
  140. for ( int i = 0; i < dgg.getFileIdClusters().length; i++ )
  141. {
  142. if ( dgg.getFileIdClusters()[i].getDrawingGroupId() == dgId )
  143. return true;
  144. }
  145. return false;
  146. }
  147. int findFreeSPIDBlock()
  148. {
  149. int max = dgg.getShapeIdMax();
  150. int next = ( ( max / 1024 ) + 1 ) * 1024;
  151. return next;
  152. }
  153. public EscherDggRecord getDgg()
  154. {
  155. return dgg;
  156. }
  157. }