1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#-----------------------------------------------------------------------------
#
# Copyright (c) 2006-2007 by Enthought, Inc.
# All rights reserved.
#
# Author: Dave Peterson <dpeterson@enthought.com>
#
#-----------------------------------------------------------------------------
"""
A runnable that restores the last opened project.
"""
# Standard library imports
import logging
# Enthought library imports.
from enthought.envisage import Runnable
from enthought.envisage.workbench.services import IWORKBENCH
from enthought.pyface.api import information
# Application imports
from services import IPROJECT_MODEL, IPROJECT_UI
# Setup a logger for this module.
logger = logging.getLogger(__name__)
class ProjectRunnable(Runnable):
"""
A runnable that restores the last opened project.
"""
##########################################################################
# 'Runnable' interface.
##########################################################################
#### public interface ####################################################
def run(self, application):
"""
Run this runnable.
Overridden here to: (a) ensure the UI service monitors for the
closing of the application, and (b) restore the last opened
project.
"""
# Ensure our UI service is listening for the application to close.
# FIXME: This ugly hack (doing this here) is necessary only because
# this plugin contributes to the workbench plugin and that means the
# workbench insists on starting us first which means our UI service
# can't directly reference the workbench service until after
# everything has been started.
ui_service = application.get_service(IPROJECT_UI)
ui_service.listen_for_application_exit()
# Load the project we were using when we last shutdown.
model_service = application.get_service(IPROJECT_MODEL)
location = model_service.preferences.get('project location',
default=None)
if location and len(location) > 0:
logger.info("Opening last project from location [%s]", location)
try:
project = model_service.factory.open(location)
except:
logger.exception('Error during opening of last project.')
project = None
if project is not None:
model_service.project = project
else:
information(self._get_parent_window(application),
'Unable to open last project from location:\t\n'
'\t%s\n' % (location) + '\n\n'
'The project may no longer exist.',
'Can Not Open Last Project',
)
else:
logger.info('No previous project to open')
return
#### protected interface ################################################
def _get_parent_window(self, application):
"""
Find and return a reference to the application window.
If one can not be found, then 'None' is returned.
"""
window = None
try:
workbench = application.get_service(IWORKBENCH)
window = workbench.active_window.control
except:
logger.warn('Unable to retrieve application window reference.')
return window
#### EOF ######################################################################
|