123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
-
- package org.apache.poi.hslf.usermodel;
-
- import java.awt.Color;
- import java.awt.geom.Rectangle2D;
-
- import org.apache.poi.ddf.AbstractEscherOptRecord;
- import org.apache.poi.ddf.EscherContainerRecord;
- import org.apache.poi.ddf.EscherProperties;
- import org.apache.poi.sl.draw.DrawPaint;
- import org.apache.poi.sl.usermodel.PaintStyle;
- import org.apache.poi.sl.usermodel.ShapeType;
- import org.apache.poi.sl.usermodel.StrokeStyle;
- import org.apache.poi.sl.usermodel.StrokeStyle.LineCompound;
- import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
- import org.apache.poi.sl.usermodel.TableCell;
-
- /**
- * Represents a cell in a ppt table
- *
- * @author Yegor Kozlov
- */
- public final class HSLFTableCell extends HSLFTextBox implements TableCell<HSLFShape,HSLFTextParagraph> {
- protected static final int DEFAULT_WIDTH = 100;
- protected static final int DEFAULT_HEIGHT = 40;
-
- /* package */ HSLFLine borderLeft;
- /* package */ HSLFLine borderRight;
- /* package */ HSLFLine borderTop;
- /* package */ HSLFLine borderBottom;
-
- /**
- * Create a TableCell object and initialize it from the supplied Record container.
- *
- * @param escherRecord EscherSpContainer which holds information about this shape
- * @param parent the parent of the shape
- */
- protected HSLFTableCell(EscherContainerRecord escherRecord, HSLFTable parent){
- super(escherRecord, parent);
- }
-
- /**
- * Create a new TableCell. This constructor is used when a new shape is created.
- *
- * @param parent the parent of this Shape. For example, if this text box is a cell
- * in a table then the parent is Table.
- */
- public HSLFTableCell(HSLFTable parent){
- super(parent);
-
- setShapeType(ShapeType.RECT);
- //_txtrun.setRunType(TextHeaderAtom.HALF_BODY_TYPE);
- //_txtrun.getRichTextRuns()[0].setFlag(false, 0, false);
- }
-
- protected EscherContainerRecord createSpContainer(boolean isChild){
- _escherContainer = super.createSpContainer(isChild);
- AbstractEscherOptRecord opt = getEscherOptRecord();
- setEscherProperty(opt, EscherProperties.TEXT__TEXTID, 0);
- setEscherProperty(opt, EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x20000);
- setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 0x150001);
- setEscherProperty(opt, EscherProperties.SHADOWSTYLE__SHADOWOBSURED, 0x20000);
- setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x40000);
-
- return _escherContainer;
- }
-
- private void anchorBorder(BorderEdge edge, final HSLFLine line) {
- if (line == null) {
- return;
- }
- Rectangle2D cellAnchor = getAnchor();
- double x,y,w,h;
- switch(edge){
- case top:
- x = cellAnchor.getX();
- y = cellAnchor.getY();
- w = cellAnchor.getWidth();
- h = 0;
- break;
- case right:
- x = cellAnchor.getX() + cellAnchor.getWidth();
- y = cellAnchor.getY();
- w = 0;
- h = cellAnchor.getHeight();
- break;
- case bottom:
- x = cellAnchor.getX();
- y = cellAnchor.getY() + cellAnchor.getHeight();
- w = cellAnchor.getWidth();
- h = 0;
- break;
- case left:
- x = cellAnchor.getX();
- y = cellAnchor.getY();
- w = 0;
- h = cellAnchor.getHeight();
- break;
- default:
- throw new IllegalArgumentException();
- }
- line.setAnchor(new Rectangle2D.Double(x,y,w,h));
- }
-
- public void setAnchor(Rectangle2D anchor){
- super.setAnchor(anchor);
-
- anchorBorder(BorderEdge.top, borderTop);
- anchorBorder(BorderEdge.right, borderRight);
- anchorBorder(BorderEdge.bottom, borderBottom);
- anchorBorder(BorderEdge.left, borderLeft);
- }
-
- @Override
- public StrokeStyle getBorderStyle(final BorderEdge edge) {
- final Double width = getBorderWidth(edge);
- return (width == null) ? null : new StrokeStyle() {
- public PaintStyle getPaint() {
- return DrawPaint.createSolidPaint(getBorderColor(edge));
- }
-
- public LineCap getLineCap() {
- return null;
- }
-
- public LineDash getLineDash() {
- return getBorderDash(edge);
- }
-
- public LineCompound getLineCompound() {
- return getBorderCompound(edge);
- }
-
- public double getLineWidth() {
- return width;
- }
- };
- }
-
- @Override
- public void setBorderStyle(BorderEdge edge, StrokeStyle style) {
- if (style == null) {
- throw new IllegalArgumentException("StrokeStyle needs to be specified.");
- }
-
- // setting the line cap is not implemented, as the border lines aren't connected
-
- LineCompound compound = style.getLineCompound();
- if (compound != null) {
- setBorderCompound(edge, compound);
- }
-
- LineDash dash = style.getLineDash();
- if (dash != null) {
- setBorderDash(edge, dash);
- }
-
- double width = style.getLineWidth();
- setBorderWidth(edge, width);
- }
-
-
- public Double getBorderWidth(BorderEdge edge) {
- HSLFLine l;
- switch (edge) {
- case bottom: l = borderBottom; break;
- case top: l = borderTop; break;
- case right: l = borderRight; break;
- case left: l = borderLeft; break;
- default: throw new IllegalArgumentException();
- }
- return (l == null) ? null : l.getLineWidth();
- }
-
- @Override
- public void setBorderWidth(BorderEdge edge, double width) {
- HSLFLine l = addLine(edge);
- l.setLineWidth(width);
- }
-
- public Color getBorderColor(BorderEdge edge) {
- HSLFLine l;
- switch (edge) {
- case bottom: l = borderBottom; break;
- case top: l = borderTop; break;
- case right: l = borderRight; break;
- case left: l = borderLeft; break;
- default: throw new IllegalArgumentException();
- }
- return (l == null) ? null : l.getLineColor();
- }
-
- @Override
- public void setBorderColor(BorderEdge edge, Color color) {
- if (edge == null || color == null) {
- throw new IllegalArgumentException("BorderEdge and/or Color need to be specified.");
- }
-
- HSLFLine l = addLine(edge);
- l.setLineColor(color);
- }
-
- public LineDash getBorderDash(BorderEdge edge) {
- HSLFLine l;
- switch (edge) {
- case bottom: l = borderBottom; break;
- case top: l = borderTop; break;
- case right: l = borderRight; break;
- case left: l = borderLeft; break;
- default: throw new IllegalArgumentException();
- }
- return (l == null) ? null : l.getLineDash();
- }
-
- @Override
- public void setBorderDash(BorderEdge edge, LineDash dash) {
- if (edge == null || dash == null) {
- throw new IllegalArgumentException("BorderEdge and/or LineDash need to be specified.");
- }
-
- HSLFLine l = addLine(edge);
- l.setLineDash(dash);
- }
-
- public LineCompound getBorderCompound(BorderEdge edge) {
- HSLFLine l;
- switch (edge) {
- case bottom: l = borderBottom; break;
- case top: l = borderTop; break;
- case right: l = borderRight; break;
- case left: l = borderLeft; break;
- default: throw new IllegalArgumentException();
- }
- return (l == null) ? null : l.getLineCompound();
- }
-
- @Override
- public void setBorderCompound(BorderEdge edge, LineCompound compound) {
- if (edge == null || compound == null) {
- throw new IllegalArgumentException("BorderEdge and/or LineCompound need to be specified.");
- }
-
- HSLFLine l = addLine(edge);
- l.setLineCompound(compound);
- }
-
-
- protected HSLFLine addLine(BorderEdge edge) {
- switch (edge) {
- case bottom: {
- if (borderBottom == null) {
- borderBottom = createBorder(edge);
- HSLFTableCell c = getSiblingCell(1,0);
- if (c != null) {
- assert(c.borderTop == null);
- c.borderTop = borderBottom;
- }
- }
- return borderBottom;
- }
- case top: {
- if (borderTop == null) {
- borderTop = createBorder(edge);
- HSLFTableCell c = getSiblingCell(-1,0);
- if (c != null) {
- assert(c.borderBottom == null);
- c.borderBottom = borderTop;
- }
- }
- return borderTop;
- }
- case right: {
- if (borderRight == null) {
- borderRight = createBorder(edge);
- HSLFTableCell c = getSiblingCell(0,1);
- if (c != null) {
- assert(c.borderLeft == null);
- c.borderLeft = borderRight;
- }
- }
- return borderRight;
- }
- case left: {
- if (borderLeft == null) {
- borderLeft = createBorder(edge);
- HSLFTableCell c = getSiblingCell(0,-1);
- if (c != null) {
- assert(c.borderRight == null);
- c.borderRight = borderLeft;
- }
- }
- return borderLeft;
- }
- default:
- throw new IllegalArgumentException();
- }
- }
-
- @Override
- public void removeBorder(BorderEdge edge) {
- switch (edge) {
- case bottom: {
- if (borderBottom == null) break;
- getParent().removeShape(borderBottom);
- borderBottom = null;
- HSLFTableCell c = getSiblingCell(1,0);
- if (c != null) {
- c.borderTop = null;
- }
- break;
- }
- case top: {
- if (borderTop == null) break;
- getParent().removeShape(borderTop);
- borderTop = null;
- HSLFTableCell c = getSiblingCell(-1,0);
- if (c != null) {
- c.borderBottom = null;
- }
- break;
- }
- case right: {
- if (borderRight == null) break;
- getParent().removeShape(borderRight);
- borderRight = null;
- HSLFTableCell c = getSiblingCell(0,1);
- if (c != null) {
- c.borderLeft = null;
- }
- break;
- }
- case left: {
- if (borderLeft == null) break;
- getParent().removeShape(borderLeft);
- borderLeft = null;
- HSLFTableCell c = getSiblingCell(0,-1);
- if (c != null) {
- c.borderRight = null;
- }
- break;
- }
- default:
- throw new IllegalArgumentException();
- }
- }
-
- protected HSLFTableCell getSiblingCell(int row, int col) {
- return getParent().getRelativeCell(this, row, col);
- }
-
- /**
- * Create a border to format this table
- *
- * @return the created border
- */
- private HSLFLine createBorder(BorderEdge edge) {
- HSLFTable table = getParent();
- HSLFLine line = new HSLFLine(table);
- table.addShape(line);
-
- AbstractEscherOptRecord opt = getEscherOptRecord();
- setEscherProperty(opt, EscherProperties.GEOMETRY__SHAPEPATH, -1);
- setEscherProperty(opt, EscherProperties.GEOMETRY__FILLOK, -1);
- setEscherProperty(opt, EscherProperties.SHADOWSTYLE__SHADOWOBSURED, 0x20000);
- setEscherProperty(opt, EscherProperties.THREED__LIGHTFACE, 0x80000);
-
- anchorBorder(edge, line);
-
- return line;
- }
-
- protected void applyLineProperties(BorderEdge edge, HSLFLine other) {
- HSLFLine line = addLine(edge);
- line.setLineWidth(other.getLineWidth());
- line.setLineColor(other.getLineColor());
- // line.setLineCompound(other.getLineCompound());
- // line.setLineDashing(other.getLineDashing());
- }
-
- @Override
- public HSLFTable getParent() {
- return (HSLFTable)super.getParent();
- }
- }
|