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.*;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XComponent;
27 import com.sun.star.table.XCellRange;
28 import com.sun.star.table.CellAddress;
29 import com.sun.star.table.CellRangeAddress;
30 import com.sun.star.container.XIndexAccess;
31 import com.sun.star.sheet.XSpreadsheetDocument;
32 import com.sun.star.beans.NamedValue;
33 
34 /**
35  *
36  * @author  fs93730
37  */
38 public class SpreadsheetDocument extends DocumentHelper
39 {
40     /** Creates a new blank spreadsheet document */
SpreadsheetDocument( XComponentContext xCtx )41     public SpreadsheetDocument( XComponentContext xCtx ) throws com.sun.star.uno.Exception
42     {
43         super( xCtx, implCreateBlankDocument( xCtx, "private:factory/scalc" ) );
44     }
45 
SpreadsheetDocument( XComponentContext xCtx, XComponent document )46     public SpreadsheetDocument( XComponentContext xCtx, XComponent document ) throws com.sun.star.uno.Exception
47     {
48         super( xCtx, document );
49     }
50 
getSheet( int index )51     public XCellRange getSheet( int index ) throws com.sun.star.uno.Exception
52     {
53         XSpreadsheetDocument spreadsheetDoc = (XSpreadsheetDocument)UnoRuntime.queryInterface( XSpreadsheetDocument.class,
54             m_documentComponent
55         );
56         XIndexAccess sheets = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class,
57             spreadsheetDoc.getSheets()
58         );
59         return (XCellRange)UnoRuntime.queryInterface( XCellRange.class,
60             sheets.getByIndex( index )
61         );
62     }
63 
64     /** creates a value binding for a given cell
65     */
createCellBinding( short sheet, short column, short row )66     public com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row )
67     {
68         return createCellBinding( sheet, column, row, false );
69     }
70 
71     /** creates a value binding which can be used to exchange a list box selection <em>index</em> with a cell
72     */
createListIndexBinding( short sheet, short column, short row )73     public com.sun.star.form.binding.XValueBinding createListIndexBinding( short sheet, short column, short row )
74     {
75         return createCellBinding( sheet, column, row, true );
76     }
77 
78     /** creates a value binding for a given cell, with or without support for integer value exchange
79     */
createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )80     private com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )
81     {
82         com.sun.star.form.binding.XValueBinding cellBinding = null;
83         try
84         {
85             CellAddress address = new CellAddress( sheet, column, row );
86             Object[] initParam = new Object[] { new NamedValue( "BoundCell", address ) };
87             cellBinding = (com.sun.star.form.binding.XValueBinding)UnoRuntime.queryInterface(
88                 com.sun.star.form.binding.XValueBinding.class,
89                 createInstanceWithArguments(
90                     supportIntegerValues ? "com.sun.star.table.ListPositionCellBinding"
91                                          : "com.sun.star.table.CellValueBinding",
92                     initParam
93                 )
94             );
95         }
96         catch( com.sun.star.uno.Exception e )
97         {
98             System.err.println( e );
99             e.printStackTrace( System.err );
100         }
101         return cellBinding;
102     }
103 
104     /** creates a source of list entries associated with a (one-column) cell range
105     */
createListEntrySource( short sheet, short column, short topRow, short bottomRow )106     public com.sun.star.form.binding.XListEntrySource createListEntrySource( short sheet, short column,
107         short topRow, short bottomRow )
108     {
109         com.sun.star.form.binding.XListEntrySource entrySource = null;
110         try
111         {
112             CellRangeAddress rangeAddress = new CellRangeAddress( sheet, column,
113                 topRow, column, bottomRow );
114             Object[] initParam = new Object[] { new NamedValue( "CellRange", rangeAddress ) };
115             entrySource = (com.sun.star.form.binding.XListEntrySource)UnoRuntime.queryInterface(
116                 com.sun.star.form.binding.XListEntrySource.class,
117                 createInstanceWithArguments(
118                     "com.sun.star.table.CellRangeListSource", initParam ) );
119         }
120         catch( com.sun.star.uno.Exception e )
121         {
122             System.err.println( e );
123             e.printStackTrace( System.err );
124         }
125         return entrySource;
126     }
127 }
128