1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 import com.sun.star.uno.UnoRuntime;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.text.XText;
34 import com.sun.star.text.XTextTable;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.form.binding.XValueBinding;
37 import com.sun.star.form.binding.XBindableValue;
38 
39 public class ValueBinding extends DocumentBasedExample
40 {
41     /** Creates a new instance of ValueBinding */
42     public ValueBinding()
43     {
44         super( DocumentType.WRITER );
45     }
46 
47     /* ------------------------------------------------------------------ */
48     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
49     {
50         super.prepareDocument();
51 
52         // insert a table with exactly one cell. The content of this table will be synced with
53         // the content of a form control
54         XTextDocument textDoc = (XTextDocument)UnoRuntime.queryInterface( XTextDocument.class,  m_document.getDocument() );
55         XText documentText = textDoc.getText();
56         XTextCursor textCursor = documentText.createTextCursor();
57         documentText.insertString( textCursor, "Below, there's a table cell, and a text field. ", false );
58         documentText.insertString( textCursor, "Both are linked via an external value binding.\n", false );
59         documentText.insertString( textCursor, "That means that anything you insert into the table cell is reflected in the ", false );
60         documentText.insertString( textCursor, "text field, and vice versa.\n", false );
61 
62         XTextTable table = (XTextTable)UnoRuntime.queryInterface( XTextTable.class,
63             m_document.createInstance( "com.sun.star.text.TextTable" )
64         );
65         table.initialize( 1, 1 );
66         documentText.insertTextContent( textCursor, table, false );
67 
68         // insert our sample control
69         XPropertySet textControl = m_formLayer.insertControlLine( "DatabaseTextField", "enter some text", "", 30 );
70 
71         // create a value binding for the first cell of the table
72         XValueBinding cellBinding = new TableCellTextBinding( table.getCellByName( "A1" ) );
73         // and bind it to the control
74         XBindableValue bindable = (XBindableValue)UnoRuntime.queryInterface(
75             XBindableValue.class, textControl
76         );
77         bindable.setValueBinding( cellBinding );
78     }
79 
80     /* ------------------------------------------------------------------ */
81     /** class entry point
82     */
83     public static void main(String argv[]) throws java.lang.Exception
84     {
85         ValueBinding aSample = new ValueBinding();
86         aSample.run( argv );
87     }
88  }
89