<s> import logging <EOL> from pyface . action . api import MenuBarManager , StatusBarManager , ToolBarManager <EOL> from pyface . api import ApplicationWindow <EOL> from traits . api import Bool , Callable , HasTraits , HasStrictTraits , Instance , List , Property , Unicode , Vetoable , on_trait_change <EOL> from pyface . tasks . action . task_action_manager_builder import TaskActionManagerBuilder <EOL> from pyface . tasks . i_dock_pane import IDockPane <EOL> from pyface . tasks . i_task_pane import ITaskPane <EOL> from pyface . tasks . task import Task , TaskLayout <EOL> from pyface . tasks . task_window_backend import TaskWindowBackend <EOL> from pyface . tasks . task_window_layout import TaskWindowLayout <EOL> logger = logging . getLogger ( __name__ ) <EOL> class TaskWindow ( ApplicationWindow ) : <EOL> """<STR_LIT>""" <EOL> title = Property ( Unicode , depends_on = '<STR_LIT>' ) <EOL> active_pane = Instance ( ITaskPane ) <EOL> active_task = Instance ( Task ) <EOL> tasks = List ( Task ) <EOL> central_pane = Instance ( ITaskPane ) <EOL> dock_panes = List ( IDockPane ) <EOL> action_manager_builder_factory = Callable ( TaskActionManagerBuilder ) <EOL> _active_state = Instance ( '<STR_LIT>' ) <EOL> _states = List ( Instance ( '<STR_LIT>' ) ) <EOL> _title = Unicode <EOL> _window_backend = Instance ( TaskWindowBackend ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _window_backend . destroy ( ) <EOL> for state in self . _states : <EOL> self . _destroy_state ( state ) <EOL> super ( TaskWindow , self ) . destroy ( ) <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> self . opening = event = Vetoable ( ) <EOL> if not event . veto : <EOL> if self . control is None : <EOL> self . _create ( ) <EOL> if self . _active_state is None and self . _states : <EOL> self . activate_task ( self . _states [ <NUM_LIT:0> ] . task ) <EOL> self . show ( True ) <EOL> self . opened = self <EOL> return self . control is not None <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . control is not None : <EOL> self . closing = event = Vetoable ( ) <EOL> if not event . veto : <EOL> self . destroy ( ) <EOL> self . closed = self <EOL> return self . control is None <EOL> def _create_contents ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> return self . _window_backend . create_contents ( parent ) <EOL> def activate_task ( self , task ) : <EOL> """<STR_LIT>""" <EOL> state = self . _get_state ( task ) <EOL> if state and state != self . _active_state : <EOL> if self . _active_state is not None : <EOL> self . _window_backend . hide_task ( self . _active_state ) <EOL> if not state . initialized : <EOL> task . initialized ( ) <EOL> state . initialized = True <EOL> self . _window_backend . show_task ( state ) <EOL> self . _active_state = state <EOL> task . activated ( ) <EOL> elif not state : <EOL> logger . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % task ) <EOL> def add_task ( self , task ) : <EOL> """<STR_LIT>""" <EOL> if task . window is not None : <EOL> logger . error ( "<STR_LIT>" <EOL> "<STR_LIT>" % task ) <EOL> return <EOL> task . window = self <EOL> state = TaskState ( task = task , layout = task . default_layout ) <EOL> self . _states . append ( state ) <EOL> if self . control is None : <EOL> self . _create ( ) <EOL> state . central_pane = task . create_central_pane ( ) <EOL> state . central_pane . task = task <EOL> state . central_pane . create ( self . control ) <EOL> state . dock_panes = task . create_dock_panes ( ) <EOL> for dock_pane_factory in task . extra_dock_pane_factories : <EOL> state . dock_panes . append ( dock_pane_factory ( task = task ) ) <EOL> for dock_pane in state . dock_panes : <EOL> dock_pane . task = task <EOL> dock_pane . create ( self . control ) <EOL> builder = self . action_manager_builder_factory ( task = task ) <EOL> state . menu_bar_manager = builder . create_menu_bar_manager ( ) <EOL> state . status_bar_manager = task . status_bar <EOL> state . tool_bar_managers = builder . create_tool_bar_managers ( ) <EOL> def remove_task ( self , task ) : <EOL> """<STR_LIT>""" <EOL> state = self . _get_state ( task ) <EOL> if state : <EOL> if self . _active_state == state : <EOL> self . _window_backend . hide_task ( state ) <EOL> self . _active_state = None <EOL> self . _destroy_state ( state ) <EOL> self . _states . remove ( state ) <EOL> else : <EOL> logger . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % task ) <EOL> def focus_next_pane ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _active_state : <EOL> panes = self . _get_pane_ring ( ) <EOL> index = <NUM_LIT:0> <EOL> if self . active_pane : <EOL> index = ( panes . index ( self . active_pane ) + <NUM_LIT:1> ) % len ( panes ) <EOL> panes [ index ] . set_focus ( ) <EOL> def focus_previous_pane ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _active_state : <EOL> panes = self . _get_pane_ring ( ) <EOL> index = - <NUM_LIT:1> <EOL> if self . active_pane : <EOL> index = panes . index ( self . active_pane ) - <NUM_LIT:1> <EOL> panes [ index ] . set_focus ( ) <EOL> def get_central_pane ( self , task ) : <EOL> """<STR_LIT>""" <EOL> state = self . _get_state ( task ) <EOL> return state . central_pane if state else None <EOL> def get_dock_pane ( self , id , task = None ) : <EOL> """<STR_LIT>""" <EOL> if task is None : <EOL> state = self . _active_state <EOL> else : <EOL> state = self . _get_state ( task ) <EOL> return state . get_dock_pane ( id ) if state else None <EOL> def get_dock_panes ( self , task ) : <EOL> """<STR_LIT>""" <EOL> state = self . _get_state ( task ) <EOL> return state . dock_panes [ : ] if state else [ ] <EOL> def get_task ( self , id ) : <EOL> """<STR_LIT>""" <EOL> state = self . _get_state ( id ) <EOL> return state . task if state else None <EOL> def get_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _active_state : <EOL> return self . _window_backend . get_layout ( ) <EOL> return None <EOL> def set_layout ( self , layout ) : <EOL> """<STR_LIT>""" <EOL> if self . _active_state : <EOL> self . _window_backend . set_layout ( layout ) <EOL> def reset_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . active_task : <EOL> self . set_layout ( self . active_task . default_layout ) <EOL> def get_window_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> result = TaskWindowLayout ( position = self . position , size = self . size , <EOL> size_state = self . size_state ) <EOL> for state in self . _states : <EOL> if state == self . _active_state : <EOL> result . active_task = state . task . id <EOL> layout = self . _window_backend . get_layout ( ) <EOL> else : <EOL> layout = state . layout . clone_traits ( ) <EOL> layout . id = state . task . id <EOL> result . items . append ( layout ) <EOL> return result <EOL> def set_window_layout ( self , window_layout ) : <EOL> """<STR_LIT>""" <EOL> self . position = window_layout . position <EOL> self . size = window_layout . size <EOL> self . size_state = window_layout . size_state <EOL> for layout in window_layout . items : <EOL> if isinstance ( layout , basestring ) : <EOL> continue <EOL> state = self . _get_state ( layout . id ) <EOL> if state : <EOL> state . layout = layout <EOL> else : <EOL> logger . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % layout . id ) <EOL> state = self . _get_state ( window_layout . get_active_task ( ) ) <EOL> if state : <EOL> if state == self . _active_state : <EOL> self . _window_backend . set_layout ( state . layout ) <EOL> else : <EOL> self . activate_task ( state . task ) <EOL> def _destroy_state ( self , state ) : <EOL> """<STR_LIT>""" <EOL> state . task . prepare_destroy ( ) <EOL> if state != self . _active_state : <EOL> if state . menu_bar_manager : <EOL> state . menu_bar_manager . destroy ( ) <EOL> for tool_bar_manager in state . tool_bar_managers : <EOL> tool_bar_manager . destroy ( ) <EOL> for dock_pane in state . dock_panes : <EOL> dock_pane . destroy ( ) <EOL> state . central_pane . destroy ( ) <EOL> state . task . window = None <EOL> def _get_pane_ring ( self ) : <EOL> """<STR_LIT>""" <EOL> panes = [ ] <EOL> if self . _active_state : <EOL> layout = self . get_layout ( ) <EOL> panes . append ( self . central_pane ) <EOL> for area in ( '<STR_LIT>' , '<STR_LIT:right>' , '<STR_LIT>' , '<STR_LIT:left>' ) : <EOL> item = getattr ( layout , area ) <EOL> if item : <EOL> panes . extend ( [ self . get_dock_pane ( pane_item . id ) <EOL> for pane_item in item . iterleaves ( ) ] ) <EOL> return panes <EOL> def _get_state ( self , id_or_task ) : <EOL> """<STR_LIT>""" <EOL> for state in self . _states : <EOL> if state . task == id_or_task or state . task . id == id_or_task : <EOL> return state <EOL> return None <EOL> def __window_backend_default ( self ) : <EOL> return TaskWindowBackend ( window = self ) <EOL> def _get_title ( self ) : <EOL> if self . _title or self . active_task is None : <EOL> return self . _title <EOL> return self . active_task . name <EOL> def _set_title ( self , title ) : <EOL> self . _title = title <EOL> def __active_state_changed ( self , state ) : <EOL> if state is None : <EOL> self . active_task = self . central_pane = None <EOL> self . dock_panes = [ ] <EOL> self . menu_bar_manager = self . status_bar_manager = None <EOL> self . tool_bar_managers = [ ] <EOL> else : <EOL> self . active_task = state . task <EOL> self . central_pane = state . central_pane <EOL> self . dock_panes = state . dock_panes <EOL> self . menu_bar_manager = state . menu_bar_manager <EOL> self . status_bar_manager = state . status_bar_manager <EOL> self . tool_bar_managers = state . tool_bar_managers <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _focus_updated ( self , obj , name , old , new ) : <EOL> if name == '<STR_LIT>' and new : <EOL> self . active_pane = obj <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _states_updated ( self ) : <EOL> self . tasks = [ state . task for state in self . _states ] <EOL> class TaskState ( HasStrictTraits ) : <EOL> """<STR_LIT>""" <EOL> task = Instance ( Task ) <EOL> layout = Instance ( TaskLayout ) <EOL> initialized = Bool ( False ) <EOL> central_pane = Instance ( ITaskPane ) <EOL> dock_panes = List ( IDockPane ) <EOL> menu_bar_manager = Instance ( MenuBarManager ) <EOL> status_bar_manager = Instance ( StatusBarManager ) <EOL> tool_bar_managers = List ( ToolBarManager ) <EOL> def get_dock_pane ( self , id ) : <EOL> """<STR_LIT>""" <EOL> for pane in self . dock_panes : <EOL> if pane . id == id : <EOL> return pane <EOL> return None </s>
