1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 31*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 32*cdf0e10cSrcweir import com.sun.star.table.XCellRange; 33*cdf0e10cSrcweir import com.sun.star.table.XCell; 34*cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 35*cdf0e10cSrcweir import com.sun.star.text.XText; 36*cdf0e10cSrcweir import com.sun.star.text.XTextTable; 37*cdf0e10cSrcweir import com.sun.star.text.XTextCursor; 38*cdf0e10cSrcweir import com.sun.star.text.XTextRange; 39*cdf0e10cSrcweir import com.sun.star.form.binding.XValueBinding; 40*cdf0e10cSrcweir import com.sun.star.form.binding.XBindableValue; 41*cdf0e10cSrcweir import com.sun.star.form.binding.XListEntrySource; 42*cdf0e10cSrcweir import com.sun.star.form.binding.XListEntrySink; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir public class SpreadsheetValueBinding extends DocumentBasedExample 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir /** Creates a new instance of SpreadsheetValueBinding */ 47*cdf0e10cSrcweir public SpreadsheetValueBinding() 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir super( DocumentType.CALC ); 50*cdf0e10cSrcweir } 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 53*cdf0e10cSrcweir protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir super.prepareDocument(); 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir SpreadsheetDocument document = (SpreadsheetDocument)m_document; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir final short sheet = (short)0; 60*cdf0e10cSrcweir final short exchangeColumn = (short)1; // B 61*cdf0e10cSrcweir final short exchangeRow = (short)1; // 2 62*cdf0e10cSrcweir final Integer backColor = new Integer( 0x00E0E0E0 ); 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir // ---------------------------------------------------------------------- 65*cdf0e10cSrcweir // a numeric control 66*cdf0e10cSrcweir XPropertySet numericControl = m_formLayer.insertControlLine( "NumericField", 67*cdf0e10cSrcweir "enter a value", "", 30 ); 68*cdf0e10cSrcweir numericControl.setPropertyValue( "ValueMin", new Short( (short)1 ) ); 69*cdf0e10cSrcweir numericControl.setPropertyValue( "ValueMax", new Short( (short)5 ) ); 70*cdf0e10cSrcweir numericControl.setPropertyValue( "Value", new Short( (short)1 ) ); 71*cdf0e10cSrcweir numericControl.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) ); 72*cdf0e10cSrcweir numericControl.setPropertyValue( "Spin", new Boolean( true ) ); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir // bind the control model to cell B2 75*cdf0e10cSrcweir implBind( numericControl, document.createCellBinding( sheet, exchangeColumn, exchangeRow ) ); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir // ---------------------------------------------------------------------- 78*cdf0e10cSrcweir // insert a list box 79*cdf0e10cSrcweir XPropertySet listBox = m_formLayer.insertControlLine( "ListBox", 80*cdf0e10cSrcweir "select an entry", "", 2, 40, 20 ); 81*cdf0e10cSrcweir listBox.setPropertyValue( "Dropdown", new Boolean( false ) ); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir // a list binding for cell range C1-C5 84*cdf0e10cSrcweir final short listSourceSheet = (short)1; 85*cdf0e10cSrcweir final short column = (short)0; 86*cdf0e10cSrcweir final short topRow = (short)0; 87*cdf0e10cSrcweir final short bottomRow = (short)4; 88*cdf0e10cSrcweir XListEntrySource entrySource = document.createListEntrySource( 89*cdf0e10cSrcweir listSourceSheet, column, topRow, bottomRow ); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir // bind it to the list box 92*cdf0e10cSrcweir XListEntrySink consumer = (XListEntrySink)UnoRuntime.queryInterface( 93*cdf0e10cSrcweir XListEntrySink.class, listBox ); 94*cdf0e10cSrcweir consumer.setListEntrySource( entrySource ); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir // and also put the list selection index into cell B2 97*cdf0e10cSrcweir implBind( listBox, document.createListIndexBinding( sheet, exchangeColumn, exchangeRow ) ); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir // ---------------------------------------------------------------------- 100*cdf0e10cSrcweir // fill the cells which we just bound the listbox to 101*cdf0e10cSrcweir XCellRange exchangeSheet = document.getSheet( listSourceSheet ); 102*cdf0e10cSrcweir String[] listContent = new String[] { "first", "second", "third", "forth", "fivth" }; 103*cdf0e10cSrcweir for ( short row = topRow; row <= bottomRow; ++row ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir XTextRange cellText = (XTextRange)UnoRuntime.queryInterface( 106*cdf0e10cSrcweir XTextRange.class, exchangeSheet.getCellByPosition( column, row ) ); 107*cdf0e10cSrcweir cellText.setString( listContent[row] ); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // some coloring 111*cdf0e10cSrcweir XPropertySet exchangeCell = UNO.queryPropertySet( 112*cdf0e10cSrcweir document.getSheet( sheet ).getCellByPosition( exchangeColumn, exchangeRow ) 113*cdf0e10cSrcweir ); 114*cdf0e10cSrcweir exchangeCell.setPropertyValue( "CellBackColor", backColor ); 115*cdf0e10cSrcweir numericControl.setPropertyValue( "BackgroundColor", backColor ); 116*cdf0e10cSrcweir listBox.setPropertyValue( "BackgroundColor", backColor ); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 120*cdf0e10cSrcweir private void implBind( XPropertySet controlModel, XValueBinding binding ) throws com.sun.star.form.binding.IncompatibleTypesException 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir XBindableValue bindable = (XBindableValue)UnoRuntime.queryInterface( 123*cdf0e10cSrcweir XBindableValue.class, controlModel 124*cdf0e10cSrcweir ); 125*cdf0e10cSrcweir bindable.setValueBinding( binding ); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 129*cdf0e10cSrcweir /** class entry point 130*cdf0e10cSrcweir */ 131*cdf0e10cSrcweir public static void main(String argv[]) throws java.lang.Exception 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir SpreadsheetValueBinding aSample = new SpreadsheetValueBinding(); 134*cdf0e10cSrcweir aSample.run( argv ); 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir } 137