1#------------------------------------------------------------------------------
2# Copyright (c) 2008, Riverbank Computing Limited
3# All rights reserved.
4#
5# This software is provided without warranty under the terms of the BSD
6# license included in enthought/LICENSE.txt and may be redistributed only
7# under the conditions described in the aforementioned license.  The license
8# is also available online at http://www.enthought.com/licenses/BSD.txt
9# Thanks for using Enthought open source!
10#
11# Author: Riverbank Computing Limited
12# Description: <Enthought permissions package component>
13#------------------------------------------------------------------------------
14
15
16# Enthought library imports.
17from traits.api import HasTraits, Instance, List, Unicode
18from traitsui.api import Item, TableEditor, View
19from traitsui.menu import OKCancelButtons
20from traitsui.table_column import ObjectColumn
21
22
23class _User(HasTraits):
24    """This represents the user model."""
25
26    #### '_User' interface ####################################################
27
28    # The user name.
29    name = Unicode
30
31    # The user description.
32    description = Unicode
33
34
35class _UsersView(HasTraits):
36    """This represents the view used to select a user."""
37
38    #### '_UsersView' interface ###############################################
39
40    # The list of users to select from.
41    model = List(_User)
42
43    # The selected user.
44    selection = Instance(_User)
45
46    # The editor used by the view.
47    table_editor = TableEditor(columns=[ObjectColumn(name='name'),
48                    ObjectColumn(name='description')],
49            selected='selection', sort_model=True, configurable=False)
50
51    # The default view.
52    traits_view = View(Item('model', show_label=False, editor=table_editor),
53            title="Select a User", style='readonly', kind='modal',
54            buttons=OKCancelButtons)
55
56
57def select_user(users):
58    """Return a single user from the given list of users."""
59
60    # Construct the model.
61    model = [_User(name=name, description=description)
62            for name, description in users]
63
64    # Construct the view.
65    view = _UsersView(model=model)
66
67    if view.configure_traits() and view.selection is not None:
68        user = view.selection.name, view.selection.description
69    else:
70        user = '', ''
71
72    return user
73