1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SpreadsheetValueBinding.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 import com
.sun
.star
.uno
.UnoRuntime
;
33 import com
.sun
.star
.lang
.XMultiServiceFactory
;
34 import com
.sun
.star
.beans
.XPropertySet
;
35 import com
.sun
.star
.table
.XCellRange
;
36 import com
.sun
.star
.table
.XCell
;
37 import com
.sun
.star
.text
.XTextDocument
;
38 import com
.sun
.star
.text
.XText
;
39 import com
.sun
.star
.text
.XTextTable
;
40 import com
.sun
.star
.text
.XTextCursor
;
41 import com
.sun
.star
.text
.XTextRange
;
42 import com
.sun
.star
.form
.binding
.XValueBinding
;
43 import com
.sun
.star
.form
.binding
.XBindableValue
;
44 import com
.sun
.star
.form
.binding
.XListEntrySource
;
45 import com
.sun
.star
.form
.binding
.XListEntrySink
;
47 public class SpreadsheetValueBinding
extends DocumentBasedExample
49 /** Creates a new instance of SpreadsheetValueBinding */
50 public SpreadsheetValueBinding()
52 super( DocumentType
.CALC
);
55 /* ------------------------------------------------------------------ */
56 protected void prepareDocument() throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
58 super.prepareDocument();
60 SpreadsheetDocument document
= (SpreadsheetDocument
)m_document
;
62 final short sheet
= (short)0;
63 final short exchangeColumn
= (short)1; // B
64 final short exchangeRow
= (short)1; // 2
65 final Integer backColor
= new Integer( 0x00E0E0E0 );
67 // ----------------------------------------------------------------------
69 XPropertySet numericControl
= m_formLayer
.insertControlLine( "NumericField",
70 "enter a value", "", 30 );
71 numericControl
.setPropertyValue( "ValueMin", new Short( (short)1 ) );
72 numericControl
.setPropertyValue( "ValueMax", new Short( (short)5 ) );
73 numericControl
.setPropertyValue( "Value", new Short( (short)1 ) );
74 numericControl
.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) );
75 numericControl
.setPropertyValue( "Spin", new Boolean( true ) );
77 // bind the control model to cell B2
78 implBind( numericControl
, document
.createCellBinding( sheet
, exchangeColumn
, exchangeRow
) );
80 // ----------------------------------------------------------------------
82 XPropertySet listBox
= m_formLayer
.insertControlLine( "ListBox",
83 "select an entry", "", 2, 40, 20 );
84 listBox
.setPropertyValue( "Dropdown", new Boolean( false ) );
86 // a list binding for cell range C1-C5
87 final short listSourceSheet
= (short)1;
88 final short column
= (short)0;
89 final short topRow
= (short)0;
90 final short bottomRow
= (short)4;
91 XListEntrySource entrySource
= document
.createListEntrySource(
92 listSourceSheet
, column
, topRow
, bottomRow
);
94 // bind it to the list box
95 XListEntrySink consumer
= (XListEntrySink
)UnoRuntime
.queryInterface(
96 XListEntrySink
.class, listBox
);
97 consumer
.setListEntrySource( entrySource
);
99 // and also put the list selection index into cell B2
100 implBind( listBox
, document
.createListIndexBinding( sheet
, exchangeColumn
, exchangeRow
) );
102 // ----------------------------------------------------------------------
103 // fill the cells which we just bound the listbox to
104 XCellRange exchangeSheet
= document
.getSheet( listSourceSheet
);
105 String
[] listContent
= new String
[] { "first", "second", "third", "forth", "fivth" };
106 for ( short row
= topRow
; row
<= bottomRow
; ++row
)
108 XTextRange cellText
= (XTextRange
)UnoRuntime
.queryInterface(
109 XTextRange
.class, exchangeSheet
.getCellByPosition( column
, row
) );
110 cellText
.setString( listContent
[row
] );
114 XPropertySet exchangeCell
= UNO
.queryPropertySet(
115 document
.getSheet( sheet
).getCellByPosition( exchangeColumn
, exchangeRow
)
117 exchangeCell
.setPropertyValue( "CellBackColor", backColor
);
118 numericControl
.setPropertyValue( "BackgroundColor", backColor
);
119 listBox
.setPropertyValue( "BackgroundColor", backColor
);
122 /* ------------------------------------------------------------------ */
123 private void implBind( XPropertySet controlModel
, XValueBinding binding
) throws com
.sun
.star
.form
.binding
.IncompatibleTypesException
125 XBindableValue bindable
= (XBindableValue
)UnoRuntime
.queryInterface(
126 XBindableValue
.class, controlModel
128 bindable
.setValueBinding( binding
);
131 /* ------------------------------------------------------------------ */
132 /** class entry point
134 public static void main(String argv
[]) throws java
.lang
.Exception
136 SpreadsheetValueBinding aSample
= new SpreadsheetValueBinding();