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 .
21 import java
.io
.PrintWriter
;
22 import java
.util
.ArrayList
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.SOfficeFactory
;
29 import com
.sun
.star
.beans
.Property
;
30 import com
.sun
.star
.beans
.PropertyAttribute
;
31 import com
.sun
.star
.beans
.XPropertySet
;
32 import com
.sun
.star
.container
.XIndexAccess
;
33 import com
.sun
.star
.container
.XNameAccess
;
34 import com
.sun
.star
.container
.XNameContainer
;
35 import com
.sun
.star
.lang
.XComponent
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.sheet
.XSpreadsheet
;
38 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
39 import com
.sun
.star
.sheet
.XSpreadsheets
;
40 import com
.sun
.star
.style
.XStyle
;
41 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
42 import com
.sun
.star
.table
.XCell
;
43 import com
.sun
.star
.uno
.AnyConverter
;
44 import com
.sun
.star
.uno
.Type
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.uno
.XInterface
;
49 * Test for object which is represented by service
50 * <code>com.sun.star.style.Style</code>. <p>
51 * Object implements the following interfaces :
53 * <li> <code>com::sun::star::container::XNamed</code></li>
54 * <li> <code>com::sun::star::style::Style</code></li>
55 * <li> <code>com::sun::star::style::XStyle</code></li>
57 * @see com.sun.star.style.Style
58 * @see com.sun.star.container.XNamed
59 * @see com.sun.star.style.Style
60 * @see com.sun.star.style.XStyle
61 * @see ifc.container._XNamed
62 * @see ifc.style._Style
63 * @see ifc.style._XStyle
65 public class ScStyleObj
extends TestCase
{
66 private XSpreadsheetDocument xSpreadsheetDoc
= null;
69 * Creates Spreadsheet document.
72 protected void initialize( TestParameters tParam
, PrintWriter log
) throws Exception
{
73 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
74 log
.println( "creating a Spreadsheet document" );
75 xSpreadsheetDoc
= SOF
.createCalcDoc(null);
79 * Disposes Spreadsheet document.
82 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
83 log
.println( " disposing xSheetDoc " );
84 XComponent oComp
= UnoRuntime
.queryInterface(XComponent
.class, xSpreadsheetDoc
);
85 util
.DesktopTools
.closeDoc(oComp
);
89 * Creating a TestEnvironment for the interfaces to be tested.
90 * Retrieves the collection of style families from the document
91 * using the interface <code>XStyleFamiliesSupplier</code>.
92 * Obtains style family with index 0 from the collection and obtains
93 * style with index 0 from this style family. Creates the instance of the
94 * service <code>com.sun.star.style.CellStyle</code>. Inserts the created
95 * style to the obtained style family. Retrieves a collection of spreadsheets
96 * from the document and takes one of them. Gets a cell from the spreadsheet
97 * and sets the value of property <code>'CellStyle'</code> to the created
98 * style. The created style is the instance of the service
99 * <code>com.sun.star.style.Style</code> also.
100 * Object relations created :
102 * <li> <code>'PoolStyle'</code> for
103 * {@link ifc.style._XStyle}(the style with index 0 that was obtained
104 * from the collection)</li>
106 * @see com.sun.star.style.CellStyle
107 * @see com.sun.star.style.Style
108 * @see com.sun.star.style.XStyleFamiliesSupplier
111 protected TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) throws Exception
{
114 // creation of the testobject here
115 // first we write what we are intend to do to log file
117 log
.println("creating a test environment");
119 log
.println("getting style");
120 XStyleFamiliesSupplier oStyleFamiliesSupplier
= UnoRuntime
.queryInterface(
121 XStyleFamiliesSupplier
.class, xSpreadsheetDoc
);
122 XNameAccess oStyleFamilies
= oStyleFamiliesSupplier
.getStyleFamilies();
123 XIndexAccess oStyleFamiliesIndexAccess
= UnoRuntime
.queryInterface(XIndexAccess
.class, oStyleFamilies
);
124 XNameAccess oStyleFamilyNameAccess
= null;
125 XStyle oStyle
= null;
126 oStyleFamilyNameAccess
= (XNameAccess
) AnyConverter
.toObject(
127 new Type(XNameAccess
.class),
128 oStyleFamiliesIndexAccess
.getByIndex(0));
130 XIndexAccess oStyleFamilyIndexAccess
= UnoRuntime
.queryInterface(XIndexAccess
.class,
131 oStyleFamilyNameAccess
);
132 oStyle
= (XStyle
) AnyConverter
.toObject(
133 new Type(XStyle
.class),oStyleFamilyIndexAccess
.getByIndex(0));
135 log
.println("Creating a user-defined style");
136 XMultiServiceFactory oMSF
= UnoRuntime
.queryInterface(
137 XMultiServiceFactory
.class, xSpreadsheetDoc
);
139 XInterface oInt
= (XInterface
)
140 oMSF
.createInstance("com.sun.star.style.CellStyle");
141 XStyle oMyStyle
= UnoRuntime
.queryInterface(XStyle
.class, oInt
);
143 XNameContainer oStyleFamilyNameContainer
= UnoRuntime
.
144 queryInterface(XNameContainer
.class, oStyleFamilyNameAccess
);
146 if (oStyleFamilyNameContainer
.hasByName("My Style")) {
147 oStyleFamilyNameContainer
.removeByName("My Style");
150 oStyleFamilyNameContainer
.insertByName("My Style", oMyStyle
);
154 log
.println("Getting spreadsheet") ;
155 XSpreadsheets oSheets
= xSpreadsheetDoc
.getSheets() ;
156 XIndexAccess oIndexSheets
= UnoRuntime
.queryInterface(XIndexAccess
.class, oSheets
);
159 XSpreadsheet oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
160 new Type(XSpreadsheet
.class),oIndexSheets
.getByIndex(0));
161 log
.println("Getting a cell from sheet") ;
162 aCell
= oSheet
.getCellByPosition(2,3) ;
164 XPropertySet xProp
= UnoRuntime
.queryInterface(XPropertySet
.class, aCell
);
166 xProp
.setPropertyValue("CellStyle", oMyStyle
.getName());
168 log
.println("creating a new environment for object");
169 TestEnvironment tEnv
= new TestEnvironment(oMyStyle
);
171 tEnv
.addObjRelation("PoolStyle", oStyle
);
173 tEnv
.addObjRelation("PropertyNames", getPropertyNames
174 (UnoRuntime
.queryInterface
175 (XPropertySet
.class, oMyStyle
)));
180 public String
[] getPropertyNames(XPropertySet props
) {
181 Property
[] the_props
= props
.getPropertySetInfo().getProperties();
182 ArrayList
<String
> names
= new ArrayList
<String
>() ;
184 for (int i
=0;i
<the_props
.length
;i
++) {
186 ((the_props
[i
].Attributes
& PropertyAttribute
.READONLY
) == 0);
188 names
.add(the_props
[i
].Name
);
191 return names
.toArray(new String
[names
.size()]) ;
193 } // finish class ScStyleObj