package izyndoc;

import com.jacob.com.*;

/**
 * <p>Title: WordDocument</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Izyn Technologies</p>
 * @author Richard Norton
 * orignal framework sourced from
 * http://forum.java.sun.com/thread.jsp?forum=40&thread=215469&tstart=135&trange=15
 * @version 1.0
 */

public class WordDocument extends com.jacob.com.Dispatch
{
  /**
   * Constructor.
   */
  public WordDocument()
  {
    super("Word.Document");
  }

  /**
   * Alternative constructor to be used if you need to return a Document
   * object from another object's method call.
   *
   * @param dispatchDocument Dispatch
   */
  public WordDocument(Dispatch dispatchDocument)
  {
    //TAKE OVER THE IDispatch POINTER
    this.m_pDispatch = dispatchDocument.m_pDispatch;
    //NULL OUT THE INPUT POINTER
    dispatchDocument.m_pDispatch = 0;
  }

  /**
   * Get Read-only property.
   *
   * @return boolean
   */
  public boolean isReadOnly()
  {
    return Dispatch.get(this, "ReadOnly").toBoolean();
  }

  /**
   * Close Word Document with Options for Saving or not Saving.
   *
   * @param closeOption Integer
   */
  public void close(Integer closeOption)
  {
    Dispatch.call(this, "Close", new Variant(closeOption));
  }

  /**
   * Set 'saved' to false so Word thinks doc is 'dirty'
   * otherwise it fails to save added custom properties
   * found in MS knowledge base article - 195425
   */
  public void dirtyDoc()
  {
    Dispatch.put(this, "Saved", new Variant(Word.FALSE));
  }

  /**
   * Get the CustomDocumentProperties collection for the document.
   *
   * @return Dispatch
   */
  public Dispatch getCustomDocumentProperties()
  {
    return Dispatch.get(this, "CustomDocumentProperties").toDispatch();
  }

  /**
   * Get the Fields collection for the body of the document,
   * does not do headers and footers.
   *
   * @return Dispatch
   */
  public Dispatch getFields()
  {
    return Dispatch.get(this, "Fields").toDispatch();
  }

  /**
   * Get the StoryRanges collection.
   *
   * @return Dispatch
   */
  public Dispatch getSections()
  {
    return Dispatch.get(this, "StoryRanges").toDispatch();
  }

  /**
   * Update all the fields in the body of the document,
   * does not do headers and footers.
   *
   * @return int 0 if no errors occur when the fields are updated,
   * or returns the index of the first field that contains an error
   */
  public int updateFields()
  {
    return Dispatch.call(getFields(), "Update").toInt();
  }

  /**
   * Save the document.
   *
   * @param fileName String
   */
  public void saveAs(String fileName)
  {
    Dispatch.call(this, "SaveAs", fileName);
  }

  /**
   * Return document name.
   *
   * @return String
   */
  public String name()
  {
    return Dispatch.get(this, "Name").toString();
  }
}