| View previous topic :: View next topic |
| Author |
Message |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Sat May 05, 2012 1:30 am Post subject: mouse listener problem in java |
|
|
Hi~
i want to add a mouselistener to XWindow in order to catch user's right click event,but i found in my code only
| Code: |
public void disposing(EventObject arg0) {
System.out.println("disposing");
} | could be done when i close the window.
these code are not be done
| Code: |
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
} |
why this happens? Is there something i need to do?
If someone can help me,i will great appreciate
here is my initialize method
| Code: |
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]);
XMultiComponentFactory xMCF = m_xContext.getServiceManager();//get service
Object desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
XDesktop xdesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop);
XFrame xFrame = xdesktop.getCurrentFrame();
XComponent xCurrentComponent = xdesktop.getCurrentComponent();
ContextMenuInterceptor mContext = new ContextMenuInterceptor(xFrame.getController() );
XWindow xWindow = xFrame.getComponentWindow();
XMouseListener xmouselistener = new XMouseListener() {
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
}
public void disposing(EventObject arg0) {
System.out.println("disposing");
}
};
xWindow.addMouseListener(xmouselistener);
XEventBroadcaster xEventBroadcaster = (XEventBroadcaster) UnoRuntime.queryInterface (
XEventBroadcaster.class, xCurrentComponent);
xEventBroadcaster.addEventListener ((XEventListener) xmouselistener);
}
}
|
PS: my project is an addon project
Moderation probe1: two identical posts removed |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Sat May 05, 2012 7:29 am Post subject: |
|
|
The document window is created by the hierarchy of the windows.
Your listener must work only for the component window but not for its child windows. |
|
| Back to top |
|
 |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Sat May 05, 2012 8:45 pm Post subject: |
|
|
| hanya wrote: | The document window is created by the hierarchy of the windows.
Your listener must work only for the component window but not for its child windows. |
Hi hanya~Thanks for your reply.
But...I couldn't understand how to correct my code...
You say "Your listener must work only for the component window but not for its child windows." , | Code: | | XWindow xWindow = xFrame.getComponentWindow(); | if it is not a component window,how can i get the component window?
If it is convenient,counld you please give me an example?I would be grateful. |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Sat May 05, 2012 10:07 pm Post subject: |
|
|
The component window itself is used just a container. It keeps some child windows on it and it is covered by them. Therefore it almost can not take mouse action on it. You can access the window which shows document content by getWindow method if required.
So, what do you want to do with it? If you want to get the mouse action on the document, please consider to switch to use mouse click handler. You can find some examples for it in this forum. |
|
| Back to top |
|
 |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Sun May 06, 2012 12:10 am Post subject: |
|
|
| hanya wrote: | The component window itself is used just a container. It keeps some child windows on it and it is covered by them. Therefore it almost can not take mouse action on it. You can access the window which shows document content by getWindow method if required.
So, what do you want to do with it? If you want to get the mouse action on the document, please consider to switch to use mouse click handler. You can find some examples for it in this forum. |
Thank you so much hanya~it works!!
Now i am facing another problem which has been troubling me long time...I want to modify the context menu , i have read many treads in this forum,but i still dont know how use those codes...
here is my code for test, what error do i made?
| Code: |
XContextMenuInterceptor xContextMenuInterceptorAction = new XContextMenuInterceptor() {
public ContextMenuInterceptorAction notifyContextMenuExecute(ContextMenuExecuteEvent aEvent) {
try {
System.out.println("works");
// Retrieve context menu container and query for service factory to
// create sub menus, menu entries and separators
com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer;
com.sun.star.lang.XMultiServiceFactory xMenuElementFactory =
(com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
com.sun.star.lang.XMultiServiceFactory.class, xContextMenu);
if (xMenuElementFactory != null) {
// create root menu entry for sub menu and sub menu
com.sun.star.beans.XPropertySet xRootMenuEntry =
(XPropertySet) UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger"));
// create a line separator for our new help sub menu
com.sun.star.beans.XPropertySet xSeparator =
(com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerSeparator"));
Short aSeparatorType = new Short(ActionTriggerSeparatorType.LINE);
xSeparator.setPropertyValue("SeparatorType", (Object) aSeparatorType);
// query sub menu for index container to get access
com.sun.star.container.XIndexContainer xSubMenuContainer =
(com.sun.star.container.XIndexContainer) UnoRuntime.queryInterface(
com.sun.star.container.XIndexContainer.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTriggerContainer"));
// intialize root menu entry "Help"
xRootMenuEntry.setPropertyValue("Text", new String("Help"));
xRootMenuEntry.setPropertyValue("CommandURL", new String("slot:5410"));
xRootMenuEntry.setPropertyValue("HelpURL", new String("5410"));
xRootMenuEntry.setPropertyValue("SubContainer", (Object) xSubMenuContainer);
// create menu entries for the new sub menu
// intialize help/content menu entry
// entry "Content"
XPropertySet xMenuEntry = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTrigger"));
xMenuEntry.setPropertyValue("Text", new String("Content"));
xMenuEntry.setPropertyValue("CommandURL", new String("slot:5401"));
xMenuEntry.setPropertyValue("HelpURL", new String("5401"));
// insert menu entry to sub menu
xSubMenuContainer.insertByIndex(0, (Object) xMenuEntry);
// intialize help/help agent
// entry "Help Agent"
xMenuEntry = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTrigger"));
xMenuEntry.setPropertyValue("Text", new String("Help Agent"));
xMenuEntry.setPropertyValue("CommandURL", new String("slot:5962"));
xMenuEntry.setPropertyValue("HelpURL", new String("5962"));
// insert menu entry to sub menu
xSubMenuContainer.insertByIndex(1, (Object) xMenuEntry);
// intialize help/tips
// entry "Tips"
xMenuEntry = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class,
xMenuElementFactory.createInstance(
"com.sun.star.ui.ActionTrigger"));
xMenuEntry.setPropertyValue("Text", new String("Tips"));
xMenuEntry.setPropertyValue("CommandURL", new String("slot:5404"));
xMenuEntry.setPropertyValue("HelpURL", new String("5404"));
// insert menu entry to sub menu
xSubMenuContainer.insertByIndex(2, (Object) xMenuEntry);
// add separator into the given context menu
xContextMenu.insertByIndex(0, (Object) xSeparator);
// add new sub menu into the given context menu
xContextMenu.insertByIndex(0, (Object) xRootMenuEntry);
// The controller should execute the modified context menu and stop notifying other
// interceptors.
return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
}
} catch (com.sun.star.beans.UnknownPropertyException ex) {
// do something useful
// we used a unknown property
} catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
// do something useful
// we used an invalid index for accessing a container
} catch (com.sun.star.uno.Exception ex) {
// something strange has happend!
} catch (java.lang.Throwable ex) {
// catch java exceptions - do something useful
}
return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;
}
};
|
|
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Sun May 06, 2012 1:27 am Post subject: |
|
|
| Quote: | | what error do i made? | I did not read your code well and I could not understand what is your problem.
But this kind of api have to be registered and in most case, it is registered by the event. So, you need to be include job setting to register it to specific event. |
|
| Back to top |
|
 |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Sun May 06, 2012 6:20 pm Post subject: |
|
|
| hanya wrote: | | Quote: | | what error do i made? | I did not read your code well and I could not understand what is your problem.
But this kind of api have to be registered and in most case, it is registered by the event. So, you need to be include job setting to register it to specific event. |
You give me such great help hanya!Sincere gratitude to you!  |
|
| Back to top |
|
 |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Sun May 06, 2012 11:25 pm Post subject: |
|
|
| hanya wrote: | | Quote: | | what error do i made? | I did not read your code well and I could not understand what is your problem.
But this kind of api have to be registered and in most case, it is registered by the event. So, you need to be include job setting to register it to specific event. |
Hi hanya~Sorry for troubling you again...I have inserted an item to context menu,and i want to do something(execute a function) when i click that item, but i dont know how to add a listener to a context item.It seems that i only can set item's PropertyValue.So can you give me some hints?
Thanks~ |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Mon May 07, 2012 3:10 am Post subject: |
|
|
| Quote: | | but i dont know how to add a listener to a context item.It seems that i only can set item's PropertyValue.So can you give me some hints? |
Do it through a protocol handler called for your command which you have set. |
|
| Back to top |
|
 |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Mon May 07, 2012 6:10 pm Post subject: |
|
|
| hanya wrote: | | Quote: | | but i dont know how to add a listener to a context item.It seems that i only can set item's PropertyValue.So can you give me some hints? |
Do it through a protocol handler called for your command which you have set. |
It seems protocol handler needs a command URL, but i couldn't get the url of my own context item,so i have two questions:
1' how can i get the url of my own context item?
2' shoud i write my own function(which i want to call by clicking the contex item) in dispatch method?
I will be grateful if you can give me some examples. |
|
| Back to top |
|
 |
hanya Super User

Joined: 04 May 2005 Posts: 543 Location: Japan
|
Posted: Mon May 07, 2012 11:37 pm Post subject: |
|
|
It seems the problem is now out of the original subject.
| Quote: | | 1' how can i get the url of my own context item? | Prepare to register your own one by the configuration. You can find the information about it in the dev guide: http://wiki.services.openoffice.org/wiki/Documentation/DevGuide
| Quote: | 2' shoud i write my own function(which i want to call by clicking the contex item) in dispatch method?
I will be grateful if you can give me some examples. | It depends on the type of your extension and the task. If it is implemented as group of macro, it is better to implement the new task executed by the context menu as a macro. If you are using netbeans plugin or something similar, you can make new own protocol handler easily, I supporse. |
|
| Back to top |
|
 |
tomoko General User

Joined: 20 Mar 2012 Posts: 11
|
Posted: Tue May 08, 2012 10:15 pm Post subject: |
|
|
| hanya wrote: |
It depends on the type of your extension and the task. If it is implemented as group of macro, it is better to implement the new task executed by the context menu as a macro. If you are using netbeans plugin or something similar, you can make new own protocol handler easily, I supporse. |
Thank you hanya,I will read it carefully~  |
|
| Back to top |
|
 |
|