PopupMenu designer issues

We have some issues with creating a popup menu linked to a button click. The popup menu must meet the following requirements:

  • The menu must consist of 3 items and a submenu also with 3 items.
  • The color of the menu items and the submenu item must be changeable.
  • The font of the menu items and the submenu item must be changeable.
  • The menu items must be enabled or disabled based on external information (tags).
  • The cursor must change to a little hand for the enabled menu items; otherwise, the cursor remains an arrow.

Although we can implement the requirements individually based on previously received examples, we are struggling to combine all the requirements successfully.
Who can help us out with a working example?
Thanks in advance!

What have you tried so far? Extending half-working code is a lot more palatable for volunteer work than starting from scratch, especially given incomplete requirements (which tags, what is their data type, what should these menu items be and how should they relate to those tags, etc).

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'