Branch libreoffice-5-0-4
[LibreOffice.git] / testtools / source / cliversioning / version.cs
blobb1717148da7223ff0b17c85af4db78df063141f0
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 using System;
20 using unoidl.com.sun.star.lang;
21 using unoidl.com.sun.star.uno;
22 using unoidl.com.sun.star.frame;
23 using unoidl.com.sun.star.util;
25 namespace cliversion
27 public class Version
29 public Version()
31 try
33 // System.Diagnostics.Debugger.Launch();
35 //link with cli_ure.dll
36 uno.util.WeakBase wb = new uno.util.WeakBase();
37 using ( SpreadsheetSample aSample = new SpreadsheetSample() )
39 aSample.doCellRangeSamples();
40 aSample.terminate();
43 catch (System.Exception )
45 //This exception is thrown if we link with a library which is not
46 //available
47 throw;
52 class SpreadsheetSample: SpreadsheetDocHelper
54 public SpreadsheetSample()
57 /** All samples regarding the service com.sun.star.sheet.SheetCellRange. */
58 public void doCellRangeSamples()
60 unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
61 unoidl.com.sun.star.table.XCellRange xCellRange = null;
62 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
63 unoidl.com.sun.star.table.CellRangeAddress aRangeAddress = null;
65 // Preparation
66 setFormula( xSheet, "B5", "First cell" );
67 setFormula( xSheet, "B6", "Second cell" );
68 // Get cell range B5:B6 by position - (column, row, column, row)
69 xCellRange = xSheet.getCellRangeByPosition( 1, 4, 1, 5 );
72 // --- Change cell range properties. ---
73 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
74 // from com.sun.star.styles.CharacterProperties
75 xPropSet.setPropertyValue(
76 "CharColor", new uno.Any( (Int32) 0x003399 ) );
77 xPropSet.setPropertyValue(
78 "CharHeight", new uno.Any( (Single) 20.0 ) );
79 // from com.sun.star.styles.ParagraphProperties
80 xPropSet.setPropertyValue(
81 "ParaLeftMargin", new uno.Any( (Int32) 500 ) );
82 // from com.sun.star.table.CellProperties
83 xPropSet.setPropertyValue(
84 "IsCellBackgroundTransparent", new uno.Any( false ) );
85 xPropSet.setPropertyValue(
86 "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
89 // --- Replace text in all cells. ---
90 unoidl.com.sun.star.util.XReplaceable xReplace =
91 (unoidl.com.sun.star.util.XReplaceable) xCellRange;
92 unoidl.com.sun.star.util.XReplaceDescriptor xReplaceDesc =
93 xReplace.createReplaceDescriptor();
94 xReplaceDesc.setSearchString( "cell" );
95 xReplaceDesc.setReplaceString( "text" );
96 // property SearchWords searches for whole cells!
97 xReplaceDesc.setPropertyValue( "SearchWords", new uno.Any( false ) );
98 int nCount = xReplace.replaceAll( xReplaceDesc );
100 // --- Merge cells. ---
101 xCellRange = xSheet.getCellRangeByName( "F3:G6" );
102 prepareRange( xSheet, "E1:H7", "XMergeable" );
103 unoidl.com.sun.star.util.XMergeable xMerge =
104 (unoidl.com.sun.star.util.XMergeable) xCellRange;
105 xMerge.merge( true );
108 // --- Column properties. ---
109 xCellRange = xSheet.getCellRangeByName( "B1" );
110 unoidl.com.sun.star.table.XColumnRowRange xColRowRange =
111 (unoidl.com.sun.star.table.XColumnRowRange) xCellRange;
112 unoidl.com.sun.star.table.XTableColumns xColumns =
113 xColRowRange.getColumns();
115 uno.Any aColumnObj = xColumns.getByIndex( 0 );
116 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) aColumnObj.Value;
117 xPropSet.setPropertyValue( "Width", new uno.Any( (Int32) 6000 ) );
119 unoidl.com.sun.star.container.XNamed xNamed =
120 (unoidl.com.sun.star.container.XNamed) aColumnObj.Value;
122 // --- Cell range data ---
123 prepareRange( xSheet, "A9:C30", "XCellRangeData" );
125 xCellRange = xSheet.getCellRangeByName( "A10:C30" );
126 unoidl.com.sun.star.sheet.XCellRangeData xData =
127 (unoidl.com.sun.star.sheet.XCellRangeData) xCellRange;
128 uno.Any [][] aValues =
130 new uno.Any [] { new uno.Any( "Name" ),
131 new uno.Any( "Fruit" ),
132 new uno.Any( "Quantity" ) },
133 new uno.Any [] { new uno.Any( "Alice" ),
134 new uno.Any( "Apples" ),
135 new uno.Any( (Double) 3.0 ) },
136 new uno.Any [] { new uno.Any( "Alice" ),
137 new uno.Any( "Oranges" ),
138 new uno.Any( (Double) 7.0 ) },
139 new uno.Any [] { new uno.Any( "Bob" ),
140 new uno.Any( "Apples" ),
141 new uno.Any( (Double) 3.0 ) },
142 new uno.Any [] { new uno.Any( "Alice" ),
143 new uno.Any( "Apples" ),
144 new uno.Any( (Double) 9.0 ) },
145 new uno.Any [] { new uno.Any( "Bob" ),
146 new uno.Any( "Apples" ),
147 new uno.Any( (Double) 5.0 ) },
148 new uno.Any [] { new uno.Any( "Bob" ),
149 new uno.Any( "Oranges" ),
150 new uno.Any( (Double) 6.0 ) },
151 new uno.Any [] { new uno.Any( "Alice" ),
152 new uno.Any( "Oranges" ),
153 new uno.Any( (Double) 3.0 ) },
154 new uno.Any [] { new uno.Any( "Alice" ),
155 new uno.Any( "Apples" ),
156 new uno.Any( (Double) 8.0 ) },
157 new uno.Any [] { new uno.Any( "Alice" ),
158 new uno.Any( "Oranges" ),
159 new uno.Any( (Double) 1.0 ) },
160 new uno.Any [] { new uno.Any( "Bob" ),
161 new uno.Any( "Oranges" ),
162 new uno.Any( (Double) 2.0 ) },
163 new uno.Any [] { new uno.Any( "Bob" ),
164 new uno.Any( "Oranges" ),
165 new uno.Any( (Double) 7.0 ) },
166 new uno.Any [] { new uno.Any( "Bob" ),
167 new uno.Any( "Apples" ),
168 new uno.Any( (Double) 1.0 ) },
169 new uno.Any [] { new uno.Any( "Alice" ),
170 new uno.Any( "Apples" ),
171 new uno.Any( (Double) 8.0 ) },
172 new uno.Any [] { new uno.Any( "Alice" ),
173 new uno.Any( "Oranges" ),
174 new uno.Any( (Double) 8.0 ) },
175 new uno.Any [] { new uno.Any( "Alice" ),
176 new uno.Any( "Apples" ),
177 new uno.Any( (Double) 7.0 ) },
178 new uno.Any [] { new uno.Any( "Bob" ),
179 new uno.Any( "Apples" ),
180 new uno.Any( (Double) 1.0 ) },
181 new uno.Any [] { new uno.Any( "Bob" ),
182 new uno.Any( "Oranges" ),
183 new uno.Any( (Double) 9.0 ) },
184 new uno.Any [] { new uno.Any( "Bob" ),
185 new uno.Any( "Oranges" ),
186 new uno.Any( (Double) 3.0 ) },
187 new uno.Any [] { new uno.Any( "Alice" ),
188 new uno.Any( "Oranges" ),
189 new uno.Any( (Double) 4.0 ) },
190 new uno.Any [] { new uno.Any( "Alice" ),
191 new uno.Any( "Apples" ),
192 new uno.Any( (Double) 9.0 ) }
194 xData.setDataArray( aValues );
197 // --- Get cell range address. ---
198 unoidl.com.sun.star.sheet.XCellRangeAddressable xRangeAddr =
199 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
200 aRangeAddress = xRangeAddr.getRangeAddress();
202 // --- Sheet operation. ---
203 // uses the range filled with XCellRangeData
204 unoidl.com.sun.star.sheet.XSheetOperation xSheetOp =
205 (unoidl.com.sun.star.sheet.XSheetOperation) xData;
206 double fResult = xSheetOp.computeFunction(
207 unoidl.com.sun.star.sheet.GeneralFunction.AVERAGE );
209 // --- Fill series ---
210 // Prepare the example
211 setValue( xSheet, "E10", 1 );
212 setValue( xSheet, "E11", 4 );
213 setDate( xSheet, "E12", 30, 1, 2002 );
214 setFormula( xSheet, "I13", "Text 10" );
215 setFormula( xSheet, "E14", "Jan" );
216 setValue( xSheet, "K14", 10 );
217 setValue( xSheet, "E16", 1 );
218 setValue( xSheet, "F16", 2 );
219 setDate( xSheet, "E17", 28, 2, 2002 );
220 setDate( xSheet, "F17", 28, 1, 2002 );
221 setValue( xSheet, "E18", 6 );
222 setValue( xSheet, "F18", 4 );
226 /** Returns the XCellSeries interface of a cell range.
227 @param xSheet The spreadsheet containing the cell range.
228 @param aRange The address of the cell range.
229 @return The XCellSeries interface. */
230 private unoidl.com.sun.star.sheet.XCellSeries getCellSeries(
231 unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
233 return (unoidl.com.sun.star.sheet.XCellSeries)
234 xSheet.getCellRangeByName( aRange );
239 /** This is a helper class for the spreadsheet and table samples.
240 It connects to a running office and creates a spreadsheet document.
241 Additionally it contains various helper functions.
243 class SpreadsheetDocHelper : System.IDisposable
246 // __ private members ___________________________________________
248 private const String msDataSheetName = "Data";
250 private unoidl.com.sun.star.uno.XComponentContext m_xContext;
251 private unoidl.com.sun.star.lang.XMultiServiceFactory mxMSFactory;
252 private unoidl.com.sun.star.sheet.XSpreadsheetDocument mxDocument;
256 public SpreadsheetDocHelper()
258 // System.Diagnostics.Debugger.Launch();
259 // Connect to a running office and get the service manager
260 mxMSFactory = connect();
261 // Create a new spreadsheet document
262 mxDocument = initDocument();
265 // __ helper methods ____________________________________________
267 /** Returns the service manager.
268 @return XMultiServiceFactory interface of the service manager. */
269 public unoidl.com.sun.star.lang.XMultiServiceFactory getServiceManager()
271 return mxMSFactory;
274 /** Returns the whole spreadsheet document.
275 @return XSpreadsheetDocument interface of the document. */
276 public unoidl.com.sun.star.sheet.XSpreadsheetDocument getDocument()
278 return mxDocument;
281 /** Returns the spreadsheet with the specified index (0-based).
282 @param nIndex The index of the sheet.
283 @return XSpreadsheet interface of the sheet. */
284 public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex )
286 // Collection of sheets
287 unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
288 mxDocument.getSheets();
290 unoidl.com.sun.star.container.XIndexAccess xSheetsIA =
291 (unoidl.com.sun.star.container.XIndexAccess) xSheets;
293 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
294 (unoidl.com.sun.star.sheet.XSpreadsheet)
295 xSheetsIA.getByIndex( nIndex ).Value;
297 return xSheet;
300 /** Inserts a new empty spreadsheet with the specified name.
301 @param aName The name of the new sheet.
302 @param nIndex The insertion index.
303 @return The XSpreadsheet interface of the new sheet. */
304 public unoidl.com.sun.star.sheet.XSpreadsheet insertSpreadsheet(
305 String aName, short nIndex )
307 // Collection of sheets
308 unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
309 mxDocument.getSheets();
311 xSheets.insertNewByName( aName, nIndex );
312 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
313 (unoidl.com.sun.star.sheet.XSpreadsheet)
314 xSheets.getByName( aName ).Value;
316 return xSheet;
320 // Methods to fill values into cells.
322 /** Writes a double value into a spreadsheet.
323 @param xSheet The XSpreadsheet interface of the spreadsheet.
324 @param aCellName The address of the cell (or a named range).
325 @param fValue The value to write into the cell. */
326 public void setValue(
327 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
328 String aCellName,
329 double fValue )
331 xSheet.getCellRangeByName( aCellName ).getCellByPosition(
332 0, 0 ).setValue( fValue );
335 /** Writes a formula into a spreadsheet.
336 @param xSheet The XSpreadsheet interface of the spreadsheet.
337 @param aCellName The address of the cell (or a named range).
338 @param aFormula The formula to write into the cell. */
339 public void setFormula(
340 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
341 String aCellName,
342 String aFormula )
344 xSheet.getCellRangeByName( aCellName ).getCellByPosition(
345 0, 0 ).setFormula( aFormula );
348 /** Writes a date with standard date format into a spreadsheet.
349 @param xSheet The XSpreadsheet interface of the spreadsheet.
350 @param aCellName The address of the cell (or a named range).
351 @param nDay The day of the date.
352 @param nMonth The month of the date.
353 @param nYear The year of the date. */
354 public void setDate(
355 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
356 String aCellName,
357 int nDay, int nMonth, int nYear )
359 // Set the date value.
360 unoidl.com.sun.star.table.XCell xCell =
361 xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 );
362 String aDateStr = nMonth + "/" + nDay + "/" + nYear;
363 xCell.setFormula( aDateStr );
365 // Set standard date format.
366 unoidl.com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier =
367 (unoidl.com.sun.star.util.XNumberFormatsSupplier) getDocument();
368 unoidl.com.sun.star.util.XNumberFormatTypes xFormatTypes =
369 (unoidl.com.sun.star.util.XNumberFormatTypes)
370 xFormatsSupplier.getNumberFormats();
371 int nFormat = xFormatTypes.getStandardFormat(
372 unoidl.com.sun.star.util.NumberFormat.DATE,
373 new unoidl.com.sun.star.lang.Locale() );
375 unoidl.com.sun.star.beans.XPropertySet xPropSet =
376 (unoidl.com.sun.star.beans.XPropertySet) xCell;
377 xPropSet.setPropertyValue(
378 "NumberFormat",
379 new uno.Any( (Int32) nFormat ) );
382 /** Draws a colored border around the range and writes the headline
383 in the first cell.
385 @param xSheet The XSpreadsheet interface of the spreadsheet.
386 @param aRange The address of the cell range (or a named range).
387 @param aHeadline The headline text. */
388 public void prepareRange(
389 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
390 String aRange, String aHeadline )
392 unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
393 unoidl.com.sun.star.table.XCellRange xCellRange = null;
395 // draw border
396 xCellRange = xSheet.getCellRangeByName( aRange );
397 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
398 unoidl.com.sun.star.table.BorderLine aLine =
399 new unoidl.com.sun.star.table.BorderLine();
400 aLine.Color = 0x99CCFF;
401 aLine.InnerLineWidth = aLine.LineDistance = 0;
402 aLine.OuterLineWidth = 100;
403 unoidl.com.sun.star.table.TableBorder aBorder =
404 new unoidl.com.sun.star.table.TableBorder();
405 aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine =
406 aBorder.RightLine = aLine;
407 aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true;
408 aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true;
409 xPropSet.setPropertyValue(
410 "TableBorder",
411 new uno.Any(
412 typeof (unoidl.com.sun.star.table.TableBorder), aBorder ) );
414 // draw headline
415 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
416 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
417 unoidl.com.sun.star.table.CellRangeAddress aAddr =
418 xAddr.getRangeAddress();
420 xCellRange = xSheet.getCellRangeByPosition(
421 aAddr.StartColumn,
422 aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow );
424 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange;
425 xPropSet.setPropertyValue(
426 "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
427 // write headline
428 unoidl.com.sun.star.table.XCell xCell =
429 xCellRange.getCellByPosition( 0, 0 );
430 xCell.setFormula( aHeadline );
431 xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCell;
432 xPropSet.setPropertyValue(
433 "CharColor", new uno.Any( (Int32) 0x003399 ) );
434 xPropSet.setPropertyValue(
435 "CharWeight",
436 new uno.Any( (Single) unoidl.com.sun.star.awt.FontWeight.BOLD ) );
440 // Methods to create cell addresses and range addresses.
442 /** Creates a unoidl.com.sun.star.table.CellAddress and initializes it
443 with the given range.
444 @param xSheet The XSpreadsheet interface of the spreadsheet.
445 @param aCell The address of the cell (or a named cell). */
446 public unoidl.com.sun.star.table.CellAddress createCellAddress(
447 unoidl.com.sun.star.sheet.XSpreadsheet xSheet,
448 String aCell )
450 unoidl.com.sun.star.sheet.XCellAddressable xAddr =
451 (unoidl.com.sun.star.sheet.XCellAddressable)
452 xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 );
453 return xAddr.getCellAddress();
456 /** Creates a unoidl.com.sun.star.table.CellRangeAddress and initializes
457 it with the given range.
458 @param xSheet The XSpreadsheet interface of the spreadsheet.
459 @param aRange The address of the cell range (or a named range). */
460 public unoidl.com.sun.star.table.CellRangeAddress createCellRangeAddress(
461 unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
463 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
464 (unoidl.com.sun.star.sheet.XCellRangeAddressable)
465 xSheet.getCellRangeByName( aRange );
466 return xAddr.getRangeAddress();
470 // Methods to convert cell addresses and range addresses to strings.
472 /** Returns the text address of the cell.
473 @param nColumn The column index.
474 @param nRow The row index.
475 @return A string containing the cell address. */
476 public String getCellAddressString( int nColumn, int nRow )
478 String aStr = "";
479 if (nColumn > 25)
480 aStr += (char) ('A' + nColumn / 26 - 1);
481 aStr += (char) ('A' + nColumn % 26);
482 aStr += (nRow + 1);
483 return aStr;
486 /** Returns the text address of the cell range.
487 @param aCellRange The cell range address.
488 @return A string containing the cell range address. */
489 public String getCellRangeAddressString(
490 unoidl.com.sun.star.table.CellRangeAddress aCellRange )
492 return
493 getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow )
494 + ":"
495 + getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow );
498 /** Returns the text address of the cell range.
499 @param xCellRange The XSheetCellRange interface of the cell range.
500 @param bWithSheet true = Include sheet name.
501 @return A string containing the cell range address. */
502 public String getCellRangeAddressString(
503 unoidl.com.sun.star.sheet.XSheetCellRange xCellRange, bool bWithSheet )
505 String aStr = "";
506 if (bWithSheet)
508 unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
509 xCellRange.getSpreadsheet();
510 unoidl.com.sun.star.container.XNamed xNamed =
511 (unoidl.com.sun.star.container.XNamed) xSheet;
512 aStr += xNamed.getName() + ".";
514 unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
515 (unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
516 aStr += getCellRangeAddressString( xAddr.getRangeAddress() );
517 return aStr;
520 /** Returns a list of addresses of all cell ranges contained in the
521 collection.
523 @param xRangesIA The XIndexAccess interface of the collection.
524 @return A string containing the cell range address list. */
525 public String getCellRangeListString(
526 unoidl.com.sun.star.container.XIndexAccess xRangesIA )
528 String aStr = "";
529 int nCount = xRangesIA.getCount();
530 for (int nIndex = 0; nIndex < nCount; ++nIndex)
532 if (nIndex > 0)
533 aStr += " ";
534 uno.Any aRangeObj = xRangesIA.getByIndex( nIndex );
535 unoidl.com.sun.star.sheet.XSheetCellRange xCellRange =
536 (unoidl.com.sun.star.sheet.XSheetCellRange) aRangeObj.Value;
537 aStr += getCellRangeAddressString( xCellRange, false );
539 return aStr;
544 /** Connect to a running office that is accepting connections.
545 @return The ServiceManager to instantiate office components. */
546 private XMultiServiceFactory connect()
549 m_xContext = uno.util.Bootstrap.bootstrap();
551 return (XMultiServiceFactory) m_xContext.getServiceManager();
554 public void Dispose()
559 /** Creates an empty spreadsheet document.
560 @return The XSpreadsheetDocument interface of the document. */
561 private unoidl.com.sun.star.sheet.XSpreadsheetDocument initDocument()
563 XComponentLoader aLoader = (XComponentLoader)
564 mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
566 XComponent xComponent = aLoader.loadComponentFromURL(
567 "private:factory/scalc", "_blank", 0,
568 new unoidl.com.sun.star.beans.PropertyValue[0] );
570 return (unoidl.com.sun.star.sheet.XSpreadsheetDocument) xComponent;
574 public void terminate()
576 XModifiable xMod = (XModifiable) mxDocument;
577 if (xMod != null)
578 xMod.setModified(false);
579 XDesktop aDesktop = (XDesktop)
580 mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
581 if (aDesktop != null)
585 aDesktop.terminate();
587 catch (DisposedException d)
589 //This exception may be thrown because shutting down OOo using
590 //XDesktop terminate does not really work. In the case of the
591 //Exception OOo will still terminate.