Events and Other Notifications

From Apache OpenOffice Wiki
Jump to: navigation, search



The RowSet supports a number of events and notifications. First, there is the com.sun.star.sdb.XRowSetApproveBroadcaster interface of the RowSet that allows the user to add or remove objects derived from the interface com.sun.star.sdb.XRowSetApproveListener. The interface com.sun.star.sdb.XRowSetApproveListener defines the following methods:

Methods of com.sun.star.sdb.XRowSetApproveListener
approveCursorMove() Called before a RowSet's cursor is moved.
approveRowChange() Called before a row is inserted, updated, or deleted.
approveRowSetChange() Called before a RowSet is changed or before a RowSet is re-executed.


All three methods return a boolean value that allows the RowSet to continue when it is true, otherwise the current action is stopped.

Additionally, the RowSet supports com.sun.star.sdbc.XRowSet that allows the user to add objects which are notified when the RowSet has changed. This has to be a com.sun.star.sdbc.XRowSetListener. The methods are:

Methods of com.sun.star.sdbc.XRowSetListener
cursorMoved Called when a RowSet's cursor has been moved.
rowChanged Called when a row has been inserted, updated, or deleted.
rowSetChanged Called when the entire row set has changed, or when the row set has been re-executed.

When an event occurs, the appropriate listener method is called to notify the registered listener(s). If a listener is not interested in a particular kind of event, it implements the method for that event as no-op. All listener methods take a com.sun.star.lang.EventObject struct that contains the RowSet object which is the source of the event.

The following table lists the order of events after a specific method call on the RowSet. First the movements.

Method Call Event Call (before) Event Call (after)
beforeFirst()

first()

next()

previous()

last()

afterLast()

absolute()

relative()

moveToBookmark()

moveRelativeToBookmark()

approveCursorMove() cursorMoved(), only when the movement was successful

modified() event from com.sun.star.beans.XPropertySet of property RowCount, only when changed

modified() event from com.sun.star.beans.XPropertySet of property RowCountFinal, only when changed

updateRow()

deleteRow()

insertRow()

approveRowChange() rowChanged()
execute() approveRowSetChange() rowSetChanged()

Consider a simple class which implements the two listener interfaces described above.

  import com.sun.star.sdb.XRowSetApproveListener;
  import com.sun.star.sdbc.XRowSetListener;
  import com.sun.star.sdb.RowChangeEvent;
  import com.sun.star.lang.EventObject;
 
  public class RowSetEventListener implements XRowSetApproveListener,XRowSetListener {
      // XEventListener
      public void disposing(com.sun.star.lang.EventObject event) {
          System.out.println("RowSet will be destroyed!");
      }
 
      // XRowSetApproveBroadcaster
      public boolean approveCursorMove(EventObject event) {
          System.out.println("Before CursorMove!");
          return true;
      }
      public boolean approveRowChange(RowChangeEvent event) {
          System.out.println("Before row change!");
          return true;
      }
      public boolean approveRowSetChange(EventObject event) {
          System.out.println("Before RowSet change!");
          return true;
      }
 
      // XRowSetListener
      public void cursorMoved(com.sun.star.lang.EventObject event) {
          System.out.println("Cursor moved!");
      }
      public void rowChanged(com.sun.star.lang.EventObject event) {
          System.out.println("Row changed!");
      }
      public void rowSetChanged(com.sun.star.lang.EventObject event) {
          System.out.println("RowSet changed!");
      }
  }

The following method uses the listener implementation above.

  public static void showRowSetEvents(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
      // first we create our RowSet object
      XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
          XRowSet.class, _rMSF.createInstance("com.sun.star.sdb.RowSet"));
 
      System.out.println("RowSet created!");
      // add our Listener
      System.out.println("Append our Listener!");
      RowSetEventListener pRow = new RowSetEventListener();
      XRowSetApproveBroadcaster xApBroad = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
          XRowSetApproveBroadcaster.class, xRowRes);
      xApBroad.addRowSetApproveListener(pRow);
      xRowRes.addRowSetListener(pRow);
 
      // set the properties needed to connect to a database
      XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
      xProp.setPropertyValue("DataSourceName", "Bibliography");
      xProp.setPropertyValue("Command", "biblio");
      xProp.setPropertyValue("CommandType", new Integer(com.sun.star.sdb.CommandType.TABLE));
 
      xRowRes.execute();
      System.out.println("RowSet executed!");
 
      // do some movements to check if we got all notifications
      XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class, xRowRes);
      System.out.println("beforeFirst");
      xRes.beforeFirst();
      // this should lead to no notifications because 
      // we should stand before the first row at the beginning
      System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
 
      System.out.println("next");
      xRes.next();
      System.out.println("next");
      xRes.next();
      System.out.println("last");
      xRes.last();
      System.out.println("next");
      xRes.next();
      System.out.println("We stand after the last row: " + xRes.isAfterLast());
      System.out.println("first");
      xRes.first();
      System.out.println("previous");
      xRes.previous();
      System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
      System.out.println("afterLast");
      xRes.afterLast();
      System.out.println("We stand after the last row: " + xRes.isAfterLast());
 
      // now destroy the RowSet
      XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class, xRowRes);
      xComp.dispose();
      System.out.println("RowSet destroyed!");
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages