Debian Codegraph @

envisage/ui/workbench/workbench_plugin.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 Envisage workbench plugin. """
11
12
13 from traits.api import Callable, List
14
15 # Enthought library imports.
16 from envisage.api import ExtensionPoint, Plugin, ServiceOffer
17
18 # This module's package.
19 PKG = ".".join(__name__.split(".")[:-1])
20
21
22 class WorkbenchPlugin(Plugin):
23 """The Envisage workbench plugin.
24
25 The workbench plugin uses the Pyface workbench to provide the basis of an
26 IDE-like user interface. The interface is made up of perspectives, views
27 and editors.
28
29 Note that this is not intended to be a 'general-purpose' plugin for user
30 interfaces - it provides an IDE-like style and that is all. If your
31 application requires another style of interface then write another plugin
32 (you can still re-use all the menu, group and action contribution stuff!).
33
34 """
35
36 # The Ids of the extension points that this plugin offers.
37 ACTION_SETS = PKG + ".action_sets"
38 PERSPECTIVES = PKG + ".perspectives"
39 PREFERENCES_PAGES = PKG + ".preferences_pages"
40 WORKBENCH_SERVICE_OFFERS = PKG + ".service_offers"
41 VIEWS = PKG + ".views"
42
43 # The Ids of the extension points that this plugin contributes to.
44 PREFERENCES = "envisage.preferences"
45 SERVICE_OFFERS = "envisage.service_offers"
46
47 #### 'IPlugin' interface ##################################################
48
49 # The plugin's unique identifier.
50 id = "envisage.ui.workbench"
51
52 # The plugin's name (suitable for displaying to the user).
53 name = "Workbench"
54
55 #### Extension points offered by this plugin ##############################
56
57 action_sets = ExtensionPoint(
58 List(Callable),
59 id=ACTION_SETS,
60 desc="""
61
62 An action set contains the toobars, menus, groups and actions that you
63 would like to add to top-level workbench windows (i.e. the main
64 application window). You can create new toolbars, menus and groups
65 and/or add to existing ones.
66
67 Each contribution to this extension point must be a factory that
68 creates an action set, where 'factory' means any callable with the
69 following signature::
70
71 callable(**traits) -> IActionSet
72
73 The easiest way to contribute such a factory is to create a class
74 that derives from 'envisage.ui.action.api.ActionSet'.
75
76 """,
77 )
78
79 perspectives = ExtensionPoint(
80 List(Callable),
81 id=PERSPECTIVES,
82 desc="""
83
84 A perspective is simply an arrangment of views around the (optionally
85 hidden) editor area.
86
87 Each contribution to this extension point must be a factory that
88 creates a perspective, where 'factory' means any callable with the
89 following signature::
90
91 callable(**traits) -> IPerspective
92
93 The easiest way to contribute such a factory is to create a class
94 that derives from 'pyface.workbench.api.IPerspective'.
95
96 """,
97 )
98
99 preferences_pages = ExtensionPoint(
100 List(Callable),
101 id=PREFERENCES_PAGES,
102 desc="""
103
104 A preferences page appears in the preferences dialog to allow the user
105 to manipulate some preference values.
106
107 Each contribution to this extension point must be a factory that
108 creates a preferences page, where 'factory' means any callable with the
109 following signature::
110
111 callable(**traits) -> IPreferencesPage
112
113 The easiest way to contribute such a factory is to create a class
114 that derives from 'apptools.preferences.ui.api.IPreferencesPage'.
115
116 """,
117 )
118
119 service_offers = ExtensionPoint(
120 List(ServiceOffer),
121 id=WORKBENCH_SERVICE_OFFERS,
122 desc="""
123
124 Services are simply objects that a plugin wants to make available to
125 other plugins. This extension point allows you to offer 'per
126 window' services that are created 'on-demand' (where 'on demand' means
127 the first time somebody looks up a service of the appropriate
128 protocol).
129 .
130
131 e.g.
132
133 my_service_offer = ServiceOffer(
134 protocol = 'acme.IMyService',
135 factory = an_object_or_a_callable_that_creates_one,
136 properties = {'a dictionary' : 'that is passed to the factory'}
137 )
138
139 Any properties specified are passed as keywrod arguments to the
140 factory, i.e. the factory signature is::
141
142 callable(**properties)
143
144 """,
145 )
146
147 views = ExtensionPoint(
148 List(Callable),
149 id=VIEWS,
150 desc="""
151
152 A view provides information to the user to support their current
153 task. Views can contain anything you like(!) and are arranged around
154 the (optionally hidden) editor area. The user can re-arrange views as
155 he/she sees fit.
156
157 Each contribution to this extension point must be a factory that
158 creates a view, where 'factory' means any callable with the following
159 signature::
160
161 callable(**traits) -> IView
162
163 The easiest way to contribute such a factory is to create a class
164 that derives from 'pyface.workbench.api.View'.
165
166 It is also common to use a simple function (especially when a view
167 is a representation of a service) e.g::
168
169 def foo_view_factory(**traits):
170 ' Create a view that is a representation of a service. '
171 foo = self.application.get_service('IFoo')
172
173 return FooView(foo=foo, **traits)
174
175 """,
176 )
177
178 #### Contributions to extension points made by this plugin ################
179
180 my_action_sets = List(contributes_to=ACTION_SETS)
181
182 def _my_action_sets_default(self):
183 """Trait initializer."""
184
185 from .default_action_set import DefaultActionSet
186
187 return [DefaultActionSet]
188
189 my_preferences = List(contributes_to=PREFERENCES)
190
191 def _my_preferences_default(self):
192 """Trait initializer."""
193
194 return ["pkgfile://envisage.ui.workbench/preferences.ini"]
195
196 my_preferences_pages = List(contributes_to=PREFERENCES_PAGES)
197
198 def _my_preferences_pages_default(self):
199 """Trait initializer."""
200
201 from .workbench_preferences_page import WorkbenchPreferencesPage
202
203 return [WorkbenchPreferencesPage]
204
205 my_service_offers = List(contributes_to=SERVICE_OFFERS)
206
207 def _my_service_offers_default(self):
208 """Trait initializer."""
209
210 preferences_manager_service_offer = ServiceOffer(
211 protocol="apptools.preferences.ui.preferences_manager"
212 ".PreferencesManager",
213 factory=self._create_preferences_manager_service,
214 )
215
216 workbench_service_offer = ServiceOffer(
217 protocol="envisage.ui.workbench.workbench.Workbench",
218 factory=self._create_workbench_service,
219 )
220
221 return [preferences_manager_service_offer, workbench_service_offer]
222
223 ###########################################################################
224 # Private interface.
225 ###########################################################################
226
227 def _create_preferences_manager_service(self, **properties):
228 """Factory method for the preferences manager service."""
229
230 from apptools.preferences.ui.api import PreferencesManager
231
232 preferences_manager = PreferencesManager(
233 pages=[factory() for factory in self.preferences_pages]
234 )
235
236 return preferences_manager
237
238 def _create_workbench_service(self, **properties):
239 """Factory method for the workbench service."""
240
241 # We don't actually create the workbench here, we just return a
242 # reference to it.
243 #
244 # fixme: This guard is really just for testing when we have the
245 # workbench plugin as a source egg (i.e. if the egg is on our path
246 # then we get the plugin for any egg-based application, even if it is
247 # not a workbench application!).
248 return getattr(self.application, "workbench", None)