| 1 |
# (C) Copyright 2004-2023 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 |
|
| 11 |
""" |
| 12 |
Demonstrates an alternative method of defining a **TreeEditor** by creating |
| 13 |
**ITreeNodeAdapter** subclasses. |
| 14 |
|
| 15 |
To run this demonstration successfully, you must have **AppTools** |
| 16 |
(``apptools``) installed. |
| 17 |
|
| 18 |
Using **ITreeNodeAdapters** can be useful in cases where the kind of content |
| 19 |
of the tree is not always known ahead of time. For example, you might be |
| 20 |
creating a reusable tool or component which can display its data in a tree |
| 21 |
view, but you do not know what kind of data it will be asked to display when |
| 22 |
you write the code. Therefore, it may be impossible for you to specify a |
| 23 |
**TreeEditor** with a correct set of **TreeNode** objects that will work |
| 24 |
in all possible future cases. |
| 25 |
|
| 26 |
Using **ITreeNodeAdapter** subclasses, you can allow the clients of your code |
| 27 |
to solve this problem by providing one of more **ITreeNodeAdapters** that |
| 28 |
can be used to provide the correct tree node information for each type of data |
| 29 |
that will appear in the **TreeEditor** view. |
| 30 |
|
| 31 |
In this demo, we define an **ITreeNodeAdapter** subclass that adapts the |
| 32 |
*apptools.io.file.File* class to be displayed in a file explorer style |
| 33 |
tree view. |
| 34 |
""" |
| 35 |
|
| 36 |
# -- Imports -------------------------------------------------------------- |
| 37 |
|
| 38 |
from os import getcwd |
| 39 |
|
| 40 |
from traits.api import ( |
| 41 |
HasTraits, |
| 42 |
Property, |
| 43 |
Directory, |
| 44 |
register_factory, |
| 45 |
cached_property |
| 46 |
) |
| 47 |
|
| 48 |
from traitsui.api import ( |
| 49 |
View, |
| 50 |
VGroup, |
| 51 |
Item, |
| 52 |
TreeEditor, |
| 53 |
ITreeNode, |
| 54 |
ITreeNodeAdapter, |
| 55 |
) |
| 56 |
|
| 57 |
from apptools.io.api import File |
| 58 |
|
| 59 |
|
| 60 |
# -- FileAdapter Class ---------------------------------------------------- |
| 61 |
|
| 62 |
|
| 63 |
class FileAdapter(ITreeNodeAdapter): |
| 64 |
|
| 65 |
# -- ITreeNodeAdapter Method Overrides ------------------------------------ |
| 66 |
|
| 67 |
def allows_children(self): |
| 68 |
"""Returns whether this object can have children.""" |
| 69 |
return self.adaptee.is_folder |
| 70 |
|
| 71 |
def has_children(self): |
| 72 |
"""Returns whether the object has children.""" |
| 73 |
children = self.adaptee.children |
| 74 |
return (children is not None) and (len(children) > 0) |
| 75 |
|
| 76 |
def get_children(self): |
| 77 |
"""Gets the object's children.""" |
| 78 |
return self.adaptee.children |
| 79 |
|
| 80 |
def get_label(self): |
| 81 |
"""Gets the label to display for a specified object.""" |
| 82 |
return self.adaptee.name + self.adaptee.ext |
| 83 |
|
| 84 |
def get_tooltip(self): |
| 85 |
"""Gets the tooltip to display for a specified object.""" |
| 86 |
return self.adaptee.absolute_path |
| 87 |
|
| 88 |
def get_icon(self, is_expanded): |
| 89 |
"""Returns the icon for a specified object.""" |
| 90 |
if self.adaptee.is_file: |
| 91 |
return '<item>' |
| 92 |
|
| 93 |
if is_expanded: |
| 94 |
return '<open>' |
| 95 |
|
| 96 |
return '<open>' |
| 97 |
|
| 98 |
def can_auto_close(self): |
| 99 |
"""Returns whether the object's children should be automatically |
| 100 |
closed. |
| 101 |
""" |
| 102 |
return True |
| 103 |
|
| 104 |
|
| 105 |
# -- FileTreeDemo Class --------------------------------------------------- |
| 106 |
|
| 107 |
|
| 108 |
class FileTreeDemo(HasTraits): |
| 109 |
|
| 110 |
# The path to the file tree root: |
| 111 |
root_path = Directory(entries=10) |
| 112 |
|
| 113 |
# The root of the file tree: |
| 114 |
root = Property(observe='root_path') |
| 115 |
|
| 116 |
# The traits view to display: |
| 117 |
view = View( |
| 118 |
VGroup( |
| 119 |
Item('root_path'), |
| 120 |
Item('root', editor=TreeEditor(editable=False, auto_open=1)), |
| 121 |
show_labels=False, |
| 122 |
), |
| 123 |
width=0.33, |
| 124 |
height=0.50, |
| 125 |
resizable=True, |
| 126 |
) |
| 127 |
|
| 128 |
# -- Traits Default Value Methods ----------------------------------------- |
| 129 |
|
| 130 |
def _root_path_default(self): |
| 131 |
return getcwd() |
| 132 |
|
| 133 |
# -- Property Implementations --------------------------------------------- |
| 134 |
|
| 135 |
@cached_property |
| 136 |
def _get_root(self): |
| 137 |
return File(path=self.root_path) |
| 138 |
|
| 139 |
|
| 140 |
# -- Create and run the demo ---------------------------------------------- |
| 141 |
|
| 142 |
|
| 143 |
register_factory(FileAdapter, File, ITreeNode) |
| 144 |
demo = FileTreeDemo() |
| 145 |
|
| 146 |
# Run the demo (if invoked form the command line): |
| 147 |
if __name__ == '__main__': |
| 148 |
demo.configure_traits() |