xref: /AOO41X/main/odk/examples/DevelopersGuide/Forms/SpreadsheetDocument.java (revision ae15d43ae9bc0d57b88b38bfa728519a0f7db121)
1*ae15d43aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22*ae15d43aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.*;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XComponent;
27cdf0e10cSrcweir import com.sun.star.table.XCellRange;
28cdf0e10cSrcweir import com.sun.star.table.CellAddress;
29cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
30cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
31cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
32cdf0e10cSrcweir import com.sun.star.beans.NamedValue;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /**
35cdf0e10cSrcweir  *
36cdf0e10cSrcweir  * @author  fs93730
37cdf0e10cSrcweir  */
38cdf0e10cSrcweir public class SpreadsheetDocument extends DocumentHelper
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     /** Creates a new blank spreadsheet document */
SpreadsheetDocument( XComponentContext xCtx )41cdf0e10cSrcweir     public SpreadsheetDocument( XComponentContext xCtx ) throws com.sun.star.uno.Exception
42cdf0e10cSrcweir     {
43cdf0e10cSrcweir         super( xCtx, implCreateBlankDocument( xCtx, "private:factory/scalc" ) );
44cdf0e10cSrcweir     }
45cdf0e10cSrcweir 
SpreadsheetDocument( XComponentContext xCtx, XComponent document )46cdf0e10cSrcweir     public SpreadsheetDocument( XComponentContext xCtx, XComponent document ) throws com.sun.star.uno.Exception
47cdf0e10cSrcweir     {
48cdf0e10cSrcweir         super( xCtx, document );
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
getSheet( int index )51cdf0e10cSrcweir     public XCellRange getSheet( int index ) throws com.sun.star.uno.Exception
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         XSpreadsheetDocument spreadsheetDoc = (XSpreadsheetDocument)UnoRuntime.queryInterface( XSpreadsheetDocument.class,
54cdf0e10cSrcweir             m_documentComponent
55cdf0e10cSrcweir         );
56cdf0e10cSrcweir         XIndexAccess sheets = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class,
57cdf0e10cSrcweir             spreadsheetDoc.getSheets()
58cdf0e10cSrcweir         );
59cdf0e10cSrcweir         return (XCellRange)UnoRuntime.queryInterface( XCellRange.class,
60cdf0e10cSrcweir             sheets.getByIndex( index )
61cdf0e10cSrcweir         );
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     /** creates a value binding for a given cell
65cdf0e10cSrcweir     */
createCellBinding( short sheet, short column, short row )66cdf0e10cSrcweir     public com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row )
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         return createCellBinding( sheet, column, row, false );
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /** creates a value binding which can be used to exchange a list box selection <em>index</em> with a cell
72cdf0e10cSrcweir     */
createListIndexBinding( short sheet, short column, short row )73cdf0e10cSrcweir     public com.sun.star.form.binding.XValueBinding createListIndexBinding( short sheet, short column, short row )
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         return createCellBinding( sheet, column, row, true );
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     /** creates a value binding for a given cell, with or without support for integer value exchange
79cdf0e10cSrcweir     */
createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )80cdf0e10cSrcweir     private com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         com.sun.star.form.binding.XValueBinding cellBinding = null;
83cdf0e10cSrcweir         try
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             CellAddress address = new CellAddress( sheet, column, row );
86cdf0e10cSrcweir             Object[] initParam = new Object[] { new NamedValue( "BoundCell", address ) };
87cdf0e10cSrcweir             cellBinding = (com.sun.star.form.binding.XValueBinding)UnoRuntime.queryInterface(
88cdf0e10cSrcweir                 com.sun.star.form.binding.XValueBinding.class,
89cdf0e10cSrcweir                 createInstanceWithArguments(
90cdf0e10cSrcweir                     supportIntegerValues ? "com.sun.star.table.ListPositionCellBinding"
91cdf0e10cSrcweir                                          : "com.sun.star.table.CellValueBinding",
92cdf0e10cSrcweir                     initParam
93cdf0e10cSrcweir                 )
94cdf0e10cSrcweir             );
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             System.err.println( e );
99cdf0e10cSrcweir             e.printStackTrace( System.err );
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir         return cellBinding;
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     /** creates a source of list entries associated with a (one-column) cell range
105cdf0e10cSrcweir     */
createListEntrySource( short sheet, short column, short topRow, short bottomRow )106cdf0e10cSrcweir     public com.sun.star.form.binding.XListEntrySource createListEntrySource( short sheet, short column,
107cdf0e10cSrcweir         short topRow, short bottomRow )
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         com.sun.star.form.binding.XListEntrySource entrySource = null;
110cdf0e10cSrcweir         try
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             CellRangeAddress rangeAddress = new CellRangeAddress( sheet, column,
113cdf0e10cSrcweir                 topRow, column, bottomRow );
114cdf0e10cSrcweir             Object[] initParam = new Object[] { new NamedValue( "CellRange", rangeAddress ) };
115cdf0e10cSrcweir             entrySource = (com.sun.star.form.binding.XListEntrySource)UnoRuntime.queryInterface(
116cdf0e10cSrcweir                 com.sun.star.form.binding.XListEntrySource.class,
117cdf0e10cSrcweir                 createInstanceWithArguments(
118cdf0e10cSrcweir                     "com.sun.star.table.CellRangeListSource", initParam ) );
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e )
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             System.err.println( e );
123cdf0e10cSrcweir             e.printStackTrace( System.err );
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir         return entrySource;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir }
128