<s> from atom . api import Typed <EOL> from enaml . application import Application <EOL> from enaml . workbench . extension import Extension <EOL> from enaml . workbench . plugin import Plugin <EOL> from . action_item import ActionItem <EOL> from . autostart import Autostart <EOL> from . branding import Branding <EOL> from . menu_helper import create_menus <EOL> from . menu_item import MenuItem <EOL> from . window_model import WindowModel <EOL> from . workspace import Workspace <EOL> import enaml <EOL> with enaml . imports ( ) : <EOL> from . workbench_window import WorkbenchWindow <EOL> ACTIONS_POINT = u'<STR_LIT>' <EOL> APPLICATION_FACTORY_POINT = u'<STR_LIT>' <EOL> BRANDING_POINT = u'<STR_LIT>' <EOL> WINDOW_FACTORY_POINT = u'<STR_LIT>' <EOL> WORKSPACES_POINT = u'<STR_LIT>' <EOL> AUTOSTART_POINT = u'<STR_LIT>' <EOL> class UIPlugin ( Plugin ) : <EOL> """<STR_LIT>""" <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _create_application ( ) <EOL> self . _create_model ( ) <EOL> self . _create_window ( ) <EOL> self . _bind_observers ( ) <EOL> self . _start_autostarts ( ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _stop_autostarts ( ) <EOL> self . _unbind_observers ( ) <EOL> self . _destroy_window ( ) <EOL> self . _release_model ( ) <EOL> self . _release_application ( ) <EOL> @ property <EOL> def window ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _window <EOL> @ property <EOL> def workspace ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _model . workspace <EOL> def show_window ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _window . show ( ) <EOL> def hide_window ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _window . hide ( ) <EOL> def start_application ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _application . start ( ) <EOL> def stop_application ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _application . stop ( ) <EOL> def close_window ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _window . close ( ) <EOL> def close_workspace ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _workspace_extension = None <EOL> self . _model . workspace . stop ( ) <EOL> self . _model . workspace . workbench = None <EOL> self . _model . workspace = Workspace ( ) <EOL> def select_workspace ( self , extension_id ) : <EOL> """<STR_LIT>""" <EOL> target = None <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( WORKSPACES_POINT ) <EOL> for extension in point . extensions : <EOL> if extension . qualified_id == extension_id : <EOL> target = extension <EOL> break <EOL> if target is None : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % extension_id ) <EOL> if target is self . _workspace_extension : <EOL> return <EOL> old_workspace = self . _model . workspace <EOL> old_workspace . stop ( ) <EOL> old_workspace . workbench = None <EOL> self . _workspace_extension = target <EOL> new_workspace = self . _create_workspace ( target ) <EOL> new_workspace . workbench = workbench <EOL> new_workspace . start ( ) <EOL> self . _model . workspace = new_workspace <EOL> _application = Typed ( Application ) <EOL> _window = Typed ( WorkbenchWindow ) <EOL> _model = Typed ( WindowModel ) <EOL> _branding_extension = Typed ( Extension ) <EOL> _workspace_extension = Typed ( Extension ) <EOL> _action_extensions = Typed ( dict , ( ) ) <EOL> def _create_application ( self ) : <EOL> """<STR_LIT>""" <EOL> if Application . instance ( ) is not None : <EOL> self . _application = Application . instance ( ) <EOL> return <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( APPLICATION_FACTORY_POINT ) <EOL> extensions = point . extensions <EOL> if not extensions : <EOL> msg = "<STR_LIT>" <EOL> raise RuntimeError ( msg % APPLICATION_FACTORY_POINT ) <EOL> extension = extensions [ - <NUM_LIT:1> ] <EOL> if extension . factory is None : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % extension . qualified_id ) <EOL> application = extension . factory ( ) <EOL> if not isinstance ( application , Application ) : <EOL> msg = "<STR_LIT>" <EOL> args = ( extension . qualified_id , type ( application ) . __name__ ) <EOL> raise TypeError ( msg % args ) <EOL> self . _application = application <EOL> def _create_model ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _model = WindowModel ( ) <EOL> self . _refresh_branding ( ) <EOL> self . _refresh_actions ( ) <EOL> def _create_window ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( WINDOW_FACTORY_POINT ) <EOL> extensions = point . extensions <EOL> if not extensions : <EOL> msg = "<STR_LIT>" <EOL> raise RuntimeError ( msg % WINDOW_FACTORY_POINT ) <EOL> extension = extensions [ - <NUM_LIT:1> ] <EOL> if extension . factory is None : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % extension . qualified_id ) <EOL> window = extension . factory ( workbench ) <EOL> if not isinstance ( window , WorkbenchWindow ) : <EOL> msg = "<STR_LIT>" <EOL> args = ( extension . qualified_id , type ( window ) . __name__ ) <EOL> raise TypeError ( msg % args ) <EOL> window . workbench = workbench <EOL> window . window_model = self . _model <EOL> self . _window = window <EOL> def _create_workspace ( self , extension ) : <EOL> """<STR_LIT>""" <EOL> if extension . factory is None : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % extension . qualified_id ) <EOL> workspace = extension . factory ( self . workbench ) <EOL> if not isinstance ( workspace , Workspace ) : <EOL> msg = "<STR_LIT>" <EOL> args = ( extension . qualified_id , type ( workspace ) . __name__ ) <EOL> raise TypeError ( msg % args ) <EOL> return workspace <EOL> def _create_action_items ( self , extension ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> menu_items = extension . get_children ( MenuItem ) <EOL> action_items = extension . get_children ( ActionItem ) <EOL> if extension . factory : <EOL> for item in extension . factory ( workbench ) : <EOL> if isinstance ( item , MenuItem ) : <EOL> menu_items . append ( item ) <EOL> elif isinstance ( item , ActionItem ) : <EOL> action_items . append ( item ) <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> raise TypeError ( msg % type ( item ) . __name__ ) <EOL> return menu_items , action_items <EOL> def _destroy_window ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _window . hide ( ) <EOL> self . _window . destroy ( ) <EOL> self . _window = None <EOL> def _release_model ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _model . workspace . stop ( ) <EOL> self . _model = None <EOL> def _release_application ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _application . stop ( ) <EOL> self . _application = None <EOL> def _refresh_branding ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( BRANDING_POINT ) <EOL> extensions = point . extensions <EOL> if not extensions : <EOL> self . _branding_extension = None <EOL> self . _model . branding = Branding ( ) <EOL> return <EOL> extension = extensions [ - <NUM_LIT:1> ] <EOL> if extension is self . _branding_extension : <EOL> return <EOL> if extension . factory : <EOL> branding = extension . factory ( workbench ) <EOL> if not isinstance ( branding , Branding ) : <EOL> msg = "<STR_LIT>" <EOL> args = ( extension . qualified_id , type ( branding ) . __name__ ) <EOL> raise TypeError ( msg % args ) <EOL> else : <EOL> branding = extension . get_child ( Branding , reverse = True ) <EOL> branding = branding or Branding ( ) <EOL> self . _branding_extension = extension <EOL> self . _model . branding = branding <EOL> def _refresh_actions ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( ACTIONS_POINT ) <EOL> extensions = point . extensions <EOL> if not extensions : <EOL> self . _action_extensions . clear ( ) <EOL> self . _model . menus = [ ] <EOL> return <EOL> menu_items = [ ] <EOL> action_items = [ ] <EOL> new_extensions = { } <EOL> old_extensions = self . _action_extensions <EOL> for extension in extensions : <EOL> if extension in old_extensions : <EOL> m_items , a_items = old_extensions [ extension ] <EOL> else : <EOL> m_items , a_items = self . _create_action_items ( extension ) <EOL> new_extensions [ extension ] = ( m_items , a_items ) <EOL> menu_items . extend ( m_items ) <EOL> action_items . extend ( a_items ) <EOL> menus = create_menus ( workbench , menu_items , action_items ) <EOL> self . _action_extensions = new_extensions <EOL> self . _model . menus = menus <EOL> def _get_autostarts ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( AUTOSTART_POINT ) <EOL> extensions = sorted ( point . extensions , key = lambda ext : ext . rank ) <EOL> autostarts = [ ] <EOL> for extension in extensions : <EOL> autostarts . extend ( extension . get_children ( Autostart ) ) <EOL> return autostarts <EOL> def _start_autostarts ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> for autostart in self . _get_autostarts ( ) : <EOL> workbench . get_plugin ( autostart . plugin_id ) <EOL> def _stop_autostarts ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> for autostart in reversed ( self . _get_autostarts ( ) ) : <EOL> plugin = workbench . get_plugin ( autostart . plugin_id ) <EOL> plugin . stop ( ) <EOL> def _on_branding_updated ( self , change ) : <EOL> """<STR_LIT>""" <EOL> self . _refresh_branding ( ) <EOL> def _on_actions_updated ( self , change ) : <EOL> """<STR_LIT>""" <EOL> self . _refresh_actions ( ) <EOL> def _bind_observers ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( BRANDING_POINT ) <EOL> point . observe ( '<STR_LIT>' , self . _on_branding_updated ) <EOL> point = workbench . get_extension_point ( ACTIONS_POINT ) <EOL> point . observe ( '<STR_LIT>' , self . _on_actions_updated ) <EOL> def _unbind_observers ( self ) : <EOL> """<STR_LIT>""" <EOL> workbench = self . workbench <EOL> point = workbench . get_extension_point ( BRANDING_POINT ) <EOL> point . unobserve ( '<STR_LIT>' , self . _on_branding_updated ) <EOL> point = workbench . get_extension_point ( ACTIONS_POINT ) <EOL> point . unobserve ( '<STR_LIT>' , self . _on_actions_updated ) </s>
