package org.apache.poi.xslf.usermodel;
import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.sl.usermodel.Comment;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthorList;
public class implements Comment {
final CTComment ;
final XSLFCommentAuthors ;
XSLFComment(final CTComment comment, final XSLFCommentAuthors authors) {
this.comment = comment;
this.authors = authors;
}
@Override
public String () {
return authors.getAuthorById(comment.getAuthorId()).getName();
}
@Override
public void (final String author) {
if (author == null) {
throw new IllegalArgumentException("author must not be null");
}
final CTCommentAuthorList list = authors.getCTCommentAuthorsList();
long maxId = -1;
for (final CTCommentAuthor aut : list.getCmAuthorArray()) {
maxId = Math.max(aut.getId(), maxId);
if (author.equals(aut.getName())) {
comment.setAuthorId(aut.getId());
return;
}
}
final CTCommentAuthor newAuthor = list.addNewCmAuthor();
newAuthor.setName(author);
newAuthor.setId(maxId+1);
newAuthor.setInitials(author.replaceAll( "\\s*(\\w)\\S*", "$1").toUpperCase(LocaleUtil.getUserLocale()));
comment.setAuthorId(maxId+1);
}
@Override
public String () {
final CTCommentAuthor aut = authors.getAuthorById(comment.getAuthorId());
return aut == null ? null : aut.getInitials();
}
@Override
public void (final String initials) {
final CTCommentAuthor aut = authors.getAuthorById(comment.getAuthorId());
if (aut != null) {
aut.setInitials(initials);
}
}
@Override
public String () {
return comment.getText();
}
@Override
public void (final String text) {
comment.setText(text);
}
@Override
public Date () {
final Calendar cal = comment.getDt();
return (cal == null) ? null : cal.getTime();
}
@Override
public void (final Date date) {
final Calendar cal = LocaleUtil.getLocaleCalendar();
cal.setTime(date);
comment.setDt(cal);
}
@Override
public Point2D () {
final CTPoint2D pos = comment.getPos();
return new Point2D.Double(Units.toPoints(pos.getX()), Units.toPoints(pos.getY()));
}
@Override
public void (final Point2D offset) {
CTPoint2D pos = comment.getPos();
if (pos == null) {
pos = comment.addNewPos();
}
pos.setX(Units.toEMU(offset.getX()));
pos.setY(Units.toEMU(offset.getY()));
}
}