2013-01-21 120 views
1

我想用Java跟蹤用戶輸入到LibreOffice/OpenOffice電子表格。目前,我開始the first LibreOffice examples並希望類似的東西附加到OpenOffice電子表格監聽器

  • com.sun.star.chart.XChartDataChangeEventListener(如圖here),也許是
  • com.sun.star.container.XContainerListener到電子表格。

當試圖將我的XSpreadsheetDocument轉換爲XContainer時,我收到了NullPointerException。 我找到API文檔非常難以導航和會感謝提示如何:

  • 搞清楚XSpreadsheetDocument的類型層次,
  • 附着其監聽到的接口,並
  • 工廠鏈到該接口

回答

1

通過猜測,我能發現,從private:factory/scalc加載XComponent屬於可以鑄造一個服務接口com.sun.star.util.XModifyBroadcaster,W這反過來又允許安裝一個com.sun.star.util.XModifyListener

這裏有一個工作示例:

import com.sun.star.beans.PropertyValue; 
import com.sun.star.comp.helper.Bootstrap; 
import com.sun.star.comp.helper.BootstrapException; 
import com.sun.star.frame.XComponentLoader; 
import com.sun.star.lang.EventObject; 
import com.sun.star.lang.XComponent; 
import com.sun.star.lang.XMultiComponentFactory; 
import com.sun.star.sheet.XSpreadsheetDocument; 
import com.sun.star.uno.Exception; 
import com.sun.star.uno.UnoRuntime; 
import com.sun.star.uno.XComponentContext; 
import com.sun.star.util.XModifyBroadcaster; 

public class CalcListener { 
    public static void main(String[] args) { 
     try { 
      // Lots of verbose code to get a Document instance 
      final XComponentContext xRemoteContext = Bootstrap.bootstrap(); 
      if(xRemoteContext == null) { 
       System.err.println("ERROR: Could not bootstrap default Office."); 
       return; 
      } 
      final XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager(); 
      final Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext); 
      final XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop); 

      final PropertyValue[] loadProps = new PropertyValue[0]; 
      final XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps); 

      // Cast to interface Document, to do anything useful later 
      final XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSpreadsheetComponent); 

      // Cast to interface XModifyBroadcaster to attach listener 
      final XModifyBroadcaster messageHost = (XModifyBroadcaster) UnoRuntime.queryInterface(XModifyBroadcaster.class, xSpreadsheetDocument); 
      messageHost.addModifyListener(new XModifyListener() 
      { 
       public void modified(EventObject eo) { 
        System.out.println("modified:"+eo.Source); 
       } 

       public void disposing(EventObject eo) { 
        System.out.println("disposing:"+eo); 
       } 
      }); 

      // Do possibly useful stuff 
      // ... 

     } catch (BootstrapException | Exception ex) { 
      System.err.println("bad things happened:"+ex); 
     } 
    } 
} 
+0

我不得不添加的路徑libjpipe.so(/ usr/lib中/ URE/lib目錄)手動類路徑這段時間。 –

+0

爲什麼LibreOffice沒有提供如此清晰的內容?幹得好,謝謝你。 – user1501247