xref: /trunk/main/odk/examples/DevelopersGuide/Drawing/GluePointDemo.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
46 import com.sun.star.container.XIndexContainer;
47 import com.sun.star.container.XIdentifierContainer;
48 
49 import com.sun.star.drawing.Alignment;
50 import com.sun.star.drawing.EscapeDirection;
51 import com.sun.star.drawing.GluePoint2;
52 import com.sun.star.drawing.XShape;
53 import com.sun.star.drawing.XShapes;
54 import com.sun.star.drawing.XDrawPage;
55 import com.sun.star.drawing.XGluePointsSupplier;
56 
57 
58 
59 // __________ Implementation __________
60 
61 /** GluePointDemo
62     @author Sven Jacobi
63  */
64 
65 public class GluePointDemo
66 {
67     public static void main( String args[] )
68     {
69         XComponent xDrawDoc = 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             xDrawDoc = Helper.createDocument( xOfficeContext,
85                 "private:factory/sdraw", "_blank", 0, pPropValues );
86 
87 
88             XDrawPage xPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
89             XShapes xShapes = (XShapes)
90                     UnoRuntime.queryInterface( XShapes.class, xPage );
91 
92             // create two rectangles
93             XShape xShape1 = ShapeHelper.createShape( xDrawDoc,
94                 new Point( 15000, 1000 ), new Size( 5000, 5000 ),
95                     "com.sun.star.drawing.RectangleShape" );
96 
97             XShape xShape2 = ShapeHelper.createShape( xDrawDoc,
98                 new Point( 2000, 15000 ), new Size( 5000, 5000 ),
99                     "com.sun.star.drawing.EllipseShape" );
100 
101             // and a connector
102             XShape xConnector = ShapeHelper.createShape( xDrawDoc,
103                 new Point( 0, 0 ),
104                     new Size( 0, 0 ),
105                         "com.sun.star.drawing.ConnectorShape" );
106 
107             xShapes.add( xShape1 );
108             xShapes.add( xShape2 );
109             xShapes.add( xConnector );
110 
111             XPropertySet xConnectorPropSet = (XPropertySet)
112                 UnoRuntime.queryInterface( XPropertySet.class, xConnector );
113 
114 //          Index value of 0 : the shape is connected at the top
115 //          Index value of 1 : the shape is connected at the left
116 //          Index value of 2 : the shape is connected at the bottom
117 //          Index value of 3 : the shape is connected at the right
118 
119             int nStartIndex = 3;
120             int nEndIndex   = 1;
121 
122             // the "StartPosition" or "EndPosition" property needs not to be set
123             // if there is a shape to connect
124             xConnectorPropSet.setPropertyValue( "StartShape", xShape1 );
125             xConnectorPropSet.setPropertyValue( "StartGluePointIndex",
126                                                 new Integer( nStartIndex ) );
127 
128             xConnectorPropSet.setPropertyValue( "EndShape", xShape2 );
129             xConnectorPropSet.setPropertyValue( "EndGluePointIndex",
130                                                 new Integer( nEndIndex ) );
131 
132             XGluePointsSupplier  xGluePointsSupplier;
133             XIndexContainer      xIndexContainer;
134             XIdentifierContainer xIdentifierContainer;
135 
136             GluePoint2 aGluePoint = new GluePoint2();
137             aGluePoint.IsRelative = false;
138             aGluePoint.PositionAlignment = Alignment.CENTER;
139             aGluePoint.Escape = EscapeDirection.SMART;
140             aGluePoint.IsUserDefined = true;
141             aGluePoint.Position.X = 0;
142             aGluePoint.Position.Y = 0;
143 
144             // create and insert a glue point at shape1
145             xGluePointsSupplier = (XGluePointsSupplier)
146                 UnoRuntime.queryInterface( XGluePointsSupplier.class, xShape1 );
147             xIndexContainer = xGluePointsSupplier.getGluePoints();
148             xIdentifierContainer = (XIdentifierContainer)
149                 UnoRuntime.queryInterface( XIdentifierContainer.class,
150                                            xIndexContainer );
151             int nIndexOfGluePoint1 = xIdentifierContainer.insert( aGluePoint );
152 
153             // create and insert a glue point at shape2
154             xGluePointsSupplier = (XGluePointsSupplier)
155                 UnoRuntime.queryInterface( XGluePointsSupplier.class, xShape2 );
156             xIndexContainer = xGluePointsSupplier.getGluePoints();
157             xIdentifierContainer = (XIdentifierContainer)
158                 UnoRuntime.queryInterface( XIdentifierContainer.class,
159                                            xIndexContainer );
160             int nIndexOfGluePoint2 = xIdentifierContainer.insert( aGluePoint );
161 
162             // create and add a connector
163             XShape xConnector2 = ShapeHelper.createShape( xDrawDoc,
164                 new Point( 0, 0 ),
165                     new Size( 0, 0 ),
166                         "com.sun.star.drawing.ConnectorShape" );
167             xShapes.add( xConnector2 );
168 
169             XPropertySet xConnector2PropSet = (XPropertySet)
170                 UnoRuntime.queryInterface( XPropertySet.class, xConnector2 );
171 
172             xConnector2PropSet.setPropertyValue( "StartShape", xShape1 );
173             xConnector2PropSet.setPropertyValue( "StartGluePointIndex",
174                 new Integer( nIndexOfGluePoint1 ) );
175 
176             xConnector2PropSet.setPropertyValue( "EndShape", xShape2 );
177             xConnector2PropSet.setPropertyValue( "EndGluePointIndex",
178                 new Integer( nIndexOfGluePoint2 ) );
179 
180 
181         }
182         catch( Exception ex )
183         {
184             System.out.println( ex );
185         }
186         System.exit( 0 );
187     }
188 }
189