1 /*************************************************************************
3 * $RCSfile: StyleDemo.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:25:23 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.lang
.XComponent
;
46 import com
.sun
.star
.awt
.Point
;
47 import com
.sun
.star
.awt
.Size
;
49 import com
.sun
.star
.beans
.PropertyValue
;
50 import com
.sun
.star
.beans
.XPropertySet
;
51 import com
.sun
.star
.beans
.XPropertySetInfo
;
53 import com
.sun
.star
.container
.XNameAccess
;
55 import com
.sun
.star
.drawing
.XShape
;
56 import com
.sun
.star
.drawing
.XShapes
;
57 import com
.sun
.star
.drawing
.XDrawPage
;
58 import com
.sun
.star
.drawing
.XDrawPages
;
59 import com
.sun
.star
.drawing
.XDrawPagesSupplier
;
61 import com
.sun
.star
.frame
.XModel
;
65 // __________ Implementation __________
71 public class StyleDemo
73 public static void main( String args
[] )
75 XComponent xComponent
= null;
78 // get the remote office context of a running office (a new office
79 // instance is started if necessary)
80 com
.sun
.star
.uno
.XComponentContext xOfficeContext
= Helper
.connect();
82 // suppress Presentation Autopilot when opening the document
83 // properties are the same as described for
84 // com.sun.star.document.MediaDescriptor
85 PropertyValue
[] pPropValues
= new PropertyValue
[ 1 ];
86 pPropValues
[ 0 ] = new PropertyValue();
87 pPropValues
[ 0 ].Name
= "Silent";
88 pPropValues
[ 0 ].Value
= new Boolean( true );
90 xComponent
= Helper
.createDocument( xOfficeContext
,
91 "private:factory/simpress", "_blank", 0, pPropValues
);
96 /* The first part of this demo will set each "CharColor" Property
97 that is available within the styles of the document to red. It
98 will also print each family and style name to the standard output */
100 (XModel
)UnoRuntime
.queryInterface(
101 XModel
.class, xComponent
);
102 com
.sun
.star
.style
.XStyleFamiliesSupplier xSFS
=
103 (com
.sun
.star
.style
.XStyleFamiliesSupplier
)
104 UnoRuntime
.queryInterface(
105 com
.sun
.star
.style
.XStyleFamiliesSupplier
.class, xModel
);
107 com
.sun
.star
.container
.XNameAccess xFamilies
= xSFS
.getStyleFamilies();
109 // the element should now contain at least two Styles. The first is
110 // "graphics" and the other one is the name of the Master page
111 String
[] Families
= xFamilies
.getElementNames();
112 for ( int i
= 0; i
< Families
.length
; i
++ )
114 // this is the family
115 System
.out
.println( "\n" + Families
[ i
] );
117 // and now all available styles
118 Object aFamilyObj
= xFamilies
.getByName( Families
[ i
] );
119 com
.sun
.star
.container
.XNameAccess xStyles
=
120 (com
.sun
.star
.container
.XNameAccess
)
121 UnoRuntime
.queryInterface(
122 com
.sun
.star
.container
.XNameAccess
.class, aFamilyObj
);
123 String
[] Styles
= xStyles
.getElementNames();
124 for( int j
= 0; j
< Styles
.length
; j
++ )
126 System
.out
.println( " " + Styles
[ j
] );
127 Object aStyleObj
= xStyles
.getByName( Styles
[ j
] );
128 com
.sun
.star
.style
.XStyle xStyle
= (com
.sun
.star
.style
.XStyle
)
129 UnoRuntime
.queryInterface(
130 com
.sun
.star
.style
.XStyle
.class, aStyleObj
);
131 // now we have the XStyle Interface and the CharColor for
132 // all styles is exemplary be set to red.
133 XPropertySet xStylePropSet
= (XPropertySet
)
134 UnoRuntime
.queryInterface( XPropertySet
.class, xStyle
);
135 XPropertySetInfo xStylePropSetInfo
=
136 xStylePropSet
.getPropertySetInfo();
137 if ( xStylePropSetInfo
.hasPropertyByName( "CharColor" ) )
139 xStylePropSet
.setPropertyValue( "CharColor",
140 new Integer( 0xff0000 ) );
147 /* now create a rectangle and apply the "title1" style of
148 the "graphics" family */
150 Object obj
= xFamilies
.getByName( "graphics" );
151 com
.sun
.star
.container
.XNameAccess xStyles
= (XNameAccess
)
152 UnoRuntime
.queryInterface(com
.sun
.star
.container
.XNameAccess
.class,
154 obj
= xStyles
.getByName( "title1" );
155 com
.sun
.star
.style
.XStyle xTitle1Style
= (com
.sun
.star
.style
.XStyle
)
156 UnoRuntime
.queryInterface( com
.sun
.star
.style
.XStyle
.class, obj
);
158 XDrawPagesSupplier xDrawPagesSupplier
=
159 (XDrawPagesSupplier
)UnoRuntime
.queryInterface(
160 XDrawPagesSupplier
.class, xComponent
);
161 XDrawPages xDrawPages
= xDrawPagesSupplier
.getDrawPages();
162 XDrawPage xDrawPage
= (XDrawPage
)UnoRuntime
.queryInterface(
163 XDrawPage
.class, xDrawPages
.getByIndex( 0 ));
164 XShapes xShapes
= (XShapes
)UnoRuntime
.queryInterface(XShapes
.class,
166 XShape xShape
= ShapeHelper
.createShape( xComponent
, new Point( 0, 0 ),
167 new Size( 5000, 5000 ), "com.sun.star.drawing.RectangleShape" );
168 xShapes
.add( xShape
);
169 XPropertySet xPropSet
= (XPropertySet
)
170 UnoRuntime
.queryInterface( XPropertySet
.class, xShape
);
171 xPropSet
.setPropertyValue( "Style", xTitle1Style
);
174 catch( Exception ex
)
176 System
.out
.println( ex
);