/libreoffice-3.6.0.2/odk/examples/CLI/CSharp/Spreadsheet/ViewSample.cs

# · C# · 165 lines · 127 code · 20 blank · 18 comment · 6 complexity · dab7049733f1163ea919708a82a2c723 MD5 · raw file

  1. using System;
  2. using System.Threading;
  3. // __________ implementation ____________________________________
  4. /** Create and modify a spreadsheet view.
  5. */
  6. public class ViewSample : SpreadsheetDocHelper
  7. {
  8. public static void Main( String [] args )
  9. {
  10. try
  11. {
  12. using ( ViewSample aSample = new ViewSample( args ) )
  13. {
  14. aSample.doSampleFunction();
  15. }
  16. Console.WriteLine( "\nSamples done." );
  17. }
  18. catch (Exception ex)
  19. {
  20. Console.WriteLine( "Sample caught exception! " + ex );
  21. }
  22. }
  23. // ________________________________________________________________
  24. public ViewSample( String[] args )
  25. : base( args )
  26. {
  27. }
  28. // ________________________________________________________________
  29. /** This sample function performs all changes on the view. */
  30. public void doSampleFunction()
  31. {
  32. unoidl.com.sun.star.sheet.XSpreadsheetDocument xDoc = getDocument();
  33. unoidl.com.sun.star.frame.XModel xModel =
  34. (unoidl.com.sun.star.frame.XModel) xDoc;
  35. unoidl.com.sun.star.frame.XController xController =
  36. xModel.getCurrentController();
  37. // --- Spreadsheet view ---
  38. // freeze the first column and first two rows
  39. unoidl.com.sun.star.sheet.XViewFreezable xFreeze =
  40. (unoidl.com.sun.star.sheet.XViewFreezable) xController;
  41. if ( null != xFreeze )
  42. Console.WriteLine( "got xFreeze" );
  43. xFreeze.freezeAtPosition( 1, 2 );
  44. // --- View pane ---
  45. // get the cell range shown in the second pane and assign
  46. // a cell background to them
  47. unoidl.com.sun.star.container.XIndexAccess xIndex =
  48. (unoidl.com.sun.star.container.XIndexAccess) xController;
  49. uno.Any aPane = xIndex.getByIndex(1);
  50. unoidl.com.sun.star.sheet.XCellRangeReferrer xRefer =
  51. (unoidl.com.sun.star.sheet.XCellRangeReferrer) aPane.Value;
  52. unoidl.com.sun.star.table.XCellRange xRange = xRefer.getReferredCells();
  53. unoidl.com.sun.star.beans.XPropertySet xRangeProp =
  54. (unoidl.com.sun.star.beans.XPropertySet) xRange;
  55. xRangeProp.setPropertyValue(
  56. "IsCellBackgroundTransparent", new uno.Any( false ) );
  57. xRangeProp.setPropertyValue(
  58. "CellBackColor", new uno.Any( (Int32) 0xFFFFCC ) );
  59. // --- View settings ---
  60. // change the view to display green grid lines
  61. unoidl.com.sun.star.beans.XPropertySet xProp =
  62. (unoidl.com.sun.star.beans.XPropertySet) xController;
  63. xProp.setPropertyValue(
  64. "ShowGrid", new uno.Any( true ) );
  65. xProp.setPropertyValue(
  66. "GridColor", new uno.Any( (Int32) 0x00CC00 ) );
  67. // --- Range selection ---
  68. // let the user select a range and use it as the view's selection
  69. unoidl.com.sun.star.sheet.XRangeSelection xRngSel =
  70. (unoidl.com.sun.star.sheet.XRangeSelection) xController;
  71. ExampleRangeListener aListener = new ExampleRangeListener();
  72. xRngSel.addRangeSelectionListener( aListener );
  73. unoidl.com.sun.star.beans.PropertyValue[] aArguments =
  74. new unoidl.com.sun.star.beans.PropertyValue[2];
  75. aArguments[0] = new unoidl.com.sun.star.beans.PropertyValue();
  76. aArguments[0].Name = "Title";
  77. aArguments[0].Value = new uno.Any( "Please select a range" );
  78. aArguments[1] = new unoidl.com.sun.star.beans.PropertyValue();
  79. aArguments[1].Name = "CloseOnMouseRelease";
  80. aArguments[1].Value = new uno.Any( true );
  81. xRngSel.startRangeSelection( aArguments );
  82. Monitor.Enter( aListener );
  83. try
  84. {
  85. Monitor.Wait( aListener ); // wait until the selection is done
  86. }
  87. finally
  88. {
  89. Monitor.Exit( aListener );
  90. }
  91. xRngSel.removeRangeSelectionListener( aListener );
  92. if ( aListener.aResult != null && aListener.aResult.Length != 0 )
  93. {
  94. unoidl.com.sun.star.view.XSelectionSupplier xSel =
  95. (unoidl.com.sun.star.view.XSelectionSupplier) xController;
  96. unoidl.com.sun.star.sheet.XSpreadsheetView xView =
  97. (unoidl.com.sun.star.sheet.XSpreadsheetView) xController;
  98. unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
  99. xView.getActiveSheet();
  100. unoidl.com.sun.star.table.XCellRange xResultRange =
  101. xSheet.getCellRangeByName( aListener.aResult );
  102. xSel.select(
  103. new uno.Any(
  104. typeof (unoidl.com.sun.star.table.XCellRange),
  105. xResultRange ) );
  106. }
  107. }
  108. // ________________________________________________________________
  109. // listener to react on finished selection
  110. private class ExampleRangeListener
  111. : unoidl.com.sun.star.sheet.XRangeSelectionListener
  112. {
  113. public String aResult;
  114. public void done( unoidl.com.sun.star.sheet.RangeSelectionEvent aEvent )
  115. {
  116. aResult = aEvent.RangeDescriptor;
  117. Monitor.Enter( this );
  118. try
  119. {
  120. Monitor.Pulse( this );
  121. }
  122. finally
  123. {
  124. Monitor.Exit( this );
  125. }
  126. }
  127. public void aborted(
  128. unoidl.com.sun.star.sheet.RangeSelectionEvent aEvent )
  129. {
  130. Monitor.Enter( this );
  131. try
  132. {
  133. Monitor.Pulse( this );
  134. }
  135. finally
  136. {
  137. Monitor.Exit( this );
  138. }
  139. }
  140. public void disposing( unoidl.com.sun.star.lang.EventObject aObj )
  141. {
  142. }
  143. }
  144. // ________________________________________________________________
  145. }