Top > OOoPython > ContextMenuInterceptor

ContextMenuInterceptor Edit

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

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

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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