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

"""
Scheduler View : messages received from Narval
"""

__revision__ = "$Id: SchedulerView.py,v 1.10 2001/11/29 13:41:50 syt Exp $"

from gtk import *
from EngineModel import EngineModel
from narval.gui import unicode_to_gtk

class SchedulerView(GtkHBox):

    _DICT_ = {}

    def __init__(self):
        self.__dict__ = self._DICT_

        if not self.__dict__ :
            GtkHBox.__init__(self)
            
            self._scheduler_queue_list = GtkCList(2,[unicode_to_gtk(_('Id')),
                                                     unicode_to_gtk(_('Scheduler'))])
            self._scheduler_queue_list.set_column_auto_resize(0,1)
            self._scheduler_queue_list.set_column_auto_resize(1,1)

            scroll = GtkScrolledWindow()
            scroll.add(self._scheduler_queue_list)
            scroll.set_policy(POLICY_AUTOMATIC,POLICY_AUTOMATIC)

            self.pack_start(scroll)

            # link to model
            EngineModel().attach_observer(self)

            # get color
            from PrefModel import PrefModel
            pm = PrefModel()
            pm.attach_observer(self)
            self._popped_colour = pm.messages_style['popped']['gtkcolor']
            self._pushed_colour = pm.messages_style['pushed']['gtkcolor']

            # remenber length
            self._length = 0
        
    ### PUBLIC ###
    
    # Observer interface
    def EngineModel_update(self,observable,changes):
        self._build_lists(observable.get_scheduler_queue(),
                          observable.get_scheduler_queue_messages())

    def PrefModel_update(self,observable,changes):
        self._popped_colour = observable.messages_style['popped']['gtkcolor']
        self._pushed_colour = observable.messages_style['pushed']['gtkcolor']

    ### PRIVATE ###

    def _build_lists(self,queue,message):
        self._scheduler_queue_list.thaw()
        self._scheduler_queue_list.clear()
        for i in range(len(queue)):
            elt = message[queue[i]]
            row = self._scheduler_queue_list.append([queue[i],elt[0]])
            if elt[1]:
                self._scheduler_queue_list.set_background(row,self._popped_colour)
            else:
                self._scheduler_queue_list.set_background(row,self._pushed_colour)
            
        self._scheduler_queue_list.freeze()
