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