home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / externaltools / __init__.py next >
Encoding:
Python Source  |  2009-04-14  |  7.5 KB  |  208 lines

  1. # -*- coding: UTF-8 -*-
  2. #    Gedit External Tools plugin
  3. #    Copyright (C) 2005-2006  Steve Fr√©cinaux <steve@istique.net>
  4. #
  5. #    This program is free software; you can redistribute it and/or modify
  6. #    it under the terms of the GNU General Public License as published by
  7. #    the Free Software Foundation; either version 2 of the License, or
  8. #    (at your option) any later version.
  9. #
  10. #    This program is distributed in the hope that it will be useful,
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #    GNU General Public License for more details.
  14. #
  15. #    You should have received a copy of the GNU General Public License
  16. #    along with this program; if not, write to the Free Software
  17. #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18.  
  19. __all__ = ('ExternalToolsPlugin', 'ExternalToolsWindowHelper',
  20.            'Manager', 'OutputPanel', 'Capture', 'UniqueById')
  21.  
  22. import gedit
  23. import gtk
  24. from manager import Manager
  25. from library import ToolLibrary
  26. from outputpanel import OutputPanel
  27. from capture import Capture
  28. from functions import *
  29.  
  30. class ToolMenu(object):
  31.     ACTION_HANDLER_DATA_KEY = "ExternalToolActionHandlerData"
  32.     ACTION_ITEM_DATA_KEY = "ExternalToolActionItemData"
  33.  
  34.     def __init__(self, library, window, menupath):
  35.         super(ToolMenu, self).__init__()
  36.         self._library = library
  37.         self._window = window
  38.         self._menupath = menupath
  39.  
  40.         self._merge_id = 0
  41.         self._action_group = gtk.ActionGroup("ExternalToolsPluginToolActions")
  42.         self._window.get_ui_manager().insert_action_group(self._action_group, -1)
  43.         self.update()
  44.  
  45.     def deactivate(self):
  46.         self.remove()
  47.         self._window.get_ui_manager().remove_action_group(self._action_group)
  48.  
  49.     def remove(self):
  50.         if self._merge_id != 0:
  51.             self._window.get_ui_manager().remove_ui(self._merge_id)
  52.             self._merge_id = 0
  53.  
  54.         for action in self._action_group.list_actions():
  55.             handler = action.get_data(self.ACTION_HANDLER_DATA_KEY)
  56.             if handler is not None:
  57.                 action.disconnect(handler)
  58.             self._action_group.remove_action(action)
  59.  
  60.     def _insert_directory(self, directory, path):
  61.         manager = self._window.get_ui_manager()
  62.  
  63.         for item in directory.subdirs:
  64.             action_name = 'ExternalToolDirectory%X' % id(item)
  65.             action = gtk.Action(action_name, item.name, None, None)
  66.             self._action_group.add_action(action)
  67.  
  68.             manager.add_ui(self._merge_id, path,
  69.                            action_name, action_name,
  70.                            gtk.UI_MANAGER_MENU, False)
  71.             self._insert_directory(item, path + '/' + action_name)
  72.  
  73.         for item in directory.tools:
  74.             action_name = 'ExternalToolTool%X' % id(item)
  75.             action = gtk.Action(action_name, item.name, item.comment, None)
  76.             handler = action.connect("activate", capture_menu_action, self._window, item)
  77.  
  78.             action.set_data(self.ACTION_ITEM_DATA_KEY, item)
  79.             action.set_data(self.ACTION_HANDLER_DATA_KEY, handler)
  80.             self._action_group.add_action_with_accel(action, item.shortcut)
  81.  
  82.             manager.add_ui(self._merge_id, path,
  83.                            action_name, action_name,
  84.                            gtk.UI_MANAGER_MENUITEM, False)
  85.  
  86.     def update(self):
  87.         self.remove()
  88.         self._merge_id = self._window.get_ui_manager().new_merge_id()
  89.         self._insert_directory(self._library.tree, self._menupath)
  90.         self.filter(self._window.get_active_document())
  91.  
  92.     def filter(self, document):
  93.         if document is None:
  94.             return
  95.  
  96.         titled = document.get_uri() is not None
  97.         remote = not document.is_local()
  98.  
  99.         states = {
  100.             'all' : True,
  101.             'local': titled and not remote,
  102.             'remote': titled and remote,
  103.             'titled': titled,
  104.             'untitled': not titled,
  105.         }
  106.  
  107.         for action in self._action_group.list_actions():
  108.             item = action.get_data(self.ACTION_ITEM_DATA_KEY)
  109.             if item is not None:
  110.                 action.set_sensitive(states[item.applicability])
  111.  
  112. class ExternalToolsWindowHelper(object):
  113.     def __init__(self, plugin, window):
  114.         super(ExternalToolsWindowHelper, self).__init__()
  115.  
  116.         self._window = window
  117.         self._plugin = plugin
  118.         self._library = ToolLibrary()
  119.  
  120.         manager = window.get_ui_manager()
  121.  
  122.         self._action_group = gtk.ActionGroup('ExternalToolsPluginActions')
  123.         self._action_group.set_translation_domain('gedit')
  124.         self._action_group.add_actions([('ExternalToolManager',
  125.                                          None,
  126.                                          _('_External Tools...'),
  127.                                          None,
  128.                                          _("Opens the External Tools Manager"),
  129.                                          lambda action: plugin.open_dialog())])
  130.         manager.insert_action_group(self._action_group, -1)
  131.  
  132.         ui_string = """
  133.             <ui>
  134.               <menubar name="MenuBar">
  135.                 <menu name="ToolsMenu" action="Tools">
  136.                   <placeholder name="ToolsOps_4">
  137.                     <separator/>
  138.                     <placeholder name="ExternalToolPlaceholder"/>
  139.                     <separator/>
  140.                   </placeholder>
  141.                   <placeholder name="ToolsOps_5">
  142.                     <menuitem name="ExternalToolManager" action="ExternalToolManager"/>
  143.                   </placeholder>
  144.                 </menu>
  145.               </menubar>
  146.             </ui>"""
  147.  
  148.         self._merge_id = manager.add_ui_from_string(ui_string)
  149.  
  150.         self.menu = ToolMenu(self._library, self._window,
  151.                              "/MenuBar/ToolsMenu/ToolsOps_4/ExternalToolPlaceholder")
  152.         manager.ensure_update()
  153.  
  154.         # Create output console
  155.         self._output_buffer = OutputPanel(self._plugin.get_data_dir(), window)
  156.         bottom = window.get_bottom_panel()
  157.         bottom.add_item(self._output_buffer.panel,
  158.                         _("Shell Output"),
  159.                         gtk.STOCK_EXECUTE)
  160.  
  161.     def update_ui(self):
  162.         self.menu.filter(self._window.get_active_document())
  163.         self._window.get_ui_manager().ensure_update()
  164.  
  165.     def deactivate(self):
  166.         manager = self._window.get_ui_manager()
  167.         self.menu.deactivate()
  168.         manager.remove_ui(self._merge_id)
  169.         manager.remove_action_group(self._action_group)
  170.         manager.ensure_update()
  171.  
  172.         bottom = self._window.get_bottom_panel()
  173.         bottom.remove_item(self._output_buffer.panel)
  174.  
  175. class ExternalToolsPlugin(gedit.Plugin):
  176.     WINDOW_DATA_KEY = "ExternalToolsPluginWindowData"
  177.  
  178.     def __init__(self):
  179.         super(ExternalToolsPlugin, self).__init__()
  180.         
  181.         ToolLibrary().set_locations(self.get_data_dir())
  182.  
  183.     def activate(self, window):
  184.         helper = ExternalToolsWindowHelper(self, window)
  185.         window.set_data(self.WINDOW_DATA_KEY, helper)
  186.  
  187.     def deactivate(self, window):
  188.         window.get_data(self.WINDOW_DATA_KEY).deactivate()
  189.         window.set_data(self.WINDOW_DATA_KEY, None)
  190.  
  191.     def update_ui(self, window):
  192.         window.get_data(self.WINDOW_DATA_KEY).update_ui()
  193.  
  194.     def create_configure_dialog(self):
  195.         return self.open_dialog()
  196.  
  197.     def open_dialog(self):
  198.         m = Manager(self.get_data_dir())
  199.  
  200.         m.run()
  201.         window = gedit.app_get_default().get_active_window()
  202.         if window:
  203.             m.dialog.set_transient_for(window)
  204.  
  205.         return m.dialog
  206.  
  207. # ex:ts=4:et:
  208.