Debian Codegraph @

envisage/ui/workbench/workbench_action_manager_builder.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ The action manager builder used to build the workbench menu/tool bars. """
11
12
13 # Standard library imports.
14 import weakref
15
16 from pyface.action.api import Action, Group, MenuManager
17 from pyface.workbench.action.api import MenuBarManager, ToolBarManager
18 from traits.api import Any, Instance
19
20 # Enthought library imports.
21 from envisage.ui.action.api import AbstractActionManagerBuilder
22
23
24 class WorkbenchActionManagerBuilder(AbstractActionManagerBuilder):
25 """
26 The action manager builder used to build the workbench menu/tool bars.
27 """
28
29 #### 'WorkbenchActionManagerBuilder' interface ############################
30
31 # The workbench window that we build the menu and tool bars for.
32 window = Instance("envisage.ui.workbench.api.WorkbenchWindow")
33
34 #### Private interface ####################################################
35
36 # All action implementations.
37 _actions = Any
38
39 ###########################################################################
40 # Protected 'AbstractActionManagerBuilder' interface.
41 ###########################################################################
42
43 def _create_action(self, definition):
44 """Create an action implementation from an action definition."""
45
46 traits = {"window": self.window}
47
48 # Override any traits that can be set in the definition.
49 if len(definition.name) > 0:
50 traits["name"] = definition.name
51
52 if len(definition.class_name) > 0:
53 action = self._actions.get(definition.class_name)
54 if action is None:
55 klass = self._import_symbol(definition.class_name)
56 action = klass(**traits)
57 self._actions[definition.class_name] = action
58
59 # fixme: Do we ever actually do this? It seems that in Envisage 3.x
60 # we always specify an action class!?!
61 else:
62 action = Action(**traits)
63
64 # fixme: We need to associate the action set with the action to
65 # allow for dynamic enabling/disabling etc. This is a *very* hacky
66 # way to do it!
67 action._action_set_ = definition._action_set_
68
69 return action
70
71 def _create_group(self, definition):
72 """Create a group implementation from a group definition."""
73
74 traits = {}
75
76 # Override any traits that can be set in the definition.
77 if len(definition.id) > 0:
78 traits["id"] = definition.id
79
80 if len(definition.class_name) > 0:
81 klass = self._import_symbol(definition.class_name)
82
83 else:
84 klass = Group
85
86 group = klass(**traits)
87
88 # fixme: We need to associate the action set with the action to
89 # allow for dynamic enabling/disabling etc. This is a *very* hacky
90 # way to do it!
91 group._action_set_ = definition._action_set_
92
93 return group
94
95 def _create_menu_manager(self, definition):
96 """Create a menu manager implementation from a menu definition."""
97
98 # fixme: 'window' is not actually a trait on 'MenuManager'! We set
99 # it here to allow the 'View' menu to be created. However, it seems
100 # that menus and actions etc should *always* have a reference to
101 # the window that they are in?!?
102 traits = {"window": self.window}
103
104 # Override any traits that can be set in the definition.
105 if len(definition.id) > 0:
106 traits["id"] = definition.id
107
108 if len(definition.name) > 0:
109 traits["name"] = definition.name
110
111 if len(definition.class_name) > 0:
112 klass = self._import_symbol(definition.class_name)
113
114 else:
115 klass = MenuManager
116
117 menu_manager = klass(**traits)
118
119 # Add any groups to the menu.
120 for group in definition.groups:
121 group._action_set_ = definition._action_set_
122 menu_manager.insert(-1, self._create_group(group))
123
124 # fixme: We need to associate the action set with the action to
125 # allow for dynamic enabling/disabling etc. This is a *very* hacky
126 # way to do it!
127 menu_manager._action_set_ = definition._action_set_
128
129 return menu_manager
130
131 def _create_menu_bar_manager(self):
132 """Create a menu bar manager from the builder's action sets."""
133
134 return MenuBarManager(window=self.window)
135
136 def _create_tool_bar_manager(self, definition):
137 """Create a tool bar manager implementation from a definition."""
138
139 traits = {"window": self.window, "show_tool_names": False}
140
141 # Override any traits that can be set in the definition.
142 if len(definition.id) > 0:
143 traits["id"] = definition.id
144
145 if len(definition.name) > 0:
146 traits["name"] = definition.name
147
148 if len(definition.class_name) > 0:
149 klass = self._import_symbol(definition.class_name)
150 else:
151 klass = ToolBarManager
152
153 # fixme: 'window' is not actually a trait on 'ToolBarManager'! We
154 # set it here because it is set on the 'MenuManager'! However, it
155 # seems that menus and actions etc should *always* have a reference
156 # to the window that they are in?!?
157 tool_bar_manager = klass(**traits)
158
159 # Add any groups to the tool bar.
160 for group in definition.groups:
161 group._action_set_ = definition._action_set_
162 tool_bar_manager.insert(-1, self._create_group(group))
163
164 # fixme: We need to associate the action set with the action to
165 # allow for dynamic enabling/disabling etc. This is a *very* hacky
166 # way to do it!
167 tool_bar_manager._action_set_ = definition._action_set_
168
169 return tool_bar_manager
170
171 ###########################################################################
172 # Private interface.
173 ###########################################################################
174
175 def __actions_default(self):
176 """Trait initializer."""
177
178 return weakref.WeakValueDictionary()
179
180 def _import_symbol(self, symbol_path):
181 """Import a symbol."""
182
183 return self.window.application.import_symbol(symbol_path)