1"""
2Displaying a list of objects in notebook tabs
3
4A list of objects can be displayed in a tabbed notebook, one object per tab.
5
6This example also shows how the currently active notebook tab of a ListEditor
7can be controlled using the ListEditor's 'selected' trait.
8
9Note the interaction between the spinner control (for the 'index' trait) and
10the currently selected notebook tab. Try changing the spinner value, then try
11clicking on various notebook tabs.
12
13Finally, note that the ListEditor will automatically scroll the tabs to make
14the selected tab completely visible.
15"""
16
17# The following text was removed from the module docstring (only works in wx):
18#  Also note that rearranging the notebook tabs (using drag and drop) does not
19#  affect the correspondence between the index value and its associated notebook
20#  tab. The correspondence is determined by the contents of the 'people' trait,
21#  and not by the physical layout of the notebook tabs.
22
23
24from traits.api import HasStrictTraits, Str, Int, Regex, List, Instance, Range
25from traitsui.api import View, VGroup, Item, ListEditor
26
27#-- Person Class ---------------------------------------------------------
28
29
30class Person(HasStrictTraits):
31
32    # Trait definitions:
33    name = Str()
34    age = Int()
35    phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d')
36
37    # Traits view definition:
38    traits_view = View('name', 'age', 'phone',
39                       width=0.18,
40                       buttons=['OK', 'Cancel'])
41
42#-- Sample Data ----------------------------------------------------------
43
44people = [
45    Person(name='Dave Chomsky', age=39, phone='555-1212'),
46    Person(name='Mike Wakowski', age=28, phone='555-3526'),
47    Person(name='Joe Higginbotham', age=34, phone='555-6943'),
48    Person(name='Tom Derringer', age=22, phone='555-7586'),
49    Person(name='Dick Van Der Hooten', age=63, phone='555-3895'),
50    Person(name='Harry McCallum', age=46, phone='555-3285'),
51    Person(name='Sally Johnson', age=43, phone='555-8797'),
52    Person(name='Fields Timberlawn', age=31, phone='555-3547')
53]
54
55#-- ListEditorNotebookSelectionDemo Class --------------------------------
56
57
58class ListEditorNotebookSelectionDemo(HasStrictTraits):
59
60    #-- Trait Definitions ----------------------------------------------------
61
62    # List of people:
63    people = List(Person)
64
65    # The currently selected person:
66    selected = Instance(Person)
67
68    # The index of the currently selected person:
69    index = Range(0, 7, mode='spinner')
70
71    #-- Traits View Definitions ----------------------------------------------
72
73    traits_view = View(
74        Item('index'),
75        '_',
76        VGroup(
77            Item('people',
78                 id='notebook',
79                 show_label=False,
80                 style='custom',
81                 editor=ListEditor(use_notebook=True,
82                                   deletable=False,
83                                   selected='selected',
84                                   export='DockWindowShell',
85                                   page_name='.name')
86                 )
87        ),
88        id='traitsui.demo.Traits UI Demo.Advanced.'
89        'List_editor_notebook_selection_demo',
90        dock='horizontal')
91
92    #-- Trait Event Handlers -------------------------------------------------
93
94    def _selected_changed(self, selected):
95        self.index = self.people.index(selected)
96
97    def _index_changed(self, index):
98        self.selected = self.people[index]
99
100#-- Set Up The Demo ------------------------------------------------------
101
102demo = ListEditorNotebookSelectionDemo(people=people)
103
104if __name__ == "__main__":
105    demo.configure_traits()
106