File: actionmanager.py

package info (click to toggle)
gaphor 0.13.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,692 kB
  • ctags: 2,971
  • sloc: python: 19,981; xml: 247; makefile: 54; sh: 40
file content (72 lines) | stat: -rw-r--r-- 2,428 bytes parent folder | download | duplicates (2)
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
"""
"""

import gtk
from zope import interface, component
from gaphor.core import inject
from gaphor.interfaces import IService, IActionProvider
from gaphor.event import ServiceInitializedEvent, ActionExecuted


class ActionManager(object):
    """
    This service is responsible for maintaining actions.
    """

    interface.implements(IService)

    def __init__(self):
        pass

    def init(self, app):
        self._app = app
        self.ui_manager = gtk.UIManager()
        log.info('Loading not yet registered action provider services')
        for name, service in component.getUtilitiesFor(IService):
            if IActionProvider.providedBy(service):
                log.debug('Loading already registered service %s' % str(service))
                self.register_action_provider(service)

        app.register_handler(self._service_initialized_handler)

    def shutdown(self):
        self._app.unregister_handler(self._service_initialized_handler)

    def execute(self, action_id, active=None):
        a = self.get_action(action_id)
        if a:
            a.activate()
            self._app.handle(ActionExecuted(action_id, a))
        else:
            log.warning('Unknown action: %s' % action_id)

    def update_actions(self):
        self.ui_manager.ensure_update()

    def get_action(self, action_id):
        for g in self.ui_manager.get_action_groups():
            a = g.get_action(action_id)
            if a: return a

    def register_action_provider(self, action_provider):
        action_provider = IActionProvider(action_provider)
        try:
            # Check if the action provider is not already registered
            action_provider.__ui_merge_id
        except AttributeError:
            log.debug('Registering actions for %s' % str(action_provider))
            
            assert action_provider.action_group
            self.ui_manager.insert_action_group(action_provider.action_group, -1)
            if action_provider.menu_xml:
                action_provider.__ui_merge_id = \
                        self.ui_manager.add_ui_from_string(action_provider.menu_xml)

    @component.adapter(ServiceInitializedEvent)
    def _service_initialized_handler(self, event):
        if IActionProvider.providedBy(event.service):
            log.debug('Loading registered service %s' % str(event.service))
            self.register_action_provider(event.service)


# vim:sw=4:et:ai