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