merged tag ooo/DEV300_m102
[LibreOffice.git] / testtools / source / cliversioning / version.cs
blob6071ad339f843489ad18185d980c67314721ca92
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 using System;
29 using unoidl.com.sun.star.lang;
30 using unoidl.com.sun.star.uno;
31 using unoidl.com.sun.star.frame;
32 using unoidl.com.sun.star.util;
34 namespace cliversion
36 public class Version
38 public Version()
40 try
42 // System.Diagnostics.Debugger.Launch();
44 //link with cli_ure.dll
45 uno.util.WeakBase wb = new uno.util.WeakBase();
46 using ( SpreadsheetSample aSample = new SpreadsheetSample() )
48 aSample.doCellRangeSamples();
49 aSample.terminate();
52 catch (System.Exception )
54 //This exception is thrown if we link with a library which is not
55 //available
56 throw;
61 class SpreadsheetSample: SpreadsheetDocHelper
63 public SpreadsheetSample()
66 /** All samples regarding the service com.sun.star.sheet.SheetCellRange. */
67 public void doCellRangeSamples()
69 unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
70 unoidl.com.sun.star.table.XCellRange xCellRange = null;
71 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
72 unoidl.com.sun.star.table.CellRangeAddress aRangeAddress = null;
74 // Preparation
75 setFormula( xSheet, "B5", "First cell" );
76 setFormula( xSheet, "B6", "Second cell" );
77 // Get cell range B5:B6 by position - (column, row, column, row)
78 xCellRange = xSheet.getCellRangeByPosition( 1, 4, 1, 5 );
81 // --- Change cell range properties. ---
82 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
83 // from com.sun.star.styles.CharacterProperties
84 xPropSet.setPropertyValue(
85 "CharColor", new uno.Any( (Int32) 0x003399 ) );
86 xPropSet.setPropertyValue(
87 "CharHeight", new uno.Any( (Single) 20.0 ) );
88 // from com.sun.star.styles.ParagraphProperties
89 xPropSet.setPropertyValue(
90 "ParaLeftMargin", new uno.Any( (Int32) 500 ) );
91 // from com.sun.star.table.CellProperties
92 xPropSet.setPropertyValue(
93 "IsCellBackgroundTransparent", new uno.Any( false ) );
94 xPropSet.setPropertyValue(
95 "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
98 // --- Replace text in all cells. ---
99 unoidl.com.sun.star.util.XReplaceable xReplace =
100 (unoidl.com.sun.star.util.XReplaceable) xCellRange;
101 unoidl.com.sun.star.util.XReplaceDescriptor xReplaceDesc =
102 xReplace.createReplaceDescriptor();
103 xReplaceDesc.setSearchString( "cell" );
104 xReplaceDesc.setReplaceString( "text" );
105 // property SearchWords searches for whole cells!
106 xReplaceDesc.setPropertyValue( "SearchWords", new uno.Any( false ) );
107 int nCount = xReplace.replaceAll( xReplaceDesc );
109 // --- Merge cells. ---
110 xCellRange = xSheet.getCellRangeByName( "F3:G6" );
111 prepareRange( xSheet, "E1:H7", "XMergeable" );
112 unoidl.com.sun.star.util.XMergeable xMerge =
113 (unoidl.com.sun.star.util.XMergeable) xCellRange;
114 xMerge.merge( true );
117 // --- Column properties. ---
118 xCellRange = xSheet.getCellRangeByName( "B1" );
119 unoidl.com.sun.star.table.XColumnRowRange xColRowRange =
120 (unoidl.com.sun.star.table.XColumnRowRange) xCellRange;
121 unoidl.com.sun.star.table.XTableColumns xColumns =
122 xColRowRange.getColumns();
124 uno.Any aColumnObj = xColumns.getByIndex( 0 );
125 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value;
126 xPropSet.setPropertyValue( "Width", new uno.Any( (Int32) 6000 ) );
128 unoidl.com.sun.star.container.XNamed xNamed =
129 (unoidl.com.sun.star.container.XNamed) aColumnObj.Value;
131 // --- Cell range data ---
132 prepareRange( xSheet, "A9:C30", "XCellRangeData" );
134 xCellRange = xSheet.getCellRangeByName( "A10:C30" );
135 unoidl.com.sun.star.sheet.XCellRangeData xData =
136 (unoidl.com.sun.star.sheet.XCellRangeData) xCellRange;
137 uno.Any [][] aValues =
139 new uno.Any [] { new uno.Any( "Name" ),
140 new uno.Any( "Fruit" ),
141 new uno.Any( "Quantity" ) },
142 new uno.Any [] { new uno.Any( "Alice" ),
143 new uno.Any( "Apples" ),
144 new uno.Any( (Double) 3.0 ) },
145 new uno.Any [] { new uno.Any( "Alice" ),
146 new uno.Any( "Oranges" ),
147 new uno.Any( (Double) 7.0 ) },
148 new uno.Any [] { new uno.Any( "Bob" ),
149 new uno.Any( "Apples" ),
150 new uno.Any( (Double) 3.0 ) },
151 new uno.Any [] { new uno.Any( "Alice" ),
152 new uno.Any( "Apples" ),
153 new uno.Any( (Double) 9.0 ) },
154 new uno.Any [] { new uno.Any( "Bob" ),
155 new uno.Any( "Apples" ),
156 new uno.Any( (Double) 5.0 ) },
157 new uno.Any [] { new uno.Any( "Bob" ),
158 new uno.Any( "Oranges" ),
159 new uno.Any( (Double) 6.0 ) },
160 new uno.Any [] { new uno.Any( "Alice" ),
161 new uno.Any( "Oranges" ),
162 new uno.Any( (Double) 3.0 ) },
163 new uno.Any [] { new uno.Any( "Alice" ),
164 new uno.Any( "Apples" ),
165 new uno.Any( (Double) 8.0 ) },
166 new uno.Any [] { new uno.Any( "Alice" ),
167 new uno.Any( "Oranges" ),
168 new uno.Any( (Double) 1.0 ) },
169 new uno.Any [] { new uno.Any( "Bob" ),
170 new uno.Any( "Oranges" ),
171 new uno.Any( (Double) 2.0 ) },
172 new uno.Any [] { new uno.Any( "Bob" ),
173 new uno.Any( "Oranges" ),
174 new uno.Any( (Double) 7.0 ) },
175 new uno.Any [] { new uno.Any( "Bob" ),
176 new uno.Any( "Apples" ),
177 new uno.Any( (Double) 1.0 ) },
178 new uno.Any [] { new uno.Any( "Alice" ),
179 new uno.Any( "Apples" ),
180 new uno.Any( (Double) 8.0 ) },
181 new uno.Any [] { new uno.Any( "Alice" ),
182 new uno.Any( "Oranges" ),
183 new uno.Any( (Double) 8.0 ) },
184 new uno.Any [] { new uno.Any( "Alice" ),
185 new uno.Any( "Apples" ),
186 new uno.Any( (Double) 7.0 ) },
187 new uno.Any [] { new uno.Any( "Bob" ),
188 new uno.Any( "Apples" ),
189 new uno.Any( (Double) 1.0 ) },
190 new uno.Any [] { new uno.Any( "Bob" ),
191 new uno.Any( "Oranges" ),
192 new uno.Any( (Double) 9.0 ) },
193 new uno.Any [] { new uno.Any( "Bob" ),
194 new uno.Any( "Oranges" ),
195 new uno.Any( (Double) 3.0 ) },
196 new uno.Any [] { new uno.Any( "Alice" ),
197 new uno.Any( "Oranges" ),
198 new uno.Any( (Double) 4.0 ) },
199 new uno.Any [] { new uno.Any( "Alice" ),
200 new uno.Any( "Apples" ),
201 new uno.Any( (Double) 9.0 ) }
203 xData.setDataArray( aValues );
206 // --- Get cell range address. ---
207 unoidl.com.sun.star.sheet.XCellRangeAddressable xRangeAddr =
208 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
209 aRangeAddress = xRangeAddr.getRangeAddress();
211 // --- Sheet operation. ---
212 // uses the range filled with XCellRangeData
213 unoidl.com.sun.star.sheet.XSheetOperation xSheetOp =
214 (unoidl.com.sun.star.sheet.XSheetOperation) xData;
215 double fResult = xSheetOp.computeFunction(
216 unoidl.com.sun.star.sheet.GeneralFunction.AVERAGE );
218 // --- Fill series ---
219 // Prepare the example
220 setValue( xSheet, "E10", 1 );
221 setValue( xSheet, "E11", 4 );
222 setDate( xSheet, "E12", 30, 1, 2002 );
223 setFormula( xSheet, "I13", "Text 10" );
224 setFormula( xSheet, "E14", "Jan" );
225 setValue( xSheet, "K14", 10 );
226 setValue( xSheet, "E16", 1 );
227 setValue( xSheet, "F16", 2 );
228 setDate( xSheet, "E17", 28, 2, 2002 );
229 setDate( xSheet, "F17", 28, 1, 2002 );
230 setValue( xSheet, "E18", 6 );
231 setValue( xSheet, "F18", 4 );
235 /** Returns the XCellSeries interface of a cell range.
236 @param xSheet The spreadsheet containing the cell range.
237 @param aRange The address of the cell range.
238 @return The XCellSeries interface. */
239 private unoidl.com.sun.star.sheet.XCellSeries getCellSeries(
240 unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
242 return (unoidl.com.sun.star.sheet.XCellSeries)
243 xSheet.getCellRangeByName( aRange );
248 /** This is a helper class for the spreadsheet and table samples.
249 It connects to a running office and creates a spreadsheet document.
250 Additionally it contains various helper functions.
252 class SpreadsheetDocHelper : System.IDisposable
255 // __ private members ___________________________________________
257 private const String msDataSheetName = "Data";
259 private unoidl.com.sun.star.uno.XComponentContext m_xContext;
260 private unoidl.com.sun.star.lang.XMultiServiceFactory mxMSFactory;
261 private unoidl.com.sun.star.sheet.XSpreadsheetDocument mxDocument;
263 // ________________________________________________________________
265 public SpreadsheetDocHelper()
267 // System.Diagnostics.Debugger.Launch();
268 // Connect to a running office and get the service manager
269 mxMSFactory = connect();
270 // Create a new spreadsheet document
271 mxDocument = initDocument();
274 // __ helper methods ____________________________________________
276 /** Returns the service manager.
277 @return XMultiServiceFactory interface of the service manager. */
278 public unoidl.com.sun.star.lang.XMultiServiceFactory getServiceManager()
280 return mxMSFactory;
283 /** Returns the whole spreadsheet document.
284 @return XSpreadsheetDocument interface of the document. */
285 public unoidl.com.sun.star.sheet.XSpreadsheetDocument getDocument()
287 return mxDocument;
290 /** Returns the spreadsheet with the specified index (0-based).
291 @param nIndex The index of the sheet.
292 @return XSpreadsheet interface of the sheet. */
293 public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex )
295 // Collection of sheets
296 unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
297 mxDocument.getSheets();
299 unoidl.com.sun.star.container.XIndexAccess xSheetsIA =
300 (unoidl.com.sun.star.container.XIndexAccess) xSheets;
302 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
303 (unoidl.com.sun.star.sheet.XSpreadsheet)
304 xSheetsIA.getByIndex( nIndex ).Value;
306 return xSheet;
309 /** Inserts a new empty spreadsheet with the specified name.
310 @param aName The name of the new sheet.
311 @param nIndex The insertion index.
312 @return The XSpreadsheet interface of the new sheet. */
313 public unoidl.com.sun.star.sheet.XSpreadsheet insertSpreadsheet(
314 String aName, short nIndex )
316 // Collection of sheets
317 unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
318 mxDocument.getSheets();
320 xSheets.insertNewByName( aName, nIndex );
321 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
322 (unoidl.com.sun.star.sheet.XSpreadsheet)
323 xSheets.getByName( aName ).Value;
325 return xSheet;
328 // ________________________________________________________________
329 // Methods to fill values into cells.
331 /** Writes a double value into a spreadsheet.
332 @param xSheet The XSpreadsheet interface of the spreadsheet.
333 @param aCellName The address of the cell (or a named range).
334 @param fValue The value to write into the cell. */
335 public void setValue(
336 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
337 String aCellName,
338 double fValue )
340 xSheet.getCellRangeByName( aCellName ).getCellByPosition(
341 0, 0 ).setValue( fValue );
344 /** Writes a formula into a spreadsheet.
345 @param xSheet The XSpreadsheet interface of the spreadsheet.
346 @param aCellName The address of the cell (or a named range).
347 @param aFormula The formula to write into the cell. */
348 public void setFormula(
349 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
350 String aCellName,
351 String aFormula )
353 xSheet.getCellRangeByName( aCellName ).getCellByPosition(
354 0, 0 ).setFormula( aFormula );
357 /** Writes a date with standard date format into a spreadsheet.
358 @param xSheet The XSpreadsheet interface of the spreadsheet.
359 @param aCellName The address of the cell (or a named range).
360 @param nDay The day of the date.
361 @param nMonth The month of the date.
362 @param nYear The year of the date. */
363 public void setDate(
364 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
365 String aCellName,
366 int nDay, int nMonth, int nYear )
368 // Set the date value.
369 unoidl.com.sun.star.table.XCell xCell =
370 xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 );
371 String aDateStr = nMonth + "/" + nDay + "/" + nYear;
372 xCell.setFormula( aDateStr );
374 // Set standard date format.
375 unoidl.com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier =
376 (unoidl.com.sun.star.util.XNumberFormatsSupplier) getDocument();
377 unoidl.com.sun.star.util.XNumberFormatTypes xFormatTypes =
378 (unoidl.com.sun.star.util.XNumberFormatTypes)
379 xFormatsSupplier.getNumberFormats();
380 int nFormat = xFormatTypes.getStandardFormat(
381 unoidl.com.sun.star.util.NumberFormat.DATE,
382 new unoidl.com.sun.star.lang.Locale() );
384 unoidl.com.sun.star.beans.XPropertySet xPropSet =
385 (unoidl.com.sun.star.beans.XPropertySet) xCell;
386 xPropSet.setPropertyValue(
387 "NumberFormat",
388 new uno.Any( (Int32) nFormat ) );
391 /** Draws a colored border around the range and writes the headline
392 in the first cell.
394 @param xSheet The XSpreadsheet interface of the spreadsheet.
395 @param aRange The address of the cell range (or a named range).
396 @param aHeadline The headline text. */
397 public void prepareRange(
398 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
399 String aRange, String aHeadline )
401 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
402 unoidl.com.sun.star.table.XCellRange xCellRange = null;
404 // draw border
405 xCellRange = xSheet.getCellRangeByName( aRange );
406 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
407 unoidl.com.sun.star.table.BorderLine aLine =
408 new unoidl.com.sun.star.table.BorderLine();
409 aLine.Color = 0x99CCFF;
410 aLine.InnerLineWidth = aLine.LineDistance = 0;
411 aLine.OuterLineWidth = 100;
412 unoidl.com.sun.star.table.TableBorder aBorder =
413 new unoidl.com.sun.star.table.TableBorder();
414 aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine =
415 aBorder.RightLine = aLine;
416 aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true;
417 aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true;
418 xPropSet.setPropertyValue(
419 "TableBorder",
420 new uno.Any(
421 typeof (unoidl.com.sun.star.table.TableBorder), aBorder ) );
423 // draw headline
424 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
425 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
426 unoidl.com.sun.star.table.CellRangeAddress aAddr =
427 xAddr.getRangeAddress();
429 xCellRange = xSheet.getCellRangeByPosition(
430 aAddr.StartColumn,
431 aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow );
433 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
434 xPropSet.setPropertyValue(
435 "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
436 // write headline
437 unoidl.com.sun.star.table.XCell xCell =
438 xCellRange.getCellByPosition( 0, 0 );
439 xCell.setFormula( aHeadline );
440 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell;
441 xPropSet.setPropertyValue(
442 "CharColor", new uno.Any( (Int32) 0x003399 ) );
443 xPropSet.setPropertyValue(
444 "CharWeight",
445 new uno.Any( (Single) unoidl.com.sun.star.awt.FontWeight.BOLD ) );
448 // ________________________________________________________________
449 // Methods to create cell addresses and range addresses.
451 /** Creates a unoidl.com.sun.star.table.CellAddress and initializes it
452 with the given range.
453 @param xSheet The XSpreadsheet interface of the spreadsheet.
454 @param aCell The address of the cell (or a named cell). */
455 public unoidl.com.sun.star.table.CellAddress createCellAddress(
456 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
457 String aCell )
459 unoidl.com.sun.star.sheet.XCellAddressable xAddr =
460 (unoidl.com.sun.star.sheet.XCellAddressable)
461 xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 );
462 return xAddr.getCellAddress();
465 /** Creates a unoidl.com.sun.star.table.CellRangeAddress and initializes
466 it with the given range.
467 @param xSheet The XSpreadsheet interface of the spreadsheet.
468 @param aRange The address of the cell range (or a named range). */
469 public unoidl.com.sun.star.table.CellRangeAddress createCellRangeAddress(
470 unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
472 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
473 (unoidl.com.sun.star.sheet.XCellRangeAddressable)
474 xSheet.getCellRangeByName( aRange );
475 return xAddr.getRangeAddress();
478 // ________________________________________________________________
479 // Methods to convert cell addresses and range addresses to strings.
481 /** Returns the text address of the cell.
482 @param nColumn The column index.
483 @param nRow The row index.
484 @return A string containing the cell address. */
485 public String getCellAddressString( int nColumn, int nRow )
487 String aStr = "";
488 if (nColumn > 25)
489 aStr += (char) ('A' + nColumn / 26 - 1);
490 aStr += (char) ('A' + nColumn % 26);
491 aStr += (nRow + 1);
492 return aStr;
495 /** Returns the text address of the cell range.
496 @param aCellRange The cell range address.
497 @return A string containing the cell range address. */
498 public String getCellRangeAddressString(
499 unoidl.com.sun.star.table.CellRangeAddress aCellRange )
501 return
502 getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow )
503 + ":"
504 + getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow );
507 /** Returns the text address of the cell range.
508 @param xCellRange The XSheetCellRange interface of the cell range.
509 @param bWithSheet true = Include sheet name.
510 @return A string containing the cell range address. */
511 public String getCellRangeAddressString(
512 unoidl.com.sun.star.sheet.XSheetCellRange xCellRange, bool bWithSheet )
514 String aStr = "";
515 if (bWithSheet)
517 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
518 xCellRange.getSpreadsheet();
519 unoidl.com.sun.star.container.XNamed xNamed =
520 (unoidl.com.sun.star.container.XNamed) xSheet;
521 aStr += xNamed.getName() + ".";
523 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
524 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
525 aStr += getCellRangeAddressString( xAddr.getRangeAddress() );
526 return aStr;
529 /** Returns a list of addresses of all cell ranges contained in the
530 collection.
532 @param xRangesIA The XIndexAccess interface of the collection.
533 @return A string containing the cell range address list. */
534 public String getCellRangeListString(
535 unoidl.com.sun.star.container.XIndexAccess xRangesIA )
537 String aStr = "";
538 int nCount = xRangesIA.getCount();
539 for (int nIndex = 0; nIndex < nCount; ++nIndex)
541 if (nIndex > 0)
542 aStr += " ";
543 uno.Any aRangeObj = xRangesIA.getByIndex( nIndex );
544 unoidl.com.sun.star.sheet.XSheetCellRange xCellRange =
545 (unoidl.com.sun.star.sheet.XSheetCellRange) aRangeObj.Value;
546 aStr += getCellRangeAddressString( xCellRange, false );
548 return aStr;
551 // ________________________________________________________________
553 /** Connect to a running office that is accepting connections.
554 @return The ServiceManager to instantiate office components. */
555 private XMultiServiceFactory connect()
558 m_xContext = uno.util.Bootstrap.bootstrap();
560 return (XMultiServiceFactory) m_xContext.getServiceManager();
563 public void Dispose()
568 /** Creates an empty spreadsheet document.
569 @return The XSpreadsheetDocument interface of the document. */
570 private unoidl.com.sun.star.sheet.XSpreadsheetDocument initDocument()
572 XComponentLoader aLoader = (XComponentLoader)
573 mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
575 XComponent xComponent = aLoader.loadComponentFromURL(
576 "private:factory/scalc", "_blank", 0,
577 new unoidl.com.sun.star.beans.PropertyValue[0] );
579 return (unoidl.com.sun.star.sheet.XSpreadsheetDocument) xComponent;
583 public void terminate()
585 XModifiable xMod = (XModifiable) mxDocument;
586 if (xMod != null)
587 xMod.setModified(false);
588 XDesktop aDesktop = (XDesktop)
589 mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
590 if (aDesktop != null)
594 aDesktop.terminate();
596 catch (DisposedException d)
598 //This exception may be thrown because shutting down OOo using
599 //XDesktop terminate does not really work. In the case of the
600 //Exception OOo will still terminate.
605 // ________________________________________________________________