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