1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.uno.UnoRuntime;
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.table.XCellRange;
29 import com.sun.star.table.XCell;
30 import com.sun.star.text.XTextDocument;
31 import com.sun.star.text.XText;
32 import com.sun.star.text.XTextTable;
33 import com.sun.star.text.XTextCursor;
34 import com.sun.star.text.XTextRange;
35 import com.sun.star.form.binding.XValueBinding;
36 import com.sun.star.form.binding.XBindableValue;
37 import com.sun.star.form.binding.XListEntrySource;
38 import com.sun.star.form.binding.XListEntrySink;
39 
40 public class SpreadsheetValueBinding extends DocumentBasedExample
41 {
42     /** Creates a new instance of SpreadsheetValueBinding */
SpreadsheetValueBinding()43     public SpreadsheetValueBinding()
44     {
45         super( DocumentType.CALC );
46     }
47 
48     /* ------------------------------------------------------------------ */
prepareDocument()49     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
50     {
51         super.prepareDocument();
52 
53         SpreadsheetDocument document = (SpreadsheetDocument)m_document;
54 
55         final short sheet = (short)0;
56         final short exchangeColumn = (short)1;  // B
57         final short exchangeRow = (short)1;     // 2
58         final Integer backColor = new Integer( 0x00E0E0E0 );
59 
60         // ----------------------------------------------------------------------
61         // a numeric control
62         XPropertySet numericControl = m_formLayer.insertControlLine( "NumericField",
63             "enter a value", "", 30 );
64         numericControl.setPropertyValue( "ValueMin", new Short( (short)1 ) );
65         numericControl.setPropertyValue( "ValueMax", new Short( (short)5 ) );
66         numericControl.setPropertyValue( "Value", new Short( (short)1 ) );
67         numericControl.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) );
68         numericControl.setPropertyValue( "Spin", new Boolean( true ) );
69 
70         // bind the control model to cell B2
71         implBind( numericControl, document.createCellBinding( sheet, exchangeColumn, exchangeRow ) );
72 
73         // ----------------------------------------------------------------------
74         // insert a list box
75         XPropertySet listBox = m_formLayer.insertControlLine( "ListBox",
76             "select  an entry", "", 2, 40, 20 );
77         listBox.setPropertyValue( "Dropdown", new Boolean( false ) );
78 
79         // a list binding for cell range C1-C5
80         final short listSourceSheet = (short)1;
81         final short column = (short)0;
82         final short topRow = (short)0;
83         final short bottomRow = (short)4;
84         XListEntrySource entrySource = document.createListEntrySource(
85             listSourceSheet, column, topRow, bottomRow );
86 
87         // bind it to the list box
88         XListEntrySink consumer = (XListEntrySink)UnoRuntime.queryInterface(
89             XListEntrySink.class, listBox );
90         consumer.setListEntrySource( entrySource );
91 
92         // and also put the list selection index into cell B2
93         implBind( listBox, document.createListIndexBinding( sheet, exchangeColumn, exchangeRow ) );
94 
95         // ----------------------------------------------------------------------
96         // fill the cells which we just bound the listbox to
97         XCellRange exchangeSheet = document.getSheet( listSourceSheet );
98         String[] listContent = new String[] { "first", "second", "third", "forth", "fivth" };
99         for ( short row = topRow; row <= bottomRow; ++row )
100         {
101             XTextRange cellText = (XTextRange)UnoRuntime.queryInterface(
102                 XTextRange.class, exchangeSheet.getCellByPosition( column, row ) );
103             cellText.setString( listContent[row] );
104         }
105 
106         // some coloring
107         XPropertySet exchangeCell = UNO.queryPropertySet(
108             document.getSheet( sheet ).getCellByPosition( exchangeColumn, exchangeRow )
109         );
110         exchangeCell.setPropertyValue( "CellBackColor", backColor );
111         numericControl.setPropertyValue( "BackgroundColor", backColor );
112         listBox.setPropertyValue( "BackgroundColor", backColor );
113     }
114 
115     /* ------------------------------------------------------------------ */
implBind( XPropertySet controlModel, XValueBinding binding )116     private void implBind( XPropertySet controlModel, XValueBinding binding ) throws com.sun.star.form.binding.IncompatibleTypesException
117     {
118         XBindableValue bindable = (XBindableValue)UnoRuntime.queryInterface(
119             XBindableValue.class, controlModel
120         );
121         bindable.setValueBinding( binding );
122     }
123 
124     /* ------------------------------------------------------------------ */
125     /** class entry point
126     */
main(String argv[])127     public static void main(String argv[]) throws java.lang.Exception
128     {
129         SpreadsheetValueBinding aSample = new SpreadsheetValueBinding();
130         aSample.run( argv );
131     }
132  }
133