class OptionDisplay(HIGTable):
    def __init__(self, option=None):
        HIGTable.__init__(self)
        self.create_and_attach_widgets()
        self.set_border_width(5)
        self.arg_type = None

    def create_and_attach_widgets(self):
        self.option_label = HIGSectionLabel('New Option')
        self.attach(self.option_label, 0, 3, 0, 1)

        self.name_label = HIGEntryLabel(_('Name:'))
        self.name_entry = HIGTextEntry()
        self.attach(self.name_label, 0, 1, 1, 2)
        self.attach(self.name_entry, 1, 3, 1, 2)

        self.hint_label = HIGEntryLabel(_('Hint:'))
        self.hint_entry = HIGTextEntry()
        self.attach(self.hint_label, 0, 1, 2, 3)
        self.attach(self.hint_entry, 1, 3, 2, 3)

        self.need_root = gtk.CheckButton(_('Need root'))
        self.attach(self.need_root, 0, 1, 3, 4)

        self.options_label = HIGEntryLabel(_('Options:'))
        hbox = HIGHBox()
        self.options_entry = HIGTextEntry()
        self.insert_arg_button = HIGButton(title='Args', stock='gtk-add')
        self.insert_arg_button.connect('clicked', self.update_args)
        self.attach(self.options_label, 0, 1, 4, 5)
        self.attach(self.options_entry, 1, 2, 4, 5)
        self.attach(self.insert_arg_button, 2, 3, 4, 5)

        self.aguments_label = HIGEntryLabel(_('Arguments:'))
        self.arguments_entry = HIGTextEntry()
        self.arguments_entry.set_editable(False)
        self.attach(self.aguments_label, 0, 1, 5, 6)
        self.attach(self.arguments_entry, 1, 3, 5, 6)

    def update_args(self, widget):
        '''
        Update aguments entry and option entry
        '''

        cursor_index = self.options_entry.get_position()
        text_entry = self.options_entry.get_text()
        arg_description, arg_key = self.dialog_args()
        if arg_key != None:
            #Update arguments

            self.arg_type = arg_key
            if text_entry.find('%s') == -1:
                left = text_entry[0:cursor_index]
                right = text_entry[cursor_index:len(text_entry)]
                final = left + "%s" + right
                self.options_entry.set_text(final)
            self.arguments_entry.set_text(arg_description)

    def dialog_args(self):
        '''
        Create a dialog
        '''

        d = HIGDialog(_('Arguments'))
        description_label = HIGEntryLabel(
            _('Insert the description to argument:'))
        description_label.show()
        description_entry = HIGTextEntry()
        text = self.arguments_entry.get_text()
        description_entry.set_text(text)
        description_entry.show()

        combo_box = gtk.combo_box_new_text()
        combo_box.set_wrap_width(1)
        index = -1
        j = 0
        for i in ARG_TYPES:
            combo_box.append_text(ARG_TYPES[i])
            if i == self.arg_type:
                index = j
            j = j + 1
        if index > -1:
            combo_box.set_active(index)
        combo_box.show()
        d.vbox.pack_start(description_label, False, False)
        d.vbox.pack_start(description_entry, False, False)
        d.vbox.pack_start(combo_box, False, False)
        d.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
        d.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
        resp = d.run()
        result = None, None
        if resp == gtk.RESPONSE_OK:
            model = combo_box.get_model()
            active = combo_box.get_active()
            if active < 0:
                return None, None
            combo_selected = model[active][0]

            for i in ARG_TYPES:
                if combo_selected == ARG_TYPES[i]:
                    combo_selected = i

            result = description_entry.get_text(), combo_selected
            #self.insert_arg_button.set_label('Edit args')
            #self.insert_arg_button.
        d.destroy()
        return result

    def clear(self):
        """ 
        Clear Option Display
        """
        self.option_label.set_new_text('New Option')
        self.name_entry.set_text('')
        self.hint_entry.set_text('')
        self.arguments_entry.set_text('')
        self.need_root.set_active(False)
        self.options_entry.set_text('')

    def set_option_list(self, list):
        """
        set option list from a dictionarie 

        @param list: Elements of a option
        @type list: Dictionarie with elements
        """
        self.clear()
        self.option_label.set_new_text(list['name'])
        self.name_entry.set_text(list['name'])
        self.hint_entry.set_text(list['hint'])

        for i in list['arguments']:
            i = self.arguments_entry.get_text() + i
            self.arguments_entry.set_text(i)

        self.options_entry.set_text(list['option'])
        self.need_root.set_active(list['need_root'])
        self.arg_type = list['arg_type']

    def set_option(self, name, hint, arguments, need_root, options, arg_type):
        """
        fill fields
        buggy arguments.
        """
        self.clear()
        self.options_entry.set_label(name)
        self.name_entry.set_text(name)
        self.hint_entry.set_text(hint)
        self.arguments_entry.set_text(arguments)
        self.need_root.set_active(need_root)
        self.arg_type = arg_type
class OptionDisplay(HIGTable):
    def __init__(self, option=None):
        HIGTable.__init__(self)
        self.create_and_attach_widgets()
        self.set_border_width(5)
        self.arg_type=None

    def create_and_attach_widgets(self):
        self.option_label = HIGSectionLabel('New Option')
        self.attach(self.option_label, 0, 3, 0, 1)

        self.name_label = HIGEntryLabel(_('Name:'))
        self.name_entry = HIGTextEntry()
        self.attach(self.name_label, 0,1,1,2)
        self.attach(self.name_entry, 1,3,1,2)

        self.hint_label = HIGEntryLabel(_('Hint:'))
        self.hint_entry = HIGTextEntry()
        self.attach(self.hint_label, 0,1,2,3)
        self.attach(self.hint_entry,1,3,2,3)

        self.need_root = gtk.CheckButton(_('Need root'))
        self.attach(self.need_root, 0,1,3,4)	



        self.options_label = HIGEntryLabel(_('Options:'))
        hbox = HIGHBox()
        self.options_entry = HIGTextEntry()
        self.insert_arg_button = HIGButton(title='Args', stock='gtk-add')
        self.insert_arg_button.connect('clicked', self.update_args)
        self.attach(self.options_label,0,1,4,5)
        self.attach(self.options_entry, 1,2,4,5)
        self.attach(self.insert_arg_button, 2,3, 4, 5)

        self.aguments_label = HIGEntryLabel(_('Arguments:'))
        self.arguments_entry = HIGTextEntry()
        self.arguments_entry.set_editable(False)
        self.attach(self.aguments_label, 0,1, 5,6)
        self.attach(self.arguments_entry, 1,3,5,6)


    def update_args(self, widget):
        '''
        Update aguments entry and option entry
        '''

        cursor_index = self.options_entry.get_position()
        text_entry = self.options_entry.get_text()
        arg_description, arg_key = self.dialog_args()
        if arg_key != None :
            #Update arguments

            self.arg_type = arg_key
            if text_entry.find('%s') == -1: 
                left = text_entry[0:cursor_index]
                right = text_entry[cursor_index:len(text_entry)]
                final = left + "%s" + right
                self.options_entry.set_text(final)
            self.arguments_entry.set_text(arg_description)


    def dialog_args(self):
        '''
        Create a dialog
        '''

        d = HIGDialog(_('Arguments'))
        description_label = HIGEntryLabel(
            _('Insert the description to argument:'))
        description_label.show()
        description_entry = HIGTextEntry()
        text = self.arguments_entry.get_text()
        description_entry.set_text(text)
        description_entry.show()

        combo_box = gtk.combo_box_new_text()
        combo_box.set_wrap_width(1)
        index = -1 
        j = 0 
        for i in ARG_TYPES:
            combo_box.append_text(ARG_TYPES[i])
            if i == self.arg_type:
                index = j 
            j = j + 1
        if index > -1 : 
            combo_box.set_active(index)
        combo_box.show()
        d.vbox.pack_start(description_label, False, False)
        d.vbox.pack_start(description_entry, False, False)
        d.vbox.pack_start(combo_box, False, False)
        d.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
        d.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
        resp = d.run()
        result  = None, None 
        if resp == gtk.RESPONSE_OK:
            model = combo_box.get_model()
            active = combo_box.get_active()
            if active < 0:
                return None, None 
            combo_selected =  model[active][0]

            for i in ARG_TYPES:
                if combo_selected == ARG_TYPES[i]:
                    combo_selected = i

            result = description_entry.get_text(), combo_selected
            #self.insert_arg_button.set_label('Edit args')
            #self.insert_arg_button.
        d.destroy()
        return result 

    def clear(self):
        """ 
        Clear Option Display
        """
        self.option_label.set_new_text('New Option')
        self.name_entry.set_text('')
        self.hint_entry.set_text('')
        self.arguments_entry.set_text('')
        self.need_root.set_active(False)
        self.options_entry.set_text('')


    def set_option_list(self, list):
        """
        set option list from a dictionarie 

        @param list: Elements of a option
        @type list: Dictionarie with elements
        """
        self.clear()
        self.option_label.set_new_text(list['name'])
        self.name_entry.set_text(list['name'])
        self.hint_entry.set_text(list['hint'])

        for i in list['arguments']:
            i = self.arguments_entry.get_text() + i
            self.arguments_entry.set_text(i)

        self.options_entry.set_text(list['option'])
        self.need_root.set_active(list['need_root'])
        self.arg_type = list['arg_type']


    def set_option(self,name, hint,
                   arguments, need_root,
                   options, arg_type):
        """
        fill fields
        buggy arguments.
        """
        self.clear()
        self.options_entry.set_label(name)
        self.name_entry.set_text(name)
        self.hint_entry.set_text(hint)
        self.arguments_entry.set_text(arguments)
        self.need_root.set_active(need_root)
        self.arg_type = arg_type