
import com.jeta.forms.components.panel.FormPanel
import java.io.FileInputStream
import javax.imageio.ImageIO
import javax.swing.JButton
import javax.swing.JComboBox
import java.awt.event.ActionListener
import javax.swing.SwingUtilities
import java.lang.Runnable


import inspect

from org.gvsig.tools.swing.api import ToolsSwingLocator
from org.gvsig.tools.task import TaskStatus
from org.gvsig.tools.observer import Observer
from org.gvsig.tools import ToolsLocator

class ComboBoxActionListener(java.awt.event.ActionListener):
  def __init__(self,function):
    self.function = function
  def actionPerformed(self,*args):
    self.function(*args)

class FunctionRunnable(java.lang.Runnable):
  def __init__(self,fn, *args):
    self.__fn = fn
    self.__args = args

  def run(self):
    self.__fn(*self.__args)

  def __call__(self):
    self.__fn(*self.__args)
  

class ProgressBarWithTaskStatus(Observer):
  def __init__(self, name, progressBar, labelProgress=None):
    self.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(name)
    self.progressBar = progressBar
    self.labelProgress = labelProgress
    self.progressBar.setMinimum(0)
    self.progressBar.setMaximum(100)
    self.progressBar.setValue(0)
    self.taskStatus.addObserver(self)

  def getTaskStatus(self):
    return self.taskStatus
    
  def update(self, taskStatus, notification):
    if not javax.swing.SwingUtilities.isEventDispatchThread():
      javax.swing.SwingUtilities.invokeLater(
        FunctionRunnable(
          self.updateGUI,
          taskStatus.isRunning(),
          taskStatus.getCompleted(),
          taskStatus.getLabel()
        )
      )
    else:
      self.updateGUI(
        taskStatus.isRunning(),
        taskStatus.getCompleted(),
        taskStatus.getLabel()
      )
      
  def updateGUI(self, isRunning, current, label):
    if not isRunning:
        self.progressBar.setIndeterminate(False);
        self.progressBar.setValue(100);
        self.labelProgress.setText("")
        return
    self.progressBar.setIndeterminate(self.taskStatus.isIndeterminate())
    self.progressBar.setValue(current)
    if self.labelProgress != None:
      self.labelProgress.setText(label)
  
class FormPanel(object):

  def __init__(self,formfile=None):
    self._panel = None
    if formfile!=None:
      self.load(formfile)

  def getPanel(self):
    return self._panel

  def hide(self):
    self._panel.setVisible(False)
      
  def __getattr__(self, name):
    if name in ("__members__", "__methods__"):
      raise AttributeError("FormPanel",name)
    attr = self._panel.getComponentByName(name)
    if attr == None:
      raise AttributeError("FormPanel",name)
    return attr
    
    
  def load(self, formfile):
    self._panel = com.jeta.forms.components.panel.FormPanel( java.io.FileInputStream(formfile) )
    members = inspect.getmembers(self)
    for name, value in members:
      if name.endswith("_click"):
        name = name[0:-len("_click")]
        component = getattr(self,name)
        if isinstance(component, javax.swing.JButton):
          component.actionPerformed.append(value)
        elif isinstance(component, javax.swing.JComboBox):
          component.addActionListener(ComboBoxActionListener(value))
        else:
          print component
          
  def load_image(self, afile):
    return javax.imageio.ImageIO.read(afile)

  def showWindow(self, title):  
    windowManager = ToolsSwingLocator.getWindowManager()
    windowManager.showWindow(self._panel,title, windowManager.MODE.WINDOW)

  def showTool(self, title):  
    windowManager = ToolsSwingLocator.getWindowManager()
    windowManager.showWindow(self._panel,title, windowManager.MODE.TOOL)

