Hi Indukumar,

ULCPopupMenu does not support getInvoker() API that is found in JPopupMenu.

I have added a feature request https://www.canoo.com/jira/browse/UBA-6912
for the same.

To implement this functionality you will need to extend ULC.

I have enclosed two samples at the end:

First approach: Extend the components on which you are setting the
PopupMenu. When a popup menu is shown on the component, a message will be
sent from the client to the server and you can execute your code  in the
handleRequest method.

Second approach: Extend ULCPopupMenu to have getInvoker() method. You will
also need to extend the components on which you are setting the popup menu.

I hope this helps.

Thanks and regards,

Janak

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Indukumar
Vellapillil H. (KSDE 211)
Sent: Thursday, April 27, 2006 3:20 PM
To: [email protected]
Subject: [ULC-developer] Sharing ULCPopupMenu


Hi,
We would like to share ULCPopupMenu among different ULCComponents to avoid
the overhead of having a menu for each component.
But, our problem is that it is not possible for us to access the component
on which the popup menu was triggered (i.e. the component on which the mouse
right-click happened).
So, does ULC provide any API to access that component?
If there is no way to access the component on which the popup menu was
triggered, is there any way we can handle mouse events? AFAIK, ULC does not
support low level events.
But maybe there is some 'hack' that enables us to do so?
Thanks in advance
V.H.Indukumar
CREDIT SUISSE
Information Technology
Market Information, Reference Data
Eggbühlstrasse 21-23
P.O. Box 600
CH-8070 Zürich
Switzerland
Tel. +41 44 334 6481
Fax. + 41 44 334 47 98
mailto:[EMAIL PROTECTED]
http://www.credit-suisse.com


This message is for the named person’s use only.  It may contain
confidential, proprietary or legally privileged information.
No confidentially or privilege is waived or lost by any transmission errors.
If you receive this message in error, please immediately delete it and all
copies of it from your system, destroy any hard copies of it and notify the
sender.  You must not, directly or indirectly, use, disclose, distribute,
print, or copy any part of this message if you are not the intended
recipient.  CREDIT SUISSE GROUP and each of its subsidiaries reserve the
right to intercept and monitor e-mail communications through its networks if
legally allowed.
Message transmission is not guaranteed to be secure.

--------------------------------------------------------

First Approach:

import java.awt.event.MouseEvent;

import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCComponent;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCMenuItem;
import com.ulcjava.base.application.ULCPopupMenu;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.util.Color;
import com.ulcjava.base.client.UIButton;
import com.ulcjava.base.client.UIPopupMenu;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.server.ULCSession;
import com.ulcjava.base.shared.internal.Anything;

public class ULCPopupMenuGetInvokerSnippet1 extends AbstractApplication {
        private ULCMenuItem items[];

        private ULCFrame frame;

        private Color colorValues[] = { Color.blue, Color.yellow, Color.red };

        public void start() {
                final ULCPopupMenu popupMenu = new ULCPopupMenu();

                ItemHandler handler = new ItemHandler();
                items = new ULCMenuItem[3];
                for (int i = 0; i < items.length; i++) {
                        items[i] = new ULCMenuItem(String.valueOf(i));
                        popupMenu.add(items[i]);
                        items[i].addActionListener(handler);
                }

                ULCBoxPane box = new ULCBoxPane(true);
                box.setVerticalGap(10);
                ULCMyButton but1 = new ULCMyButton("Button 1");
                but1.setName("Button1");
                but1.setComponentPopupMenu(popupMenu);
                box.add(but1);
                ULCMyButton but2 = new ULCMyButton("Button 2");
                but2.setName("Button2");
                but2.setComponentPopupMenu(popupMenu);
                box.add(but2);

                frame = new ULCFrame("StartExcelSnippet");
                frame.setContentPane(box);
                frame.getContentPane().setBackground(Color.white);
                frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);

                frame.setVisible(true);
        }

        public static void main(String[] args) {
                
DevelopmentRunner.setApplicationClass(ULCPopupMenuGetInvokerSnippet1.class
);
                DevelopmentRunner.main(args);
        }

        private class ItemHandler implements IActionListener {
                public void actionPerformed(ActionEvent e) {
                        // determine which menu item was selected
                        for (int i = 0; i < items.length; i++) {
                                if (e.getSource() == items[i]) {
                                        ULCMenuItem menuItem = (ULCMenuItem) 
e.getSource();
                                        ULCPopupMenu menu = (ULCPopupMenu) 
menuItem.getParent();
                                        frame.getContentPane().setOpaque(true);
                                        
frame.getContentPane().setBackground(colorValues[i]);
                                        System.out.println("Menu Action 
Performed");
                                        return;
                                }
                        }
                }
        }

        public static class ULCMyButton extends ULCButton {
                public ULCMyButton(String text) {
                        super(text);
                }

                protected String typeString() {
                        return UIMyButton.class.getName();
                }

                public void handleRequest(String request, Anything args) {
                        if (request.equals("PopupShown")) {
                                ULCComponent component = (ULCComponent)
ULCSession.currentSession().getRegistry().find(args.asInt(-1));
                                System.out.println("Menu popped on: " + 
component.getName());
                        } else {
                                super.handleRequest(request, args);
                        }
                }
        }

        public static class UIMyButton extends UIButton {
                protected void showPopupMenu(MouseEvent event) {
                        super.showPopupMenu(event);
                        sendULC("PopupShown", new Anything(getId()));
                }
        }
}

----------------------------------------

Second Approach:

import java.awt.event.MouseEvent;

import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCComponent;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCMenuItem;
import com.ulcjava.base.application.ULCPopupMenu;
import com.ulcjava.base.application.ULCProxy;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.util.Color;
import com.ulcjava.base.client.UIButton;
import com.ulcjava.base.client.UIPopupMenu;
import com.ulcjava.base.client.UIProxy;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.server.ULCSession;
import com.ulcjava.base.shared.internal.Anything;

public class ULCPopupMenuGetInvokerSnippet2 extends AbstractApplication {
        private ULCMenuItem items[];
        private ULCFrame frame;
        private Color colorValues[] = { Color.blue, Color.yellow, Color.red };

        public void start() {
                final MyULCPopupMenu popupMenu = new MyULCPopupMenu();

                ItemHandler handler = new ItemHandler();
                items = new ULCMenuItem[3];
                for (int i = 0; i < items.length; i++) {
                        items[i] = new ULCMenuItem(String.valueOf(i));
                        popupMenu.add(items[i]);
                        items[i].addActionListener(handler);
                }

                ULCBoxPane box = new ULCBoxPane(true);
                box.setVerticalGap(10);
                ULCMyButton but1 = new ULCMyButton("Button 1");
                but1.setName("Button1");
                but1.setComponentPopupMenu(popupMenu);
                box.add(but1);
                ULCMyButton but2 = new ULCMyButton("Button 2");
                but2.setName("Button2");
                but2.setComponentPopupMenu(popupMenu);
                box.add(but2);

                frame = new ULCFrame("StartExcelSnippet");
                frame.setContentPane(box);
                frame.getContentPane().setBackground(Color.white);
                frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);

                frame.setVisible(true);
        }

        public static void main(String[] args) {
                
DevelopmentRunner.setApplicationClass(ULCPopupMenuGetInvokerSnippet2.class
);
                DevelopmentRunner.main(args);
        }

        private class ItemHandler implements IActionListener {
                public void actionPerformed(ActionEvent e) {
                        for (int i = 0; i < items.length; i++) {
                                if (e.getSource() == items[i]) {

                                        // Get the invoker of ULCPopupMenu
                                        ULCMenuItem menuItem = (ULCMenuItem) 
e.getSource();
                                        MyULCPopupMenu menu = (MyULCPopupMenu) 
menuItem.getParent();
                                        ULCComponent comp = (ULCComponent) 
menu.getInvoker();
                                        System.out.println("Popup on: " + 
comp.getName());

                                        frame.getContentPane().setOpaque(true);
                                        
frame.getContentPane().setBackground(colorValues[i]);
                                        System.out.println("Menu Action 
Performed");
                                        return;
                                }
                        }
                }
        }

        // Extension for ULCButton

        public static class ULCMyButton extends ULCButton {
                public ULCMyButton(String text) {
                        super(text);
                }

                protected String typeString() {
                        return UIMyButton.class.getName();
                }
        }

        public static class UIMyButton extends UIButton {
                protected void showPopupMenu(MouseEvent event) {
                        super.showPopupMenu(event);
                        
((UIMyPopupMenu)getComponentPopupMenu()).setInvoker(this);
                }
        }

        // Extension for ULCPopupMenu

        public static class MyULCPopupMenu extends ULCPopupMenu {
                private ULCComponent invoker;

                public ULCComponent getInvoker() {
                        return invoker;
                }

                public void handleRequest(String request, Anything args) {
                        if (request.equals("Invoker")) {
                                invoker = (ULCComponent)
ULCSession.currentSession().getRegistry().find(args.asInt(-1));
                        } else {
                                super.handleRequest(request, args);
                        }
                }

                protected String typeString() {
                        return UIMyPopupMenu.class.getName();
                }
        }

        public static class UIMyPopupMenu extends UIPopupMenu {
                public void setInvoker(UIProxy proxy) {
                        sendULC("Invoker", new Anything(proxy.getId()));
                }
        }
}
---------------------------------

_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to