# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307
# USA.

"""
Tree View of the Recipe
"""

__revision__ = "$Id: RecipeTreeView.py,v 1.15 2002/01/04 14:13:05 syt Exp $"

from narval.lib import *
from os.path import normpath,join
from gtk import *
from logilab.xmltools.XmlEditor import *
from logilab.xmltools.XmlTree import *
from RecipeModel import RecipeModel
from xml.dom.ext import StripXml
from narval.gui import unicode_to_gtk
from NarvalDTD import NarvalDTD
from HornAbstraction import complete

TARGET_STRING = 1212

target = [
('STRING', 0, TARGET_STRING),
('text/plain', 0, TARGET_STRING)]

class RecipeTreeView(GtkVBox):
    """
    View of recipe using xmltree
    """

    def __init__(self,editable,controller):
        GtkVBox.__init__(self)
        ## no editor for the moment
        ## wait for update from RecipeModel
        self._editable = editable
        self._editor = None
        ## drag and drop connection
        self.drag_dest_set(DEST_DEFAULT_ALL,target,GDK.ACTION_COPY)
        self.connect('drag_data_received',controller.received_drag_data)
        
    def set_info_generator(self,ig):
        self._ig = ig

    def RecipeModel_update(self,observable,changes): 
        if self._editor is not None:
            self._editor.destroy()
            if not self._editable:
                self._scroll.destroy()   
        if not self._editable:
            self._editor = XmlTree()
            self._scroll = GtkScrolledWindow()
            self._scroll.set_policy(POLICY_AUTOMATIC,POLICY_AUTOMATIC)
            self._scroll.add(self._editor)
            self.pack_start(self._scroll)
            self._editor.set_info_generator(self._ig)
            self._editor.set_document(observable._recipe)
        else:
            self._editor = XmlEditor(NarvalDTD().dtd, observable._recipe)
            self._editor.tree.set_info_generator(self._ig)
            self.pack_start(self._editor)
        self.show_all()

    def tree_to_recipe(self):
        return complete(self._editor.node)

    def expand_node(self,node):
        if self._editable:
            nodes = self._editor.tree.base_nodes()
            tree = self._editor.tree
        else:
            nodes = self._editor.base_nodes()
            tree = self._editor

        try:
            n = select_one(tree.document,'*[@id="%s"]'%node.getAttributeNS(NO_NS,'id'))
        except SelectException:
            print unicode_to_gtk(_('node not found in document'))
            
        for b in nodes:
            tree_node = tree.find_by_row_data(b,n)
            if tree_node:
                tree.expand_recursive(tree_node)
                tree.select(tree_node)
                break
    
