1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 11 * Licensed to the Apache Software Foundation (ASF) under one or more 12 * contributor license agreements. See the NOTICE file distributed 13 * with this work for additional information regarding copyright 14 * ownership. The ASF licenses this file to you under the Apache 15 * License, Version 2.0 (the "License"); you may not use this file 16 * except in compliance with the License. You may obtain a copy of 17 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 18 */ 19 20 import com.sun.star.uno.UnoRuntime; 21 22 import com.sun.star.beans.XPropertySet; 23 import com.sun.star.text.XTextDocument; 24 import com.sun.star.text.XText; 25 import com.sun.star.text.XTextTable; 26 import com.sun.star.text.XTextCursor; 27 import com.sun.star.form.binding.XValueBinding; 28 import com.sun.star.form.binding.XBindableValue; 29 30 public class ValueBinding extends DocumentBasedExample 31 { 32 /** Creates a new instance of ValueBinding */ ValueBinding()33 public ValueBinding() 34 { 35 super( DocumentType.WRITER ); 36 } 37 38 /* ------------------------------------------------------------------ */ 39 @Override prepareDocument()40 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception 41 { 42 super.prepareDocument(); 43 44 // insert a table with exactly one cell. The content of this table will be synced with 45 // the content of a form control 46 XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, m_document.getDocument() ); 47 XText documentText = textDoc.getText(); 48 XTextCursor textCursor = documentText.createTextCursor(); 49 documentText.insertString( textCursor, "Below, there's a table cell, and a text field. ", false ); 50 documentText.insertString( textCursor, "Both are linked via an external value binding.\n", false ); 51 documentText.insertString( textCursor, "That means that anything you insert into the table cell is reflected in the ", false ); 52 documentText.insertString( textCursor, "text field, and vice versa.\n", false ); 53 54 XTextTable table = UnoRuntime.queryInterface( XTextTable.class, 55 m_document.createInstance( "com.sun.star.text.TextTable" ) 56 ); 57 table.initialize( 1, 1 ); 58 documentText.insertTextContent( textCursor, table, false ); 59 60 // insert our sample control 61 XPropertySet textControl = m_formLayer.insertControlLine( "DatabaseTextField", "enter some text", "", 30 ); 62 63 // create a value binding for the first cell of the table 64 XValueBinding cellBinding = new TableCellTextBinding( table.getCellByName( "A1" ) ); 65 // and bind it to the control 66 XBindableValue bindable = UnoRuntime.queryInterface( 67 XBindableValue.class, textControl 68 ); 69 bindable.setValueBinding( cellBinding ); 70 } 71 72 /* ------------------------------------------------------------------ */ 73 /** class entry point 74 */ main(String argv[])75 public static void main(String argv[]) throws java.lang.Exception 76 { 77 ValueBinding aSample = new ValueBinding(); 78 aSample.run( argv ); 79 } 80 } 81 82 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 83