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