PageRenderTime 503ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/openmicroscopy/bioformats
Java | 154 lines | 85 code | 18 blank | 51 comment | 11 complexity | 59154360b890e81907d275266ee0785f 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.EscherDgRecord;
  39. import loci.poi.ddf.EscherDggRecord;
  40. import java.util.List;
  41. import java.util.ArrayList;
  42. /**
  43. * Provides utilities to manage drawing groups.
  44. *
  45. * @author Glen Stampoultzis (glens at apache.org)
  46. */
  47. public class DrawingManager2
  48. {
  49. EscherDggRecord dgg;
  50. List drawingGroups = new ArrayList( );
  51. public DrawingManager2( 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. drawingGroups.add(dg);
  64. dgg.addCluster( dgId, 0 );
  65. dgg.setDrawingsSaved( dgg.getDrawingsSaved() + 1 );
  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. dgg.setNumShapesSaved( dgg.getNumShapesSaved() + 1 );
  76. // Add to existing cluster if space available
  77. for (int i = 0; i < dgg.getFileIdClusters().length; i++)
  78. {
  79. EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i];
  80. if (c.getDrawingGroupId() == drawingGroupId && c.getNumShapeIdsUsed() != 1024)
  81. {
  82. int result = c.getNumShapeIdsUsed() + (1024 * (i+1));
  83. c.incrementShapeId();
  84. EscherDgRecord dg = getDrawingGroup(drawingGroupId);
  85. dg.setNumShapes( dg.getNumShapes() + 1 );
  86. dg.setLastMSOSPID( result );
  87. if (result >= dgg.getShapeIdMax())
  88. dgg.setShapeIdMax( result + 1 );
  89. return result;
  90. }
  91. }
  92. // Create new cluster
  93. dgg.addCluster( drawingGroupId, 0 );
  94. dgg.getFileIdClusters()[dgg.getFileIdClusters().length-1].incrementShapeId();
  95. EscherDgRecord dg = getDrawingGroup(drawingGroupId);
  96. dg.setNumShapes( dg.getNumShapes() + 1 );
  97. int result = (1024 * dgg.getFileIdClusters().length);
  98. dg.setLastMSOSPID( result );
  99. if (result >= dgg.getShapeIdMax())
  100. dgg.setShapeIdMax( result + 1 );
  101. return result;
  102. }
  103. //////////// Non-public methods /////////////
  104. short findNewDrawingGroupId()
  105. {
  106. short dgId = 1;
  107. while ( drawingGroupExists( dgId ) )
  108. dgId++;
  109. return dgId;
  110. }
  111. EscherDgRecord getDrawingGroup(int drawingGroupId)
  112. {
  113. return (EscherDgRecord) drawingGroups.get(drawingGroupId-1);
  114. }
  115. boolean drawingGroupExists( short dgId )
  116. {
  117. for ( int i = 0; i < dgg.getFileIdClusters().length; i++ )
  118. {
  119. if ( dgg.getFileIdClusters()[i].getDrawingGroupId() == dgId )
  120. return true;
  121. }
  122. return false;
  123. }
  124. int findFreeSPIDBlock()
  125. {
  126. int max = dgg.getShapeIdMax();
  127. int next = ( ( max / 1024 ) + 1 ) * 1024;
  128. return next;
  129. }
  130. public EscherDggRecord getDgg()
  131. {
  132. return dgg;
  133. }
  134. }