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.table.XCellRange; 33 import com.sun.star.table.XCell; 34 import com.sun.star.text.XTextDocument; 35 import com.sun.star.text.XText; 36 import com.sun.star.text.XTextTable; 37 import com.sun.star.text.XTextCursor; 38 import com.sun.star.text.XTextRange; 39 import com.sun.star.form.binding.XValueBinding; 40 import com.sun.star.form.binding.XBindableValue; 41 import com.sun.star.form.binding.XListEntrySource; 42 import com.sun.star.form.binding.XListEntrySink; 43 44 public class SpreadsheetValueBinding extends DocumentBasedExample 45 { 46 /** Creates a new instance of SpreadsheetValueBinding */ 47 public SpreadsheetValueBinding() 48 { 49 super( DocumentType.CALC ); 50 } 51 52 /* ------------------------------------------------------------------ */ 53 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception 54 { 55 super.prepareDocument(); 56 57 SpreadsheetDocument document = (SpreadsheetDocument)m_document; 58 59 final short sheet = (short)0; 60 final short exchangeColumn = (short)1; // B 61 final short exchangeRow = (short)1; // 2 62 final Integer backColor = new Integer( 0x00E0E0E0 ); 63 64 // ---------------------------------------------------------------------- 65 // a numeric control 66 XPropertySet numericControl = m_formLayer.insertControlLine( "NumericField", 67 "enter a value", "", 30 ); 68 numericControl.setPropertyValue( "ValueMin", new Short( (short)1 ) ); 69 numericControl.setPropertyValue( "ValueMax", new Short( (short)5 ) ); 70 numericControl.setPropertyValue( "Value", new Short( (short)1 ) ); 71 numericControl.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) ); 72 numericControl.setPropertyValue( "Spin", new Boolean( true ) ); 73 74 // bind the control model to cell B2 75 implBind( numericControl, document.createCellBinding( sheet, exchangeColumn, exchangeRow ) ); 76 77 // ---------------------------------------------------------------------- 78 // insert a list box 79 XPropertySet listBox = m_formLayer.insertControlLine( "ListBox", 80 "select an entry", "", 2, 40, 20 ); 81 listBox.setPropertyValue( "Dropdown", new Boolean( false ) ); 82 83 // a list binding for cell range C1-C5 84 final short listSourceSheet = (short)1; 85 final short column = (short)0; 86 final short topRow = (short)0; 87 final short bottomRow = (short)4; 88 XListEntrySource entrySource = document.createListEntrySource( 89 listSourceSheet, column, topRow, bottomRow ); 90 91 // bind it to the list box 92 XListEntrySink consumer = (XListEntrySink)UnoRuntime.queryInterface( 93 XListEntrySink.class, listBox ); 94 consumer.setListEntrySource( entrySource ); 95 96 // and also put the list selection index into cell B2 97 implBind( listBox, document.createListIndexBinding( sheet, exchangeColumn, exchangeRow ) ); 98 99 // ---------------------------------------------------------------------- 100 // fill the cells which we just bound the listbox to 101 XCellRange exchangeSheet = document.getSheet( listSourceSheet ); 102 String[] listContent = new String[] { "first", "second", "third", "forth", "fivth" }; 103 for ( short row = topRow; row <= bottomRow; ++row ) 104 { 105 XTextRange cellText = (XTextRange)UnoRuntime.queryInterface( 106 XTextRange.class, exchangeSheet.getCellByPosition( column, row ) ); 107 cellText.setString( listContent[row] ); 108 } 109 110 // some coloring 111 XPropertySet exchangeCell = UNO.queryPropertySet( 112 document.getSheet( sheet ).getCellByPosition( exchangeColumn, exchangeRow ) 113 ); 114 exchangeCell.setPropertyValue( "CellBackColor", backColor ); 115 numericControl.setPropertyValue( "BackgroundColor", backColor ); 116 listBox.setPropertyValue( "BackgroundColor", backColor ); 117 } 118 119 /* ------------------------------------------------------------------ */ 120 private void implBind( XPropertySet controlModel, XValueBinding binding ) throws com.sun.star.form.binding.IncompatibleTypesException 121 { 122 XBindableValue bindable = (XBindableValue)UnoRuntime.queryInterface( 123 XBindableValue.class, controlModel 124 ); 125 bindable.setValueBinding( binding ); 126 } 127 128 /* ------------------------------------------------------------------ */ 129 /** class entry point 130 */ 131 public static void main(String argv[]) throws java.lang.Exception 132 { 133 SpreadsheetValueBinding aSample = new SpreadsheetValueBinding(); 134 aSample.run( argv ); 135 } 136 } 137