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