File: context_monitor.py

package info (click to toggle)
python-apptools 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,652 kB
  • sloc: python: 16,657; makefile: 77
file content (110 lines) | stat: -rw-r--r-- 3,630 bytes parent folder | download | duplicates (5)
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
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought naming package component>
#------------------------------------------------------------------------------
""" A monitor that detects changes to a naming context. """


# Enthought library imports.
from pyface.tree.api import NodeMonitor


class ContextMonitor(NodeMonitor):
    """ A monitor that detects changes to a naming context. """

    ###########################################################################
    # 'NodeMonitor' interface.
    ###########################################################################

    def start(self):
        """ Start listening to changes to the object. """

        context = self.node.obj

        context.on_trait_change(self._on_object_added, 'object_added')
        context.on_trait_change(self._on_object_changed, 'object_changed')
        context.on_trait_change(self._on_object_removed, 'object_removed')
        context.on_trait_change(self._on_object_renamed, 'object_renamed')
        context.on_trait_change(self._on_context_changed, 'context_changed')

        return

    def stop(self):
        """ Stop listening to changes to the object. """

        context = self.node.obj

        context.on_trait_change(
            self._on_object_added, 'object_added', remove=True
        )

        context.on_trait_change(
            self._on_object_changed, 'object_changed', remove=True
        )

        context.on_trait_change(
            self._on_object_removed, 'object_removed', remove=True
        )

        context.on_trait_change(
            self._on_object_renamed, 'object_renamed', remove=True
        )

        context.on_trait_change(
            self._on_context_changed, 'context_changed', remove=True
        )

        return

    ###########################################################################
    # Private interface.
    ###########################################################################

    #### Trait event handlers #################################################

    def _on_object_added(self, event):
        """ Called when an object has been added to the context. """

        self.fire_nodes_inserted([event.new_binding])

        return

    def _on_object_changed(self, event):
        """ Called when an object in the context has been changed. """

        # fixme: Can we get enough information to fire a 'nodes_replaced'
        # event?  Something a little more granular than this!
        self.fire_structure_changed()

        return

    def _on_object_removed(self, event):
        """ Called when an object has been removed from the context. """

        self.fire_nodes_removed([event.old_binding])

        return

    def _on_object_renamed(self, event):
        """ Called when an object has been renamed in the context. """

        self.fire_nodes_replaced([event.old_binding], [event.new_binding])

        return

    def _on_context_changed(self, event):
        """ Called when a context has changed dramatically. """
        self.fire_structure_changed()

        return

##### EOF #####################################################################