/****************************************************************************
 *                                                                          *
 * NOA (Nice Office Access)                                     						*
 * ------------------------------------------------------------------------ *
 *                                                                          *
 * The Contents of this file are made available subject to                  *
 * the terms of GNU Lesser General Public License Version 2.1.              *
 *                                                                          * 
 * GNU Lesser General Public License Version 2.1                            *
 * ======================================================================== *
 * Copyright 2003-2006 by IOn AG                                            *
 *                                                                          *
 * This library is free software; you can redistribute it and/or            *
 * modify it under the terms of the GNU Lesser General Public               *
 * License version 2.1, as published by the Free Software Foundation.       *
 *                                                                          *
 * This library is distributed in the hope that it will be useful,          *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
 * Lesser General Public License for more details.                          *
 *                                                                          *
 * You should have received a copy of the GNU Lesser General Public         *
 * License along with this library; if not, write to the Free Software      *
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,                    *
 * MA  02111-1307  USA                                                      *
 *                                                                          *
 * Contact us:                                                              *
 *  http://www.ion.ag																												*
 *  http://ubion.ion.ag                                                     *
 *  info@ion.ag                                                             *
 *                                                                          *
 ****************************************************************************/

/*
 * Last changes made by $Author: andreas $, $Date: 2006-10-04 14:14:28 +0200 (Mi, 04 Okt 2006) $
 */
import java.util.HashMap;

import ag.ion.bion.officelayer.application.IOfficeApplication;
import ag.ion.bion.officelayer.application.OfficeApplicationRuntime;
import ag.ion.bion.officelayer.document.DocumentDescriptor;
import ag.ion.bion.officelayer.document.IDocument;
import ag.ion.bion.officelayer.document.IDocumentService;
import ag.ion.bion.officelayer.text.ITextDocument;
import ag.ion.bion.officelayer.text.ITextField;
import ag.ion.bion.officelayer.text.ITextFieldService;
import ag.ion.bion.officelayer.text.ITextRange;
import ag.ion.bion.officelayer.text.ITextTable;
import ag.ion.bion.officelayer.text.IVariableTextFieldMaster;
import ag.ion.bion.officelayer.text.IViewCursor;
import ag.ion.bion.officelayer.text.IViewCursorService;

import com.sun.star.beans.XPropertySet;
import com.sun.star.text.SetVariableType;
import com.sun.star.uno.UnoRuntime;

public class WorkWithVariables2 {

  private final static String OPEN_OFFICE_ORG_PATH = "C:\\Programme\\OpenOffice.org 3";

  public static void main(String[] args) {
    HashMap configuration = new HashMap();
    configuration.put(IOfficeApplication.APPLICATION_HOME_KEY, OPEN_OFFICE_ORG_PATH);
    configuration.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);

    try {
      IOfficeApplication officeAplication = OfficeApplicationRuntime.getApplication(configuration);
      officeAplication.activate();
      IDocumentService documentService = officeAplication.getDocumentService();
      IDocument document = documentService.constructNewDocument(IDocument.WRITER,
          DocumentDescriptor.DEFAULT);
      ITextDocument textDocument = (ITextDocument) document;

      //create a tabel just for some content
      ITextTable textTable = textDocument.getTextTableService().constructTextTable(2, 4);
      textDocument.getTextService().getTextContentService().insertTextContent(textTable);
      textTable.getCell(0, 0).getTextService().getText().setText("A");
      textTable.getCell(1, 0).getTextService().getText().setText("B");
      textTable.getCell(2, 0).getTextService().getText().setText("C");
      textTable.getCell(3, 0).getTextService().getText().setText("D");

      //fake user click into cell B2
      IViewCursorService viewCursorService = textDocument.getViewCursorService();
      IViewCursor viewCursor = viewCursorService.getViewCursor();
      viewCursor.goToRange(textTable.getCell(1, 1).getTextService().getCursorService().getTextCursor().getStart(),
          false);

      //get current position
      ITextRange currentPosition = viewCursor.getTextCursorFromEnd().getEnd();

      //check if variable already in document and create it if not
      String varName = "myVariable";
      String varInitialValue = "myVariable";
      ITextFieldService textFieldService = textDocument.getTextFieldService();
      IVariableTextFieldMaster master = textFieldService.getVariableTextFieldMaster(varName);
      if (master == null) {
        master = textFieldService.createVariableTextFieldMaster(varName, SetVariableType.STRING);
      }

      //insert variable
      ITextField variableField = master.constructNewVariableTextField(varInitialValue, true);
      textDocument.getTextService().getTextContentService().insertTextContent(currentPosition,
          variableField);
      textDocument.getTextFieldService().refresh();
      textDocument.setModified(true);

      //change variable value everywhere in the document
      String varNewValue = "myNewValue";
      ITextField[] variables = textFieldService.getVariableTextFieldMaster(varName).getVariableTextFields();
      for (int i = 0; i < variables.length; i++) {
        XPropertySet xPropertySetField = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
            variables[i].getXTextContent());
        xPropertySetField.setPropertyValue("Content", varNewValue);
      }
      textDocument.getTextFieldService().refresh();
      textDocument.setModified(true);

    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }
}
