You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import org.apache.poi.ddf.*;
  17. import org.apache.poi.hslf.model.Line;
  18. import org.apache.poi.hslf.usermodel.*;
  19. import org.apache.poi.sl.usermodel.ShapeContainer;
  20. import org.apache.poi.util.LittleEndian;
  21. import java.util.*;
  22. import java.util.List;
  23. import java.awt.*;
  24. /**
  25. * Represents a table in a PowerPoint presentation
  26. *
  27. * @author Yegor Kozlov
  28. */
  29. public final class HSLFTable extends HSLFGroupShape {
  30. protected static final int BORDER_TOP = 1;
  31. protected static final int BORDER_RIGHT = 2;
  32. protected static final int BORDER_BOTTOM = 3;
  33. protected static final int BORDER_LEFT = 4;
  34. protected static final int BORDERS_ALL = 5;
  35. protected static final int BORDERS_OUTSIDE = 6;
  36. protected static final int BORDERS_INSIDE = 7;
  37. protected static final int BORDERS_NONE = 8;
  38. protected HSLFTableCell[][] cells;
  39. /**
  40. * Create a new Table of the given number of rows and columns
  41. *
  42. * @param numrows the number of rows
  43. * @param numcols the number of columns
  44. */
  45. public HSLFTable(int numrows, int numcols) {
  46. super();
  47. if(numrows < 1) throw new IllegalArgumentException("The number of rows must be greater than 1");
  48. if(numcols < 1) throw new IllegalArgumentException("The number of columns must be greater than 1");
  49. int x=0, y=0, tblWidth=0, tblHeight=0;
  50. cells = new HSLFTableCell[numrows][numcols];
  51. for (int i = 0; i < cells.length; i++) {
  52. x = 0;
  53. for (int j = 0; j < cells[i].length; j++) {
  54. cells[i][j] = new HSLFTableCell(this);
  55. Rectangle anchor = new Rectangle(x, y, HSLFTableCell.DEFAULT_WIDTH, HSLFTableCell.DEFAULT_HEIGHT);
  56. cells[i][j].setAnchor(anchor);
  57. x += HSLFTableCell.DEFAULT_WIDTH;
  58. }
  59. y += HSLFTableCell.DEFAULT_HEIGHT;
  60. }
  61. tblWidth = x;
  62. tblHeight = y;
  63. setAnchor(new Rectangle(0, 0, tblWidth, tblHeight));
  64. EscherContainerRecord spCont = (EscherContainerRecord) getSpContainer().getChild(0);
  65. EscherOptRecord opt = new EscherOptRecord();
  66. opt.setRecordId((short)0xF122);
  67. opt.addEscherProperty(new EscherSimpleProperty((short)0x39F, 1));
  68. EscherArrayProperty p = new EscherArrayProperty((short)(0x4000 | 0x3A0), false, null);
  69. p.setSizeOfElements(0x0004);
  70. p.setNumberOfElementsInArray(numrows);
  71. p.setNumberOfElementsInMemory(numrows);
  72. opt.addEscherProperty(p);
  73. List<EscherRecord> lst = spCont.getChildRecords();
  74. lst.add(lst.size()-1, opt);
  75. spCont.setChildRecords(lst);
  76. }
  77. /**
  78. * Create a Table object and initilize it from the supplied Record container.
  79. *
  80. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  81. * @param parent the parent of the shape
  82. */
  83. public HSLFTable(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape> parent) {
  84. super(escherRecord, parent);
  85. }
  86. /**
  87. * Gets a cell
  88. *
  89. * @param row the row index (0-based)
  90. * @param col the column index (0-based)
  91. * @return the cell
  92. */
  93. public HSLFTableCell getCell(int row, int col) {
  94. return cells[row][col];
  95. }
  96. public int getNumberOfColumns() {
  97. return cells[0].length;
  98. }
  99. public int getNumberOfRows() {
  100. return cells.length;
  101. }
  102. protected void afterInsert(HSLFSheet sh){
  103. super.afterInsert(sh);
  104. EscherContainerRecord spCont = (EscherContainerRecord) getSpContainer().getChild(0);
  105. List<EscherRecord> lst = spCont.getChildRecords();
  106. EscherOptRecord opt = (EscherOptRecord)lst.get(lst.size()-2);
  107. EscherArrayProperty p = opt.lookup(0x3A0);
  108. for (int i = 0; i < cells.length; i++) {
  109. HSLFTableCell cell = cells[i][0];
  110. int rowHeight = cell.getAnchor().height*MASTER_DPI/POINT_DPI;
  111. byte[] val = new byte[4];
  112. LittleEndian.putInt(val, 0, rowHeight);
  113. p.setElement(i, val);
  114. for (int j = 0; j < cells[i].length; j++) {
  115. HSLFTableCell c = cells[i][j];
  116. addShape(c);
  117. Line bt = c.getBorderTop();
  118. if(bt != null) addShape(bt);
  119. Line br = c.getBorderRight();
  120. if(br != null) addShape(br);
  121. Line bb = c.getBorderBottom();
  122. if(bb != null) addShape(bb);
  123. Line bl = c.getBorderLeft();
  124. if(bl != null) addShape(bl);
  125. }
  126. }
  127. }
  128. protected void initTable(){
  129. List<HSLFShape> shapeList = getShapeList();
  130. Collections.sort(shapeList, new Comparator<HSLFShape>(){
  131. public int compare( HSLFShape o1, HSLFShape o2 ) {
  132. Rectangle anchor1 = o1.getAnchor();
  133. Rectangle anchor2 = o2.getAnchor();
  134. int delta = anchor1.y - anchor2.y;
  135. if(delta == 0) delta = anchor1.x - anchor2.x;
  136. return delta;
  137. }
  138. });
  139. int y0 = -1;
  140. int maxrowlen = 0;
  141. List<List<HSLFShape>> lst = new ArrayList<List<HSLFShape>>();
  142. List<HSLFShape> row = null;
  143. for (HSLFShape sh : shapeList) {
  144. if(sh instanceof HSLFTextShape){
  145. Rectangle anchor = sh.getAnchor();
  146. if(anchor.y != y0){
  147. y0 = anchor.y;
  148. row = new ArrayList<HSLFShape>();
  149. lst.add(row);
  150. }
  151. row.add(sh);
  152. maxrowlen = Math.max(maxrowlen, row.size());
  153. }
  154. }
  155. cells = new HSLFTableCell[lst.size()][maxrowlen];
  156. for (int i = 0; i < lst.size(); i++) {
  157. row = lst.get(i);
  158. for (int j = 0; j < row.size(); j++) {
  159. HSLFTextShape tx = (HSLFTextShape)row.get(j);
  160. cells[i][j] = new HSLFTableCell(tx.getSpContainer(), getParent());
  161. cells[i][j].setSheet(tx.getSheet());
  162. }
  163. }
  164. }
  165. /**
  166. * Assign the <code>SlideShow</code> this shape belongs to
  167. *
  168. * @param sheet owner of this shape
  169. */
  170. public void setSheet(HSLFSheet sheet){
  171. super.setSheet(sheet);
  172. if(cells == null) initTable();
  173. }
  174. /**
  175. * Sets the row height.
  176. *
  177. * @param row the row index (0-based)
  178. * @param height the height to set (in pixels)
  179. */
  180. public void setRowHeight(int row, int height){
  181. int currentHeight = cells[row][0].getAnchor().height;
  182. int dy = height - currentHeight;
  183. for (int i = row; i < cells.length; i++) {
  184. for (int j = 0; j < cells[i].length; j++) {
  185. Rectangle anchor = cells[i][j].getAnchor();
  186. if(i == row) anchor.height = height;
  187. else anchor.y += dy;
  188. cells[i][j].setAnchor(anchor);
  189. }
  190. }
  191. Rectangle tblanchor = getAnchor();
  192. tblanchor.height += dy;
  193. setAnchor(tblanchor);
  194. }
  195. /**
  196. * Sets the column width.
  197. *
  198. * @param col the column index (0-based)
  199. * @param width the width to set (in pixels)
  200. */
  201. public void setColumnWidth(int col, int width){
  202. int currentWidth = cells[0][col].getAnchor().width;
  203. int dx = width - currentWidth;
  204. for (int i = 0; i < cells.length; i++) {
  205. Rectangle anchor = cells[i][col].getAnchor();
  206. anchor.width = width;
  207. cells[i][col].setAnchor(anchor);
  208. if(col < cells[i].length - 1) for (int j = col+1; j < cells[i].length; j++) {
  209. anchor = cells[i][j].getAnchor();
  210. anchor.x += dx;
  211. cells[i][j].setAnchor(anchor);
  212. }
  213. }
  214. Rectangle tblanchor = getAnchor();
  215. tblanchor.width += dx;
  216. setAnchor(tblanchor);
  217. }
  218. /**
  219. * Format the table and apply the specified Line to all cell boundaries,
  220. * both outside and inside
  221. *
  222. * @param line the border line
  223. */
  224. public void setAllBorders(Line line){
  225. for (int i = 0; i < cells.length; i++) {
  226. for (int j = 0; j < cells[i].length; j++) {
  227. HSLFTableCell cell = cells[i][j];
  228. cell.setBorderTop(cloneBorder(line));
  229. cell.setBorderLeft(cloneBorder(line));
  230. if(j == cells[i].length - 1) cell.setBorderRight(cloneBorder(line));
  231. if(i == cells.length - 1) cell.setBorderBottom(cloneBorder(line));
  232. }
  233. }
  234. }
  235. /**
  236. * Format the outside border using the specified Line object
  237. *
  238. * @param line the border line
  239. */
  240. public void setOutsideBorders(Line line){
  241. for (int i = 0; i < cells.length; i++) {
  242. for (int j = 0; j < cells[i].length; j++) {
  243. HSLFTableCell cell = cells[i][j];
  244. if(j == 0) cell.setBorderLeft(cloneBorder(line));
  245. if(j == cells[i].length - 1) cell.setBorderRight(cloneBorder(line));
  246. else {
  247. cell.setBorderLeft(null);
  248. cell.setBorderLeft(null);
  249. }
  250. if(i == 0) cell.setBorderTop(cloneBorder(line));
  251. else if(i == cells.length - 1) cell.setBorderBottom(cloneBorder(line));
  252. else {
  253. cell.setBorderTop(null);
  254. cell.setBorderBottom(null);
  255. }
  256. }
  257. }
  258. }
  259. /**
  260. * Format the inside border using the specified Line object
  261. *
  262. * @param line the border line
  263. */
  264. public void setInsideBorders(Line line){
  265. for (int i = 0; i < cells.length; i++) {
  266. for (int j = 0; j < cells[i].length; j++) {
  267. HSLFTableCell cell = cells[i][j];
  268. if(j != cells[i].length - 1)
  269. cell.setBorderRight(cloneBorder(line));
  270. else {
  271. cell.setBorderLeft(null);
  272. cell.setBorderLeft(null);
  273. }
  274. if(i != cells.length - 1) cell.setBorderBottom(cloneBorder(line));
  275. else {
  276. cell.setBorderTop(null);
  277. cell.setBorderBottom(null);
  278. }
  279. }
  280. }
  281. }
  282. private Line cloneBorder(Line line){
  283. Line border = createBorder();
  284. border.setLineWidth(line.getLineWidth());
  285. border.setLineDashing(line.getLineDashing());
  286. border.setLineColor(line.getLineColor());
  287. border.setLineCompound(line.getLineCompound());
  288. return border;
  289. }
  290. /**
  291. * Create a border to format this table
  292. *
  293. * @return the created border
  294. */
  295. public Line createBorder(){
  296. Line line = new Line(this);
  297. EscherOptRecord opt = getEscherOptRecord();
  298. setEscherProperty(opt, EscherProperties.GEOMETRY__SHAPEPATH, -1);
  299. setEscherProperty(opt, EscherProperties.GEOMETRY__FILLOK, -1);
  300. setEscherProperty(opt, EscherProperties.SHADOWSTYLE__SHADOWOBSURED, 0x20000);
  301. setEscherProperty(opt, EscherProperties.THREED__LIGHTFACE, 0x80000);
  302. return line;
  303. }
  304. }