Example: Creating a Simple Organizational Chart

From Apache OpenOffice Wiki
Jump to: navigation, search



The following example creates a simple organizational chart with two levels. It consists of five rectangle shapes and four connectors that connect the boxes on the second level with the root box on the first level.

Sample Organigram

The method getRemoteServiceManager() that is used in the example connects to the office. The First Steps discussed this method. First an empty drawing document is loaded and retrieves the draw page object of slide number 1 to find the page dimensions. Then the organigram data is prepared and the shape sizes are calculated. The shapes are added in a for loop that iterates over the organigram data, and connectors are added for all shapes on the second level of the organigram.

  public void drawOrganigram() throws java.lang.Exception {
      xRemoteServiceManager = this.getRemoteServiceManager(
          "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"); 
      Object desktop = xRemoteServiceManager.createInstanceWithContext(
          "com.sun.star.frame.Desktop", xRemoteContext);
      XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
          XComponentLoader.class, desktop);
      PropertyValue[] loadProps = new PropertyValue[0];
      XComponent xDrawComponent = xComponentLoader.loadComponentFromURL(
          "private:factory/sdraw", "_blank", 0, loadProps);
 
      // get draw page by index
      com.sun.star.drawing.XDrawPagesSupplier xDrawPagesSupplier =
          (com.sun.star.drawing.XDrawPagesSupplier)
              UnoRuntime.queryInterface(
      com.sun.star.drawing.XDrawPagesSupplier.class, xDrawComponent );
      com.sun.star.drawing.XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
      Object drawPage = xDrawPages.getByIndex(0);
      com.sun.star.drawing.XDrawPage xDrawPage = (com.sun.star.drawing.XDrawPage)
          UnoRuntime.queryInterface(
              com.sun.star.drawing.XDrawPage.class, drawPage);
 
      // find out page dimensions
      com.sun.star.beans.XPropertySet xPageProps = (com.sun.star.beans.XPropertySet)
          UnoRuntime.queryInterface(
              com.sun.star.beans.XPropertySet.class, xDrawPage);
      int pageWidth = AnyConverter.toInt(xPageProps.getPropertyValue("Width"));
      int pageHeight = AnyConverter.toInt(xPageProps.getPropertyValue("Height"));
      int pageBorderTop = AnyConverter.toInt(xPageProps.getPropertyValue("BorderTop"));
      int pageBorderLeft = AnyConverter.toInt(xPageProps.getPropertyValue("BorderLeft"));
      int pageBorderRight = AnyConverter.toInt(xPageProps.getPropertyValue("BorderRight"));
      int drawWidth = pageWidth - pageBorderLeft - pageBorderRight;
      int horCenter = pageBorderLeft + drawWidth / 2;
 
      // data for organigram
      String[][] orgUnits = new String[2][4];
      orgUnits[0][0] = "Management"; // level 0
      orgUnits[1][0] = "Production"; // level 1
      orgUnits[1][1] = "Purchasing"; // level 1
      orgUnits[1][2] = "IT Services"; // level 1
      orgUnits[1][3] = "Sales"; // level 1
      int[] levelCount = {1, 4};
 
      // calculate shape sizes and positions
      int horSpace = 300;
      int verSpace = 3000;
      int shapeWidth = (drawWidth - (levelCount[1] - 1) * horSpace) / levelCount[1];
      int shapeHeight = pageHeight / 20;
      int shapeX = pageWidth / 2 - shapeWidth / 2;
      int levelY = 0;
      com.sun.star.drawing.XShape xStartShape = null;
      // get document factory
      com.sun.star.lang.XMultiServiceFactory xDocumentFactory = (com.sun.star.lang.XMultiServiceFactory)
          UnoRuntime.queryInterface(
              com.sun.star.lang.XMultiServiceFactory.class, xDrawComponent);
      // creating and adding RectangleShapes and Connectors 
      for (int level = 0; level <= 1; level++) {
          levelY = pageBorderTop + 2000 + level * (shapeHeight + verSpace);
          for (int i = levelCount[level] - 1; i > -1; i--) {
              shapeX = horCenter - 
                       (levelCount[level] * shapeWidth + (levelCount[level] - 1) * horSpace) / 2 + 
                       i * shapeWidth + i * horSpace;
              Object shape = xDocumentFactory.createInstance("com.sun.star.drawing.RectangleShape");
              com.sun.star.drawing.XShape xShape = (com.sun.star.drawing.XShape)
                  UnoRuntime.queryInterface(
                      com.sun.star.drawing.XShape.class, shape);
              xShape.setPosition(new com.sun.star.awt.Point(shapeX, levelY));
              xShape.setSize(new com.sun.star.awt.Size(shapeWidth, shapeHeight)); 
              xDrawPage.add(xShape);
 
              // set the text
              com.sun.star.text.XText xText = (com.sun.star.text.XText)
                  UnoRuntime.queryInterface(
                      com.sun.star.text.XText.class, xShape);
              xText.setString(orgUnits[level][i]); 
              // memorize the root shape, for connectors
              if (level == 0 && i == 0)
                  xStartShape = xShape; 
 
              // add connectors for level 1
              if (level == 1) {
                  Object connector = xDocumentFactory.createInstance(
                      "com.sun.star.drawing.ConnectorShape");
                  com.sun.star.drawing.XShape xConnector = (com.sun.star.drawing.XShape)
                      UnoRuntime.queryInterface(
                  com.sun.star.drawing.XShape.class, connector); 
                  xDrawPage.add(xConnector);
                  com.sun.star.beans.XPropertySet xConnectorProps = (com.sun.star.beans.XPropertySet)
                      UnoRuntime.queryInterface(
                          com.sun.star.beans.XPropertySet.class, connector);
                  xConnectorProps.setPropertyValue("StartShape", xStartShape);
                  xConnectorProps.setPropertyValue("EndShape", xShape);
                  // glue point positions: 0=top 1=left 2=bottom 3=right
                  xConnectorProps.setPropertyValue("StartGluePointIndex", new Integer(2)); 
                  xConnectorProps.setPropertyValue("EndGluePointIndex", new Integer(0)); 
              }
 
          }
      }
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages