Example: 示例:连接识别客户机

From Apache OpenOffice Wiki
Jump to: navigation, search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.



以下示例说明了一个高级客户机,它可以获知远程桥的状态。准备工作 一章中给出了一个简单客户机的完整示例。


下面的 Java 示例打开一个 awt 小窗口,该窗口包含按钮新建 Writer 和新建 Calc ,用于打开一个新文档和一个状态标签。首次单击一个按钮时,该视窗链接到一个正在执行的办公软件。因此,它使用连接器/桥工厂组合,并将自己作为一个事件侦听器注册到进程间桥。


办公软件终止时,会终止处理事件,而且 Java 程序将状态标签中的文本设置为“已断开连接”并清除办公软件的桌面引用。下一次单击按钮时,程序就知道必须重新建立连接。


方法 getComponentLoader() 根据需要获取 XComponentLoader 引用:

  XComponentLoader _officeComponentLoader = null;
  
  // local component context
  XComponentContext _ctx; 
  
  protected com.sun.star.frame.XComponentLoader getComponentLoader()
          throws com.sun.star.uno.Exception {
      XComponentLoader officeComponentLoader = _officeComponentLoader;
  
      if (officeComponentLoader == null) {
          // instantiate connector service
          Object x = _ctx.getServiceManager().createInstanceWithContext(
              "com.sun.star.connection.Connector", _ctx);
  
          XConnector xConnector = (XConnector) UnoRuntime.queryInterface(XConnector.class, x);
  
          // helper function to parse the UNO URL into a string array
          String a[] = parseUnoUrl(_url);
          if (null == a) {
              throw new com.sun.star.uno.Exception("Couldn't parse UNO URL "+ _url);
          }
  
          // connect using the connection string part of the UNO URL only.
          XConnection connection = xConnector.connect(a[0]);
  
          x = _ctx.getServiceManager().createInstanceWithContext(
          "com.sun.star.bridge.BridgeFactory", _ctx);
  
          XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(
              XBridgeFactory.class , x);
  
          // create a nameless bridge with no instance provider
          // using the middle part of the UNO URL
          XBridge bridge = xBridgeFactory.createBridge("" , a[1] , connection , null);
  
          // query for the XComponent interface and add this as event listener
          XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
              XComponent.class, bridge);
          xComponent.addEventListener(this);
  
          // get the remote instance 
          x = bridge.getInstance(a[2]);
  
          // Did the remote server export this object ?
          if (null == x) {
              throw new com.sun.star.uno.Exception(
                  "Server didn't provide an instance for" + a[2], null);
          }
  
          // Query the initial object for its main factory interface
          XMultiComponentFactory xOfficeMultiComponentFactory = (XMultiComponentFactory)
              UnoRuntime.queryInterface(XMultiComponentFactory.class, x);
  
          // retrieve the component context (it's not yet exported from the office)
          // Query for the XPropertySet interface.
          XPropertySet xProperySet = (XPropertySet)
              UnoRuntime.queryInterface(XPropertySet.class, xOfficeMultiComponentFactory);
  
          // Get the default context from the office server.
          Object oDefaultContext =
              xProperySet.getPropertyValue("DefaultContext");
  
          // Query for the interface XComponentContext.
          XComponentContext xOfficeComponentContext =
              (XComponentContext) UnoRuntime.queryInterface(
                  XComponentContext.class, oDefaultContext);
  
  
          // now create the desktop service
          // NOTE: use the office component context here !
          Object oDesktop = xOfficeMultiComponentFactory.createInstanceWithContext(
              "com.sun.star.frame.Desktop", xOfficeComponentContext);
  
          officeComponentLoader = (XComponentLoader)
              UnoRuntime.queryInterface( XComponentLoader.class, oDesktop);
  
          if (officeComponentLoader == null) {
              throw new com.sun.star.uno.Exception(
                  "Couldn't instantiate com.sun.star.frame.Desktop" , null);
          }
          _officeComponentLoader = officeComponentLoader;
      }
      return officeComponentLoader;
  }

这是一个按钮事件处理程序:

  public void actionPerformed(ActionEvent event) {
      try {
          String sUrl;
          if (event.getSource() == _btnWriter) {
              sUrl = "private:factory/swriter";
          } else {
              sUrl = "private:factory/scalc";
          }
          getComponentLoader().loadComponentFromURL(
              sUrl, "_blank", 0,new com.sun.star.beans.PropertyValue[0]);
          _txtLabel.setText("connected");
      } catch (com.sun.star.connection.NoConnectException exc) {
          _txtLabel.setText(exc.getMessage());
      } catch (com.sun.star.uno.Exception exc) {
          _txtLabel.setText(exc.getMessage());
          exc.printStackTrace();
          throw new java.lang.RuntimeException(exc.getMessage());
      }
  }


而且,disposing 处理程序清除 _officeComponentLoader 引用:

  public void disposing(com.sun.star.lang.EventObject event) {
      // remote bridge has gone down, because the office crashed or was terminated.
      _officeComponentLoader = null;
      _txtLabel.setText("disconnected");
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages