xref: /trunk/main/forms/qa/integration/forms/SpreadsheetDocument.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package integration.forms;
29 
30 import com.sun.star.uno.*;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.table.XCellRange;
34 import com.sun.star.table.CellAddress;
35 import com.sun.star.table.CellRangeAddress;
36 import com.sun.star.container.XIndexAccess;
37 import com.sun.star.sheet.XSpreadsheetDocument;
38 import com.sun.star.sheet.XSpreadsheets;
39 import com.sun.star.beans.NamedValue;
40 
41 /**
42  *
43  * @author  fs93730
44  */
45 public class SpreadsheetDocument extends DocumentHelper
46 {
47     /** Creates a new blank spreadsheet document */
48     /* ------------------------------------------------------------------ */
49     public SpreadsheetDocument( XMultiServiceFactory orb ) throws com.sun.star.uno.Exception
50     {
51         super( orb, implLoadAsComponent( orb, "private:factory/scalc" ) );
52     }
53 
54     /* ------------------------------------------------------------------ */
55     public SpreadsheetDocument( XMultiServiceFactory orb, XComponent document ) throws com.sun.star.uno.Exception
56     {
57         super( orb, document );
58     }
59 
60     /* ------------------------------------------------------------------ */
61     /** returns the sheets collection
62     */
63     public XSpreadsheets getSheets() throws com.sun.star.uno.Exception
64     {
65         XSpreadsheetDocument spreadsheetDoc = (XSpreadsheetDocument)UnoRuntime.queryInterface( XSpreadsheetDocument.class,
66             getDocument()
67         );
68         return spreadsheetDoc.getSheets();
69     }
70 
71     /* ------------------------------------------------------------------ */
72     /** returns the sheet with the given index
73     */
74     public XCellRange getSheet( int index ) throws com.sun.star.uno.Exception
75     {
76         XIndexAccess sheets = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class,
77             getSheets()
78         );
79         return (XCellRange)UnoRuntime.queryInterface( XCellRange.class,
80             sheets.getByIndex( index )
81         );
82     }
83 
84     /* ------------------------------------------------------------------ */
85     /** creates a value binding for a given cell
86     */
87     public com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row )
88     {
89         return createCellBinding( sheet, column, row, false );
90     }
91 
92     /* ------------------------------------------------------------------ */
93     /** creates a value binding which can be used to exchange a list box selection <em>index</em> with a cell
94     */
95     public com.sun.star.form.binding.XValueBinding createListIndexBinding( short sheet, short column, short row )
96     {
97         return createCellBinding( sheet, column, row, true );
98     }
99 
100     /* ------------------------------------------------------------------ */
101     /** creates a value binding for a given cell, with or without support for integer value exchange
102     */
103     private com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )
104     {
105         com.sun.star.form.binding.XValueBinding cellBinding = null;
106         try
107         {
108             CellAddress address = new CellAddress( sheet, column, row );
109             Object[] initParam = new Object[] { new NamedValue( "BoundCell", address ) };
110             cellBinding = (com.sun.star.form.binding.XValueBinding)UnoRuntime.queryInterface(
111                 com.sun.star.form.binding.XValueBinding.class,
112                 createInstanceWithArguments(
113                     supportIntegerValues ? "com.sun.star.table.ListPositionCellBinding"
114                                          : "com.sun.star.table.CellValueBinding",
115                     initParam
116                 )
117             );
118         }
119         catch( com.sun.star.uno.Exception e )
120         {
121             System.err.println( e );
122             e.printStackTrace( System.err );
123         }
124         return cellBinding;
125     }
126 
127     /* ------------------------------------------------------------------ */
128     /** creates a source of list entries associated with a (one-column) cell range
129     */
130     public com.sun.star.form.binding.XListEntrySource createListEntrySource( short sheet, short column,
131         short topRow, short bottomRow )
132     {
133         com.sun.star.form.binding.XListEntrySource entrySource = null;
134         try
135         {
136             CellRangeAddress rangeAddress = new CellRangeAddress( sheet, column,
137                 topRow, column, bottomRow );
138             Object[] initParam = new Object[] { new NamedValue( "CellRange", rangeAddress ) };
139             entrySource = (com.sun.star.form.binding.XListEntrySource)UnoRuntime.queryInterface(
140                 com.sun.star.form.binding.XListEntrySource.class,
141                 createInstanceWithArguments(
142                     "com.sun.star.table.CellRangeListSource", initParam ) );
143         }
144         catch( com.sun.star.uno.Exception e )
145         {
146             System.err.println( e );
147             e.printStackTrace( System.err );
148         }
149         return entrySource;
150     }
151 }
152