Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / SpreadsheetValueBinding.java
blob1f08f2e7e796a17c95ab467d14b17b7be5915889
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 import com.sun.star.uno.UnoRuntime;
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;
30 public class SpreadsheetValueBinding extends DocumentBasedExample
32 /** Creates a new instance of SpreadsheetValueBinding */
33 public SpreadsheetValueBinding()
35 super( DocumentType.CALC );
38 /* ------------------------------------------------------------------ */
39 @Override
40 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
42 super.prepareDocument();
44 SpreadsheetDocument document = (SpreadsheetDocument)m_document;
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 );
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 );
61 // bind the control model to cell B2
62 implBind( numericControl, document.createCellBinding( sheet, exchangeColumn, exchangeRow ) );
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 );
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 );
78 // bind it to the list box
79 XListEntrySink consumer = UnoRuntime.queryInterface(
80 XListEntrySink.class, listBox );
81 consumer.setListEntrySource( entrySource );
83 // and also put the list selection index into cell B2
84 implBind( listBox, document.createListIndexBinding( sheet, exchangeColumn, exchangeRow ) );
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 )
92 XTextRange cellText = UnoRuntime.queryInterface(
93 XTextRange.class, exchangeSheet.getCellByPosition( column, row ) );
94 cellText.setString( listContent[row] );
97 // some coloring
98 XPropertySet exchangeCell = UNO.queryPropertySet(
99 document.getSheet( sheet ).getCellByPosition( exchangeColumn, exchangeRow )
101 exchangeCell.setPropertyValue( "CellBackColor", backColor );
102 numericControl.setPropertyValue( "BackgroundColor", backColor );
103 listBox.setPropertyValue( "BackgroundColor", backColor );
106 /* ------------------------------------------------------------------ */
107 private void implBind( XPropertySet controlModel, XValueBinding binding ) throws com.sun.star.form.binding.IncompatibleTypesException
109 XBindableValue bindable = UnoRuntime.queryInterface(
110 XBindableValue.class, controlModel
112 bindable.setValueBinding( binding );
115 /* ------------------------------------------------------------------ */
116 /** class entry point
118 public static void main(String argv[]) throws java.lang.Exception
120 SpreadsheetValueBinding aSample = new SpreadsheetValueBinding();
121 aSample.run( argv );
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */