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