The script that we have so far:
# Using the mouseReleased event handler
# Create a listner that reapplies the selected property of all JySubMenu items
# ...after the BasicMenuUI listener has done all of its required work
from java.awt.event import MouseAdapter
from java.awt import Font
class NonDeselectingListener(MouseAdapter):
def __init__(self, originalListener):
# print '__init__'
self.originalListener = originalListener
def mouseEntered(self, event):
# print 'mouseEntered'
self.originalListener.mouseEntered(event)
self.restoreSelections(event)
def mouseExited(self, event):
# print 'mouseExited'
self.originalListener.mouseExited(event)
if 'JySubMenu' in event.source.__class__.__name__:
event.source.background = subMenuBGColor
else:
event.source.background = menuItemBGColor
def mouseReleased(self, event):
# print 'mouseReleased'
self.originalListener.mouseReleased(event)
def restoreSelections(self, event):
# print 'restoreSelection'
for component in event.source.parent.components:
if 'JySubMenu' in component.__class__.__name__:
component.selected = True # zorgt ervoor dat submenu wordt geopend
else:
component.background = menuItemBGColor
# Recursively sets the background and foreground colors of all the sub components of a popup menu
def setMenuColor(menu, background, foreground):
# print 'setMenuColor'
menu.background = background
menu.foreground = foreground
for component in menu.components:
# If a submenu is found, color its popup and set it selected property to true
if 'JySubMenu' in component.__class__.__name__:
setMenuColor(component, subMenuBGColor, foregroundColor)
setMenuColor(component.popupMenu, subMenuBGColor, foregroundColor)
component.selected = True
else:
setMenuColor(component, menuItemBGColor, foregroundColor)
# component.enabled=False # place to disable menuitem?
# Locate the Basic UI listener of the menu item
# ...and wrap in a listener that listener that will restore the selected coloring
# ...before the component is repainted
for listener in component.mouseListeners:
if 'BasicMenuUI' in listener.__class__.__name__ or 'BasicMenuItemUI' in listener.__class__.__name__:
component.removeMouseListener(listener)
component.addMouseListener(NonDeselectingListener(listener))
def dummy(event):
print 'Dummy'
def setMenuCursor(menu, cursor):
# Set the current menu item's cursor property to the given cursor
menu.cursor = cursor
# Iterate through all the menu item's sub componentns, and apply the cursor
for component in menu.components:
setMenuCursor(component, cursor)
# If a submenu is found, process the submenu items as well
if 'JySubMenu' in component.__class__.__name__:
if component.popupMenu.enabled:
setMenuCursor(component.popupMenu, cursor)
###################################################################################
subMenuBGColor = system.gui.color(220, 220, 220)
# Match color with selected color
menuItemBGColor = subMenuBGColor
foregroundColor = system.gui.color('black')
Submenu=[
['Sub 1', 'Sub 2', 'Sub 3'],
[dummy, dummy, dummy]
]
SubEnabled=[False, True, True]
menu = system.gui.createPopupMenu(
['Main 1', 'Main 2', 'Main 3', 'Devider', 'Sub'],
[dummy, dummy, dummy, None, Submenu]
)
MainEnabled=[True, True, True, None, True]
setMenuColor(menu, menuItemBGColor, foregroundColor)
menu.focusable = False
originalCursor = menu.cursor
newCursor = originalCursor.getPredefinedCursor(originalCursor.HAND_CURSOR)
setMenuCursor(menu, newCursor)
menu.show(event)
print 'end'