Issue 125691 - [netbeans-integration] add-on constructor is called as many times as there are menu items
Summary: [netbeans-integration] add-on constructor is called as many times as there ar...
Status: CONFIRMED
Alias: None
Product: App Dev
Classification: Unclassified
Component: sdk (show other issues)
Version: 4.1.1
Hardware: All All
: P3 Normal
Target Milestone: ---
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-28 10:04 UTC by John R. D'Orazio
Modified: 2017-09-27 07:12 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Latest Confirmation in: 4.1.1
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description John R. D'Orazio 2014-09-28 10:04:38 UTC
If I make a sample add-on project in Netbeans using the OpenOffice plugin, and I make two menu items, and I put a 'System.out.println("Hello World!");' in the constructor, then compile and "Install and run in OpenOffice", I see "Hello World!" printed twice to the console.

If I make a project with three menu items, I see "Hello World!" printed three times to the console. And then as soon as I click on the menu to open it, I see "Hello World!" printed three more times to the console.

It seems that the dispatch provider is calling the constructor for each menu item / toolbar item?

See the code below generated by the add-on wizard, the only line I have added is in the constructor 'System.out.println("Hello World!");'.

package com.example;

import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;


public final class AddOnTest2 extends WeakBase
   implements com.sun.star.frame.XDispatchProvider,
              com.sun.star.frame.XDispatch,
              com.sun.star.lang.XServiceInfo,
              com.sun.star.lang.XInitialization
{
    private final XComponentContext m_xContext;
    private com.sun.star.frame.XFrame m_xFrame;
    private static final String m_implementationName = AddOnTest2.class.getName();
    private static final String[] m_serviceNames = {
        "com.sun.star.frame.ProtocolHandler" };


    public AddOnTest2( XComponentContext context )
    {
        m_xContext = context;
        System.out.println("Hello World!");
    };

    public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
        XSingleComponentFactory xFactory = null;

        if ( sImplementationName.equals( m_implementationName ) )
            xFactory = Factory.createComponentFactory(AddOnTest2.class, m_serviceNames);
        return xFactory;
    }

    public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
        return Factory.writeRegistryServiceInfo(m_implementationName,
                                                m_serviceNames,
                                                xRegistryKey);
    }

    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,
                                                       String sTargetFrameName,
                                                       int iSearchFlags )
    {
        if ( aURL.Protocol.compareTo("com.example.addontest2:") == 0 )
        {
            if ( aURL.Path.compareTo("Command0") == 0 )
                return this;
            if ( aURL.Path.compareTo("Command1") == 0 )
                return this;
        }
        return null;
    }

    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch[] queryDispatches(
         com.sun.star.frame.DispatchDescriptor[] seqDescriptors )
    {
        int nCount = seqDescriptors.length;
        com.sun.star.frame.XDispatch[] seqDispatcher =
            new com.sun.star.frame.XDispatch[seqDescriptors.length];

        for( int i=0; i < nCount; ++i )
        {
            seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
                                             seqDescriptors[i].FrameName,
                                             seqDescriptors[i].SearchFlags );
        }
        return seqDispatcher;
    }

    // com.sun.star.frame.XDispatch:
     public void dispatch( com.sun.star.util.URL aURL,
                           com.sun.star.beans.PropertyValue[] aArguments )
    {
         if ( aURL.Protocol.compareTo("com.example.addontest2:") == 0 )
        {
            if ( aURL.Path.compareTo("Command0") == 0 )
            {
                // add your own code here
                return;
            }
            if ( aURL.Path.compareTo("Command1") == 0 )
            {
                // add your own code here
                return;
            }
        }
    }

    public void addStatusListener( com.sun.star.frame.XStatusListener xControl,
                                    com.sun.star.util.URL aURL )
    {
        // add your own code here
    }

    public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,
                                       com.sun.star.util.URL aURL )
    {
        // add your own code here
    }

    // com.sun.star.lang.XServiceInfo:
    public String getImplementationName() {
         return m_implementationName;
    }

    public boolean supportsService( String sService ) {
        int len = m_serviceNames.length;

        for( int i=0; i < len; i++) {
            if (sService.equals(m_serviceNames[i]))
                return true;
        }
        return false;
    }

    public String[] getSupportedServiceNames() {
        return m_serviceNames;
    }

    // com.sun.star.lang.XInitialization:
    public void initialize( Object[] object )
        throws com.sun.star.uno.Exception
    {
        if ( object.length > 0 )
        {
            m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(
                com.sun.star.frame.XFrame.class, object[0]);
        }
    }

}
Comment 1 Carl Marcum 2014-09-28 18:23:57 UTC
I can confirm the described behavior.
I started a new AddOn project AddOnTest2 and created menu items Command0, 1, and 2
I assigned:
0 to writer and spreadsheet context
1 to writer
2 to spreadsheet

I added this to the AddOnTest2 constructor:
System.out.println("Contructor Context " + context.toString());
and this to each of the commands in the dispatch method
System.out.println("CommandX");
I deployed to AOO and opened a new writer there was no output yet.
When I clicked the the Menu I received:
Contructor Context com.sun.star.bridges.jni_uno.JNI_proxy@5b74bd37 [oid=7f2bf924e6d0;gcc3[0];f644c4deeb694c8d96e296f02b7a8e88, type=com.sun.star.uno.XComponentContext]
Contructor Context com.sun.star.bridges.jni_uno.JNI_proxy@5b74bd37 [oid=7f2bf924e6d0;gcc3[0];f644c4deeb694c8d96e296f02b7a8e88, type=com.sun.star.uno.XComponentContext]
then when selecting the commands 0 and 1 I received the expected:
Command0
Command1
I closed Writer and opened Calc and received no further output.
When I clicked the the Menu I received:
Contructor Context com.sun.star.bridges.jni_uno.JNI_proxy@5b74bd37 [oid=7f2bf924e6d0;gcc3[0];f644c4deeb694c8d96e296f02b7a8e88, type=com.sun.star.uno.XComponentContext]
Contructor Context com.sun.star.bridges.jni_uno.JNI_proxy@5b74bd37 [oid=7f2bf924e6d0;gcc3[0];f644c4deeb694c8d96e296f02b7a8e88, type=com.sun.star.uno.XComponentContext]
then when selecting the commands 0 and 2 I received the expected:
Command0
Command2

I'm not sure Netbeans is causing this issue. I will ask on the dev mailing list on how the extension constructor is called by AOO and if this is expected behavior.

Carl
Comment 2 Carl Marcum 2016-02-02 00:22:18 UTC
Determined not to be a bug in the netbeans plugin.
Changed to enhancement because this could be further coded as a singleton perhaps.
Comment 3 Rushna Jabi 2017-09-27 07:12:13 UTC
I also got this issue but it's our mistake not OpenOffice nor NetBeans.

when you create a plugin by NetBeans there are some steps:-
1)File-> New Project -> select category Apache OpenOffice -> Project : Apache OpenOffice AddOn (Next)
2)Project Name (Next)
3)Define User Commands(Next)

please note 4th step:-in which you are getting issue

4)Create UI Structure ,here please select a CheckBox Writer or Spreadsheet or Presentation only.(Next)
(Finish)

Inshallah It will be sort out... :)