Top > OOoPython > ContextMenuInterceptor

* ContextMenuInterceptor [#rdbf45a9]

ContextMenuInterceptor を Python から利用しようとすると項目の作成時に利用するデフォルトの css.ui.ActionTrigger サービスのインスタンスにプロパティ値を設定できません。バインディングとリフレクションの相性が悪いようです。
右クリックしたときのメニューを編集する ContextMenuInterceptor を Python から利用しようとすると項目の作成時に利用するデフォルトの css.ui.ActionTrigger サービスのインスタンスにプロパティ値を設定できません。バインディングとリフレクションの相性が悪いようです。

次のようにして Python のオブジェクトを渡してやります。

#code(python){{

import uno
import unohelper

from com.sun.star.ui import XContextMenuInterceptor
from com.sun.star.ui.ContextMenuInterceptorAction import (
	IGNORED, CONTINUE_MODIFIED, EXECUTE_MODIFIED)
from com.sun.star.beans import XPropertySet
from com.sun.star.lang import XMultiServiceFactory, XServiceInfo
from com.sun.star.container import XIndexContainer, XContainer


class BaseServiceInfo(XServiceInfo):
	# XServiceInfo
	def getImplementationName(self):
		return self.IMPL_NAME
	def supportsService(self, name):
		return name == self.SERVICE_NAME
	def getSupportedServiceNames(self):
		return self.SERVICE_NAME,


class ActionTriggerContainer(unohelper.Base, BaseServiceInfo, XIndexContainer, XMultiServiceFactory):
	""" Provides container for items. """
	
	SERVICE_NAME = "com.sun.star.ui.ActionTriggerContainer"
	IMPL_NAME = "hoge"
	
	def __init__(self):
		self.items = []
	
	def append(self, item):
		self.items.append(item)
	
	# XMultiServiceFactory
	def createInstance(self, name):
		pass
	def createInstanceWithArguments(self, name, args):
		pass
	def getAvailableServiceNames(self):
		return ()
	
	# XIndexContainer
	def hasElements(self):
		return len(self.items) != 0
	def getElementType(self):
		return uno.Type("com.sun.star.beans.XPropertyValue")
	def getCount(self):
		return len(self.items)
	def getByIndex(self, n):
		if 0 <= n < len(self.items):
			return self.items[n]
		return None # should be raise IllegalArgumentException
	def replaceByIndex(self, n, item):
		if 0 <= n < len(self.items):
			self.items[n] = item
	def insertByIndex(self, n, item):
		if 0 <= n <= len(self.items):
			self.items.insert(n, item)
	def removeByIndex(self, n):
		if 0 <= n < len(self.items):
			self.items.pop(n)


class BasePropertySet(unohelper.Base, XPropertySet):
	# XPropertySet
	def addPropertyChangeListener(self, l): pass
	def removePropertyChangeListener(self, l): pass
	def addVetoableChangeListener(self, l): pass
	def removeVetoableChangeListener(self, l): pass
	def getPropertySetInfo(self): pass
	def setPropertyValue(self, name, value): pass
	def getPropertyValue(self, name): pass


class ActionTriggerItem(BasePropertySet, BaseServiceInfo, object):
	""" Provides item. """
	
	SERVICE_NAME = "com.sun.star.ui.ActionTrigger"
	IMPL_NAME = "hoge"
	
	def __init__(self, command_url, text, help_url, sub_menu=None):
		self.command_url = command_url
		self.text = text
		self.help_url = help_url
		self.sub_menu = sub_menu
	
	def getPropertyValue(self, name):
		if name == "CommandURL":
			return self.command_url
		elif name == "Text":
			return self.text
		elif name == "HelpURL":
			return self.help_url
		elif name == "SubContainer":
			return self.sub_menu
		else:
			return None
	

from com.sun.star.ui.ActionTriggerSeparatorType import LINE

class SheetTabContextMenuInterceptor(unohelper.Base, XContextMenuInterceptor):
	
	INSERT_SHEET = "slot:26269"
	
	def __init__(self, doc):
		self.doc = doc
	
	def notifyContextMenuExecute(self, ev):
		cont = ev.ActionTriggerContainer
		if cont.getByIndex(0).CommandURL == self.INSERT_SHEET:
			#item = cont.createInstance("com.sun.star.ui.ActionTriggerSeparator")
			#item.SeparatorType = LINE
			#cont.insertByIndex(8, item)
			items = ActionTriggerContainer()
			items.insertByIndex(0, ActionTriggerItem(
				".uno:SelectTables", "Sheet...", 
				"", None))
			
			item = ActionTriggerItem("GoTo", "Go to", "123", items)
			cont.insertByIndex(7, item)
			
			return EXECUTE_MODIFIED
		return IGNORED


def register(*args):
	#ctx = XSCRIPTCONTEXT.getComponentContext()
	doc = XSCRIPTCONTEXT.getDocument()
	controller = doc.getCurrentController()
	controller.registerContextMenuInterceptor(
		SheetTabContextMenuInterceptor(doc))

}}

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes