-
Notifications
You must be signed in to change notification settings - Fork 26
/
project_view.py
437 lines (318 loc) · 11.8 KB
/
project_view.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# (C) Copyright 2007-2019 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in 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!
"""
The single project plugin's project view
"""
# Standard library imports.
import logging
# Enthought library imports
from apptools.naming.api import Binding
from traits.api import register_factory, Any, HasTraits, Instance, Str
from traitsui.api import (
Item,
Group,
TreeEditor,
ITreeNode,
ITreeNodeAdapter,
View,
)
# Application specific imports.
from envisage.api import IApplication
from envisage.ui.single_project.project import Project
from envisage.ui.single_project.services import IPROJECT_MODEL, IPROJECT_UI
# Setup a logger for this module.
logger = logging.getLogger(__name__)
# Dummy EmptyProject class for when the ProjectView doesn't have a reference
# to a Project.
class EmptyProject(Project):
pass
class EmptyProjectAdapter(ITreeNodeAdapter):
""" Adapter for our EmptyProject. """
# -- ITreeNodeAdapter Method Overrides --------------------------------------
def get_label(self):
""" Gets the label to display for a specified object.
"""
return "No project loaded."
register_factory(EmptyProjectAdapter, EmptyProject, ITreeNode)
class ProjectAdapter(ITreeNodeAdapter):
""" Base ProjectAdapter for the root of the tree. """
# -- ITreeNodeAdapter Method Overrides --------------------------------------
def allows_children(self):
""" Returns whether this object can have children.
"""
return False
def has_children(self):
""" Returns whether the object has children.
"""
return False
def get_children(self):
""" Gets the object's children.
"""
return []
def get_label(self):
""" Gets the label to display for a specified object.
"""
return self.adaptee.name
def get_tooltip(self):
""" Gets the tooltip to display for a specified object.
"""
return "Project"
def get_icon(self, is_expanded):
""" Returns the icon for a specified object.
"""
return "<open>"
def can_auto_close(self):
""" Returns whether the object's children should be automatically
closed.
"""
return True
register_factory(ProjectAdapter, Project, ITreeNode)
class ProjectView(HasTraits):
"""
The single project plugin's project view
"""
##########################################################################
# Traits
##########################################################################
#### public 'ProjectView' interface ######################################
# The Envisage application that this service is part of.
application = Instance(IApplication)
# The suffix currently applied to our name
name_suffix = Str("")
# The suffix applied to titles when the current project is dirty.
title_suffix = Str("*")
# Root node for the project.
root = Instance(Project)
# The traits view to display:
view = View(
Item(
"root",
editor=TreeEditor(editable=False, auto_open=1),
show_label=False,
),
resizable=True,
)
##########################################################################
# 'View' interface.
##########################################################################
def __init__(self, **traits):
super(ProjectView, self).__init__(**traits)
# Make sure our view stays in sync with the current project
model_service = self._get_model_service()
model_service.on_trait_change(self._on_project_changed, "project")
model_service.on_trait_change(
self._on_project_selection_changed, "selection"
)
# Make sure our control is initialized to the current project.
self._switch_projects(EmptyProject(), model_service.project)
##########################################################################
# 'ProjectView' interface.
##########################################################################
#### protected 'ProjectView' interface #################################
def _are_list_contents_different(self, list1, list2):
"""
Convenience method to determine if two lists contain different
items. Returns True if the lists are different and False
otherwise.
"""
set1 = set(list1)
set2 = set(list2)
return set1 != set2
def _clear_state(self):
"""
Clears out all indications of any project state from this view.
"""
# self.name_suffix = ''
# Set the root to an EmptyProject.
self.root = EmptyProject()
return
def _get_model_service(self):
"""
Return a reference to the single project plugin's model service.
"""
return self.application.get_service(IPROJECT_MODEL)
def _get_ui_service(self):
"""
Return a reference to the single project plugin's UI service.
"""
return self.window.application.get_service(IPROJECT_UI)
def _switch_projects(self, old, new):
"""
Switches this view to the specified new project from the old project.
Either value may be None.
"""
# Remove listeners on the old project, if any.
if old is not None and not isinstance(old, EmptyProject):
self._update_project_listeners(old, remove=True)
# Update our state according to what the new project is, making sure
# to add listeners to any new project.
if new is not None:
logger.debug("Changing ProjectView to Project [%s]", new.name)
self._sync_state_to_project(new)
self._update_project_listeners(new)
else:
logger.debug("Changing ProjectView to no project")
self._clear_state()
return
def _sync_state_to_project(self, project):
"""
Sync the state of this view to the specified project's state.
"""
logger.debug(
"Syncing ProjectView [%s] to project state [%s]", self, project
)
# Update our Project reference.
self.root = project
# Update our name suffix based on the dirty state of the project.
# self.name_suffix = (project.dirty and self.title_suffix) or ''
return
def _update_project_listeners(self, project, remove=False):
"""
Update listeners on the specified project instance.
If remove is False then listeners are added, else listeners
are removed.
"""
project.on_trait_change(self._on_project_name_changed, "name", remove)
project.on_trait_change(
self._on_project_dirty_changed, "dirty", remove
)
return
#### trait handlers ######################################################
def _name_suffix_changed(self, old, new):
"""
Handle changes to our name suffix.
"""
logger.debug(
"Detected change in name suffix to [%s] within "
+ "ProjectView [%s]",
new,
self,
)
# Update our own name by removing the old suffix, if any, and adding
# on the new suffix, if any.
name = self.name
if old is not None and len(old) > 0:
index = (" " + old).rfind(name)
if index > -1:
name = name[0:index]
if new is not None and len(new) > 0:
name = name + " " + new
self.name = name
# Update the containing window's suffix as well
self.window.title_suffix = new
return
def _on_control_right_clicked(self, event):
"""
Handle events when the tree control itself is right-clicked.
"""
logger.debug("ProjectView control right-clicked")
self._get_ui_service().display_default_context_menu(
self.control, event
)
return
def _on_key_pressed_changed(self, event):
"""
Handle key presses while our control has focus.
"""
logger.debug("ProjectView key pressed [%s]", event)
# If the delete key was pressed, then delete the current selected
# object.
if event.key_code == 127:
self._get_ui_service().delete_selection()
return
def _on_node_activated_changed(self, node):
"""
Handle nodes being activated (i.e. double-clicked.)
"""
logger.debug("ProjectView node activated [%s]", node)
return
def _on_project_changed(self, obj, trait_name, old, new):
"""
Handle when the current project changes.
"""
logger.debug("\n\n ***************** \n\n")
logger.debug(
"Detected project changed from [%s] to [%s] in "
"ProjectView [%s]",
old,
new,
self,
)
self._switch_projects(old, new)
return
def _on_project_dirty_changed(self, obj, trait_name, old, new):
"""
Handle the open project's dirty flag changing.
"""
logger.debug(
"Detected change in project dirty to [%s] within "
+ "ProjectView [%s]",
new,
self,
)
return
def _on_project_name_changed(self, obj, trait_name, old, new):
"""
Handle the open project's name changing.
"""
self._project_control.root.name = new
return
def _on_project_selection_changed(self, obj, trait_name, old, new):
"""
Handle the current project's selection changing.
"""
logger.debug(
"Detected project selection changed from [%s] "
+ "to [%s] within ProjectView [%s]",
old,
new,
self,
)
# Ensure that the Tree control's selection matches the selection in
# the project.
control_selection = self._project_control.selection
if self._are_list_contents_different(new, control_selection):
logger.debug(
" Updating selection on tree control for "
+ "ProjectView [%s]",
self,
)
self._project_control.set_selection(new)
# Ensure that this view's selection contains whatever was selected
# within the project.
if self._are_list_contents_different(new, self.selection):
logger.debug(" Updating selection on ProjectView [%s]", self)
self.selection = new
return
def _on_selection_changed(self, obj, trait_name, old, new):
"""
Handle selection changes in our tree control.
"""
logger.debug(
"Detected tree control selection change from [%s] "
+ "to [%s] within ProjectView [%s]",
old,
new,
self,
)
# Ensure that the project model service's record of selection contains
# the same elements as the tree control.
model_service = self._get_model_service()
if self._are_list_contents_different(new, model_service.selection):
logger.debug(" Updating selection on project model service")
model_service.selection = new
# Note that we don't have to update this view's selection record as
# we do that through our listener on the project selection.
return
def _on_closing_changed(self, old, new):
"""
Handle when this view closes.
"""
logger.debug("ProjectView [%s] closing!", self)
return