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