1*ae15d43aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ae15d43aSAndrew Rist  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ae15d43aSAndrew Rist  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19*ae15d43aSAndrew Rist  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22*ae15d43aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
29cdf0e10cSrcweir import com.sun.star.text.XText;
30cdf0e10cSrcweir import com.sun.star.text.XTextTable;
31cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
32cdf0e10cSrcweir import com.sun.star.form.binding.XValueBinding;
33cdf0e10cSrcweir import com.sun.star.form.binding.XBindableValue;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir public class ValueBinding extends DocumentBasedExample
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     /** Creates a new instance of ValueBinding */
ValueBinding()38cdf0e10cSrcweir     public ValueBinding()
39cdf0e10cSrcweir     {
40cdf0e10cSrcweir         super( DocumentType.WRITER );
41cdf0e10cSrcweir     }
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
prepareDocument()44cdf0e10cSrcweir     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         super.prepareDocument();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         // insert a table with exactly one cell. The content of this table will be synced with
49cdf0e10cSrcweir         // the content of a form control
50cdf0e10cSrcweir         XTextDocument textDoc = (XTextDocument)UnoRuntime.queryInterface( XTextDocument.class,  m_document.getDocument() );
51cdf0e10cSrcweir         XText documentText = textDoc.getText();
52cdf0e10cSrcweir         XTextCursor textCursor = documentText.createTextCursor();
53cdf0e10cSrcweir         documentText.insertString( textCursor, "Below, there's a table cell, and a text field. ", false );
54cdf0e10cSrcweir         documentText.insertString( textCursor, "Both are linked via an external value binding.\n", false );
55cdf0e10cSrcweir         documentText.insertString( textCursor, "That means that anything you insert into the table cell is reflected in the ", false );
56cdf0e10cSrcweir         documentText.insertString( textCursor, "text field, and vice versa.\n", false );
57cdf0e10cSrcweir 
58cdf0e10cSrcweir         XTextTable table = (XTextTable)UnoRuntime.queryInterface( XTextTable.class,
59cdf0e10cSrcweir             m_document.createInstance( "com.sun.star.text.TextTable" )
60cdf0e10cSrcweir         );
61cdf0e10cSrcweir         table.initialize( 1, 1 );
62cdf0e10cSrcweir         documentText.insertTextContent( textCursor, table, false );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         // insert our sample control
65cdf0e10cSrcweir         XPropertySet textControl = m_formLayer.insertControlLine( "DatabaseTextField", "enter some text", "", 30 );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         // create a value binding for the first cell of the table
68cdf0e10cSrcweir         XValueBinding cellBinding = new TableCellTextBinding( table.getCellByName( "A1" ) );
69cdf0e10cSrcweir         // and bind it to the control
70cdf0e10cSrcweir         XBindableValue bindable = (XBindableValue)UnoRuntime.queryInterface(
71cdf0e10cSrcweir             XBindableValue.class, textControl
72cdf0e10cSrcweir         );
73cdf0e10cSrcweir         bindable.setValueBinding( cellBinding );
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
77cdf0e10cSrcweir     /** class entry point
78cdf0e10cSrcweir     */
main(String argv[])79cdf0e10cSrcweir     public static void main(String argv[]) throws java.lang.Exception
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         ValueBinding aSample = new ValueBinding();
82cdf0e10cSrcweir         aSample.run( argv );
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir  }
85