OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Access TableColumnSeparator from Java

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Sun Nov 14, 2004 2:49 pm    Post subject: Access TableColumnSeparator from Java Reply with quote

Hi!

I've got a problem: I want to insert a table with 4 columns into a text document and I want to change the column width seperatly. Is this possible via the OpenOffice Java API? I searched this forum with "TableColumnSeparator java" but didn't find anything. I found a thread with some StarBasic code. But I couldn't convert that to Java. I only managed to insert a new table with equally wide columns.

I have a second question concerning tables: Is it possible to add new rows into an already existing table via the Java API? I didn't find anything that offers me that possibility.

Any suggestions?

With kind regards
hol.sten
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Sun Nov 14, 2004 3:28 pm    Post subject: Reply with quote

To question 2:
The XTableRows interface provides the method insertByIndex(...), that should do it.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon Nov 15, 2004 9:45 am    Post subject: Reply with quote

I assume that you saw this....
http://www.oooforum.org/forum/viewtopic.php?p=12205#12205

I'm translating from the Basic example.

This line....
Code:
oTblColSeps = oTable.TableColumnSeparators

in Java would need to be several steps....
1. QueryInterface to get the XPropertySet interface
2. Call getPropertyValue() to get the TableColumnSeparators property.

What you get back is an Array of TableColumnSeparator.

So next, you need to do a Java typecast, to cast into an array of the structs.

So far, the entire code looks like...
Code:
XPropertySet xTableProps = UnoRuntime.queryInterface( XPropertySet.class, oTable );
Object colSepsArray = xTableProps.getProperty( "TableColumnSeparators" );
TableColumnSeparators[] colSepsArray2 = (TableColumnSeparators[]) colSepsArray;

and that mess is just to accomplish what was one simple assignment statement in Basic.

Next, you adjust the columns, with code translated from these Basic lines in the above linked example in Basic....
Code:
   ' Change the positions of the two separators.
   oTblColSeps( 0 ).Position = 2000
   oTblColSeps( 1 ).Position = 8000

In your Java array, you would do something like...
Code:
colSepsArray2[0].Position = 2000
colSepsArray2[1].Position = 8000


Then, as in Basic....
Code:
   oTable.TableColumnSeparators = oTblColSeps

you need to assign the Entire Array back to the TableColumnSeparators property. You would use the XPropertySet interface you already obtained, and call setPropertyValue() to assign the entire array back to the property, as in the Basic example.






Some links....

Insert Tables in Writer
http://www.oooforum.org/forum/viewtopic.php?t=3737
....and Change table cell colors
....and insert text into table cells
http://www.oooforum.org/forum/viewtopic.php?t=6437
Change width of columns in Writer Table
http://www.oooforum.org/forum/viewtopic.php?t=3299

Copying TextTable in Writer
http://www.oooforum.org/forum/viewtopic.php?t=10924
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Mon Nov 15, 2004 1:18 pm    Post subject: Reply with quote

Hi Danny!

DannyB wrote:
I assume that you saw this....
http://www.oooforum.org/forum/viewtopic.php?p=12205#12205

Right!

DannyB wrote:
I'm translating from the Basic example.
...
What you get back is an Array of TableColumnSeparator.

That was my problem! I always got ClassCastExceptions but I didn't found the cause. Now it works. Because your translation contains some minor errors, I post here an example from my Java IDE. Most of the code is based on code from this superb forum and from the Developer's Guide:
Code:
   private void insertTable(XComponent xComponent) {

        try
        {
         // Get the text document
         XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, xComponent);

         // Get the text
         XText xText = xTextDocument.getText();
         
         // Create a text cursor for inserting the new table
         XTextCursor xTextCursor = xText.createTextCursor();
         
         // Jump to the end of the text
         xTextCursor.gotoEnd(false);
         
         // Create a new text table from the document's factory
         XMultiServiceFactory xWriterFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xComponent);
         XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));

         // Specify that we want the table to have 4 rows and 4 columns
         xTable.initialize(4, 4);
         
         // Insert the table into the document
         xText.insertTextContent(xTextCursor, xTable, false);
         
         // Get the property set of the text table
         XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTable);
         
         // Get the array(!) of table column seperators
         TableColumnSeparator[] separators = (TableColumnSeparator[])TableProps.getPropertyValue("TableColumnSeparators");

         // In this example the column seperators are now 2500, 5000 and 7500
         // Now Change the table column seperators
         separators[0].Position = 7000;
         separators[1].Position = 8000;
         separators[2].Position = 9000;
         
         // Set the array of table column seperators
         xTableProps.setPropertyValue("TableColumnSeparators", separators);

         // Although it's a little stupid to increase the row number of the table now,
         // we'll do it here for demonstration purpose
         
         // Get the table rows
         XTableRows tableRows = (XTableRows)UnoRuntime.queryInterface(XTableRows.class, xTable.getRows());
         
         // Insert after the second row 4 new rows
         tableRows.insertByIndex(2,4);
                        
        } catch (Exception e) {
            // Do something!
        }
   }


DannyB wrote:
Some links....

Your link lists contains alway good suggestions. Thank you!

A BIG thank you also to Christian for his advice!

With kind regards
hol.sten
Back to top
View user's profile Send private message
ankush
General User
General User


Joined: 04 Aug 2008
Posts: 15
Location: INDIA, MUMBAI

PostPosted: Wed Aug 06, 2008 10:43 pm    Post subject: Reply with quote

Hello

I tried to create just table in the OOo writer actually with macro.

I used your reference code which is


try {
// Get the text document
XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);

// Get the text
XText xText = xTextDocument.getText();

// Create a text cursor for inserting the new table
XTextCursor xTextCursor = xText.createTextCursor();

// Jump to the end of the text
xTextCursor.gotoEnd(false);

// Create a new text table from the document's factory
XMultiServiceFactory xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xComponent);

// Problem *** nullPointer Exception at following line***

XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.XTextTable"));

// Specify that we want the table to have 4 rows and 4 columns
xTable.initialize(4, 4);

// Insert the table into the document
xText.insertTextContent(xTextCursor, xTable, false);
} catch (Exception e) {
XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xSc.getDocument());
XText xText = xtextdocument.getText();

XTextRange xTextRange = xText.getEnd();
xTextRange.setString("Exception ....AnkusH 015 ::" + e + "\n");
xTextRange = xText.getEnd();
}


but I am getting "java.lang.NullPointerException" exactly at
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));

Can you please help me out to create a table through java macro in the OOo writer?

Thanks in advance
AnkusH
Back to top
View user's profile Send private message
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Fri Aug 08, 2008 11:26 am    Post subject: Reply with quote

ankush wrote:
I tried to create just table in the OOo writer actually with macro.

What did you do with the code from my posting above? A simple copy and paste would most likely have prevented your Exception.

My posting above shows this line:
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));

Your posting shows a slightly different line:
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.XTextTable"));

com.sun.star.text.TextTable is a service and can be used to create an instance. com.sun.star.text.XTextTable is an interface to manage tables. A slight but important difference.


ankush wrote:
I used your reference code which is

No, you didn't Wink
Back to top
View user's profile Send private message
ankush
General User
General User


Joined: 04 Aug 2008
Posts: 15
Location: INDIA, MUMBAI

PostPosted: Sun Aug 10, 2008 10:48 pm    Post subject: Reply with quote

hey Thanks :D
Back to top
View user's profile Send private message
ankush
General User
General User


Joined: 04 Aug 2008
Posts: 15
Location: INDIA, MUMBAI

PostPosted: Wed Aug 13, 2008 12:07 am    Post subject: Reply with quote

Hello

Do you know how to read text and table from the writer?

Thanks in advance
AnkusH
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group