# 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.

"""
AddStepWindow is a dialog window used to add a step
in recipe edition
"""

__revision__ = "$Id: AddStepWindow.py,v 1.9 2001/11/15 15:43:30 alf Exp $"

from DialogWindow import *
from logilab.xmltools.XmlTree import XmlTree
from MemoryModel import MemoryModel
from narval.lib import *

class AddStepWindow(DialogWindow):
    """
    AddStepWindow class, parent class is DialogWindow
    """
    def __init__(self):
        DialogWindow.__init__(self,unicode_to_gtk(_("Add step")))
        self.connect("delete_event",self._exit)

        notebook = GtkNotebook()

        self.append_dialog(GtkLabel(unicode_to_gtk(_("Select an element : "))),expand=FALSE)

        label_selected = GtkLabel(unicode_to_gtk(_('No element selected.')))

        list = ('action','transform','recipe')
        self.list_name= {}
        self.list_key = []
        self.group_name = ""
        self.element_name = ""
        self.type_name = ""

        type_combo = GtkCombo()
        type_combo.show()
        type_combo.entry.set_editable(FALSE)
        self.append_dialog(type_combo)
        
        hbox = GtkHBox()
        hbox.set_border_width(5)
        
        group_clist = GtkCList(1,[unicode_to_gtk(_('Group'))])
        group_clist.set_selection_mode(SELECTION_SINGLE)
        group_clist.set_column_auto_resize(0,TRUE)
        hbox.pack_start(group_clist)

        name_clist = GtkCList(1,[unicode_to_gtk(_('Name'))])
        name_clist.set_selection_mode(SELECTION_SINGLE)
        name_clist.set_column_auto_resize(0,TRUE)
        hbox.pack_end(name_clist)

        name_clist.connect('select-row',self._name_row_selected, label_selected)
        group_clist.connect('select-row',self._group_row_selected, name_clist)
        type_combo.entry.connect('changed',self._type_changed, group_clist, name_clist)
        type_combo.set_popdown_strings(list)

        self.append_dialog(hbox)
        
        label_selected.show()
        self.append_dialog(label_selected,expand=FALSE)
        
        self.ok_button.show()
        self.ok_button.set_flags(CAN_DEFAULT|HAS_DEFAULT)

        self.cancel_button.show()
        self.cancel_button.connect_object("clicked",lambda x:x.destroy(),self)

    ### PRIVATE ###

    def _exit(self,*args):
        self.destroy()

    def _group_row_selected(self,*args):
        args[4].clear()
        self.group_name = self.list_key[args[0].__getattr__("selection")[0]]
        for data in self.list_name[self.group_name]:
            args[4].append((str(data),))

        args[4].select_row(0,0)

    def _name_row_selected(self,*args):
        self.element_name = self.list_name[self.group_name][args[0].__getattr__("selection")[0]]
        args[-1].set_text(unicode_to_gtk(self.group_name+'.'+self.element_name+_(' has been selected.')))
        args[-1].show()

    def _type_changed(self,*args):
        self.type_name = args[0].get_text()
        self.list_name = {}
        self.list_key = []
        args[1].clear()
        for element in MemoryModel().get_elements_list(self.type_name):
            group, name = element[2], element[3]
            if not self.list_name.has_key(group):
                args[1].append((str(group),))
                self.list_name[group]=[]
                self.list_key.append(group)
            
            self.list_name[group].append(name)
        args[1].select_row(0,0)
