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

Calling dispatcher synchronously in Java

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
anestikas
General User
General User


Joined: 10 May 2008
Posts: 43

PostPosted: Mon May 26, 2008 3:55 am    Post subject: Calling dispatcher synchronously in Java Reply with quote

I think this code might help a couple of you...

Code:
   private Semaphore lock = new Semaphore(1, true);
   
   private void dispatch(String command, XTextDocument doc) {
      try {
         lock.acquire();
         XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
               XMultiServiceFactory.class, xMCF);

         XDispatchProvider xDispatchProvider = (XDispatchProvider)UnoRuntime.queryInterface(XDispatchProvider.class, doc.getCurrentController().getFrame());
         
         Object object = xFactory.createInstance("com.sun.star.util.URLTransformer");
          
         XURLTransformer xURLTransformer = (XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, object);

         URL[] url = new URL[1];
         url[0] = new URL();
         url[0].Complete = ".uno:" + command;
         
         xURLTransformer.parseStrict(url);

         XDispatch xDispatcher = xDispatchProvider.queryDispatch(url[0], "", 0);

         XNotifyingDispatch xNotifyingDispatcher = (XNotifyingDispatch)UnoRuntime.queryInterface(XNotifyingDispatch.class, xDispatcher);
         if(xNotifyingDispatcher != null) {
            xNotifyingDispatcher.dispatchWithNotification(url[0], new PropertyValue[0], this);
            lock.acquire();
         }
         
         lock.release();
      } catch (java.lang.Exception e) {
         e.printStackTrace();
      }
   }

   public void dispatchFinished(DispatchResultEvent e) {
      lock.release();
   }

   public void disposing(EventObject e) {

   }


Note that this implements XDispatchResultListener

This solution does not work for every call eg .uno:ApplyStyleDefault and .uno:SetDefault since the XNotifyingDispatch is not available

If you can improve this code, please do...
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Wed May 28, 2008 12:59 pm    Post subject: Reply with quote

very interesting ... do you have a list or some examples of dispatches, which support XNotifyingDispatch ?
Back to top
View user's profile Send private message
anestikas
General User
General User


Joined: 10 May 2008
Posts: 43

PostPosted: Wed May 28, 2008 2:37 pm    Post subject: Reply with quote

I have only used it successfully with:

.uno:Copy
.uno:Paste
.uno:ResetAttributes

I don't know which other commands support XNotifyingDispatch. If anyone has such a list, please post it here.
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 Code Snippets 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