OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Event Handling

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
bcris
General User
General User


Joined: 28 Sep 2011
Posts: 12

PostPosted: Fri Sep 30, 2011 8:33 am    Post subject: Event Handling Reply with quote

Hi,

In an AddIn for Calc I have the following MenuInterceptor class:

Code:

public final class MenuInterceptor implements XDispatchProviderInterceptor {
   private static Logger log = Logger.getLogger(MenuInterceptor.class);
   private XDispatchProvider slaveDispatchProvider;
   private XDispatchProvider masterDispatchProvider;
   private XComponentContext xContext;

   public MenuInterceptor(final XComponentContext xContext) {
      this.xContext = xContext;
   }
   public XDispatchProvider getMasterDispatchProvider() {
      return masterDispatchProvider;
   }
   public XDispatchProvider getSlaveDispatchProvider() {
      return slaveDispatchProvider;
   }
   public void setMasterDispatchProvider(final XDispatchProvider arg0) {
      masterDispatchProvider = arg0;
   }
   public void setSlaveDispatchProvider(final XDispatchProvider arg0) {
      slaveDispatchProvider = arg0;
   }

   public XDispatch queryDispatch(final URL arg0, final String arg1, final int arg2) {
      if (arg0.Complete.equalsIgnoreCase(".uno:SaveAll")) {
         return new SaveAllEventHandler();
      } else if (shouldInterceptSave(xContext)) {
         if (arg0.Complete.equals(".uno:Save")) {
            return new SaveEventHandler(xContext);
         } else if (arg0.Complete.equalsIgnoreCase(".uno:SaveAs")
                  ||
                  arg0.Complete.equalsIgnoreCase(".uno:Signature")) {
            return new SaveAsEventHandler(xContext);
         }            
      }
      
      return getSlaveDispatchProvider().queryDispatch(arg0, arg1, arg2);
   }
   
   public XDispatch[] queryDispatches(final DispatchDescriptor[] arg0) {
      return getSlaveDispatchProvider().queryDispatches(arg0);
   }
}


which is dynamically registered with the following code:

Code:

   MenuInterceptor menuInterceptor = new MenuInterceptor(xContext);
   try {
      Object desktop = xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop",
            xContext);
      XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
      XFrame xFrame = xDesktop.getCurrentFrame();
      XDispatchProviderInterception xDispatchProvInterception = (XDispatchProviderInterception) UnoRuntime
            .queryInterface(XDispatchProviderInterception.class, xFrame);
      xDispatchProvInterception.registerDispatchProviderInterceptor(menuInterceptor);
      log.trace("DocumentEventListener registriert.");
   } catch (Exception e) {
      log.error("Fehler beim Initialisieren des DocumentEventListener.", e);
   }


The MenuInterceptor class returns project specific event handler classes for the events ".uno:Save" and ".uno:SaveAs" but only in case the result of the method shouldInterceptSave() is true.

Such an event handler class looks like this:

Code:

public final class SaveEventHandler implements XDispatch {

   private XComponentContext xContext;
   
   public SaveEventHandler(final XComponentContext xContext) {
      this.xContext = xContext;
   }

   public void addStatusListener(final XStatusListener status, final URL url) {
   }

   public void dispatch(final URL url, final PropertyValue[] values) {
      // do something
   }

   public void removeStatusListener(final XStatusListener status, final URL url) {
   }

}


My problem is that after the first saving, when I make some additional changes and try to save again the following happens in this order:

1. the "dispatch" method of the SaveEventHandler class is called directly (without going through the "queryDispatch" method of the MenuInterceptor class)
2. afterwards, immediately when I start typing in a cell of the current document (immediately after I typed the first letter or number) the "queryDispatch" method of the MenuInterceptor class is called

The problem is that in the queryDispatch method of the MenuInterceptor class I have to decide dynamically if the events should be intercepted or the standard save mechanism should be processed (this is decided based on the return value of the shouldInterceptSave method) but if the dispatch method of the SaveEventHandler is called directly (without going through MenuInterceptor.queryDispatch) than the decision is not made and my own save mechanism is processed every time.

Does anyone have an ideea why this is happening and how could I solve my problem (dynamically deciding if the standard or my own save mechanism should be processed) ?

Thanks in advance !
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Fri Sep 30, 2011 7:37 pm    Post subject: Reply with quote

Hi,

AFAIK there is no chance to predict when queryDispatch is called. There, a shouldInterceptSave(xContext) will not work.

In queryDispatch in the SaveAs case, you may also pass the getSlaveDispatchProvider().queryDispatch(arg0, arg1, arg2) as an argument to the constructor SaveEventHandler. Then, within SaveEventHandler.dispatch, you can decide to do your own things or to call the original dispatch used in the constructor.

Good luck,

ms777

P.S.: I just posted an interceptor, which follows this concept http://www.oooforum.org/forum/viewtopic.phtml?t=131536
Back to top
View user's profile Send private message
bcris
General User
General User


Joined: 28 Sep 2011
Posts: 12

PostPosted: Sat Oct 01, 2011 10:56 pm    Post subject: Reply with quote

Hi,

I implemented it as you suggested, and it works fine, thanks a lot for your help !

Best regards
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group