Variable Results

From Apache OpenOffice Wiki
Jump to: navigation, search



It is also possible to implement functions with results that change over time. Whenever such a result changes, the formulas that use the result are recalculated and the new values are shown in the spreadsheet. This can be used to display data from a real-time data feed in a spreadsheet.

In its interface, a function with a variable result must be defined with a return type of com.sun.star.sheet.XVolatileResult, such as the getCounter() function from the example interface above. The function's implementation must return an object that implements the com.sun.star.sheet.VolatileResult service. Subsequent calls to the same function with the same arguments return the same object. An implementation that returns a different result object for every name looks like this:

  private java.util.Hashtable aResults = new java.util.Hashtable();
 
  public com.sun.star.sheet.XVolatileResult getCounter(String aName) {
      ExampleAddInResult aResult = (ExampleAddInResult) aResults.get(aName);
      if (aResult == null) {
          aResult = new ExampleAddInResult(aName);
          aResults.put(aName, aResult);
      }
      return aResult;
  }

The result object has to implement the addResultListener() and removeResultListener() methods from the com.sun.star.sheet.XVolatileResult interface to maintain a list of listeners, and notify each of these listeners by calling the com.sun.star.sheet.XResultListener interface's modified() method whenever a new result is available. The com.sun.star.sheet.ResultEvent object that is passed to the modified() call must contain the new result in the Value member. The possible types for the result are the same as for a function's return value if no volatile results are involved.

If a result is already available when addResultListener() is called, it can be publicized by immediately calling modified() for the new listener. Otherwise, the spreadsheet application displays a "#N/A" error value until a result is available.

The following example shows a simple implementation of a result object. Every time the incrementValue method is called, for example, from a background thread, the result value is incremented and the listeners are notified.

 class ExampleAddInResult implements com.sun.star.sheet.XVolatileResult {
     private String aName;
     private int nValue;
     private java.util.Vector aListeners = new java.util.Vector();
     
     public ExampleAddInResult(String aNewName) {
         aName = aNewName;
     }
     
     private com.sun.star.sheet.ResultEvent getResult() {
         com.sun.star.sheet.ResultEvent aEvent = new com.sun.star.sheet.ResultEvent();
         aEvent.Value = aName + " " + String.valueOf(nValue);
         aEvent.Source = this;
         return aEvent;
     }
     
     public void addResultListener(com.sun.star.sheet.XResultListener aListener) {
         aListeners.addElement(aListener);
     
         // immediately notify of initial value
         aListener.modified(getResult());
     }
     
     public void removeResultListener(com.sun.star.sheet.XResultListener aListener) {
         aListeners.removeElement(aListener);
     }
     
     public void incrementValue() {
         ++nValue;
         com.sun.star.sheet.ResultEvent aEvent = getResult();
     
         java.util.Enumeration aEnum = aListeners.elements();
         while (aEnum.hasMoreElements())
         ((com.sun.star.sheet.XResultListener)aEnum.nextElement()).modified(aEvent);
     }
 }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages