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