JAVA- Registering a context menu interceptor in addon.

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

Hi,
I have writen an addon to OO Writer in JAVA language
Now I want to add some menu items to the contextual menu that is poped up when right clicking a selection in the Writer document.

Reading the Developpers' guide I understand I have to write some kind of listener (Interceptor) that implements the interface XContextMenuInterceptor. I saw the example given and I think I understand.
My problem arise when I think about registering this listener because I have trouble understanding how (from what object) and when (or where in the program) to get the XContextMenuInterception interface.
Regarding how, my first thought was to query this interface from the current component, like this:

Code: Select all

        //retrieve the current component
       XComponent xCurrentComponent = xdesktop.getCurrentComponent();

        // get the XModel interface from the component
      XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xCurrentComponent);
        // the model knows its controller
       XController xController = xModel.getCurrentController();
  
               XContextMenuInterception xCTMIntercept = (XContextMenuInterception) UnoRuntime.queryInterface(
        XContextMenuInterception.class, xController);
This code lines seem to return someting when placed in a method called from a command (from a menu or from a pushbutton) but my intention is to get the interface independently of any command in order to have the interceptor ready when I right click the selection in the Writer document.

This said my questions are:
  • 1- are these code lines acceptable
    2- where to place them.
Thank you for any help.
Last edited by meaulnes on Wed Feb 03, 2010 10:22 pm, edited 1 time in total.
OOo 3.1 on Ubuntu 9.10
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a menu interceptor in an addon.

Post by meaulnes »

Since I posted the previous I went a bit further.

In the Initialize of my addons I put this:

Code: Select all

         Object GEBC=null;
        GEBC=m_xContext.getServiceManager().createInstanceWithContext(
                "com.sun.star.frame.GlobalEventBroadcaster", m_xContext);
        XEventBroadcaster xEVBC = (XEventBroadcaster) UnoRuntime.queryInterface(
        XEventBroadcaster.class, GEBC);
       xEVBC.addEventListener(new MyEventListener());
I then created an Event listener to catch the loading of the Writer document, in order to register the ContextMenuInterceptor, like this:

Code: Select all

public class MyEventListener implements XEventListener {
        public void notifyEvent(com.sun.star.document.EventObject arg0) {
            if (arg0.EventName.contentEquals("OnLoadFinished")){
                 //retrieve the current component
                 XComponent xCurrentComponent = xdesktop.getCurrentComponent();
                 // query its XTextDocument interface
                 XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
                 XTextDocument.class, xCurrentComponent);
                // get the XModel interface from the component
                XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDocument);
               // the model knows its controller
               XController xController = xModel.getCurrentController();
               xCTMIntercept = (XContextMenuInterception) UnoRuntime.queryInterface(
               XContextMenuInterception.class, xController);
               xCTMIntercept.registerContextMenuInterceptor(new ContextMenuInterceptor());
            }
        }

It seems that the code line for registering is executed but when I right clik a selection in the document, the notifyContextMenuExecute method of the ContextMenuInterceptor is never called.

My question is: is it right to query the XContextMenuInterception interface from the controller of the TextDocument?
OOo 3.1 on Ubuntu 9.10
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

I went a bit more further?

The answer to the previous question: is it right to query the XContextMenuInterception interface from the controller of the TextDocument?
is "Yes".

The registering is OK and works.

But I still have the problem of where to place it.
My intention was to use the OnLoadFinished event as described in the code I submitted in the previous post but I met the following problem/

1- the constructor of my add-on is called a number of times corresponding to the number of commands in the add-on and this at each loading of a new document.
It seems that this problem also occurs with a quasi empty skeleton provided by the NetBeans IDE.
2- After the last call of the constructor the loading of the document occurs but I get a great number of event that triggers the Event Listener so that even with a filter on the name event and a flag to prevents multiple registerings, things are very disordered.

In the following code, the System.Out.println() lines help understanding what happens.

Code: Select all

 public class MyEventListener implements XEventListener {

        public void notifyEvent(com.sun.star.document.EventObject arg0) {
            System.out.println(arg0.EventName+" notifying");
            if (arg0.EventName.contentEquals("OnLoadFinished") &&
                    (ContextMenuInterceptorRegistered == false)) {

                  System.out.println(arg0.EventName+" registering");
                ContextMenuInterceptorRegistered = true;
                XComponent xCurrentComponent = xdesktop.getCurrentComponent();
                XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
                        XTextDocument.class, xCurrentComponent);
                XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDocument);
                XController xController = xModel.getCurrentController();
                xCTMIntercept = (XContextMenuInterception) UnoRuntime.queryInterface(
                        XContextMenuInterception.class, xController);
                m_ContextMenuInterceptor = new ContextMenuInterceptor();
                xCTMIntercept.registerContextMenuInterceptor(m_ContextMenuInterceptor);
                System.out.println("valeur de m_ContextMenuInterceptor " + m_ContextMenuInterceptor.toString() +
                        "boolean " + Boolean.valueOf(ContextMenuInterceptorRegistered));//debug
            } else {
                System.out.println("value of Event name different from OnLoadFinished, boolean flag " +
                        Boolean.valueOf(ContextMenuInterceptorRegistered));
            }
        }

Assuming that the boolean flag ContextMenuInterceptorRegistered is set to false in the constructor of the addon, here is the out put.

Code: Select all



setting up UNO environment ...
build UNO idl files finished
uno-idl-compile:
init:
deps-jar:
init:
deps-jar:
compile:
jar:
compile:
Building jar: /home/jaaf/NetBeansProjects/Java/Jilt/dist/Jilt.jar
Updating property file: /home/jaaf/NetBeansProjects/Java/Jilt/build/regclass.properties
Updating property file: /home/jaaf/NetBeansProjects/Java/Jilt/build/regclass.properties
jar:
Building jar: /home/jaaf/NetBeansProjects/Java/Jilt/dist/Jilt.jar
images:
Created dir: /home/jaaf/NetBeansProjects/Java/Jilt/build/img/images
Copying 10 files to /home/jaaf/NetBeansProjects/Java/Jilt/build/img/images
registry:
Created dir: /home/jaaf/NetBeansProjects/Java/Jilt/build/cpreg/registry
Copying 4 files to /home/jaaf/NetBeansProjects/Java/Jilt/build/cpreg/registry
Building zip: /home/jaaf/NetBeansProjects/Java/Jilt/dist/Jilt.oxt
Updating zip: /home/jaaf/NetBeansProjects/Java/Jilt/dist/Jilt.oxt
creating UNO extension package ...
Updating zip: /home/jaaf/NetBeansProjects/Java/Jilt/dist/Jilt.oxt
uno-package:
debugging UNO extension package ...
/usr/lib/openoffice/program/unopkg gui -f /home/jaaf/NetBeansProjects/Java/Jilt/dist/Jilt.oxt
starting the Office with ...
user installation: file:///home/jaaf/NetBeansProjects/Java/Jilt/build/soffice_debug
debug options:     "-Xdebug" "-Xrunjdwp:transport=dt_socket,address=jaaf-desktop:37532"
/usr/lib/openoffice/program/soffice
Picked up JAVA_TOOL_OPTIONS: "-Xdebug" "-Xrunjdwp:transport=dt_socket,address=jaaf-desktop:37532"
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag false
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag false
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag false
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag false
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag false
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnNew notifying
value of Event name different from OnLoadFinished, boolean flag false
OnNew notifying
value of Event name different from OnLoadFinished, boolean flag false
OnNew notifying
value of Event name different from OnLoadFinished, boolean flag false
OnNew notifying
value of Event name different from OnLoadFinished, boolean flag false
OnNew notifying
value of Event name different from OnLoadFinished, boolean flag false
OnNew notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareViewClosing notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareViewClosing notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareViewClosing notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareViewClosing notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareViewClosing notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareViewClosing notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareUnload notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareUnload notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareUnload notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareUnload notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareUnload notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPrepareUnload notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoadFinished notifying
OnLoadFinished registering
valeur de m_ContextMenuInterceptor com.zoraldia.openoffice.Jilt$ContextMenuInterceptor@e49d67cboolean true
OnLoadFinished notifying
OnLoadFinished registering
valeur de m_ContextMenuInterceptor com.zoraldia.openoffice.Jilt$ContextMenuInterceptor@1264ab4dboolean true
OnLoadFinished notifying
OnLoadFinished registering
valeur de m_ContextMenuInterceptor com.zoraldia.openoffice.Jilt$ContextMenuInterceptor@245e13adboolean true
OnLoadFinished notifying
OnLoadFinished registering
valeur de m_ContextMenuInterceptor com.zoraldia.openoffice.Jilt$ContextMenuInterceptor@546c585aboolean true
OnLoadFinished notifying
OnLoadFinished registering
valeur de m_ContextMenuInterceptor com.zoraldia.openoffice.Jilt$ContextMenuInterceptor@c5a67c9boolean true
OnLoadFinished notifying
OnLoadFinished registering
valeur de m_ContextMenuInterceptor com.zoraldia.openoffice.Jilt$ContextMenuInterceptor@70453807boolean true
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag true
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag true
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag true
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag true
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag true
OnTitleChanged notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewClosed notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewClosed notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewClosed notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewClosed notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewClosed notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewClosed notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnload notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnload notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnload notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnload notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnload notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnload notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnfocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnfocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnfocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnfocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnfocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnUnfocus notifying
value of Event name different from OnLoadFinished, boolean flag true
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag false
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnFocus notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag false
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag true
OnViewCreated notifying
value of Event name different from OnLoadFinished, boolean flag true
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag false
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag true
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag true
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag true
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag true
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag true
OnPageCountChange notifying
value of Event name different from OnLoadFinished, boolean flag true
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag false
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag true
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag true
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag true
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag true
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag true
OnLoad notifying
value of Event name different from OnLoadFinished, boolean flag true
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag false
 notifying
value of Event name different from OnLoadFinished, boolean flag true
 notifying
value of Event name different from OnLoadFinished, boolean flag true
 notifying
value of Event name different from OnLoadFinished, boolean flag true
 notifying
value of Event name different from OnLoadFinished, boolean flag true
 notifying
value of Event name different from OnLoadFinished, boolean flag true
 notifying
value of Event name different from OnLoadFinished, boolean flag true
From this, it can be seen that there are two series of

Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
Printed from the constructor of the addon
Printed from Initialize
The first one occurs when opening writer and loading a blank document and the second when manually loading a file.

After that the interception doesn't work. But if a put the code lines behind a menu command after it works but it is tedious for the user.
Has somebody an idea of how to make this work automatically?

Thanks.
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

I think css.task.XJob interface is normally used to initiate a service at the event occurred. The document object is passed in the case of using css.task.XJob interface to initialize the component. And it is clear when the service is initialized, its instance is created for each document frame event occurred.
I made an example in python.
ctmp.oxt.zip
Minimum Job Implementation with context menu interceptor in python. Remove .zip extension before install.
(1.81 KiB) Downloaded 373 times
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

Hi hanya,

Thank you.

I don't know python at all (I am not a computer scientist). All the same I don't know what is css.task.XJob but I will have a look at this. Maybe I will understand , maybe I wont.
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

Read Jobs section of DevGuide to know well:
http://wiki.services.openoffice.org/wik ... figuration
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

I am reading it presently.
Do I understand well telling that the main interest resides in the fact that the task can be synchronous, thus guaranteeing the good registering process ?
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

I made the following service implements css.task.XJob interface and it is registered as "mytools.example.Job" service. I forgot to check the document model supports specific document service in the code but it is not so important as an example...

Code: Select all

package mytools.example.job;

import com.sun.star.beans.NamedValue;
import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.task.XJob;
import com.sun.star.ui.XContextMenuInterception;
import com.sun.star.ui.XContextMenuInterceptor;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public final class JobExecutor {

	public static class _JobExecutor extends WeakBase implements XJob {

		XComponentContext m_xContext;
		
		public _JobExecutor(XComponentContext context)
		{
			System.out.println("JobExecutor initialized.");
			 m_xContext = context;
		}
		
		@Override
		public Object execute(NamedValue[] args)
				throws IllegalArgumentException, Exception {

			XModel xModel = null;
			boolean correctEvent = false;

			for (NamedValue arg : args) {
				if (arg.Name.equals("Environment")) {
					NamedValue[] values = (NamedValue[]) AnyConverter.toObject(
							new Type("[]com.sun.star.beans.NamedValue"),
							arg.Value);
					for (NamedValue value : values) {
						if (value.Name.equals("EnvType")
								&& value.Value.equals("DOCUMENTEVENT")) {
							correctEvent = true;
						} else if (value.Name.equals("EventName")) {

						} else if (value.Name.equals("Model")) {
							xModel = UnoRuntime.queryInterface(XModel.class,
									value.Value);
						}
					}
				}
			}
			if (correctEvent) {
				MyContextMenuInterceptor interceptor = new MyContextMenuInterceptor();
				XContextMenuInterceptor xInterceptor = UnoRuntime
						.queryInterface(XContextMenuInterceptor.class,
								interceptor);
				XController xController = xModel.getCurrentController();
				XContextMenuInterception xInterception = UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
				xInterception.registerContextMenuInterceptor(xInterceptor);
				
				System.out.println("Context menu interceptor has been registered.");
			}
			return null;
		}
	}

	private static final String __serviceName = "mytools.example.Job";
	private static final String[] __services = { __serviceName };

	public static XSingleServiceFactory __getServiceFactory(String implName,
			XMultiServiceFactory multiFactory, XRegistryKey regKey) {
		XSingleServiceFactory xSingleServiceFactory = null;

		if (implName.equals(JobExecutor._JobExecutor.class.getName())) {
			xSingleServiceFactory = FactoryHelper.getServiceFactory(
					JobExecutor._JobExecutor.class, __serviceName,
					multiFactory, regKey);
		}
		return xSingleServiceFactory;
	}

	public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {

		String impl = JobExecutor._JobExecutor.class.getName();
		for (String service : __services) {
			if (!FactoryHelper.writeRegistryServiceInfo(impl, service, regKey)) {
				return false;
			}
		}
		return true;
	}
}
This is the prototype of the context menu interceptor and this class is instantiated by the above service:

Code: Select all

package mytools.example.job;

import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.ui.ContextMenuExecuteEvent;
import com.sun.star.ui.ContextMenuInterceptorAction;
import com.sun.star.ui.XContextMenuInterceptor;

public class MyContextMenuInterceptor extends WeakBase implements XContextMenuInterceptor
{

	@Override
	public ContextMenuInterceptorAction notifyContextMenuExecute(
			ContextMenuExecuteEvent ev) {
		System.out.println("notified.");
		return null;
	}
}
Here is the configuration file to register the service implementing css.task.XJob interface, to global events. In this case, the "execute" method of css.task.XJob interface implemented by the service named "mytools.example.Job" is called on "OnNew" and "OnLoad" event.

Code: Select all

<?xml version="1.0"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:install="http://openoffice.org/2004/installation" oor:name="Jobs" 
oor:package="org.openoffice.Office">
<node oor:name="Jobs">
	<node oor:name="mytools.JobExample" oor:op="replace">
		<prop oor:name="Service">
			<value>mytools.example.Job</value>
		</prop>
	</node>
</node>
<node oor:name="Events">
	<node oor:name="OnNew" oor:op="fuse">
		<node oor:name="JobList">
			<node oor:name="mytools.JobExample" oor:op="replace"/>
		</node>
	</node>
	<node oor:name="OnLoad" oor:op="fuse">
		<node oor:name="JobList">
			<node oor:name="mytools.JobExample" oor:op="replace"/>
		</node>
	</node>
</node>
</oor:component-data>
When "OnNew" or "OnLoad" event occurred, "mytools.example.Job" service is instantiated and its "execute" method is called. The XContextMenuInterceptor interface is registered to the new or loaded document controller in the execute method.
execute method is called per event registered in the /org.openoffice.Office.Events node.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

Thank you.

Need to read a little bit more before adapting it to my case.
OOo 3.1 on Ubuntu 9.10
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

Hi hanya,

I am trying to adapt your example but I cannot obtain the JobExecutor initialized.
I send you back your file with my changes.

My source package where I have placed the JobExecutor.java and MyContextMenuInterceptor.java files is:
com.zoraldia.openoffice
I have placed the Jobs.xcu file in registry.data.org.openoffice.Office

Code: Select all

package com.zoraldia.openoffice;



import com.sun.star.beans.NamedValue;
import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.task.XJob;
import com.sun.star.ui.XContextMenuInterception;
import com.sun.star.ui.XContextMenuInterceptor;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public final class JobExecutor {

   public static class _JobExecutor extends WeakBase implements XJob {

      XComponentContext m_xContext;

      public _JobExecutor(XComponentContext context)
      {
         System.out.println("JobExecutor initialized.");
          m_xContext = context;
      }

      @Override
      public Object execute(NamedValue[] args)
            throws IllegalArgumentException, Exception {

         XModel xModel = null;
         boolean correctEvent = false;

         for (NamedValue arg : args) {
            if (arg.Name.equals("Environment")) {
               NamedValue[] values = (NamedValue[]) AnyConverter.toObject(
                     new Type("[]com.sun.star.beans.NamedValue"),
                     arg.Value);
               for (NamedValue value : values) {
                  if (value.Name.equals("EnvType")
                        && value.Value.equals("DOCUMENTEVENT")) {
                     correctEvent = true;
                  } else if (value.Name.equals("EventName")) {

                  } else if (value.Name.equals("Model")) {
                     xModel = (XModel) UnoRuntime.queryInterface(XModel.class,
                           value.Value);
                  }
               }
            }
         }
         if (correctEvent) {
            MyContextMenuInterceptor interceptor = new MyContextMenuInterceptor();
            XContextMenuInterceptor xInterceptor = (XContextMenuInterceptor) UnoRuntime
                  .queryInterface(XContextMenuInterceptor.class,
                        interceptor);
            XController xController = xModel.getCurrentController();
            XContextMenuInterception xInterception = (XContextMenuInterception) UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
            xInterception.registerContextMenuInterceptor(xInterceptor);

            System.out.println("Context menu interceptor has been registered.");
         }
         return null;
      }
   }

   private static final String __serviceName = "mytools.example.Job";
   private static final String[] __services = { __serviceName };

   public static XSingleServiceFactory __getServiceFactory(String implName,
         XMultiServiceFactory multiFactory, XRegistryKey regKey) {
      XSingleServiceFactory xSingleServiceFactory = null;

      if (implName.equals(JobExecutor._JobExecutor.class.getName())) {
         xSingleServiceFactory = FactoryHelper.getServiceFactory(
               JobExecutor._JobExecutor.class, __serviceName,
               multiFactory, regKey);
      }
      return xSingleServiceFactory;
   }

   public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {

      String impl = JobExecutor._JobExecutor.class.getName();
      for (String service : __services) {
         if (!FactoryHelper.writeRegistryServiceInfo(impl, service, regKey)) {
            return false;
         }
      }
      return true;
   }
}

Code: Select all

package com.zoraldia.openoffice;


import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.ui.ContextMenuExecuteEvent;
import com.sun.star.ui.ContextMenuInterceptorAction;
import com.sun.star.ui.XContextMenuInterceptor;

public class MyContextMenuInterceptor extends WeakBase implements XContextMenuInterceptor
{

   @Override
   public ContextMenuInterceptorAction notifyContextMenuExecute(
         ContextMenuExecuteEvent ev) {
      System.out.println("notified.");
      return null;
   }
}

Code: Select all

<?xml version="1.0"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:install="http://openoffice.org/2004/installation" oor:name="Jobs"
oor:package="org.openoffice.Office">
<node oor:name="Jobs">
   <node oor:name="mytools.JobExample" oor:op="replace">
      <prop oor:name="Service">
         <value>mytools.example.Job</value>
      </prop>
   </node>
</node>
<node oor:name="Events">
   <node oor:name="OnNew" oor:op="fuse">
      <node oor:name="JobList">
         <node oor:name="mytools.JobExample" oor:op="replace"/>
      </node>
   </node>
   <node oor:name="OnLoad" oor:op="fuse">
      <node oor:name="JobList">
         <node oor:name="mytools.JobExample" oor:op="replace"/>
      </node>
   </node>
</node>
</oor:component-data>
At the moment I haven't coded the NotifyContextMenuExecute method because I want to validate the general behavior before.
I get no message in the output window of my IDE.

Could you please have a look at my changes and help me understanding why it doesn't work properly.

Best regards.
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

Do you have correct entry for jobs.xcu file in your META-INF/manifest.xml?
Attachments
ExampleJob2.zip
in Java, zipped files.
(16.11 KiB) Downloaded 369 times
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

Hi,thanks,

I think yes. Here is this file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
  <manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Java"
                       manifest:full-path="Jilt.jar"/>
  <manifest:file-entry manifest:media-type="application/vnd.sun.star.package-bundle-description"
                       manifest:full-path="description/description.txt"/>
  <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-schema"
                       manifest:full-path="registry/schema/com/zoraldia/openoffice/OptionPage2.xcs"/>
  <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data"
                       manifest:full-path="registry/data/org/openoffice/Office/ProtocolHandler.xcu"/>
  <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data"
                       manifest:full-path="registry/data/org/openoffice/Office/Resources.xcu"/>
  <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data"
                       manifest:full-path="registry/data/org/openoffice/Office/Addons.xcu"/>
    <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data"
                       manifest:full-path="registry/data/org/openoffice/Office/Jobs.xcu"/>
</manifest:manifest>


What I don't understand in your .xcu file is the value of the node "Service"

Code: Select all

   <node oor:name="mytools.JobExample" oor:op="replace">
      <prop oor:name="Service">
         <value>mytools.example.Job</value>
      </prop>
   </node>
OOo 3.1 on Ubuntu 9.10
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

previous message updated
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

meaulnes wrote:What I don't understand in your .xcu file is the value of the node "Service"
This is name of the service or implementation that implements css.task.XJob interface and the execute method of the interface is called when the events are occurred. _JobExecutor class is provided as a component named "mytools.example.Job".

If you don't understand about the single component factory, please have a look:
http://wiki.services.openoffice.org/wik ... chitecture
http://wiki.services.openoffice.org/wik ... nt_in_Java

Now css.lang.XSingleServiceFactory is deprecated, use css.lang.XSingleComponentFactory and com.sun.star.lib.uno.helper.Factory service to registration, please see the SDK example.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

Hi,

I read a lot but I cannot figure out how to aggregate your example and the existing structure of my existing addon.

Is the way I procede correct ?
  • I added the files JobExecutor.java and MyContextMenuInterceptor.java to the same source directory where the addon.java file is normally placed.
    I added the file Jobs.xcu to the diectory where normally reside Addons.xcu and ProtocolHandler.xcu
    I added the entry for the Jobs.xcu file in the manifest
When starting the addon in debug in target OpenOffice.org I can see that the _getComponentFactory and _writeServiceInfo of the addon are executed but that the _getServiceFactory and _writeServiceInfo in JobExecutor are not. I never get the message from System.out.println("JobExecutor initialized.");

I saw in the documentation that
To assemble several service implementations in one component file, implement each service in its own class and add a separate class containing the static component operations. The following code sample features two services: TestComponentA and TestComponentB implementing the interfaces XSomethingA and XSomethingB with a separate static class TestServiceProvider containing the component operations.
with the following example

Code: Select all

 package JavaComp;
  ...
  public class TestServiceProvider
  {
      public static XSingleServiceFactory __getServiceFactory(String implName,
                                               XMultiServiceFactory multiFactory,
                                               XRegistryKey regKey) {
          XSingleServiceFactory xSingleServiceFactory = null;
 
          if (implName.equals( TestComponentA.class.getName()) )
              xSingleServiceFactory = FactoryHelper.getServiceFactory( TestComponentA.class,
                                          TestComponentA.__serviceName, multiFactory, regKey); 
          else if (implName.equals(TestComponentB.class.getName()))
              xSingleServiceFactory= FactoryHelper.getServiceFactory( TestComponentB.class,
                                          TestComponentB.__serviceName, multiFactory, regKey);
          return xSingleServiceFactory;
      }
 
      public static boolean __writeRegistryServiceInfo(XRegistryKey regKey){
          boolean bregA= FactoryHelper.writeRegistryServiceInfo( TestComponentA.class.getName(),
                                          TestComponentA.__serviceName, regKey);
          boolean bregB= FactoryHelper.writeRegistryServiceInfo( TestComponentB.class.getName(),
                                          TestComponentB.__serviceName, regKey);
          return bregA && bregB;
      } 
  }

But the trouble I have is that on one side I have a _getComponetFactory method and on the other side a _getServiceFactory method .
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

meaulnes wrote:When starting the addon in debug in target OpenOffice.org I can see that the _getComponentFactory and _writeServiceInfo of the addon are executed but that the _getServiceFactory and _writeServiceInfo in JobExecutor are not. I never get the message from System.out.println("JobExecutor initialized.");
Do you have an entry for the class implements the service in the manifest of your jar file? Can you see the service name in the list of available service names?

Code: Select all

RegistrationClassName: mytools.example.job.JobExecutor
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

I haven't.

Here is this file, in the dist/jilt.jar/META-INF directory .It's read only.

Code: Select all

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.0-b16 (Sun Microsystems Inc.)
UNO-Type-Path: 
RegistrationClassName: com.zoraldia.openoffice.CentralRegistrationClas
 s
Class-Path: lib/japUtils.jar

Name: com/zoraldia/openoffice/CentralRegistrationClass.class
RegistrationClasses: com.zoraldia.openoffice.Jilt

I don't know where to check the available services.
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

I have not used Netbeans plug-in of OpenOffice.org but your com.zoraldia.openoffice.CentralRegistrationClass reads manifest file of the jar file and registrate files listed in the entry RegistrationClasses. I forgot well about it...
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
meaulnes
Posts: 77
Joined: Thu May 07, 2009 8:38 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by meaulnes »

I understand.
But, independently of the IDE, before I go in deeper searches, do you think it's possible to assemble two components in the same .jar file?
As both the ProtocolHandler of the addon and the JobExecutor extend weekbase, it seems there are 2 components.
OOo 3.1 on Ubuntu 9.10
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

meaulnes wrote:But, independently of the IDE, before I go in deeper searches, do you think it's possible to assemble two components in the same .jar file?
See examples/DevelopersGuide/Components/JavaComponent/TestServiceProvider.java in the OpenOffice.org SDK.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
dooteo
Posts: 7
Joined: Thu Jan 13, 2011 12:30 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by dooteo »

Hi hanya and meaulnes,

I developed a context menu interceptor, and works because of your suggestions. But it does not work at all.

The matter is that when document is on Preview mode and back to normal mode, context menu interceptor does not work. Same happen when doc is opened in Window -> New Window.

So, how can I register the interceptor again?

Thanks in advance
OOo 3.2 Debian GNU/Linux
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

The matter is that when document is on Preview mode and back to normal mode, context menu interceptor does not work.
You can detect the change between default view and preview mode using frame action listener. I do not know what kind of document you are targeting but you can check their controller in the both mode yourself. "Default" controller is used at default view and "PrintPreview" controller is used for preview mode, their name can be seen in their ViewControllerName attribute, for Writer documents.

I have no idea for Window -> New Window now.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
dooteo
Posts: 7
Joined: Thu Jan 13, 2011 12:30 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by dooteo »

By default, document type will be TextDocument (com.sun.star.text.TextDocument)

I tried to add a FramActionListener, but I don't know where and how to add it. I guest it should be in the XJob interface, but how to add it to Frame?

Code: Select all

package com.uzei.thesaurus;

import com.uzei.thesaurus.contextmenu.ContextMenuInterceptor;
import com.sun.star.beans.NamedValue;
import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.task.XJob;
import com.sun.star.ui.XContextMenuInterception;
import com.sun.star.ui.XContextMenuInterceptor;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.frame.XFrame;

public final class JobExecutor {

 public static class _JobExecutor extends WeakBase 
         implements XJob {

  XComponentContext m_xContext;
  XFrame m_xFrame;
  
  public _JobExecutor(XComponentContext context)
  {
   System.out.println("JobExecutor initialized.");
    m_xContext = context;
  }

  @Override
  public Object execute(NamedValue[] args)
    throws IllegalArgumentException, Exception {

   XModel xModel = null;
   boolean correctEvent = false;

   for (NamedValue arg : args) {
    if (arg.Name.equals("Environment")) {
     NamedValue[] values = (NamedValue[]) AnyConverter.toObject(
       new Type("[]com.sun.star.beans.NamedValue"),
       arg.Value);
     for (NamedValue value : values) {
      if (value.Name.equals("EnvType")
        && value.Value.equals("DOCUMENTEVENT")) {
       correctEvent = true;
      } else if (value.Name.equals("EventName")) {

      } else if (value.Name.equals("Model")) {
       xModel = UnoRuntime.queryInterface(XModel.class,
         value.Value);
      }
     }
    }
   }
   if (correctEvent) {
    ContextMenuInterceptor interceptor = new ContextMenuInterceptor();
    XContextMenuInterceptor xInterceptor = UnoRuntime
      .queryInterface(XContextMenuInterceptor.class,
        interceptor);
    XController xController = xModel.getCurrentController();
    XContextMenuInterception xInterception = UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
    xInterception.registerContextMenuInterceptor(xInterceptor);

    System.out.println("Context menu interceptor has been registered.");
   }
   return null;
  }
 }



 private static final String __serviceName = "UzeiJobs";
 private static final String[] __services = { __serviceName };

 public static XSingleServiceFactory __getServiceFactory(String implName,
   XMultiServiceFactory multiFactory, XRegistryKey regKey) {
  XSingleServiceFactory xSingleServiceFactory = null;

  if (implName.equals(JobExecutor._JobExecutor.class.getName())) {
   xSingleServiceFactory = FactoryHelper.getServiceFactory(
     JobExecutor._JobExecutor.class, __serviceName,
     multiFactory, regKey);
  }
  return xSingleServiceFactory;
 }

 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {

  String impl = JobExecutor._JobExecutor.class.getName();
  for (String service : __services) {
   if (!FactoryHelper.writeRegistryServiceInfo(impl, service, regKey)) {
    return false;
   }
  }
  return true;
 }

}
Thanks
OOo 3.2 Debian GNU/Linux
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

Get css.frame.XFrame interface from the controller and see the following reference:
http://api.openoffice.org/docs/common/r ... Frame.html
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
dooteo
Posts: 7
Joined: Thu Jan 13, 2011 12:30 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by dooteo »

So if I get a frame from xCrontroller, I can use xFrame.FrameActionListener to compare new frame action with COMPONENT_ATTACHED or COMPONENT_REATTACHED

Code: Select all

 (...)
    XController xController = xModel.getCurrentController();
    XContextMenuInterception xInterception = UnoRuntime.queryInterface(XContextMenuInterception.class, xController);
    xInterception.registerContextMenuInterceptor(xInterceptor);
    System.out.println("Context menu interceptor has been registered.");

    /* ------- Added new code --------- */
    XFrame xFrame = xController.getFrame();
    xFrame.addFrameActionListener(new XFrameActionListener(){

                   public void frameAction(FrameActionEvent arg0) {
                        if ( (arg0.Action.equals(FrameAction.COMPONENT_ATTACHED)) || (arg0.Action.equals(FrameAction.COMPONENT_REATTACHED)) ) {
                            // do something
                        }
                    }

                    public void disposing(EventObject arg0) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
    });
is it correct?
OOo 3.2 Debian GNU/Linux
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

This example witten in Python and you can see what is happen when the view type changed.

Code: Select all

import unohelper

from com.sun.star.frame import XFrameActionListener
from com.sun.star.frame.FrameAction import COMPONENT_REATTACHED as FA_COMPONENT_REATTACHED
#CONTEXT_CHANGED as FA_CONTEXT_CHANGED

class FrameActionListener(unohelper.Base, XFrameActionListener):
	def __init__(self):
		pass
	
	def disposing(self, ev): pass
	def frameAction(self, ev):
		print(ev.Action)
		if ev.Action == FA_COMPONENT_REATTACHED:
			frame = ev.Source
			print(frame.getController().ViewControllerName)


def attache_frame_action_listener():
	doc = XSCRIPTCONTEXT.getDocument()
	frame = doc.getCurrentController().getFrame()
	frame.addFrameActionListener(FrameActionListener())
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

And "View created" event can be used to detect Window - New Window. Or dispatch interceptor can be used for this task but it it tricky.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
dooteo
Posts: 7
Joined: Thu Jan 13, 2011 12:30 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by dooteo »

Hi hanya,

I'm still loosing my mind (did I say I really newbie?) on how and where should I implement XFrameActionListener. Should I create a new class for XFrameActionListener to attach frame to it?

Thanks & Best Regards,
OOo 3.2 Debian GNU/Linux
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: JAVA- Registering a context menu interceptor in addon.

Post by hanya »

I made an example.
ExampleJob.zip
Example including the frame action listener and to use OnViewCreated event
(15.64 KiB) Downloaded 355 times
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
dooteo
Posts: 7
Joined: Thu Jan 13, 2011 12:30 pm

Re: JAVA- Registering a context menu interceptor in addon.

Post by dooteo »

Hi hanya,

I'm afraid previous attachment were builded one (there are not Java files, but Class files)

Could you attach a non builded example for me, please?

Thanks & Best Regards
OOo 3.2 Debian GNU/Linux
Post Reply