openoffice-api mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ariel Constenla-Haile <arie...@apache.org>
Subject Re: [EXT]How to declare a unique-instance and all-contexts component
Date Thu, 10 Jul 2014 13:18:51 GMT
Hello Amenel,

On Thu, Jul 10, 2014 at 01:40:21PM +0100, Amenel VOGLOZIN wrote:
> Hi all,
> 
> For a few weeks, I have been writing an extension that can be seen as
> a "Recent files manager". I'm using Java and it's going great so far.
> 
> 
> The problem that I'm encountering is that (I have confirmed this fact
> several times) the extension is being instantiated once for each
> document that I open. It is also instantiated the first time that
> I click on its menu in the menu bar.
> 
> 
> There's probably something that I didn't do well. How can I make the
> extension's component a singleton? Should I add an entry in the
> service names in addition to "com.sun.star.frame.ProtocolHandler"? If
> yes, what value should I provide? Is it the fully qualified name of
> the main class?

You didn't do anything wrong, a ProtocolHandler is supposed to be
instantiated and initialized on a per Frame/UI element controller basis
(that is, add your command to a toolbar item and a menu bar item, then
you'll find that the component gets instantiated twice, or even more).

You can design UNO components as singletons, make the component factory
return always the same instance, etc.; but in this case, the
ProtocolHandler was not designed for that; it is better to implement the
singleton patter in another class. You have different options:

- it is a better practice to separate the implementation from the
  ProtocolHandler and the Dispatch object (I assume you are implementing
  both in the same class, and queryDispatch/es returns this) so that you
  have different roles and responsibilities; you can make the
  ProtocolHandle's return always the same dispatch object per Frame so
  that you have a n-1 relationship

- implement the singleton pattern on a helper class that handles all the
  recent documents handling

How are you tracking the recent documents? The ProtocolHandler is not
the place to do that ;)


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina

Mime
View raw message