All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.xlsx.part.SsmlCommentsPart Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.xlsx.part;


import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.xlsx.SsmlRelationshipType;
import com.hfg.xml.msofficexml.xlsx.Xlsx;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorksheet;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.XlsxComment;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/*

taylora% more xl/drawings/vmlDrawing1.vml

 
  
 
  
  
 



Alex TaylorAlex Taylor:
A message from beyond
 */
public class SsmlCommentsPart extends XlsxPart
{
   private static int    sIndexSrc = 1;

   private SsmlWorksheet mParentWorksheet;
   private SsmlComments  mCommentsTag;
   private XMLTag        mCommentListTag;
   private XMLTag        mAuthorsTag;
   private int           mIndex = sIndexSrc++;
   private Map mAuthorMap;

   //---------------------------------------------------------------------------
   public SsmlCommentsPart(SsmlWorksheet inParentWorksheet)
   {
      super(inParentWorksheet.getParentDoc());
      mParentWorksheet = inParentWorksheet;

      // Register the content type
      mParentWorksheet.getParentDoc().getContentTypesPart().addOverride(this, SsmlContentType.COMMENTS);

      // Register the relationship
      mParentWorksheet.getParentWorksheetPart().getWorksheetRelationshipPart().addRelationship(SsmlRelationshipType.COMMENTS, this);

      mCommentsTag = new SsmlComments(mParentWorksheet.getParentDoc());
      setRootNode(mCommentsTag);

      mCommentListTag = new XMLTag(SsmlXML.COMMENT_LIST);
      mCommentsTag.addSubtag(mCommentListTag);

      // A boilerplate VML drawing part is also needed
      //TODO
   }

   //---------------------------------------------------------------------------
   @Override
   public File getFile()
   {
      return new File(SsmlXML.XL_DIR, "comments" + mIndex + ".xml");
   }

   //---------------------------------------------------------------------------
   public SsmlCommentsPart addComment(XlsxComment inComment)
   {
      SsmlComment commentTag = new SsmlComment(getParentDoc());
      commentTag.setAttribute(SsmlXML.REF_RANGE_ATT, inComment.getCellRef());
      commentTag.setAttribute(SsmlXML.AUTHOR_ID_ATT, getAuthorIdx(inComment.getAuthor()));
      commentTag.addSubtag(new SsmlCommentText(getParentDoc(), inComment.getText()));

      mCommentListTag.addSubtag(commentTag);

      return this;
   }

   //---------------------------------------------------------------------------
   private int getAuthorIdx(String inAuthor)
   {
      if (null == mAuthorMap)
      {
         mAuthorMap = new HashMap(10);

         mAuthorsTag = new XMLTag(SsmlXML.AUTHORS);
         mCommentsTag.addSubtag(mAuthorsTag);
      }

      Integer index = mAuthorMap.get(inAuthor);
      if (null == index)
      {
         // New author
         index = mAuthorMap.size();
         mAuthorMap.put(inAuthor, index);

         mAuthorsTag.addSubtag(new XMLTag(SsmlXML.AUTHOR).setContent(inAuthor));
      }

      return index;
   }

   private class SsmlComments extends SsmlXMLTag
   {
      //---------------------------------------------------------------------------
      public SsmlComments(Xlsx inXlsx)
      {
         super(SsmlXML.COMMENTS, inXlsx);
      }
   }

   private class SsmlComment extends SsmlXMLTag
   {
      //---------------------------------------------------------------------------
      public SsmlComment(Xlsx inXlsx)
      {
         super(SsmlXML.COMMENT, inXlsx);
      }
   }

   private class SsmlCommentText extends SsmlXMLTag
   {
      //---------------------------------------------------------------------------
      public SsmlCommentText(Xlsx inXlsx, SsmlXMLTag inTextTag)
      {
         super(SsmlXML.COMMENT_TEXT, inXlsx);
         addSubtag(inTextTag);
      }
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy