OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

mouse listener problem in java

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Sat May 05, 2012 1:30 am    Post subject: mouse listener problem in java Reply with quote

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 Very Happy

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
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Sat May 05, 2012 7:29 am    Post subject: Reply with quote

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
View user's profile Send private message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Sat May 05, 2012 8:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Sat May 05, 2012 10:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Sun May 06, 2012 12:10 am    Post subject: Reply with quote

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
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Sun May 06, 2012 1:27 am    Post subject: Reply with quote

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
View user's profile Send private message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Sun May 06, 2012 6:20 pm    Post subject: Reply with quote

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! Very Happy
Back to top
View user's profile Send private message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Sun May 06, 2012 11:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Mon May 07, 2012 3:10 am    Post subject: Reply with quote

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
View user's profile Send private message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Mon May 07, 2012 6:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Mon May 07, 2012 11:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
tomoko
General User
General User


Joined: 20 Mar 2012
Posts: 11

PostPosted: Tue May 08, 2012 10:15 pm    Post subject: Reply with quote

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~ Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group