Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / SpreadsheetValueBinding.java
blobd860c2c6c840e2dee3db8b3dc377c085192f0aec
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.uno.UnoRuntime;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.table.XCellRange;
23 import com.sun.star.text.XTextRange;
24 import com.sun.star.form.binding.XValueBinding;
25 import com.sun.star.form.binding.XBindableValue;
26 import com.sun.star.form.binding.XListEntrySource;
27 import com.sun.star.form.binding.XListEntrySink;
29 public class SpreadsheetValueBinding extends DocumentBasedExample
31 /** Creates a new instance of SpreadsheetValueBinding */
32 public SpreadsheetValueBinding()
34 super( DocumentType.CALC );
37 /* ------------------------------------------------------------------ */
38 @Override
39 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
41 super.prepareDocument();
43 SpreadsheetDocument document = (SpreadsheetDocument)m_document;
45 final short sheet = (short)0;
46 final short exchangeColumn = (short)1; // B
47 final short exchangeRow = (short)1; // 2
48 final Integer backColor = Integer.valueOf( 0x00E0E0E0 );
51 // a numeric control
52 XPropertySet numericControl = m_formLayer.insertControlLine( "NumericField",
53 "enter a value", "", 30 );
54 numericControl.setPropertyValue( "ValueMin", Short.valueOf( (short)1 ) );
55 numericControl.setPropertyValue( "ValueMax", Short.valueOf( (short)5 ) );
56 numericControl.setPropertyValue( "Value", Short.valueOf( (short)1 ) );
57 numericControl.setPropertyValue( "DecimalAccuracy", Short.valueOf( (short)0 ) );
58 numericControl.setPropertyValue( "Spin", Boolean.TRUE );
60 // bind the control model to cell B2
61 implBind( numericControl, document.createCellBinding( sheet, exchangeColumn, exchangeRow ) );
64 // insert a list box
65 XPropertySet listBox = m_formLayer.insertControlLine( "ListBox",
66 "select an entry", "", 2, 40, 20 );
67 listBox.setPropertyValue( "Dropdown", Boolean.FALSE );
69 // a list binding for cell range C1-C5
70 final short listSourceSheet = (short)1;
71 final short column = (short)0;
72 final short topRow = (short)0;
73 final short bottomRow = (short)4;
74 XListEntrySource entrySource = document.createListEntrySource(
75 listSourceSheet, column, topRow, bottomRow );
77 // bind it to the list box
78 XListEntrySink consumer = UnoRuntime.queryInterface(
79 XListEntrySink.class, listBox );
80 consumer.setListEntrySource( entrySource );
82 // and also put the list selection index into cell B2
83 implBind( listBox, document.createListIndexBinding( sheet, exchangeColumn, exchangeRow ) );
86 // fill the cells which we just bound the listbox to
87 XCellRange exchangeSheet = document.getSheet( listSourceSheet );
88 String[] listContent = new String[] { "first", "second", "third", "forth", "fivth" };
89 for ( short row = topRow; row <= bottomRow; ++row )
91 XTextRange cellText = UnoRuntime.queryInterface(
92 XTextRange.class, exchangeSheet.getCellByPosition( column, row ) );
93 cellText.setString( listContent[row] );
96 // some coloring
97 XPropertySet exchangeCell = UNO.queryPropertySet(
98 document.getSheet( sheet ).getCellByPosition( exchangeColumn, exchangeRow )
100 exchangeCell.setPropertyValue( "CellBackColor", backColor );
101 numericControl.setPropertyValue( "BackgroundColor", backColor );
102 listBox.setPropertyValue( "BackgroundColor", backColor );
105 /* ------------------------------------------------------------------ */
106 private void implBind( XPropertySet controlModel, XValueBinding binding ) throws com.sun.star.form.binding.IncompatibleTypesException
108 XBindableValue bindable = UnoRuntime.queryInterface(
109 XBindableValue.class, controlModel
111 bindable.setValueBinding( binding );
114 /* ------------------------------------------------------------------ */
115 /** class entry point
117 public static void main(String argv[]) throws java.lang.Exception
119 SpreadsheetValueBinding aSample = new SpreadsheetValueBinding();
120 aSample.run( argv );