001package com.hfg.xml.msofficexml.xlsx.part;
002
003
004import com.hfg.xml.XMLTag;
005import com.hfg.xml.msofficexml.xlsx.SsmlRelationshipType;
006import com.hfg.xml.msofficexml.xlsx.Xlsx;
007import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorksheet;
008import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
009import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
010import com.hfg.xml.msofficexml.xlsx.spreadsheetml.XlsxComment;
011
012import java.io.File;
013import java.util.HashMap;
014import java.util.Map;
015
016/*
017
018taylora% more xl/drawings/vmlDrawing1.vml
019<xml xmlns:v="urn:schemas-microsoft-com:vml"
020 xmlns:o="urn:schemas-microsoft-com:office:office"
021 xmlns:x="urn:schemas-microsoft-com:office:excel">
022 <o:shapelayout v:ext="edit">
023  <o:idmap v:ext="edit" data="1"/>
024 </o:shapelayout><v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202"
025  path="m0,0l0,21600,21600,21600,21600,0xe">
026  <v:stroke joinstyle="miter"/>
027  <v:path gradientshapeok="t" o:connecttype="rect"/>
028 </v:shapetype><v:shape id="_x0000_s1025" type="#_x0000_t202" style='position:absolute;
029  margin-left:90pt;margin-top:2pt;width:124pt;height:27pt;z-index:1;
030  visibility:hidden;mso-wrap-style:square' fillcolor="#ffffa1 [80]"
031  o:insetmode="auto">
032  <v:fill color2="#ffffa1 [80]"/>
033  <v:shadow on="t" obscured="t"/>
034  <v:path o:connecttype="none"/>
035  <v:textbox style='mso-fit-text-with-word-wrap:t'>
036   <div style='text-align:left'></div>
037  </v:textbox>
038  <x:ClientData ObjectType="Note">
039   <x:MoveWithCells/>
040   <x:SizeWithCells/>
041   <x:Anchor>
042    1, 15, 0, 2, 2, 64, 2, 3</x:Anchor>
043   <x:AutoFill>False</x:AutoFill>
044   <x:Row>0</x:Row>
045   <x:Column>0</x:Column>
046  </x:ClientData>
047 </v:shape></xml>
048
049
050<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
051<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><authors><author>Alex Taylor</author></authors><commentList><comment ref="A1" authorId="0"><text><r><rPr><b/><sz val="9"/><color indexed="81"/><rFont val="Verdana"/></rPr><t>Alex Taylor:</t></r><r><rPr><sz val="9"/><color indexed="81"/><rFont val="Verdana"/></rPr><t xml:space="preserve">
052A message from beyond</t></r></text></comment></commentList></comments>
053 */
054public class SsmlCommentsPart extends XlsxPart
055{
056   private static int    sIndexSrc = 1;
057
058   private SsmlWorksheet mParentWorksheet;
059   private SsmlComments  mCommentsTag;
060   private XMLTag        mCommentListTag;
061   private XMLTag        mAuthorsTag;
062   private int           mIndex = sIndexSrc++;
063   private Map<String, Integer> mAuthorMap;
064
065   //---------------------------------------------------------------------------
066   public SsmlCommentsPart(SsmlWorksheet inParentWorksheet)
067   {
068      super(inParentWorksheet.getParentDoc());
069      mParentWorksheet = inParentWorksheet;
070
071      // Register the content type
072      mParentWorksheet.getParentDoc().getContentTypesPart().addOverride(this, SsmlContentType.COMMENTS);
073
074      // Register the relationship
075      mParentWorksheet.getParentWorksheetPart().getWorksheetRelationshipPart().addRelationship(SsmlRelationshipType.COMMENTS, this);
076
077      mCommentsTag = new SsmlComments(mParentWorksheet.getParentDoc());
078      setRootNode(mCommentsTag);
079
080      mCommentListTag = new XMLTag(SsmlXML.COMMENT_LIST);
081      mCommentsTag.addSubtag(mCommentListTag);
082
083      // A boilerplate VML drawing part is also needed
084      //TODO
085   }
086
087   //---------------------------------------------------------------------------
088   @Override
089   public File getFile()
090   {
091      return new File(SsmlXML.XL_DIR, "comments" + mIndex + ".xml");
092   }
093
094   //---------------------------------------------------------------------------
095   public SsmlCommentsPart addComment(XlsxComment inComment)
096   {
097      SsmlComment commentTag = new SsmlComment(getParentDoc());
098      commentTag.setAttribute(SsmlXML.REF_RANGE_ATT, inComment.getCellRef());
099      commentTag.setAttribute(SsmlXML.AUTHOR_ID_ATT, getAuthorIdx(inComment.getAuthor()));
100      commentTag.addSubtag(new SsmlCommentText(getParentDoc(), inComment.getText()));
101
102      mCommentListTag.addSubtag(commentTag);
103
104      return this;
105   }
106
107   //---------------------------------------------------------------------------
108   private int getAuthorIdx(String inAuthor)
109   {
110      if (null == mAuthorMap)
111      {
112         mAuthorMap = new HashMap<String, Integer>(10);
113
114         mAuthorsTag = new XMLTag(SsmlXML.AUTHORS);
115         mCommentsTag.addSubtag(mAuthorsTag);
116      }
117
118      Integer index = mAuthorMap.get(inAuthor);
119      if (null == index)
120      {
121         // New author
122         index = mAuthorMap.size();
123         mAuthorMap.put(inAuthor, index);
124
125         mAuthorsTag.addSubtag(new XMLTag(SsmlXML.AUTHOR).setContent(inAuthor));
126      }
127
128      return index;
129   }
130
131   private class SsmlComments extends SsmlXMLTag
132   {
133      //---------------------------------------------------------------------------
134      public SsmlComments(Xlsx inXlsx)
135      {
136         super(SsmlXML.COMMENTS, inXlsx);
137      }
138   }
139
140   private class SsmlComment extends SsmlXMLTag
141   {
142      //---------------------------------------------------------------------------
143      public SsmlComment(Xlsx inXlsx)
144      {
145         super(SsmlXML.COMMENT, inXlsx);
146      }
147   }
148
149   private class SsmlCommentText extends SsmlXMLTag
150   {
151      //---------------------------------------------------------------------------
152      public SsmlCommentText(Xlsx inXlsx, SsmlXMLTag inTextTag)
153      {
154         super(SsmlXML.COMMENT_TEXT, inXlsx);
155         addSubtag(inTextTag);
156      }
157   }
158
159}