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