You are viewing a plain text version of this content. The canonical link for it is here.
Posted to api@openoffice.apache.org by Amenel VOGLOZIN <wa...@yahoo.fr> on 2014/07/10 14:40:21 UTC
[EXT]How to declare a unique-instance and all-contexts component
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?
Thank you.
Re: [EXT]How to declare a unique-instance and all-contexts component
Posted by Amenel VOGLOZIN <wa...@yahoo.fr>.
Yes, I was indeed using the same class for everything, with the intention of creating dedicated classes for each responsibility (component, event handlers, listeners, etc.).
As to tracking recent documents. For now, I'm installing a document event listener in the initialize (com.sun.star.lang.XInitialization interface) method... Yes, it's wrong: each event is notified several times depending on whether several documents are opened or whether a menu item of the extension was clicked. I'm "intercepting" OnLoad, OnSaveDone and OnSaveAsDone events. My list of files will be stored in a text format (JSON or INI or else, haven't chosen yet) in (I guess) the user's application data folder. This means I do not replace the standard MRU files list; I just complement it with a richer set of features (pinning, grouping, tagging, hiding, "forgetting about", etc.). The text file is loaded in the initialize method and saved on the OnCloseApp event.
It's coming along nicely but definitely not beautifully :)
Thanks for your reply.
How are you tracking the recent documents? The ProtocolHandler is not
the place to do that ;)
Re: [EXT]How to declare a unique-instance and all-contexts component
Posted by Ariel Constenla-Haile <ar...@apache.org>.
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