diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /odk/examples/DevelopersGuide/Charts | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'odk/examples/DevelopersGuide/Charts')
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/AddInChart.ods | bin | 0 -> 18045 bytes | |||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/CalcHelper.java | 396 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/ChartHelper.java | 244 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/ChartInCalc.java | 417 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/ChartInDraw.java | 294 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/ChartInWriter.java | 163 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/Helper.java | 141 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.components | 10 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.java | 449 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/ListenAtCalcRangeInDraw.java | 193 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/Makefile | 199 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/SelectionChangeListener.java | 220 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/Charts/bullet.gif | bin | 0 -> 335 bytes |
13 files changed, 2726 insertions, 0 deletions
diff --git a/odk/examples/DevelopersGuide/Charts/AddInChart.ods b/odk/examples/DevelopersGuide/Charts/AddInChart.ods Binary files differnew file mode 100644 index 000000000..01708ed7b --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/AddInChart.ods diff --git a/odk/examples/DevelopersGuide/Charts/CalcHelper.java b/odk/examples/DevelopersGuide/Charts/CalcHelper.java new file mode 100644 index 000000000..d7d932282 --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/CalcHelper.java @@ -0,0 +1,396 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + +import java.util.Random; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.lang.XMultiServiceFactory; +// container access +import com.sun.star.container.*; + +// application specific classes +import com.sun.star.sheet.*; +import com.sun.star.table.*; +import com.sun.star.chart.*; +import com.sun.star.text.XText; + +import com.sun.star.document.XEmbeddedObjectSupplier; +import com.sun.star.frame.XModel; +// base graphics things +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +import com.sun.star.awt.Rectangle; + +// Exceptions +import com.sun.star.uno.RuntimeException; +import com.sun.star.container.NoSuchElementException; + +// __________ Implementation __________ + +// Helper for accessing a calc document + +public class CalcHelper +{ + public CalcHelper( XSpreadsheetDocument aDoc ) + { + maSpreadSheetDoc = aDoc; + initSpreadSheet(); + } + + + + public XSpreadsheet getChartSheet() throws RuntimeException + { + XNameAccess aSheetsNA = UnoRuntime.queryInterface( + XNameAccess.class, maSpreadSheetDoc.getSheets() ); + + XSpreadsheet aSheet = null; + try + { + aSheet = UnoRuntime.queryInterface( + XSpreadsheet.class, aSheetsNA.getByName( msChartSheetName ) ); + } + catch( NoSuchElementException ex ) + { + System.out.println( "Couldn't find sheet with name " + msChartSheetName + ": " + ex ); + } + catch( Exception ex ) + {} + + return aSheet; + } + + + + public XSpreadsheet getDataSheet() throws RuntimeException + { + XNameAccess aSheetsNA = UnoRuntime.queryInterface( + XNameAccess.class, maSpreadSheetDoc.getSheets() ); + + XSpreadsheet aSheet = null; + if( aSheetsNA != null ) + { + try + { + aSheet = UnoRuntime.queryInterface( + XSpreadsheet.class, aSheetsNA.getByName( msDataSheetName ) ); + } + catch( NoSuchElementException ex ) + { + System.out.println( "Couldn't find sheet with name " + msDataSheetName + ": " + ex ); + } + catch( Exception ex ) + {} + } + + return aSheet; + } + + + + /** Insert a chart using the given name as name of the OLE object and the range as corresponding + range of data to be used for rendering. The chart is placed in the sheet for charts at + position aUpperLeft extending as large as given in aExtent. + + The service name must be the name of a diagram service that can be instantiated via the + factory of the chart document + */ + public XChartDocument insertChart( + String sChartName, + CellRangeAddress aRange, + Point aUpperLeft, + Size aExtent, + String sChartServiceName ) + { + XChartDocument aResult = null; + XTableChartsSupplier aSheet; + + // get the sheet to insert the chart + try + { + aSheet = UnoRuntime.queryInterface( + XTableChartsSupplier.class, getChartSheet() ); + } + catch( Exception ex ) + { + System.out.println( "Sheet not found" + ex ); + return aResult; + } + + XTableCharts aChartCollection = aSheet.getCharts(); + XNameAccess aChartCollectionNA = UnoRuntime.queryInterface( + XNameAccess.class, aChartCollection ); + + if( aChartCollectionNA != null && + ! aChartCollectionNA.hasByName( sChartName ) ) + { + Rectangle aRect = new Rectangle( aUpperLeft.X, aUpperLeft.Y, aExtent.Width, aExtent.Height ); + + CellRangeAddress[] aAddresses = new CellRangeAddress[ 1 ]; + aAddresses[ 0 ] = aRange; + + // first bool: ColumnHeaders + // second bool: RowHeaders + aChartCollection.addNewByName( sChartName, aRect, aAddresses, true, false ); + + try + { + XTableChart aTableChart = UnoRuntime.queryInterface( + XTableChart.class, aChartCollectionNA.getByName( sChartName )); + + // the table chart is an embedded object which contains the chart document + aResult = UnoRuntime.queryInterface( + XChartDocument.class, + UnoRuntime.queryInterface( + XEmbeddedObjectSupplier.class, + aTableChart ).getEmbeddedObject()); + + // create a diagram via the factory and set this as new diagram + aResult.setDiagram( + UnoRuntime.queryInterface( + XDiagram.class, + UnoRuntime.queryInterface( + XMultiServiceFactory.class, + aResult ).createInstance( sChartServiceName ))); + } + catch( NoSuchElementException ex ) + { + System.out.println( "Couldn't find chart with name " + sChartName + ": " + ex ); + } + catch( Exception ex ) + {} + } + + return aResult; + } + + + + /** Fill a rectangular range with random numbers. + The first column has increasing values + */ + public XCellRange insertRandomRange( int nColumnCount, int nRowCount ) + { + XCellRange aRange = null; + + // get the sheet to insert the chart + try + { + XSpreadsheet aSheet = getDataSheet(); + XCellRange aSheetRange = UnoRuntime.queryInterface( XCellRange.class, aSheet ); + + aRange = aSheetRange.getCellRangeByPosition( + 0, 0, + nColumnCount - 1, nRowCount - 1 ); + + int nCol, nRow; + double fBase = 0.0; + double fRange = 10.0; + double fValue; + Random aGenerator = new Random(); + + + for( nCol = 0; nCol < nColumnCount; nCol++ ) + { + if( 0 == nCol ) + { + (aSheet.getCellByPosition( nCol, 0 )).setFormula( "X" ); + } + else + { + (aSheet.getCellByPosition( nCol, 0 )).setFormula( "Random " + nCol ); + } + + for( nRow = 1; nRow < nRowCount; nRow++ ) + { + if( 0 == nCol ) + { + // x values: ascending numbers + fValue = nRow + aGenerator.nextDouble(); + } + else + { + fValue = fBase + ( aGenerator.nextGaussian() * fRange ); + } + + // put value into cell + + // note: getCellByPosition is a method at ...table.XCellRange which + // the XSpreadsheet inherits via ...sheet.XSheetCellRange + (aSheet.getCellByPosition( nCol, nRow )).setValue( fValue ); + } + } + + } + catch( Exception ex ) + { + System.out.println( "Sheet not found" + ex ); + } + + return aRange; + } + + + + public XCellRange insertFormulaRange( int nColumnCount, int nRowCount ) + { + XCellRange aRange = null; + + // get the sheet to insert the chart + try + { + XSpreadsheet aSheet = getDataSheet(); + XCellRange aSheetRange = UnoRuntime.queryInterface( XCellRange.class, aSheet ); + + aRange = aSheetRange.getCellRangeByPosition( + 0, 0, + nColumnCount - 1, nRowCount - 1 ); + + int nCol, nRow; + double fValue; + double fFactor = 2.0 * Math.PI / (nRowCount - 1); + String aFormula; + + // set variable factor for cos formula + int nFactorCol = nColumnCount + 2; + (aSheet.getCellByPosition( nFactorCol - 1, 0 )).setValue( 0.2 ); + + XText xCellText = UnoRuntime.queryInterface( XText.class, aSheet.getCellByPosition( nFactorCol - 1, 1 ) ); + xCellText.setString( "Change the factor above and\nwatch the changes in the chart" ); + + for( nCol = 0; nCol < nColumnCount; nCol++ ) + { + for( nRow = 0; nRow < nRowCount; nRow++ ) + { + if( 0 == nCol ) + { + // x values: ascending numbers + fValue = nRow * fFactor; + (aSheet.getCellByPosition( nCol, nRow )).setValue( fValue ); + } + else + { + aFormula = "="; + if( nCol % 2 == 0 ) + aFormula += "SIN"; + else + aFormula += "COS"; + aFormula += "(INDIRECT(ADDRESS(" + (nRow + 1) + ";1)))+RAND()*INDIRECT(ADDRESS(1;" + nFactorCol + "))"; + (aSheet.getCellByPosition( nCol, nRow )).setFormula( aFormula ); + } + } + } + + } + catch( Exception ex ) + { + System.out.println( "Sheet not found" + ex ); + } + + return aRange; + } + + + + /** Bring the sheet containing charts visually to the foreground + */ + public void raiseChartSheet() + { + UnoRuntime.queryInterface( + XSpreadsheetView.class, + UnoRuntime.queryInterface( + XModel.class, + maSpreadSheetDoc ).getCurrentController()).setActiveSheet( getChartSheet() ); + } + + + // __________ private members and methods __________ + + private static final String msDataSheetName = "Data"; + private static final String msChartSheetName = "Chart"; + + private final XSpreadsheetDocument maSpreadSheetDoc; + + + + + /** create two sheets, one for data and one for charts in the document + */ + private void initSpreadSheet() + { + if( maSpreadSheetDoc != null ) + { + XSpreadsheets aSheets = maSpreadSheetDoc.getSheets(); + XNameContainer aSheetsNC = UnoRuntime.queryInterface( + XNameContainer.class, aSheets ); + XIndexAccess aSheetsIA = UnoRuntime.queryInterface( + XIndexAccess.class, aSheets ); + + if( aSheets != null && + aSheetsNC != null && + aSheetsIA != null ) + { + try + { + // remove all sheets except one + for( int i = aSheetsIA.getCount() - 1; i > 0; i-- ) + { + aSheetsNC.removeByName( + UnoRuntime.queryInterface( + XNamed.class, aSheetsIA.getByIndex( i ) ).getName() ); + } + + XNamed aFirstSheet = UnoRuntime.queryInterface( + XNamed.class, + aSheetsIA.getByIndex( 0 )); + + // first sheet becomes data sheet + aFirstSheet.setName( msDataSheetName ); + + // second sheet becomes chart sheet + aSheets.insertNewByName( msChartSheetName, (short)1 ); + } + catch( Exception ex ) + { + System.out.println( "Couldn't initialize Spreadsheet Document: " + ex ); + } + } + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/ChartHelper.java b/odk/examples/DevelopersGuide/Charts/ChartHelper.java new file mode 100644 index 000000000..7263ea7ad --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/ChartHelper.java @@ -0,0 +1,244 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + + +// base graphics things +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +import com.sun.star.beans.XPropertySet; +// application specific classes +import com.sun.star.chart.XChartDocument; +import com.sun.star.chart.XDiagram; +import com.sun.star.drawing.XDrawPageSupplier; +import com.sun.star.drawing.XDrawPagesSupplier; +import com.sun.star.drawing.XShape; +import com.sun.star.drawing.XShapes; +import com.sun.star.frame.XModel; +// factory for creating components +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.text.XText; +import com.sun.star.text.XTextContent; +import com.sun.star.text.XTextCursor; +import com.sun.star.text.XTextDocument; +import com.sun.star.uno.Any; +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; + +// __________ Implementation __________ + +// Helper for creating an OLE chart + +public class ChartHelper +{ + public ChartHelper( XModel aContainerDoc ) + { + maContainerDocument = aContainerDoc; + } + + public XChartDocument insertOLEChartInWriter( + Point aUpperLeft, + Size aExtent, + String sChartServiceName ) + { + XChartDocument aResult = null; + + XMultiServiceFactory aFact = UnoRuntime.queryInterface(XMultiServiceFactory.class, + maContainerDocument ); + + if( aFact != null ) + { + try + { + XTextContent xTextContent = UnoRuntime.queryInterface( + XTextContent.class, + aFact.createInstance("com.sun.star.text.TextEmbeddedObject")); + + if ( xTextContent != null ) + { + XPropertySet xPropSet = UnoRuntime.queryInterface( + XPropertySet.class, xTextContent); + + Any aAny = new Any(new Type(String.class), msChartClassID); + xPropSet.setPropertyValue("CLSID", aAny ); + + XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class, + maContainerDocument); + XText xText = xTextDoc.getText(); + XTextCursor xCursor = xText.createTextCursor(); + + //insert embedded object in text -> object will be created + xText.insertTextContent( xCursor, xTextContent, true ); + + // set size and position + XShape xShape = UnoRuntime.queryInterface( + XShape.class, xTextContent); + xShape.setSize( aExtent ); + + aAny = new Any(new Type(Short.class), + Short.valueOf(com.sun.star.text.VertOrientation.NONE)); + xPropSet.setPropertyValue("VertOrient", aAny ); + aAny = new Any(new Type(Short.class), + Short.valueOf(com.sun.star.text.HoriOrientation.NONE)); + xPropSet.setPropertyValue("HoriOrient", aAny ); + aAny = new Any(new Type(Integer.class), Integer.valueOf(aUpperLeft.Y)); + xPropSet.setPropertyValue("VertOrientPosition", aAny ); + aAny = new Any(new Type(Integer.class), Integer.valueOf(aUpperLeft.X)); + xPropSet.setPropertyValue("HoriOrientPosition", aAny ); + + // retrieve the chart document as model of the OLE shape + aResult = UnoRuntime.queryInterface( + XChartDocument.class, + xPropSet.getPropertyValue( "Model" )); + + // create a diagram via the factory and set this as + // new diagram + aResult.setDiagram( + UnoRuntime.queryInterface( + XDiagram.class, + UnoRuntime.queryInterface( + XMultiServiceFactory.class, + aResult ).createInstance(sChartServiceName ))); + } + } catch( Exception ex) + { + System.out.println( "caught exception: " + ex ); + } + } + + return aResult; + } + + public XChartDocument insertOLEChartInDraw( + Point aUpperLeft, + Size aExtent, + String sChartServiceName ) + { + XChartDocument aResult = null; + + XShapes aPage = null; + + // try interface for multiple pages in a document + XDrawPagesSupplier aSupplier = UnoRuntime.queryInterface(XDrawPagesSupplier.class, + maContainerDocument ); + + if( aSupplier != null ) + { + try + { + // get first page + aPage = UnoRuntime.queryInterface( + XShapes.class, aSupplier.getDrawPages().getByIndex( 0 ) ); + } + catch( Exception ex ) + { + System.out.println( "First page not found in shape collection: " + + ex ); + } + } + else + { + // try interface for single draw page (e.g. spreadsheet) + XDrawPageSupplier aOnePageSupplier = UnoRuntime.queryInterface(XDrawPageSupplier.class, + maContainerDocument ); + + if( aOnePageSupplier != null ) + { + aPage = UnoRuntime.queryInterface( + XShapes.class, aOnePageSupplier.getDrawPage()); + } + } + + if( aPage != null ) + { + XMultiServiceFactory aFact = UnoRuntime.queryInterface(XMultiServiceFactory.class, + maContainerDocument ); + + if( aFact != null ) + { + try + { + // create an OLE shape + XShape aShape = UnoRuntime.queryInterface( + XShape.class, + aFact.createInstance( "com.sun.star.drawing.OLE2Shape" )); + + // insert the shape into the page + aPage.add( aShape ); + aShape.setPosition( aUpperLeft ); + aShape.setSize( aExtent ); + + // make the OLE shape a chart + XPropertySet aShapeProp = UnoRuntime.queryInterface(XPropertySet.class, aShape ); + if( aShapeProp != null ) + { + // set the class id for charts + aShapeProp.setPropertyValue( "CLSID", msChartClassID ); + + // retrieve the chart document as model of the OLE shape + aResult = UnoRuntime.queryInterface( + XChartDocument.class, + aShapeProp.getPropertyValue( "Model" )); + + // create a diagram via the factory and set this as + // new diagram + aResult.setDiagram( + UnoRuntime.queryInterface( + XDiagram.class, + UnoRuntime.queryInterface( + XMultiServiceFactory.class, + aResult ).createInstance(sChartServiceName ))); + } + } + catch( Exception ex ) + { + System.out.println( "Couldn't change the OLE shape into a chart: " + ex ); + } + } + } + + return aResult; + } + + + // __________ private members and methods __________ + + private static final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e"; + + private final XModel maContainerDocument; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/ChartInCalc.java b/odk/examples/DevelopersGuide/Charts/ChartInCalc.java new file mode 100644 index 000000000..d974d6e45 --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/ChartInCalc.java @@ -0,0 +1,417 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.lang.*; + +// property access +import com.sun.star.beans.*; + +// application specific classes +import com.sun.star.chart.*; +import com.sun.star.drawing.*; + +import com.sun.star.table.CellRangeAddress; +import com.sun.star.table.XCellRange; +import com.sun.star.sheet.XCellRangeAddressable; + +import com.sun.star.frame.XModel; +import com.sun.star.util.XNumberFormatsSupplier; +import com.sun.star.util.XNumberFormats; + +// base graphics things +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +import com.sun.star.awt.FontWeight; +// Exceptions +import com.sun.star.uno.Exception; +import com.sun.star.uno.RuntimeException; +import com.sun.star.beans.UnknownPropertyException; +import com.sun.star.lang.IndexOutOfBoundsException; +import com.sun.star.util.MalformedNumberFormatException; + + +// __________ Implementation __________ + +// Create a spreadsheet add some data and add a chart + +public class ChartInCalc +{ + + + public static void main( String args[] ) + { + Helper aHelper = new Helper( args ); + + CalcHelper aCalcHelper = new CalcHelper( aHelper.createSpreadsheetDocument() ); + + // insert a cell range with 4 columns and 24 rows filled with random numbers + XCellRange aRange = aCalcHelper.insertRandomRange( 4, 24 ); + CellRangeAddress aRangeAddress = UnoRuntime.queryInterface( + XCellRangeAddressable.class, aRange).getRangeAddress(); + + // change view to sheet containing the chart + aCalcHelper.raiseChartSheet(); + + // the unit for measures is 1/100th of a millimeter + // position at (1cm, 1cm) + Point aPos = new Point( 1000, 1000 ); + + // size of the chart is 15cm x 9.271cm + Size aExtent = new Size( 15000, 9271 ); + + // insert a new chart into the "Chart" sheet of the + // spreadsheet document + XChartDocument aChartDoc = aCalcHelper.insertChart( + "ScatterChart", + aRangeAddress, + aPos, + aExtent, + "com.sun.star.chart.XYDiagram" ); + + // instantiate test class with newly created chart + ChartInCalc aTest = new ChartInCalc( aChartDoc ); + + try + { + aTest.lockControllers(); + + aTest.testDiagram(); + aTest.testArea(); + aTest.testWall(); + aTest.testTitle(); + aTest.testAxis(); + aTest.testGrid(); + + // show an intermediate state, ... + aTest.unlockControllers(); + aTest.lockControllers(); + + // ..., because the following takes a while: + // an internet URL has to be resolved + aTest.testDataRowProperties(); + aTest.testDataPointProperties(); + + aTest.unlockControllers(); + } + catch( Exception ex ) + { + System.out.println( "UNO Exception caught: " + ex ); + System.out.println( "Message: " + ex.getMessage() ); + } + + System.exit( 0 ); + } + + + + + public ChartInCalc( XChartDocument aChartDoc ) + { + maChartDocument = aChartDoc; + maDiagram = maChartDocument.getDiagram(); + } + + + + public void lockControllers() + throws RuntimeException + { + UnoRuntime.queryInterface( XModel.class, maChartDocument ).lockControllers(); + } + + + + public void unlockControllers() + throws RuntimeException + { + UnoRuntime.queryInterface( XModel.class, maChartDocument ).unlockControllers(); + } + + + + public void testDiagram() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aDiaProp = UnoRuntime.queryInterface( XPropertySet.class, maDiagram ); + + if( aDiaProp != null ) + { + // change chart type + aDiaProp.setPropertyValue( "Lines", Boolean.TRUE); + + // change attributes for all series + // set line width to 0.5mm + aDiaProp.setPropertyValue( "LineWidth", Integer.valueOf( 50 )); + } + } + + + + public void testDataRowProperties() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + // change properties of the data series + try + { + XPropertySet aSeriesProp; + for( int i = 1; i <= 3; i++ ) + { + aSeriesProp = maDiagram.getDataRowProperties( i ); + aSeriesProp.setPropertyValue( "LineColor", Integer.valueOf( + 0x400000 * i + + 0x005000 * i + + 0x0000ff - 0x40 * i )); + if( 1 == i ) + { + StringBuffer sUrl = new StringBuffer("file:///"); + try { + /* for use without net it's easier to load a local graphic */ + java.io.File sourceFile = new java.io.File("bullet.gif"); + sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/')); + } catch (java.io.IOException e) { + sUrl = new StringBuffer("http://graphics.openoffice.org/chart/bullet1.gif"); + } + + // set a bitmap via URL as symbol for the first series + aSeriesProp.setPropertyValue( "SymbolType", Integer.valueOf( ChartSymbolType.BITMAPURL )); + aSeriesProp.setPropertyValue( "SymbolBitmapURL", sUrl.toString() ); + } + else + { + aSeriesProp.setPropertyValue( "SymbolType", Integer.valueOf( ChartSymbolType.SYMBOL1 )); + aSeriesProp.setPropertyValue( "SymbolSize", new Size( 250, 250 )); + } + } + } + catch( IndexOutOfBoundsException ex ) + { + System.out.println( "Oops, there not enough series for setting properties: " + ex ); + } + } + + + + public void testDataPointProperties() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + // set properties for a single data point + try + { + // determine the maximum value of the first series + + XChartDataArray aDataArray = UnoRuntime.queryInterface( + XChartDataArray.class, maChartDocument.getData()); + double aData[][] = aDataArray.getData(); + + int i; + double fMax = aData[ 0 ][ 1 ]; + for( i = 1; i < aData.length; i++ ) + { + if( aData[ i ][ 1 ] > fMax ) + { + fMax = aData[ i ][ 1 ]; + } + } + + // first parameter is the index of the point, the second one is the series + XPropertySet aPointProp = maDiagram.getDataPointProperties( 0, 1 ); + + // set a different, larger symbol + aPointProp.setPropertyValue( "SymbolType", Integer.valueOf( ChartSymbolType.SYMBOL6 )); + aPointProp.setPropertyValue( "SymbolSize", new Size( 600, 600 )); + + // add a label text with bold font, bordeaux red 14pt + aPointProp.setPropertyValue( "DataCaption", Integer.valueOf( ChartDataCaption.VALUE )); + aPointProp.setPropertyValue( "CharHeight", new Float( 14.0 )); + aPointProp.setPropertyValue( "CharColor", Integer.valueOf( 0x993366 )); + aPointProp.setPropertyValue( "CharWeight", new Float( FontWeight.BOLD )); + } + catch( IndexOutOfBoundsException ex ) + { + System.out.println( "Oops, there not enough data points or series for setting properties: " + ex ); + } + } + + + + public void testArea() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aArea = maChartDocument.getArea(); + + if( aArea != null ) + { + // change background color of entire chart + aArea.setPropertyValue( "FillStyle", FillStyle.SOLID ); + aArea.setPropertyValue( "FillColor", Integer.valueOf( 0xeeeeee )); + } + } + + + + public void testWall() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aWall = UnoRuntime.queryInterface( + X3DDisplay.class, maDiagram ).getWall(); + + // change background color of area + aWall.setPropertyValue( "FillStyle", FillStyle.SOLID ); + aWall.setPropertyValue( "FillColor", Integer.valueOf( 0xcccccc )); + } + + + + public void testTitle() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + // change main title + XPropertySet aDocProp = UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument ); + aDocProp.setPropertyValue( "HasMainTitle", Boolean.TRUE); + + XShape aTitle = maChartDocument.getTitle(); + XPropertySet aTitleProp = UnoRuntime.queryInterface( XPropertySet.class, aTitle ); + + // set new text + if( aTitleProp != null ) + { + aTitleProp.setPropertyValue( "String", "Random Scatter Chart" ); + aTitleProp.setPropertyValue( "CharHeight", new Float(14.0) ); + } + + // align title with y axis + XShape aAxis = UnoRuntime.queryInterface( + XShape.class, UnoRuntime.queryInterface( + XAxisYSupplier.class, maDiagram ).getYAxis() ); + + if( aAxis != null && + aTitle != null ) + { + Point aPos = aTitle.getPosition(); + aPos.X = ( aAxis.getPosition() ).X; + aTitle.setPosition( aPos ); + } + } + + + + public void testAxis() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException, + MalformedNumberFormatException + { + // x axis + XPropertySet aAxisProp = UnoRuntime.queryInterface( + XAxisXSupplier.class, maDiagram ).getXAxis(); + if( aAxisProp != null ) + { + aAxisProp.setPropertyValue( "Max", Integer.valueOf( 24 )); + aAxisProp.setPropertyValue( "StepMain", Integer.valueOf( 3 )); + } + + // change number format for y axis + aAxisProp = UnoRuntime.queryInterface( + XAxisYSupplier.class, maDiagram ).getYAxis(); + + // add a new custom number format and get the new key + int nNewNumberFormat = 0; + XNumberFormatsSupplier aNumFmtSupp = UnoRuntime.queryInterface( + XNumberFormatsSupplier.class, maChartDocument ); + + if( aNumFmtSupp != null ) + { + XNumberFormats aFormats = aNumFmtSupp.getNumberFormats(); + Locale aLocale = new Locale( "de", "DE", "de" ); + + String aFormatStr = aFormats.generateFormat( nNewNumberFormat, aLocale, true, true, (short)3, (short)1 ); + nNewNumberFormat = aFormats.addNew( aFormatStr, aLocale ); + } + + if( aAxisProp != null ) + { + aAxisProp.setPropertyValue( "NumberFormat", Integer.valueOf( nNewNumberFormat )); + } + } + + + + public void testGrid() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + // y major grid + XPropertySet aGridProp = UnoRuntime.queryInterface( + XPropertySet.class, + UnoRuntime.queryInterface( + XAxisYSupplier.class, maDiagram ).getYMainGrid()); + + if( aGridProp != null ) + { + LineDash aDash = new LineDash(); + aDash.Style = DashStyle.ROUND; + aDash.Dots = 2; + aDash.DotLen = 10; + aDash.Dashes = 1; + aDash.DashLen = 200; + aDash.Distance = 100; + + aGridProp.setPropertyValue( "LineColor", Integer.valueOf( 0x999999 )); + aGridProp.setPropertyValue( "LineStyle", LineStyle.DASH ); + aGridProp.setPropertyValue( "LineDash", aDash ); + aGridProp.setPropertyValue( "LineWidth", Integer.valueOf( 30 )); + } + } + + + + + // private members + + + private final XChartDocument maChartDocument; + private final XDiagram maDiagram; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/ChartInDraw.java b/odk/examples/DevelopersGuide/Charts/ChartInDraw.java new file mode 100644 index 000000000..d13b6d07f --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/ChartInDraw.java @@ -0,0 +1,294 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.lang.*; + +// property access +import com.sun.star.beans.*; + +// application specific classes +import com.sun.star.chart.*; +import com.sun.star.drawing.*; + +import com.sun.star.frame.XModel; +// base graphics things +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +// Exceptions +import com.sun.star.uno.Exception; +import com.sun.star.uno.RuntimeException; +import com.sun.star.beans.UnknownPropertyException; + + +// __________ Implementation __________ + +// Create a spreadsheet add some data and add a chart + +public class ChartInDraw +{ + + + public static void main( String args[] ) + { + Helper aHelper = new Helper( args ); + + ChartHelper aChartHelper = new ChartHelper( aHelper.createDrawingDocument()); + + // the unit for measures is 1/100th of a millimeter + // position at (1cm, 1cm) + Point aPos = new Point( 1000, 1000 ); + + // size of the chart is 15cm x 12cm + Size aExtent = new Size( 15000, 13000 ); + + // insert a new chart into the "Chart" sheet of the + // spreadsheet document + XChartDocument aChartDoc = aChartHelper.insertOLEChartInDraw( + aPos, + aExtent, + "com.sun.star.chart.BarDiagram" ); + + // instantiate test class with newly created chart + ChartInDraw aTest = new ChartInDraw( aChartDoc ); + + try + { + aTest.lockControllers(); + + aTest.testArea(); + aTest.testWall(); + aTest.testTitle(); + aTest.testLegend(); + aTest.testThreeD(); + + aTest.unlockControllers(); + } + catch( Exception ex ) + { + System.out.println( "UNO Exception caught: " + ex ); + System.out.println( "Message: " + ex.getMessage() ); + } + + System.exit( 0 ); + } + + + + + public ChartInDraw( XChartDocument aChartDoc ) + { + maChartDocument = aChartDoc; + maDiagram = maChartDocument.getDiagram(); + } + + + + public void lockControllers() + throws RuntimeException + { + UnoRuntime.queryInterface( XModel.class, maChartDocument ).lockControllers(); + } + + + + public void unlockControllers() + throws RuntimeException + { + UnoRuntime.queryInterface( XModel.class, maChartDocument ).unlockControllers(); + } + + + + public void testArea() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aArea = maChartDocument.getArea(); + + if( aArea != null ) + { + // change background color of entire chart + aArea.setPropertyValue( "FillStyle", FillStyle.SOLID ); + aArea.setPropertyValue( "FillColor", Integer.valueOf( 0xeeeeee )); + } + } + + + + public void testWall() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aWall = UnoRuntime.queryInterface( + X3DDisplay.class, maDiagram ).getWall(); + + // change background color of area + aWall.setPropertyValue( "FillColor", Integer.valueOf( 0xcccccc )); + aWall.setPropertyValue( "FillStyle", FillStyle.SOLID ); + } + + + + public void testTitle() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + // change main title + XPropertySet aDocProp = UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument ); + aDocProp.setPropertyValue( "HasMainTitle", Boolean.TRUE); + + XShape aTitle = maChartDocument.getTitle(); + XPropertySet aTitleProp = UnoRuntime.queryInterface( XPropertySet.class, aTitle ); + + // set new text + if( aTitleProp != null ) + { + aTitleProp.setPropertyValue( "String", "Bar Chart in a Draw Document" ); + } + } + + + + public void testLegend() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XShape aLegend = maChartDocument.getLegend(); + XPropertySet aLegendProp = UnoRuntime.queryInterface( XPropertySet.class, aLegend ); + + aLegendProp.setPropertyValue( "Alignment", ChartLegendPosition.LEFT ); + aLegendProp.setPropertyValue( "FillStyle", FillStyle.SOLID ); + aLegendProp.setPropertyValue( "FillColor", Integer.valueOf( 0xeeddee )); + } + + + + public void testThreeD() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aDiaProp = UnoRuntime.queryInterface( XPropertySet.class, maDiagram ); + Boolean aTrue = Boolean.TRUE; + + aDiaProp.setPropertyValue( "Dim3D", aTrue ); + aDiaProp.setPropertyValue( "Deep", aTrue ); + // from Chart3DBarProperties: + aDiaProp.setPropertyValue( "SolidType", Integer.valueOf( ChartSolidType.CYLINDER )); + + // change floor color to Magenta6 + XPropertySet aFloor = UnoRuntime.queryInterface( + X3DDisplay.class, maDiagram ).getFloor(); + aFloor.setPropertyValue( "FillColor", Integer.valueOf( 0x6b2394 )); + + // apply changes to get a 3d scene + unlockControllers(); + lockControllers(); + + + // rotate scene to a different angle + HomogenMatrix aMatrix = new HomogenMatrix(); + HomogenMatrixLine aLines[] = new HomogenMatrixLine[] + { + new HomogenMatrixLine( 1.0, 0.0, 0.0, 0.0 ), + new HomogenMatrixLine( 0.0, 1.0, 0.0, 0.0 ), + new HomogenMatrixLine( 0.0, 0.0, 1.0, 0.0 ), + new HomogenMatrixLine( 0.0, 0.0, 0.0, 1.0 ) + }; + + aMatrix.Line1 = aLines[ 0 ]; + aMatrix.Line2 = aLines[ 1 ]; + aMatrix.Line3 = aLines[ 2 ]; + aMatrix.Line4 = aLines[ 3 ]; + + // rotate 10 degrees along the x axis + double fAngle = 10.0; + double fCosX = Math.cos( Math.PI / 180.0 * fAngle ); + double fSinX = Math.sin( Math.PI / 180.0 * fAngle ); + + // rotate -20 degrees along the y axis + fAngle = -20.0; + double fCosY = Math.cos( Math.PI / 180.0 * fAngle ); + double fSinY = Math.sin( Math.PI / 180.0 * fAngle ); + + // rotate -5 degrees along the z axis + fAngle = -5.0; + double fCosZ = Math.cos( Math.PI / 180.0 * fAngle ); + double fSinZ = Math.sin( Math.PI / 180.0 * fAngle ); + + aMatrix.Line1.Column1 = fCosY * fCosZ; + aMatrix.Line1.Column2 = fCosY * -fSinZ; + aMatrix.Line1.Column3 = fSinY; + + aMatrix.Line2.Column1 = fSinX * fSinY * fCosZ + fCosX * fSinZ; + aMatrix.Line2.Column2 = -fSinX * fSinY * fSinZ + fCosX * fCosZ; + aMatrix.Line2.Column3 = -fSinX * fCosY; + + aMatrix.Line3.Column1 = -fCosX * fSinY * fCosZ + fSinX * fSinZ; + aMatrix.Line3.Column2 = fCosX * fSinY * fSinZ + fSinX * fCosZ; + aMatrix.Line3.Column3 = fCosX * fCosY; + + aDiaProp.setPropertyValue( "D3DTransformMatrix", aMatrix ); + + // add a red light source + + // in a chart by default only the second (non-specular) light source is switched on + // light source 1 is a specular light source + aDiaProp.setPropertyValue( "D3DSceneLightColor1", Integer.valueOf( 0xff3333 )); + + // set direction + com.sun.star.drawing.Direction3D aDirection = new com.sun.star.drawing.Direction3D(); + + aDirection.DirectionX = -0.75; + aDirection.DirectionY = 0.5; + aDirection.DirectionZ = 0.5; + + aDiaProp.setPropertyValue( "D3DSceneLightDirection1", aDirection ); + aDiaProp.setPropertyValue( "D3DSceneLightOn1", Boolean.TRUE); + } + + + + // private members + + + private final XChartDocument maChartDocument; + private final XDiagram maDiagram; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/ChartInWriter.java b/odk/examples/DevelopersGuide/Charts/ChartInWriter.java new file mode 100644 index 000000000..b84404d8b --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/ChartInWriter.java @@ -0,0 +1,163 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.lang.*; + +// property access +import com.sun.star.beans.*; + +// application specific classes +import com.sun.star.chart.*; +import com.sun.star.drawing.*; +import com.sun.star.frame.XModel; +// base graphics things +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +// Exceptions +import com.sun.star.uno.Exception; +import com.sun.star.uno.RuntimeException; +import com.sun.star.beans.UnknownPropertyException; + + +// __________ Implementation __________ + +/** Test to create a writer document and insert an OLE Chart. + + Be careful! This does not really work. The Writer currently has no + interface for dealing with OLE objects. You can add an OLE shape to the + Writer's drawing layer, but it is not treated correctly as OLE object. + Thus, you can not activate the chart by double-clicking. The office may + also crash when the document is closed! + + */ +public class ChartInWriter +{ + + + public static void main( String args[] ) + { + Helper aHelper = new Helper( args ); + + ChartHelper aChartHelper = new ChartHelper( + UnoRuntime.queryInterface( XModel.class, + aHelper.createTextDocument())); + + // the unit for measures is 1/100th of a millimeter + // position at (1cm, 1cm) + Point aPos = new Point( 1000, 1000 ); + + // size of the chart is 15cm x 12cm + Size aExtent = new Size( 15000, 13000 ); + + // insert a new chart into the "Chart" sheet of the + // spreadsheet document + XChartDocument aChartDoc = aChartHelper.insertOLEChartInWriter( + aPos, + aExtent, + "com.sun.star.chart.AreaDiagram" ); + + // instantiate test class with newly created chart + ChartInWriter aTest = new ChartInWriter( aChartDoc ); + + try + { + aTest.lockControllers(); + + // do tests here + aTest.testWall(); + + aTest.unlockControllers(); + } + catch( Exception ex ) + { + System.out.println( "UNO Exception caught: " + ex ); + System.out.println( "Message: " + ex.getMessage() ); + } + + System.exit( 0 ); + } + + + + + public ChartInWriter( XChartDocument aChartDoc ) + { + maChartDocument = aChartDoc; + maDiagram = maChartDocument.getDiagram(); + } + + + + public void lockControllers() + throws RuntimeException + { + UnoRuntime.queryInterface( XModel.class, maChartDocument ).lockControllers(); + } + + + + public void unlockControllers() + throws RuntimeException + { + UnoRuntime.queryInterface( XModel.class, maChartDocument ).unlockControllers(); + } + + + + public void testWall() + throws RuntimeException, UnknownPropertyException, PropertyVetoException, + com.sun.star.lang.IllegalArgumentException, WrappedTargetException + { + XPropertySet aWall = UnoRuntime.queryInterface( + X3DDisplay.class, maDiagram ).getWall(); + + // change background color of area + aWall.setPropertyValue( "FillColor", Integer.valueOf( 0xeecc99 )); + aWall.setPropertyValue( "FillStyle", FillStyle.SOLID ); + } + + + + // private members + + + private final XChartDocument maChartDocument; + private final XDiagram maDiagram; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/Helper.java b/odk/examples/DevelopersGuide/Charts/Helper.java new file mode 100644 index 000000000..ef1faccbc --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/Helper.java @@ -0,0 +1,141 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.lang.*; + +import com.sun.star.frame.XComponentLoader; + +// property access +import com.sun.star.beans.*; + +// application specific classes +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.frame.XModel; + +// __________ Implementation __________ + +// Helper for creating a calc document adding cell values and charts + +public class Helper +{ + public Helper( String[] args ) + { + // connect to a running office and get the ServiceManager + try { + // get the remote office component context + maContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + System.out.println("Connected to a running office ..."); + + // get the remote office service manager + maMCFactory = maContext.getServiceManager(); + } + catch( Exception e) { + System.out.println( "Couldn't get ServiceManager: " + e ); + e.printStackTrace(); + System.exit(1); + } + } + + + + public XSpreadsheetDocument createSpreadsheetDocument() + { + return UnoRuntime.queryInterface( + XSpreadsheetDocument.class, createDocument( "scalc" )); + } + + + + + + + + public XModel createDrawingDocument() + { + return createDocument( "sdraw" ); + } + + + + public XModel createTextDocument() + { + return createDocument( "swriter" ); + } + + + + private XModel createDocument( String sDocType ) + { + XModel aResult = null; + try + { + XComponentLoader aLoader = UnoRuntime.queryInterface(XComponentLoader.class, + maMCFactory.createInstanceWithContext("com.sun.star.frame.Desktop", + maContext) ); + + aResult = UnoRuntime.queryInterface( + XModel.class, + aLoader.loadComponentFromURL( "private:factory/" + sDocType, + "_blank", + 0, + new PropertyValue[ 0 ] ) ); + } + catch( Exception e ) + { + System.err.println("Couldn't create Document of type "+ sDocType +": "+e); + e.printStackTrace(); + System.exit( 0 ); + } + + return aResult; + } + + public XComponentContext getComponentContext(){ + return maContext; + + } + + // __________ private members and methods __________ + + + private XComponentContext maContext; + private XMultiComponentFactory maMCFactory; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.components b/odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.components new file mode 100644 index 000000000..e9a7baa48 --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.components @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<components xmlns="http://openoffice.org/2010/uno-components"> + <component loader="com.sun.star.loader.Java2" uri="JavaSampleChartAddIn.uno.jar"> + <implementation name="JavaSampleChartAddIn"> + <service name="com.sun.star.comp.Chart.JavaSampleChartAddIn"/> + <service name="com.sun.star.chart.Diagram"/> + <service name="com.sun.star.chart.ChartAxisYSupplier"/> + </implementation> + </component> +</components> diff --git a/odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.java b/odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.java new file mode 100644 index 000000000..54a39d172 --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/JavaSampleChartAddIn.java @@ -0,0 +1,449 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.container.XIndexAccess; +import com.sun.star.lib.uno.helper.WeakBase; + +// factories +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XSingleServiceFactory; + +// graphics stuff +import com.sun.star.drawing.*; +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; + +// chart stuff +import com.sun.star.chart.*; + +// property access +import com.sun.star.beans.*; + +// Add-In stuff +import com.sun.star.lang.XInitialization; +import com.sun.star.util.XRefreshable; +import com.sun.star.lang.XServiceName; +import com.sun.star.lang.XServiceInfo; +// Exceptions +import com.sun.star.uno.Exception; +import com.sun.star.uno.RuntimeException; + +import javax.swing.JOptionPane; + +public class JavaSampleChartAddIn extends WeakBase implements + XInitialization, + XRefreshable, + XDiagram, + XServiceName, + XServiceInfo +{ + + // __________ interface methods __________ + + // XInitialization + public void initialize( Object[] aArguments ) + throws Exception, RuntimeException + { + if( aArguments.length > 0 ) + { + maChartDocument = UnoRuntime.queryInterface( + XChartDocument.class, aArguments[ 0 ]); + + XPropertySet aDocProp = UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument ); + if( aDocProp != null ) + { + // set base diagram which will be extended in refresh() + aDocProp.setPropertyValue( "BaseDiagram", "com.sun.star.chart.XYDiagram" ); + } + + // get the draw page + XDrawPageSupplier aPageSupp = UnoRuntime.queryInterface( + XDrawPageSupplier.class, maChartDocument ); + if( aPageSupp != null ) + maDrawPage = UnoRuntime.queryInterface( + XDrawPage.class, aPageSupp.getDrawPage() ); + + // get a factory for creating shapes + maShapeFactory = UnoRuntime.queryInterface( + XMultiServiceFactory.class, maChartDocument ); + } + } + + // XRefreshable + public void refresh() throws RuntimeException + { + // recycle shapes in first call, if document was loaded + if( maBottomLine == null || + maTopLine == null ) + { + // try to recycle loaded shapes + XPropertySet aDocProp = UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument ); + if( aDocProp != null ) + { + try + { + XIndexAccess aShapesIA = UnoRuntime.queryInterface( + XIndexAccess.class, aDocProp.getPropertyValue( "AdditionalShapes" )); + if( aShapesIA != null && + aShapesIA.getCount() > 0 ) + { + XShape aShape; + String aName; + for( int i = aShapesIA.getCount() - 1; i >= 0; --i ) + { + aShape = UnoRuntime.queryInterface( + XShape.class, aShapesIA.getByIndex( i )); + if( aShape != null ) + { + XPropertySet aProp = UnoRuntime.queryInterface( + XPropertySet.class, aShape ); + aName = (String) aProp.getPropertyValue( "Name" ); + + if( aName.equals( "top" )) + { + maTopLine = aShape; + } + else if( aName.equals( "bottom" )) + { + maBottomLine = aShape; + } + } + } + } + } + catch( Exception ex ) + { + JOptionPane.showMessageDialog( null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE ); + } + } + } + + // create top line if it does not yet exist + try + { + if( maTopLine == null ) + { + maTopLine = UnoRuntime.queryInterface( + XShape.class, maShapeFactory.createInstance( "com.sun.star.drawing.LineShape" )); + maDrawPage.add( maTopLine ); + + // make line red and thicker + XPropertySet aShapeProp = UnoRuntime.queryInterface( + XPropertySet.class, maTopLine ); + + aShapeProp.setPropertyValue( "LineColor", Integer.valueOf( 0xe01010 )); + aShapeProp.setPropertyValue( "LineWidth", Integer.valueOf( 50 )); + aShapeProp.setPropertyValue( "Name", "top" ); + } + } + catch( Exception ex ) + { + JOptionPane.showMessageDialog( null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE ); + } + + // create bottom line if it does not yet exist + try + { + if( maBottomLine == null ) + { + maBottomLine = UnoRuntime.queryInterface( + XShape.class, maShapeFactory.createInstance( "com.sun.star.drawing.LineShape" )); + maDrawPage.add( maBottomLine ); + + // make line green and thicker + XPropertySet aShapeProp = UnoRuntime.queryInterface( + XPropertySet.class, maBottomLine ); + + aShapeProp.setPropertyValue( "LineColor", Integer.valueOf( 0x10e010 )); + aShapeProp.setPropertyValue( "LineWidth", Integer.valueOf( 50 )); + aShapeProp.setPropertyValue( "Name", "bottom" ); + } + } + catch( Exception ex ) + { + JOptionPane.showMessageDialog( null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE ); + } + + if( maTopLine == null || + maBottomLine == null ) + { + JOptionPane.showMessageDialog( null, "One of the lines is still null", "Assertion", JOptionPane.WARNING_MESSAGE ); + return; + } + + // position lines + + + // get data + XChartDataArray aDataArray = UnoRuntime.queryInterface( + XChartDataArray.class, maChartDocument.getData()); + double aData[][] = aDataArray.getData(); + + // get axes + XDiagram aDiagram = maChartDocument.getDiagram(); + XShape aXAxis = UnoRuntime.queryInterface( + XShape.class, UnoRuntime.queryInterface( + XAxisXSupplier.class, aDiagram ).getXAxis() ); + XShape aYAxis = UnoRuntime.queryInterface( + XShape.class, UnoRuntime.queryInterface( + XAxisYSupplier.class, aDiagram ).getYAxis() ); + + // calculate points for hull + final int nLength = aData.length; + int i, j; + double fMax, fMin; + + Point aMaxPtSeq[][] = new Point[ 1 ][]; + aMaxPtSeq[ 0 ] = new Point[ nLength ]; + Point aMinPtSeq[][] = new Point[ 1 ][]; + aMinPtSeq[ 0 ] = new Point[ nLength ]; + + for( i = 0; i < nLength; i++ ) + { + fMin = fMax = aData[ i ][ 1 ]; + for( j = 1; j < aData[ i ].length; j++ ) + { + if( aData[ i ][ j ] > fMax ) + fMax = aData[ i ][ j ]; + else if( aData[ i ][ j ] < fMin ) + fMin = aData[ i ][ j ]; + } + aMaxPtSeq[ 0 ][ i ] = new Point( getAxisPosition( aXAxis, aData[ i ][ 0 ], false ), + getAxisPosition( aYAxis, fMax, true )); + aMinPtSeq[ 0 ][ i ] = new Point( getAxisPosition( aXAxis, aData[ i ][ 0 ], false ), + getAxisPosition( aYAxis, fMin, true )); + } + + // apply point sequences to lines + try + { + XPropertySet aShapeProp = UnoRuntime.queryInterface( + XPropertySet.class, maTopLine ); + aShapeProp.setPropertyValue( "PolyPolygon", aMaxPtSeq ); + + aShapeProp = UnoRuntime.queryInterface( + XPropertySet.class, maBottomLine ); + aShapeProp.setPropertyValue( "PolyPolygon", aMinPtSeq ); + } + catch( Exception ex ) + { + JOptionPane.showMessageDialog( null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE ); + } + } + + public void addRefreshListener( com.sun.star.util.XRefreshListener aListener ) + throws RuntimeException + { + // we don't want this but we have to implement the interface + } + + public void removeRefreshListener( com.sun.star.util.XRefreshListener aListener ) + throws RuntimeException + { + // we don't want this but we have to implement the interface + } + + + // XServiceName + public String getServiceName() throws RuntimeException + { + return smServiceName; + } + + // XServiceInfo + public boolean supportsService( String aServiceName ) + { + String[] aServices = getSupportedServiceNames_Static(); + int i, nLength = aServices.length; + boolean bResult = false; + + for( i = 0; !bResult && i < nLength; ++i ) + bResult = aServiceName.equals( aServices[ i ] ); + + return bResult; + } + + public String getImplementationName() + { + return( JavaSampleChartAddIn.class.getName() ); + } + + public String[] getSupportedServiceNames() + { + return getSupportedServiceNames_Static(); + } + + // XDiagram + public String getDiagramType() throws RuntimeException + { + return smServiceName; + } + + public XPropertySet getDataRowProperties( int nRow ) + throws com.sun.star.lang.IndexOutOfBoundsException, RuntimeException + { + return maChartDocument.getDiagram().getDataRowProperties( nRow ); + } + + public XPropertySet getDataPointProperties( int nCol, int nRow ) + throws com.sun.star.lang.IndexOutOfBoundsException, RuntimeException + { + return maChartDocument.getDiagram().getDataPointProperties( nCol, nRow ); + } + + // XShape : XDiagram + public Size getSize() throws RuntimeException + { + return UnoRuntime.queryInterface( XShape.class, maChartDocument.getDiagram()).getSize(); + } + public void setSize( Size aSize ) throws RuntimeException, PropertyVetoException + { + UnoRuntime.queryInterface( XShape.class, maChartDocument.getDiagram()).setSize( aSize ); + } + + public Point getPosition() throws RuntimeException + { + return UnoRuntime.queryInterface( XShape.class, maChartDocument.getDiagram()).getPosition(); + } + public void setPosition( Point aPos ) throws RuntimeException + { + UnoRuntime.queryInterface( XShape.class, maChartDocument.getDiagram()).setPosition( aPos ); + } + + // XShapeDescriptor : XShape : XDiagram + public String getShapeType() throws RuntimeException + { + return "com.sun.star.comp.Chart.JavaSampleDiagramShape"; + } + + + // __________ private members __________ + private com.sun.star.chart.XChartDocument maChartDocument; + private com.sun.star.drawing.XDrawPage maDrawPage; + private com.sun.star.lang.XMultiServiceFactory maShapeFactory; + + // shapes added by add-in + private com.sun.star.drawing.XShape maTopLine; + private com.sun.star.drawing.XShape maBottomLine; + + // __________ private methods __________ + + private int getAxisPosition( XShape aAxis, double fValue, boolean bVertical ) + { + int nResult = 0; + + if( aAxis != null ) + { + XPropertySet aAxisProp = UnoRuntime.queryInterface( + XPropertySet.class, aAxis ); + + try + { + double fMin, fMax; + fMin = ((Double) aAxisProp.getPropertyValue( "Min" )).doubleValue(); + fMax = ((Double) aAxisProp.getPropertyValue( "Max" )).doubleValue(); + double fRange = fMax - fMin; + + if( fMin <= fValue && fValue <= fMax && + fRange != 0 ) + { + if( bVertical ) + { + nResult = aAxis.getPosition().Y + + (int)((aAxis.getSize().Height) * + (1.0 - (( fValue - fMin ) / fRange ))); + } + else + { + nResult = aAxis.getPosition().X + + (int)((aAxis.getSize().Width) * + (( fValue - fMin ) / fRange )); + } + } + } + catch( Exception ex ) + { + JOptionPane.showMessageDialog( null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE ); + } + } + return nResult; + } + + // __________ static things __________ + + private static final String smServiceName = "com.sun.star.comp.Chart.JavaSampleChartAddIn"; + + public static String[] getSupportedServiceNames_Static() + { + String[] aResult = { smServiceName, + "com.sun.star.chart.Diagram", + "com.sun.star.chart.ChartAxisYSupplier" }; + return aResult; + } + + + /** + * Returns a factory for creating the service. + * This method is called by the <code>JavaLoader</code> + * <p> + * @return returns a <code>XSingleServiceFactory</code> for creating the component + * @param implName the name of the implementation for which a service is desired + * @param multiFactory the service manager to be used if needed + * @param regKey the registryKey + * @see com.sun.star.comp.loader.JavaLoader + */ + public static XSingleServiceFactory __getServiceFactory( + String implName, + XMultiServiceFactory multiFactory, + com.sun.star.registry.XRegistryKey regKey ) + { + XSingleServiceFactory xSingleServiceFactory = null; + + if( implName.equals( JavaSampleChartAddIn.class.getName()) ) + { + xSingleServiceFactory = com.sun.star.comp.loader.FactoryHelper.getServiceFactory( + JavaSampleChartAddIn.class, smServiceName, + multiFactory, regKey ); + } + + return xSingleServiceFactory; + } + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/ListenAtCalcRangeInDraw.java b/odk/examples/DevelopersGuide/Charts/ListenAtCalcRangeInDraw.java new file mode 100644 index 000000000..76808b4ae --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/ListenAtCalcRangeInDraw.java @@ -0,0 +1,193 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// __________ Imports __________ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.lang.*; + +// property access +import com.sun.star.beans.*; + +// application specific classes +import com.sun.star.chart.*; +import com.sun.star.table.XCellRange; +import com.sun.star.sheet.XSpreadsheetDocument; + +import com.sun.star.frame.XModel; +// base graphics things +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +// Exceptions +import com.sun.star.uno.Exception; + + +// __________ Implementation __________ + +/** Create a spreadsheet add some data. + Create a presentation and add a chart. + Connect the chart to a calc range via a listener + */ +public class ListenAtCalcRangeInDraw implements XChartDataChangeEventListener +{ + public static void main( String args[] ) + { + ListenAtCalcRangeInDraw aMySelf = new ListenAtCalcRangeInDraw( args ); + + aMySelf.run(); + } + + public ListenAtCalcRangeInDraw( String args[] ) + { + Helper aHelper = new Helper( args ); + + maSheetDoc = aHelper.createSpreadsheetDocument(); + XModel aDrawDoc = aHelper.createDrawingDocument(); + CalcHelper aCalcHelper = new CalcHelper( maSheetDoc ); + ChartHelper aChartHelper = new ChartHelper( aDrawDoc ); + + XCellRange aRange = aCalcHelper.insertFormulaRange( 3, 30 ); + + // the unit for measures is 1/100th of a millimeter + // position at (1cm, 1cm) + Point aPos = new Point( 1000, 1000 ); + + // size of the chart is 15cm x 9.271cm + Size aExtent = new Size( 15000, 9271 ); + + // insert a new chart into the "Chart" sheet of the + // spreadsheet document + maChartDocument = aChartHelper.insertOLEChartInDraw( + aPos, + aExtent, + "com.sun.star.chart.XYDiagram" ); + + // attach the data coming from the cell range to the chart + maChartData = UnoRuntime.queryInterface( XChartData.class, aRange ); + maChartDocument.attachData( maChartData ); + } + + + + public void run() + { + try + { + UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument ).setPropertyValue( + "HasSubTitle", Boolean.TRUE); + + // start listening for death of spreadsheet + UnoRuntime.queryInterface( + XComponent.class, maSheetDoc ).addEventListener( this ); + + // start listening for death of chart + UnoRuntime.queryInterface( + XComponent.class, maChartDocument ).addEventListener( this ); + + //start listening for change of data + maChartData.addChartDataChangeEventListener( this ); + } + catch( Exception ex ) + { + System.out.println( "Oops: " + ex ); + } + + // call listener + ChartDataChangeEvent aEvent = new ChartDataChangeEvent(); + aEvent.Type = ChartDataChangeType.ALL; + chartDataChanged( aEvent ); + } + + + + // XEventListener (base of XChartDataChangeEventListener) + public void disposing( EventObject aSourceObj ) + { + if( UnoRuntime.queryInterface( XChartDocument.class, aSourceObj.Source ) != null ) + System.out.println( "Disconnecting Listener because Chart was shut down" ); + + if( UnoRuntime.queryInterface( XSpreadsheetDocument.class, aSourceObj.Source ) != null ) + System.out.println( "Disconnecting Listener because Spreadsheet was shut down" ); + + // remove data change listener + maChartData.removeChartDataChangeEventListener( this ); + + // remove dispose listeners + UnoRuntime.queryInterface( + XComponent.class, maSheetDoc ).removeEventListener( this ); + UnoRuntime.queryInterface( + XComponent.class, maChartDocument ).removeEventListener( this ); + + System.exit( 0 ); + } + + + + // XChartDataChangeEventListener + public void chartDataChanged( ChartDataChangeEvent aEvent ) + { + // update subtitle + String aTitle = "Last Update: " + new java.util.Date(); + + try + { + XPropertySet aDocProp = UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument ); + aDocProp.setPropertyValue( "HasMainTitle", Boolean.TRUE); + + UnoRuntime.queryInterface( + XPropertySet.class, maChartDocument.getSubTitle()).setPropertyValue( + "String", aTitle ); + + maChartDocument.attachData( maChartData ); + } + catch( Exception ex ) + { + System.out.println( "Oops: " + ex ); + } + + System.out.println( "Data has changed" ); + } + + + // __________ private __________ + + private final XSpreadsheetDocument maSheetDoc; + private final XChartDocument maChartDocument; + private final XChartData maChartData; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/Makefile b/odk/examples/DevelopersGuide/Charts/Makefile new file mode 100644 index 000000000..dbb55d51c --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/Makefile @@ -0,0 +1,199 @@ +#************************************************************************* +# +# The Contents of this file are made available subject to the terms of +# the BSD license. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of Sun Microsystems, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +#************************************************************************** + +# Builds the Charts examples of the Developers Guide. + +PRJ=../../.. +SETTINGS=$(PRJ)/settings + +include $(SETTINGS)/settings.mk +include $(SETTINGS)/std.mk + +# Define non-platform/compiler specific settings +SAMPLE_NAME=DevGuideChartExample +SAMPLE_CLASS_OUT = $(OUT_CLASS)/$(SAMPLE_NAME) +SAMPLE_GEN_OUT = $(OUT_MISC)/$(SAMPLE_NAME) + +APP1_NAME=ChartInCalc +APP1_JAR=$(SAMPLE_CLASS_OUT)/$(APP1_NAME).jar +APP2_NAME=ChartInDraw +APP2_JAR=$(SAMPLE_CLASS_OUT)/$(APP2_NAME).jar +APP3_NAME=ChartInWriter +APP3_JAR=$(SAMPLE_CLASS_OUT)/$(APP3_NAME).jar +APP4_NAME=ListenAtCalcRangeInDraw +APP4_JAR=$(SAMPLE_CLASS_OUT)/$(APP4_NAME).jar +APP5_NAME=SelectionChangeListener +APP5_JAR=$(SAMPLE_CLASS_OUT)/$(APP5_NAME).jar + +COMP_NAME=JavaSampleChartAddIn +COMP_PACKAGE = $(OUT_BIN)/$(COMP_NAME).$(UNOOXT_EXT) +COMP_PACKAGE_URL = $(subst \\,\,"$(COMP_PACKAGE_DIR)$(PS)$(COMP_NAME).$(UNOOXT_EXT)") +COMP_JAR_NAME = $(COMP_NAME).uno.jar +COMP_JAR = $(SAMPLE_CLASS_OUT)/$(COMP_JAR_NAME) +COMP_MANIFESTFILE = $(SAMPLE_CLASS_OUT)/$(COMP_NAME).uno.Manifest +COMP_UNOPKG_MANIFEST = $(SAMPLE_CLASS_OUT)/$(COMP_NAME)/META-INF/manifest.xml +COMP_REGISTERFLAG = $(SAMPLE_GEN_OUT)/devguide_$(COMP_NAME)_register_component.flag +COMP_COMPONENTS=$(COMP_NAME).components + +COMPJAVAFILES = \ + JavaSampleChartAddIn.java + +APP_JAVAFILES = \ + Helper.java \ + CalcHelper.java \ + ChartHelper.java + +COMPCLASSFILES= $(patsubst %.java,$(SAMPLE_CLASS_OUT)/%.class,$(COMPJAVAFILES)) + +APP_CLASSFILES= $(patsubst %.java,$(SAMPLE_CLASS_OUT)/%.class,$(APP_JAVAFILES)) +APP_CLASSNAMES= $(patsubst %.java,%.class,$(APP_JAVAFILES)) + +$(COMP_NAME)_CLASSFILES = $(COMP_NAME).class + +SDK_CLASSPATH = $(subst $(EMPTYSTRING) $(PATH_SEPARATOR),$(PATH_SEPARATOR),$(CLASSPATH)\ + $(PATH_SEPARATOR)$(SAMPLE_CLASS_OUT)) + + +# Targets +.PHONY: ALL +ALL : $(SAMPLE_NAME) + +include $(SETTINGS)/stdtarget.mk + +$(SAMPLE_CLASS_OUT)/%.Manifest : + -$(MKDIR) $(subst /,$(PS),$(@D)) + @echo RegistrationClassName: $(basename $(basename $(@F)))> $@ + +# rule for client/example application class files, explicit dependencies to common +# java files which are used in all examples. +$(SAMPLE_CLASS_OUT)/%.class : %.java $(APP_JAVAFILES) + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(SDK_JAVAC) $(JAVAC_FLAGS) -classpath "$(SDK_CLASSPATH)" -d $(SAMPLE_CLASS_OUT) $< $(APP_JAVAFILES) + +$(COMPCLASSFILES) : $(JAVAFILES) + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(SDK_JAVAC) $(JAVAC_FLAGS) -classpath "$(SDK_CLASSPATH)" -d $(SAMPLE_CLASS_OUT) $(COMPJAVAFILES) + +# rule for client/example application manifest file +$(SAMPLE_CLASS_OUT)/%.mf : + -$(MKDIR) $(subst /,$(PS),$(@D)) + @echo Main-Class: com.sun.star.lib.loader.Loader> $@ + $(ECHOLINE)>> $@ + @echo Name: com/sun/star/lib/loader/Loader.class>> $@ + @echo Application-Class: $*>> $@ + +# rule for client/example application jar file +$(SAMPLE_CLASS_OUT)/%.jar : $(SAMPLE_CLASS_OUT)/%.mf $(SAMPLE_CLASS_OUT)/%.class + -$(DEL) $(subst \\,\,$(subst /,$(PS),$@)) + -$(MKDIR) $(subst /,$(PS),$(@D)) + +cd $(subst /,$(PS),$(SAMPLE_CLASS_OUT)) && $(SDK_JAR) cvfm $(@F) $*.mf $**.class $(APP_CLASSNAMES) + + +$(SDK_JAR) uvf $@ $(SDK_JAVA_UNO_BOOTSTRAP_FILES) + +# rule for component jar file +$(COMP_JAR) : $(COMP_MANIFESTFILE) $(COMPCLASSFILES) + -$(DEL) $(subst \\,\,$(subst /,$(PS),$@)) + -$(MKDIR) $(subst /,$(PS),$(@D)) + cd $(subst /,$(PS),$(SAMPLE_CLASS_OUT)) && $(SDK_JAR) cvfm $(@F) $(<F) $($(basename $(basename $(@F)))_CLASSFILES) + +# rule for component package manifest +$(SAMPLE_CLASS_OUT)/%/manifest.xml : + -$(MKDIR) $(subst /,$(PS),$(@D)) + @echo $(OSEP)?xml version="$(QM)1.0$(QM)" encoding="$(QM)UTF-8$(QM)"?$(CSEP) > $@ + @echo $(OSEP)!DOCTYPE manifest:manifest PUBLIC "$(QM)-//OpenOffice.org//DTD Manifest 1.0//EN$(QM)" "$(QM)Manifest.dtd$(QM)"$(CSEP) >> $@ + @echo $(OSEP)manifest:manifest xmlns:manifest="$(QM)http://openoffice.org/2001/manifest$(QM)"$(CSEP) >> $@ + @echo $(SQM) $(SQM)$(OSEP)manifest:file-entry manifest:media-type="$(QM)application/vnd.sun.star.uno-components$(QM)">> $@ + @echo $(SQM) $(SQM)manifest:full-path="$(QM)$(COMP_COMPONENTS)$(QM)"/$(CSEP)>> $@ + @echo $(OSEP)/manifest:manifest$(CSEP) >> $@ + +# rule for component package file +$(COMP_PACKAGE) : $(COMP_JAR) $(COMP_UNOPKG_MANIFEST) $(COMP_COMPONENTS) + -$(DEL) $(subst \\,\,$(subst /,$(PS),$@)) + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(SDK_ZIP) $@ $(COMP_COMPONENTS) + cd $(subst /,$(PS),$(SAMPLE_CLASS_OUT)) && $(SDK_ZIP) ../../bin/$(@F) -u $(<F) + cd $(subst /,$(PS),$(SAMPLE_CLASS_OUT)/$(subst .$(UNOOXT_EXT),,$(@F))) && $(SDK_ZIP) ../../../bin/$(@F) -u META-INF/manifest.xml + +$(COMP_REGISTERFLAG) : $(COMP_PACKAGE) +ifeq "$(SDK_AUTO_DEPLOYMENT)" "YES" + -$(DEL) $(subst \\,\,$(subst /,$(PS),$@)) + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(DEPLOYTOOL) $(COMP_PACKAGE_URL) + @echo flagged > $(subst /,$(PS),$@) +else + @echo -------------------------------------------------------------------------------- + @echo If you want to install your component automatically, please set the environment + @echo variable SDK_AUTO_DEPLOYMENT = YES. But note that auto deployment is only + @echo possible if no office instance is running. + @echo -------------------------------------------------------------------------------- +endif + +$(APP1_JAR) : $(SAMPLE_CLASS_OUT)/$(APP1_NAME).mf $(SAMPLE_CLASS_OUT)/$(APP1_NAME).class +$(APP2_JAR) : $(SAMPLE_CLASS_OUT)/$(APP2_NAME).mf $(SAMPLE_CLASS_OUT)/$(APP2_NAME).class +$(APP3_JAR) : $(SAMPLE_CLASS_OUT)/$(APP3_NAME).mf $(SAMPLE_CLASS_OUT)/$(APP3_NAME).class +$(APP4_JAR) : $(SAMPLE_CLASS_OUT)/$(APP4_NAME).mf $(SAMPLE_CLASS_OUT)/$(APP4_NAME).class +$(APP5_JAR) : $(SAMPLE_CLASS_OUT)/$(APP5_NAME).mf $(SAMPLE_CLASS_OUT)/$(APP5_NAME).class + +$(SAMPLE_NAME) : $(COMP_REGISTERFLAG) $(APP1_JAR) $(APP2_JAR) $(APP3_JAR) $(APP4_JAR) $(APP5_JAR) + @echo -------------------------------------------------------------------------------- + @echo When you run the "$(QM)$(APP5_NAME)$(QM)" example please select the + @echo legend or the title to add a $(APP5_NAME). The example terminates + @echo when the document is closed. + @echo - + @echo Please use the following commands to execute the examples! + @echo - + @echo $(MAKE) $(APP1_NAME).run + @echo $(MAKE) $(APP2_NAME).run + @echo $(MAKE) $(APP3_NAME).run + @echo $(MAKE) $(APP4_NAME).run + @echo $(MAKE) $(APP5_NAME).run + @echo -------- + @echo The Chart add-in component was installed if SDK_AUTO_DEPLOYMENT = YES. + @echo Load the "$(QM)AddInChart.ods$(QM)" document in your office to see the new chart type, + @echo and see the example description. + @echo - + @echo $(MAKE) AddInChart.ods.load + @echo -------------------------------------------------------------------------------- + +%.run: $(SAMPLE_CLASS_OUT)/%.jar + $(SDK_JAVA) -Dcom.sun.star.lib.loader.unopath="$(OFFICE_PROGRAM_PATH)" -jar $< + +AddInChart.ods.load : $(REGISTERFLAG) + "$(OFFICE_PROGRAM_PATH)$(PS)soffice" $(basename $@) + +.PHONY: clean +clean : + -$(DELRECURSIVE) $(subst /,$(PS),$(SAMPLE_CLASS_OUT)) + -$(DELRECURSIVE) $(subst /,$(PS),$(SAMPLE_GEN_OUT)) + -$(DEL) $(subst \\,\,$(subst /,$(PS),$(COMP_PACKAGE_URL))) diff --git a/odk/examples/DevelopersGuide/Charts/SelectionChangeListener.java b/odk/examples/DevelopersGuide/Charts/SelectionChangeListener.java new file mode 100644 index 000000000..e9ba0581c --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/SelectionChangeListener.java @@ -0,0 +1,220 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +import com.sun.star.awt.XMessageBox; +import com.sun.star.awt.XWindowPeer; +import com.sun.star.frame.XDesktop; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.lang.*; +import com.sun.star.frame.XModel; +import com.sun.star.frame.XController; + +// application specific classes +import com.sun.star.chart.*; +import com.sun.star.uno.XComponentContext; +import com.sun.star.uno.XInterface; + +import com.sun.star.view.XSelectionChangeListener; +import com.sun.star.view.XSelectionSupplier; + +import com.sun.star.table.CellRangeAddress; +import com.sun.star.table.XCellRange; +import com.sun.star.sheet.XCellRangeAddressable; + +import com.sun.star.awt.Point; +import com.sun.star.awt.Size; +import com.sun.star.awt.XMessageBoxFactory; +import com.sun.star.awt.MessageBoxType; +import com.sun.star.awt.XWindow; + +// __________ Implementation __________ + +/** Create a spreadsheet add some data. + * Create a presentation and add a chart. + * Connect the chart to a calc range via a listener + * + * Note: This example does not work in StarOffice 6.0. It will be available + * in the StarOffice Accessibility release. + */ +public class SelectionChangeListener implements XSelectionChangeListener { + public static void main( String args[] ) { + SelectionChangeListener aMySelf = new SelectionChangeListener( args ); + + aMySelf.run(); + } + + public SelectionChangeListener( String args[] ) { + Helper aHelper = new Helper( args ); + + maContext = aHelper.getComponentContext(); + + CalcHelper aCalcHelper = new CalcHelper( aHelper.createSpreadsheetDocument() ); + + // insert a cell range with 4 columns and 12 rows filled with random numbers + XCellRange aRange = aCalcHelper.insertRandomRange( 4, 12 ); + CellRangeAddress aRangeAddress = UnoRuntime.queryInterface( + XCellRangeAddressable.class, aRange).getRangeAddress(); + + // change view to sheet containing the chart + aCalcHelper.raiseChartSheet(); + + // the unit for measures is 1/100th of a millimeter + // position at (1cm, 1cm) + Point aPos = new Point( 1000, 1000 ); + + // size of the chart is 15cm x 9.271cm + Size aExtent = new Size( 15000, 9271 ); + + // insert a new chart into the "Chart" sheet of the + // spreadsheet document + maChartDocument = aCalcHelper.insertChart( + "SampleChart", + aRangeAddress, + aPos, + aExtent, + "com.sun.star.chart.XYDiagram" ); + } + + + + public void run() { + boolean bTrying = true; + + while( bTrying ) { + // start listening for selection changes + XSelectionSupplier aSelSupp = UnoRuntime.queryInterface( + XSelectionSupplier.class, + (UnoRuntime.queryInterface( + XModel.class, maChartDocument ).getCurrentController()) ); + if( aSelSupp != null ) { + aSelSupp.addSelectionChangeListener( this ); + System.out.println( "Successfully attached as selection change listener" ); + bTrying = false; + } + + // start listening for death of Controller + XComponent aComp = UnoRuntime.queryInterface( XComponent.class, aSelSupp ); + if( aComp != null ) { + aComp.addEventListener( this ); + System.out.println( "Successfully attached as dispose listener" ); + } + + try { + Thread.sleep( 500 ); + } catch( InterruptedException ex ) { + } + } + } + + + + // XEventListener (base of XSelectionChangeListener) + public void disposing( EventObject aSourceObj ) { + System.out.println( "disposing called. detaching as listener" ); + + // stop listening for selection changes + XSelectionSupplier aCtrl = UnoRuntime.queryInterface( + XSelectionSupplier.class, aSourceObj ); + if( aCtrl != null ) + aCtrl.removeSelectionChangeListener( this ); + + // remove as dispose listener + XComponent aComp = UnoRuntime.queryInterface( XComponent.class, aSourceObj ); + if( aComp != null ) + aComp.removeEventListener( this ); + + // bail out + System.exit( 0 ); + } + + + + // XSelectionChangeListener + public void selectionChanged( EventObject aEvent ) { + XController aCtrl = UnoRuntime.queryInterface( XController.class, aEvent.Source ); + if( aCtrl != null ) { + XMultiComponentFactory mMCF = maContext.getServiceManager(); + + MyMessageBox aMsgBox = new MyMessageBox(mMCF); + + aMsgBox.start(); + + System.out.println("Listener finished"); + } + } + + // __________ private __________ + + private class MyMessageBox extends Thread{ + private final XMultiComponentFactory mMCF; + + public MyMessageBox(XMultiComponentFactory xMCF){ + mMCF = xMCF; + } + + @Override + public void run() { + XDesktop aDesktop = null; + XInterface aToolKit = null; + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + try { + Object oDesktop = mMCF.createInstanceWithContext("com.sun.star.frame.Desktop", maContext); + Object oToolKit = mMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", maContext); + + aDesktop = UnoRuntime.queryInterface(XDesktop.class, oDesktop); + aToolKit = UnoRuntime.queryInterface(XInterface.class, oToolKit); + } catch (Exception ex) { + ex.printStackTrace(); + } + + XWindow xWin = aDesktop.getCurrentFrame().getContainerWindow(); + XWindowPeer aWinPeer = UnoRuntime.queryInterface(XWindowPeer.class, xWin); + + int button = com.sun.star.awt.MessageBoxButtons.BUTTONS_OK; + XMessageBoxFactory aMBF = UnoRuntime.queryInterface(XMessageBoxFactory.class, aToolKit); + XMessageBox xMB = aMBF.createMessageBox(aWinPeer, MessageBoxType.INFOBOX, button, "Event-Notify", "Listener was called, selection has changed"); + xMB.execute(); + } + } + + private final XChartDocument maChartDocument; + private final XComponentContext maContext; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/DevelopersGuide/Charts/bullet.gif b/odk/examples/DevelopersGuide/Charts/bullet.gif Binary files differnew file mode 100644 index 000000000..0f8efd140 --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/bullet.gif |