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 import com.sun.star.beans.PropertyValue;
25 import com.sun.star.beans.XPropertySet;
26 
27 import com.sun.star.bridge.XUnoUrlResolver;
28 
29 import com.sun.star.frame.XComponentLoader;
30 
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.lang.XMultiComponentFactory;
33 
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.XComponentContext;
37 
38 
39 /*
40  * OpenQuery.java
41  *
42  * Created on 6. Juli 2002, 10:25
43  */
44 
45 /**
46  *
47  * @author  dschulten
48  */
49 public class Organigram {
50 
51     private XComponentContext xRemoteContext = null;
52     private XMultiComponentFactory xRemoteServiceManager = null;
53 
54     /** Creates a new instance of OpenQuery */
Organigram()55     public Organigram() {
56     }
57 
58     /**
59      * @param args the command line arguments
60      */
main(String[] args)61     public static void main(String[] args) {
62         Organigram organigram1 = new Organigram();
63         try {
64             organigram1.drawOrganigram();
65         }
66         catch (java.lang.Exception e){
67             e.printStackTrace();
68         }
69         finally {
70             System.exit(0);
71         }
72     }
drawOrganigram()73     public void drawOrganigram() throws java.lang.Exception {
74         // get the remote office component context
75         xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
76         System.out.println("Connected to a running office ...");
77         // get the remote service manager
78         xRemoteServiceManager = xRemoteContext.getServiceManager();
79 
80         Object desktop = xRemoteServiceManager.createInstanceWithContext(
81             "com.sun.star.frame.Desktop", xRemoteContext);
82         XComponentLoader xComponentLoader = (XComponentLoader)
83             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
84 
85         PropertyValue[] loadProps = new PropertyValue[0];
86         XComponent xDrawComponent = xComponentLoader.loadComponentFromURL(
87             "private:factory/sdraw", "_blank", 0, loadProps);
88 
89         // get draw page by index
90         com.sun.star.drawing.XDrawPagesSupplier xDrawPagesSupplier =
91             (com.sun.star.drawing.XDrawPagesSupplier)UnoRuntime.queryInterface(
92                 com.sun.star.drawing.XDrawPagesSupplier.class, xDrawComponent );
93         com.sun.star.drawing.XDrawPages xDrawPages =
94             xDrawPagesSupplier.getDrawPages();
95         Object drawPage = xDrawPages.getByIndex(0);
96         com.sun.star.drawing.XDrawPage xDrawPage = (com.sun.star.drawing.XDrawPage)
97             UnoRuntime.queryInterface(com.sun.star.drawing.XDrawPage.class,
98                                       drawPage);
99 
100         com.sun.star.lang.XMultiServiceFactory xDocumentFactory =
101             (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
102                 com.sun.star.lang.XMultiServiceFactory.class, xDrawComponent);
103 
104         com.sun.star.beans.XPropertySet xPageProps =
105             (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
106                 com.sun.star.beans.XPropertySet.class, xDrawPage);
107 
108         int pageWidth = AnyConverter.toInt(xPageProps.getPropertyValue("Width"));
109         int pageHeight = AnyConverter.toInt(xPageProps.getPropertyValue("Height"));
110         int pageBorderTop = AnyConverter.toInt(xPageProps.getPropertyValue("BorderTop"));
111         int pageBorderLeft = AnyConverter.toInt(xPageProps.getPropertyValue("BorderLeft"));
112         int pageBorderRight = AnyConverter.toInt(xPageProps.getPropertyValue("BorderRight"));
113         int drawWidth = pageWidth - pageBorderLeft - pageBorderRight;
114         int horCenter = pageBorderLeft + drawWidth / 2;
115 
116         String[][] orgUnits = new String[2][4];
117         orgUnits[0][0] = "Management";
118         orgUnits[1][0] = "Production";
119         orgUnits[1][1] = "Purchasing";
120         orgUnits[1][2] = "IT Services";
121         orgUnits[1][3] = "Sales";
122         int[] levelCount = {1, 4};
123 
124         int horSpace = 300;
125         int verSpace = 3000;
126 
127         int shapeWidth = (drawWidth - (levelCount[1] - 1) * horSpace) / levelCount[1];
128         int shapeHeight = pageHeight / 20;
129         int shapeX = pageWidth / 2 - shapeWidth / 2;
130         int shapeY = pageBorderTop;
131 
132         int levelY;
133         int levelX;
134 
135         com.sun.star.drawing.XShape xStartShape = null;
136 
137         for (int level = 0; level <= 1; level++) {
138             levelY = pageBorderTop + 2000 + level * (shapeHeight + verSpace);
139             for (int i = levelCount[level] - 1; i > -1; i--) {
140                 shapeX = horCenter - (levelCount[level] * shapeWidth +
141                          (levelCount[level] - 1) * horSpace) / 2
142                          + i * shapeWidth + i * horSpace;
143                 Object shape = xDocumentFactory.createInstance("com.sun.star.drawing.RectangleShape");
144                 com.sun.star.drawing.XShape xShape = (com.sun.star.drawing.XShape)
145                     UnoRuntime.queryInterface(
146                         com.sun.star.drawing.XShape.class, shape);
147                 xShape.setPosition(new com.sun.star.awt.Point(shapeX, levelY));
148                 xShape.setSize(new com.sun.star.awt.Size(shapeWidth, shapeHeight));
149                 xDrawPage.add(xShape);
150 
151                 com.sun.star.text.XText xText = (com.sun.star.text.XText)
152                     UnoRuntime.queryInterface(
153                         com.sun.star.text.XText.class, xShape);
154 
155                 xText.setString(orgUnits[level][i]);
156 
157                 // memorize the root shape
158                 if (level == 0 && i == 0)
159                     xStartShape = xShape;
160 
161                 if (level == 1) {
162                     Object connector = xDocumentFactory.createInstance("com.sun.star.drawing.ConnectorShape");
163                     com.sun.star.beans.XPropertySet xConnectorProps =
164                         (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
165                             com.sun.star.beans.XPropertySet.class, connector);
166                 com.sun.star.drawing.XShape xConnector =
167                     (com.sun.star.drawing.XShape)UnoRuntime.queryInterface(
168                         com.sun.star.drawing.XShape.class, connector);
169                     xDrawPage.add(xConnector);
170                     xConnectorProps.setPropertyValue("StartShape", xStartShape);
171                     xConnectorProps.setPropertyValue("EndShape", xShape);
172                     xConnectorProps.setPropertyValue("StartGluePointIndex",
173                                       new Integer(2)); // 2 = bottom glue point
174                     xConnectorProps.setPropertyValue("EndGluePointIndex",
175                                       new Integer(0));   // 0 = top glue point
176                 }
177             }
178         }
179     }
180 }
181