xref: /trunk/main/odk/examples/DevelopersGuide/Drawing/StyleDemo.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.awt.Point;
30cdf0e10cSrcweir import com.sun.star.awt.Size;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.drawing.XShape;
39cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
40cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
41cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
42cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import com.sun.star.frame.XModel;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // __________ Implementation __________
49cdf0e10cSrcweir 
50cdf0e10cSrcweir /** StyleDemo
51cdf0e10cSrcweir     @author Sven Jacobi
52cdf0e10cSrcweir  */
53cdf0e10cSrcweir 
54cdf0e10cSrcweir public class StyleDemo
55cdf0e10cSrcweir {
main( String args[] )56cdf0e10cSrcweir     public static void main( String args[] )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         XComponent xComponent = null;
59cdf0e10cSrcweir         try
60cdf0e10cSrcweir         {
61cdf0e10cSrcweir             // get the remote office context of a running office (a new office
62cdf0e10cSrcweir             // instance is started if necessary)
63cdf0e10cSrcweir             com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             // suppress Presentation Autopilot when opening the document
66cdf0e10cSrcweir             // properties are the same as described for
67cdf0e10cSrcweir             // com.sun.star.document.MediaDescriptor
68cdf0e10cSrcweir             PropertyValue[] pPropValues = new PropertyValue[ 1 ];
69cdf0e10cSrcweir             pPropValues[ 0 ] = new PropertyValue();
70cdf0e10cSrcweir             pPropValues[ 0 ].Name = "Silent";
71cdf0e10cSrcweir             pPropValues[ 0 ].Value = new Boolean( true );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             xComponent = Helper.createDocument( xOfficeContext,
74cdf0e10cSrcweir                 "private:factory/simpress", "_blank", 0, pPropValues );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             /* The first part of this demo will set each "CharColor" Property
80cdf0e10cSrcweir                that is available within the styles of the document to red. It
81cdf0e10cSrcweir                will also print each family and style name to the standard output */
82cdf0e10cSrcweir             XModel xModel =
83cdf0e10cSrcweir                 (XModel)UnoRuntime.queryInterface(
84cdf0e10cSrcweir                     XModel.class, xComponent );
85cdf0e10cSrcweir             com.sun.star.style.XStyleFamiliesSupplier xSFS =
86cdf0e10cSrcweir                 (com.sun.star.style.XStyleFamiliesSupplier)
87cdf0e10cSrcweir                 UnoRuntime.queryInterface(
88cdf0e10cSrcweir                     com.sun.star.style.XStyleFamiliesSupplier.class, xModel );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             com.sun.star.container.XNameAccess xFamilies = xSFS.getStyleFamilies();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             // the element should now contain at least two Styles. The first is
93cdf0e10cSrcweir             // "graphics" and the other one is the name of the Master page
94cdf0e10cSrcweir             String[] Families = xFamilies.getElementNames();
95cdf0e10cSrcweir             for ( int i = 0; i < Families.length; i++ )
96cdf0e10cSrcweir             {
97cdf0e10cSrcweir                 // this is the family
98cdf0e10cSrcweir                 System.out.println( "\n" + Families[ i ] );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir                 // and now all available styles
101cdf0e10cSrcweir                 Object aFamilyObj = xFamilies.getByName( Families[ i ] );
102cdf0e10cSrcweir                 com.sun.star.container.XNameAccess xStyles =
103cdf0e10cSrcweir                     (com.sun.star.container.XNameAccess)
104cdf0e10cSrcweir                     UnoRuntime.queryInterface(
105cdf0e10cSrcweir                         com.sun.star.container.XNameAccess.class, aFamilyObj );
106cdf0e10cSrcweir                 String[] Styles = xStyles.getElementNames();
107cdf0e10cSrcweir                 for( int j = 0; j < Styles.length; j++ )
108cdf0e10cSrcweir                 {
109cdf0e10cSrcweir                     System.out.println( "   " + Styles[ j ] );
110cdf0e10cSrcweir                     Object aStyleObj = xStyles.getByName( Styles[ j ] );
111cdf0e10cSrcweir                     com.sun.star.style.XStyle xStyle = (com.sun.star.style.XStyle)
112cdf0e10cSrcweir                         UnoRuntime.queryInterface(
113cdf0e10cSrcweir                             com.sun.star.style.XStyle.class, aStyleObj );
114cdf0e10cSrcweir                     // now we have the XStyle Interface and the CharColor for
115cdf0e10cSrcweir                     // all styles is exemplary be set to red.
116cdf0e10cSrcweir                     XPropertySet xStylePropSet = (XPropertySet)
117cdf0e10cSrcweir                         UnoRuntime.queryInterface( XPropertySet.class, xStyle );
118cdf0e10cSrcweir                     XPropertySetInfo xStylePropSetInfo =
119cdf0e10cSrcweir                         xStylePropSet.getPropertySetInfo();
120cdf0e10cSrcweir                     if ( xStylePropSetInfo.hasPropertyByName( "CharColor" ) )
121cdf0e10cSrcweir                     {
122cdf0e10cSrcweir                         xStylePropSet.setPropertyValue( "CharColor",
123cdf0e10cSrcweir                                                         new Integer( 0xff0000 ) );
124cdf0e10cSrcweir                     }
125cdf0e10cSrcweir                 }
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             /* now create a rectangle and apply the "title1" style of
131cdf0e10cSrcweir                the "graphics" family */
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             Object obj = xFamilies.getByName( "graphics" );
134cdf0e10cSrcweir             com.sun.star.container.XNameAccess xStyles = (XNameAccess)
135cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.container.XNameAccess.class,
136cdf0e10cSrcweir                                           obj );
137cdf0e10cSrcweir             obj = xStyles.getByName( "title1" );
138cdf0e10cSrcweir             com.sun.star.style.XStyle xTitle1Style = (com.sun.star.style.XStyle)
139cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.style.XStyle.class, obj );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             XDrawPagesSupplier xDrawPagesSupplier =
142cdf0e10cSrcweir                 (XDrawPagesSupplier)UnoRuntime.queryInterface(
143cdf0e10cSrcweir                     XDrawPagesSupplier.class, xComponent );
144cdf0e10cSrcweir             XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
145cdf0e10cSrcweir             XDrawPage xDrawPage = (XDrawPage)UnoRuntime.queryInterface(
146cdf0e10cSrcweir                 XDrawPage.class, xDrawPages.getByIndex( 0 ));
147cdf0e10cSrcweir             XShapes xShapes = (XShapes)UnoRuntime.queryInterface(XShapes.class,
148cdf0e10cSrcweir                                                                  xDrawPage );
149cdf0e10cSrcweir             XShape xShape = ShapeHelper.createShape( xComponent, new Point( 0, 0 ),
150cdf0e10cSrcweir                 new Size( 5000, 5000 ), "com.sun.star.drawing.RectangleShape" );
151cdf0e10cSrcweir             xShapes.add( xShape );
152cdf0e10cSrcweir             XPropertySet xPropSet = (XPropertySet)
153cdf0e10cSrcweir                 UnoRuntime.queryInterface( XPropertySet.class, xShape );
154cdf0e10cSrcweir             xPropSet.setPropertyValue( "Style", xTitle1Style );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir         catch( Exception ex )
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             System.out.println( ex );
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir         System.exit( 0 );
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir }
164