| 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 |
""" An extensible workbench window. """ |
| 11 |
|
| 12 |
|
| 13 |
# Standard library imports. |
| 14 |
import logging |
| 15 |
|
| 16 |
# Enthought library imports. |
| 17 |
import pyface.workbench.api as pyface |
| 18 |
from pyface.action.api import StatusBarManager |
| 19 |
from traits.api import Delegate, Instance, List, Property, provides |
| 20 |
|
| 21 |
from envisage.api import ( |
| 22 |
ExtensionPoint, |
| 23 |
IExtensionPointUser, |
| 24 |
IExtensionRegistry, |
| 25 |
IServiceRegistry, |
| 26 |
ServiceRegistry, |
| 27 |
) |
| 28 |
from envisage.ui.action.api import ActionSet |
| 29 |
|
| 30 |
# Local imports. |
| 31 |
from .workbench_action_manager_builder import WorkbenchActionManagerBuilder |
| 32 |
from .workbench_editor_manager import WorkbenchEditorManager |
| 33 |
|
| 34 |
# Logging. |
| 35 |
logger = logging.getLogger(__name__) |
| 36 |
|
| 37 |
|
| 38 |
@provides(IServiceRegistry, IExtensionPointUser) |
| 39 |
class WorkbenchWindow(pyface.WorkbenchWindow): |
| 40 |
"""An extensible workbench window.""" |
| 41 |
|
| 42 |
# Extension point Ids. |
| 43 |
ACTION_SETS = "envisage.ui.workbench.action_sets" |
| 44 |
VIEWS = "envisage.ui.workbench.views" |
| 45 |
PERSPECTIVES = "envisage.ui.workbench.perspectives" |
| 46 |
SERVICE_OFFERS = "envisage.ui.workbench.service_offers" |
| 47 |
|
| 48 |
#### 'WorkbenchWindow' interface ########################################## |
| 49 |
|
| 50 |
# The application that the window is part of. |
| 51 |
# |
| 52 |
# This is equivalent to 'self.workbench.application', and is provided just |
| 53 |
# as a convenience since windows often want access to the application. |
| 54 |
application = Delegate("workbench", modify=True) |
| 55 |
|
| 56 |
# The action sets that provide the toolbars, menus groups and actions |
| 57 |
# used in the window. |
| 58 |
action_sets = List(Instance(ActionSet)) |
| 59 |
|
| 60 |
# The service registry for 'per window' services. |
| 61 |
service_registry = Instance(IServiceRegistry, factory=ServiceRegistry) |
| 62 |
|
| 63 |
#### 'IExtensionPointUser' interface ###################################### |
| 64 |
|
| 65 |
# The extension registry that the object's extension points are stored in. |
| 66 |
extension_registry = Property(Instance(IExtensionRegistry)) |
| 67 |
|
| 68 |
#### Private interface #################################################### |
| 69 |
|
| 70 |
# The workbench menu and tool bar builder. |
| 71 |
# |
| 72 |
# The builder is used to create the window's tool bar and menu bar by |
| 73 |
# combining all of the contributed action sets. |
| 74 |
_action_manager_builder = Instance(WorkbenchActionManagerBuilder) |
| 75 |
|
| 76 |
# Contributed action sets (each contribution is actually a factory). |
| 77 |
_action_sets = ExtensionPoint(id=ACTION_SETS) |
| 78 |
|
| 79 |
# Contributed views (each contribution is actually a factory). |
| 80 |
_views = ExtensionPoint(id=VIEWS) |
| 81 |
|
| 82 |
# Contributed perspectives (each contribution is actually a factory). |
| 83 |
_perspectives = ExtensionPoint(id=PERSPECTIVES) |
| 84 |
|
| 85 |
# Contributed service offers. |
| 86 |
_service_offers = ExtensionPoint(id=SERVICE_OFFERS) |
| 87 |
|
| 88 |
# The Ids of the services that were automatically registered. |
| 89 |
_service_ids = List |
| 90 |
|
| 91 |
########################################################################### |
| 92 |
# 'IExtensionPointUser' interface. |
| 93 |
########################################################################### |
| 94 |
|
| 95 |
def _get_extension_registry(self): |
| 96 |
"""Trait property getter.""" |
| 97 |
|
| 98 |
return self.application |
| 99 |
|
| 100 |
########################################################################### |
| 101 |
# 'pyface.Window' interface. |
| 102 |
########################################################################### |
| 103 |
|
| 104 |
#### Trait initializers ################################################### |
| 105 |
|
| 106 |
def _menu_bar_manager_default(self): |
| 107 |
"""Trait initializer.""" |
| 108 |
|
| 109 |
return self._action_manager_builder.create_menu_bar_manager("MenuBar") |
| 110 |
|
| 111 |
def _status_bar_manager_default(self): |
| 112 |
"""Trait initializer.""" |
| 113 |
|
| 114 |
return StatusBarManager() |
| 115 |
|
| 116 |
def _tool_bar_managers_default(self): |
| 117 |
"""Trait initializer.""" |
| 118 |
|
| 119 |
return self._action_manager_builder.create_tool_bar_managers("ToolBar") |
| 120 |
|
| 121 |
#### Trait change handlers ################################################ |
| 122 |
|
| 123 |
def _opening_changed(self): |
| 124 |
"""Static trait change handler.""" |
| 125 |
|
| 126 |
self._service_ids = self._register_service_offers(self._service_offers) |
| 127 |
|
| 128 |
def _closed_changed(self): |
| 129 |
"""Static trait change handler.""" |
| 130 |
|
| 131 |
self._unregister_service_offers(self._service_ids) |
| 132 |
|
| 133 |
########################################################################### |
| 134 |
# 'pyface.WorkbenchWindow' interface. |
| 135 |
########################################################################### |
| 136 |
|
| 137 |
#### Trait initializers ################################################### |
| 138 |
|
| 139 |
def _editor_manager_default(self): |
| 140 |
"""Trait initializer.""" |
| 141 |
|
| 142 |
return WorkbenchEditorManager(window=self) |
| 143 |
|
| 144 |
def _icon_default(self): |
| 145 |
"""Trait initializer.""" |
| 146 |
|
| 147 |
return self.workbench.application.icon |
| 148 |
|
| 149 |
def _perspectives_default(self): |
| 150 |
"""Trait initializer.""" |
| 151 |
|
| 152 |
return [factory() for factory in self._perspectives] |
| 153 |
|
| 154 |
def _title_default(self): |
| 155 |
"""Trait initializer.""" |
| 156 |
|
| 157 |
return self.workbench.application.name |
| 158 |
|
| 159 |
def _views_default(self): |
| 160 |
"""Trait initializer.""" |
| 161 |
|
| 162 |
return [factory(window=self) for factory in self._views] |
| 163 |
|
| 164 |
########################################################################### |
| 165 |
# 'WorkbenchWindow' interface. |
| 166 |
########################################################################### |
| 167 |
|
| 168 |
def _action_sets_default(self): |
| 169 |
"""Trait initializer.""" |
| 170 |
|
| 171 |
return [factory(window=self) for factory in self._action_sets] |
| 172 |
|
| 173 |
########################################################################### |
| 174 |
# 'IServiceRegistry' interface. |
| 175 |
########################################################################### |
| 176 |
|
| 177 |
def get_service(self, protocol, query="", minimize="", maximize=""): |
| 178 |
"""Return at most one service that matches the specified query.""" |
| 179 |
|
| 180 |
service = self.service_registry.get_service( |
| 181 |
protocol, query, minimize, maximize |
| 182 |
) |
| 183 |
|
| 184 |
return service |
| 185 |
|
| 186 |
def get_service_properties(self, service_id): |
| 187 |
"""Return the dictionary of properties associated with a service.""" |
| 188 |
|
| 189 |
return self.service_registry.get_service_properties(service_id) |
| 190 |
|
| 191 |
def get_services(self, protocol, query="", minimize="", maximize=""): |
| 192 |
"""Return all services that match the specified query.""" |
| 193 |
|
| 194 |
services = self.service_registry.get_services( |
| 195 |
protocol, query, minimize, maximize |
| 196 |
) |
| 197 |
|
| 198 |
return services |
| 199 |
|
| 200 |
def register_service(self, protocol, obj, properties=None): |
| 201 |
"""Register a service.""" |
| 202 |
|
| 203 |
service_id = self.service_registry.register_service( |
| 204 |
protocol, obj, properties |
| 205 |
) |
| 206 |
|
| 207 |
return service_id |
| 208 |
|
| 209 |
def set_service_properties(self, service_id, properties): |
| 210 |
"""Set the dictionary of properties associated with a service.""" |
| 211 |
|
| 212 |
self.service_registry.set_service_properties(service_id, properties) |
| 213 |
|
| 214 |
def unregister_service(self, service_id): |
| 215 |
"""Unregister a service.""" |
| 216 |
|
| 217 |
self.service_registry.unregister_service(service_id) |
| 218 |
|
| 219 |
########################################################################### |
| 220 |
# Private interface. |
| 221 |
########################################################################### |
| 222 |
|
| 223 |
def __action_manager_builder_default(self): |
| 224 |
"""Trait initializer.""" |
| 225 |
|
| 226 |
action_manager_builder = WorkbenchActionManagerBuilder( |
| 227 |
window=self, action_sets=self.action_sets |
| 228 |
) |
| 229 |
|
| 230 |
return action_manager_builder |
| 231 |
|
| 232 |
def _register_service_offers(self, service_offers): |
| 233 |
"""Register all service offers.""" |
| 234 |
|
| 235 |
return list(map(self._register_service_offer, service_offers)) |
| 236 |
|
| 237 |
def _register_service_offer(self, service_offer): |
| 238 |
"""Register a service offer.""" |
| 239 |
|
| 240 |
# Add the window to the service offer properties (this is so that it |
| 241 |
# is available to the factory when it is called to create the actual |
| 242 |
# service). |
| 243 |
service_offer.properties["window"] = self |
| 244 |
|
| 245 |
service_id = self.register_service( |
| 246 |
protocol=service_offer.protocol, |
| 247 |
obj=service_offer.factory, |
| 248 |
properties=service_offer.properties, |
| 249 |
) |
| 250 |
|
| 251 |
return service_id |
| 252 |
|
| 253 |
def _unregister_service_offers(self, service_ids): |
| 254 |
"""Unregister all service offers.""" |
| 255 |
|
| 256 |
# Unregister the services in the reverse order that we registered |
| 257 |
# them. |
| 258 |
service_ids_copy = service_ids[:] |
| 259 |
service_ids_copy.reverse() |
| 260 |
|
| 261 |
for service_id in service_ids_copy: |
| 262 |
self.unregister_service(service_id) |