Page 1 of 1

Write UNO JAVA API get cursor current position in the table

PostPosted: Wed Jul 11, 2018 1:20 pm
by carlos.lima
Hi guys!

I've got a document with some text and a table with the cursor with the focus in the first cell.

How can I get the cursor current position?

Re: Write UNO JAVA API get cursor current position in the ta

PostPosted: Wed Jul 11, 2018 3:40 pm
by FJCC
If the View Cursor is in a text table, it has a non-void TextTable property and also a Cell property. I don't use Java but I recorded some Java code with MRI.
Code: Select all   Expand viewCollapse view
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNamed;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.table.XCell;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XModel xModel = UnoRuntime.queryInterface(
         XModel.class, oInitialTarget);
      XController xController = xModel.getCurrentController();
      
      XTextViewCursorSupplier xTextViewCursorSupplier = UnoRuntime.queryInterface(
         XTextViewCursorSupplier.class, xController);
      XTextViewCursor xTextViewCursor = xTextViewCursorSupplier.getViewCursor();
      
      XPropertySet xPropSet = UnoRuntime.queryInterface(
         XPropertySet.class, xTextViewCursor);
      XCell xCell = UnoRuntime.queryInterface(XCell.class, xPropSet.getPropertyValue("Cell"));
      
      XTextTable xTextTable = UnoRuntime.queryInterface(XTextTable.class, xPropSet.getPropertyValue("TextTable"));
      
      XNamed xNamed = UnoRuntime.queryInterface(
         XNamed.class, xTextTable);
      String sName = xNamed.getName();
      
      XPropertySet xPropSet2 = UnoRuntime.queryInterface(
         XPropertySet.class, xCell);
      String sCellName = AnyConverter.toString(xPropSet2.getPropertyValue("CellName"));
      
   }
   catch (WrappedTargetException e1)
   {
      // getPropertyValue
      e1.printStackTrace();
   }
   catch (IllegalArgumentException e2)
   {
      //
      e2.printStackTrace();
   }
   catch (UnknownPropertyException e3)
   {
      // getPropertyValue
      e3.printStackTrace();
   }
}

When I ran MRI in my sample document, sName had the value Table1 and sCellName was A1. I hope that helps.