1*b9b79128SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*b9b79128SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*b9b79128SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*b9b79128SAndrew Rist * distributed with this work for additional information 6*b9b79128SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*b9b79128SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*b9b79128SAndrew Rist * "License"); you may not use this file except in compliance 9*b9b79128SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*b9b79128SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*b9b79128SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*b9b79128SAndrew Rist * software distributed under the License is distributed on an 15*b9b79128SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b9b79128SAndrew Rist * KIND, either express or implied. See the License for the 17*b9b79128SAndrew Rist * specific language governing permissions and limitations 18*b9b79128SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*b9b79128SAndrew Rist *************************************************************/ 21*b9b79128SAndrew Rist 22*b9b79128SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package integration.forms; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.uno.*; 27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 28cdf0e10cSrcweir import com.sun.star.lang.XComponent; 29cdf0e10cSrcweir import com.sun.star.table.XCellRange; 30cdf0e10cSrcweir import com.sun.star.table.CellAddress; 31cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 32cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 33cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 34cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 35cdf0e10cSrcweir import com.sun.star.beans.NamedValue; 36cdf0e10cSrcweir 37cdf0e10cSrcweir /** 38cdf0e10cSrcweir * 39cdf0e10cSrcweir * @author fs93730 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir public class SpreadsheetDocument extends DocumentHelper 42cdf0e10cSrcweir { 43cdf0e10cSrcweir /** Creates a new blank spreadsheet document */ 44cdf0e10cSrcweir /* ------------------------------------------------------------------ */ SpreadsheetDocument( XMultiServiceFactory orb )45cdf0e10cSrcweir public SpreadsheetDocument( XMultiServiceFactory orb ) throws com.sun.star.uno.Exception 46cdf0e10cSrcweir { 47cdf0e10cSrcweir super( orb, implLoadAsComponent( orb, "private:factory/scalc" ) ); 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir /* ------------------------------------------------------------------ */ SpreadsheetDocument( XMultiServiceFactory orb, XComponent document )51cdf0e10cSrcweir public SpreadsheetDocument( XMultiServiceFactory orb, XComponent document ) throws com.sun.star.uno.Exception 52cdf0e10cSrcweir { 53cdf0e10cSrcweir super( orb, document ); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 57cdf0e10cSrcweir /** returns the sheets collection 58cdf0e10cSrcweir */ getSheets()59cdf0e10cSrcweir public XSpreadsheets getSheets() throws com.sun.star.uno.Exception 60cdf0e10cSrcweir { 61cdf0e10cSrcweir XSpreadsheetDocument spreadsheetDoc = (XSpreadsheetDocument)UnoRuntime.queryInterface( XSpreadsheetDocument.class, 62cdf0e10cSrcweir getDocument() 63cdf0e10cSrcweir ); 64cdf0e10cSrcweir return spreadsheetDoc.getSheets(); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 68cdf0e10cSrcweir /** returns the sheet with the given index 69cdf0e10cSrcweir */ getSheet( int index )70cdf0e10cSrcweir public XCellRange getSheet( int index ) throws com.sun.star.uno.Exception 71cdf0e10cSrcweir { 72cdf0e10cSrcweir XIndexAccess sheets = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class, 73cdf0e10cSrcweir getSheets() 74cdf0e10cSrcweir ); 75cdf0e10cSrcweir return (XCellRange)UnoRuntime.queryInterface( XCellRange.class, 76cdf0e10cSrcweir sheets.getByIndex( index ) 77cdf0e10cSrcweir ); 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 81cdf0e10cSrcweir /** creates a value binding for a given cell 82cdf0e10cSrcweir */ createCellBinding( short sheet, short column, short row )83cdf0e10cSrcweir public com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row ) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir return createCellBinding( sheet, column, row, false ); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 89cdf0e10cSrcweir /** creates a value binding which can be used to exchange a list box selection <em>index</em> with a cell 90cdf0e10cSrcweir */ createListIndexBinding( short sheet, short column, short row )91cdf0e10cSrcweir public com.sun.star.form.binding.XValueBinding createListIndexBinding( short sheet, short column, short row ) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir return createCellBinding( sheet, column, row, true ); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir 96cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 97cdf0e10cSrcweir /** creates a value binding for a given cell, with or without support for integer value exchange 98cdf0e10cSrcweir */ createCellBinding( short sheet, short column, short row, boolean supportIntegerValues )99cdf0e10cSrcweir private com.sun.star.form.binding.XValueBinding createCellBinding( short sheet, short column, short row, boolean supportIntegerValues ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir com.sun.star.form.binding.XValueBinding cellBinding = null; 102cdf0e10cSrcweir try 103cdf0e10cSrcweir { 104cdf0e10cSrcweir CellAddress address = new CellAddress( sheet, column, row ); 105cdf0e10cSrcweir Object[] initParam = new Object[] { new NamedValue( "BoundCell", address ) }; 106cdf0e10cSrcweir cellBinding = (com.sun.star.form.binding.XValueBinding)UnoRuntime.queryInterface( 107cdf0e10cSrcweir com.sun.star.form.binding.XValueBinding.class, 108cdf0e10cSrcweir createInstanceWithArguments( 109cdf0e10cSrcweir supportIntegerValues ? "com.sun.star.table.ListPositionCellBinding" 110cdf0e10cSrcweir : "com.sun.star.table.CellValueBinding", 111cdf0e10cSrcweir initParam 112cdf0e10cSrcweir ) 113cdf0e10cSrcweir ); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir System.err.println( e ); 118cdf0e10cSrcweir e.printStackTrace( System.err ); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir return cellBinding; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 124cdf0e10cSrcweir /** creates a source of list entries associated with a (one-column) cell range 125cdf0e10cSrcweir */ createListEntrySource( short sheet, short column, short topRow, short bottomRow )126cdf0e10cSrcweir public com.sun.star.form.binding.XListEntrySource createListEntrySource( short sheet, short column, 127cdf0e10cSrcweir short topRow, short bottomRow ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir com.sun.star.form.binding.XListEntrySource entrySource = null; 130cdf0e10cSrcweir try 131cdf0e10cSrcweir { 132cdf0e10cSrcweir CellRangeAddress rangeAddress = new CellRangeAddress( sheet, column, 133cdf0e10cSrcweir topRow, column, bottomRow ); 134cdf0e10cSrcweir Object[] initParam = new Object[] { new NamedValue( "CellRange", rangeAddress ) }; 135cdf0e10cSrcweir entrySource = (com.sun.star.form.binding.XListEntrySource)UnoRuntime.queryInterface( 136cdf0e10cSrcweir com.sun.star.form.binding.XListEntrySource.class, 137cdf0e10cSrcweir createInstanceWithArguments( 138cdf0e10cSrcweir "com.sun.star.table.CellRangeListSource", initParam ) ); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir System.err.println( e ); 143cdf0e10cSrcweir e.printStackTrace( System.err ); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir return entrySource; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir } 148