Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / SpreadsheetDocument.java
blob9596b01ea9a8a89a38e67af66772310a05fea93b
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.uno.*;
20 import com.sun.star.lang.XComponent;
21 import com.sun.star.table.XCellRange;
22 import com.sun.star.table.CellAddress;
23 import com.sun.star.table.CellRangeAddress;
24 import com.sun.star.container.XIndexAccess;
25 import com.sun.star.sheet.XSpreadsheetDocument;
26 import com.sun.star.beans.NamedValue;
28 public class SpreadsheetDocument extends DocumentHelper
30 /** Creates a new blank spreadsheet document */
31 public SpreadsheetDocument( XComponentContext xCtx ) throws com.sun.star.uno.Exception
33 super( xCtx, implCreateBlankDocument( xCtx, "private:factory/scalc" ) );
36 public SpreadsheetDocument( XComponentContext xCtx, XComponent document )
38 super( xCtx, document );
41 public XCellRange getSheet( int index ) throws com.sun.star.uno.Exception
43 XSpreadsheetDocument spreadsheetDoc = UnoRuntime.queryInterface( XSpreadsheetDocument.class,
44 m_documentComponent
46 XIndexAccess sheets = UnoRuntime.queryInterface( XIndexAccess.class,
47 spreadsheetDoc.getSheets()
49 return UnoRuntime.queryInterface( XCellRange.class,
50 sheets.getByIndex( index )
54 /** creates a value binding for a given cell
56 public com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row )
58 return createCellBinding( sheet, column, row, false );
61 /** creates a value binding which can be used to exchange a list box selection <em>index</em> with a cell
63 public com.sun.star.form.binding.XValueBinding createListIndexBinding( short sheet, short column, short row )
65 return createCellBinding( sheet, column, row, true );
68 /** creates a value binding for a given cell, with or without support for integer value exchange
70 private com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )
72 com.sun.star.form.binding.XValueBinding cellBinding = null;
73 try
75 CellAddress address = new CellAddress( sheet, column, row );
76 Object[] initParam = new Object[] { new NamedValue( "BoundCell", address ) };
77 cellBinding = UnoRuntime.queryInterface(
78 com.sun.star.form.binding.XValueBinding.class,
79 createInstanceWithArguments(
80 supportIntegerValues ? "com.sun.star.table.ListPositionCellBinding"
81 : "com.sun.star.table.CellValueBinding",
82 initParam
86 catch( com.sun.star.uno.Exception e )
88 System.err.println( e );
89 e.printStackTrace( System.err );
91 return cellBinding;
94 /** creates a source of list entries associated with a (one-column) cell range
96 public com.sun.star.form.binding.XListEntrySource createListEntrySource( short sheet, short column,
97 short topRow, short bottomRow )
99 com.sun.star.form.binding.XListEntrySource entrySource = null;
102 CellRangeAddress rangeAddress = new CellRangeAddress( sheet, column,
103 topRow, column, bottomRow );
104 Object[] initParam = new Object[] { new NamedValue( "CellRange", rangeAddress ) };
105 entrySource = UnoRuntime.queryInterface(
106 com.sun.star.form.binding.XListEntrySource.class,
107 createInstanceWithArguments(
108 "com.sun.star.table.CellRangeListSource", initParam ) );
110 catch( com.sun.star.uno.Exception e )
112 System.err.println( e );
113 e.printStackTrace( System.err );
115 return entrySource;