package de.emvau.ulctests;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCLabel;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.util.ULCIcon;
import com.ulcjava.base.client.UILabel;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.shared.IUlcEventConstants;
import com.ulcjava.base.shared.internal.Anything;

public class ActionLabelSnippet extends AbstractApplication
{
  private ULCIcon open_icon = null;
  private ULCIcon close_icon = null;

  private static ULCIcon focusselected_icon = null;
  private static ULCIcon focusunselected_icon = null;
  private static ULCIcon selected_icon = null;
  private static ULCIcon unselected_icon = null;

  public void start()
  {
    ULCBoxPane contentPane = new ULCBoxPane(1, 0);

    selected_icon = new ULCIcon(getClass().getResource("true.gif"));
    unselected_icon = new ULCIcon(getClass().getResource("false.gif"));

    MVActionLabel actionLabel = new MVActionLabel("Action Label",unselected_icon,0);

//    actionLabel.addSelectionListener(new ISelectionChangedListener()
//        {
//          public void selectionChanged(SelectionChangedEvent event)
//          {
//            ULCActionLabel label = (ULCActionLabel)event.getSource();
//            ULCIcon icon = label.getIcon();
//            if(icon == unselected_icon)
//              label.setIcon(selected_icon);
//            else
//              label.setIcon(unselected_icon);
//            System.out.println("SelectionChanged-Event");
//          }
//        });

    actionLabel.addActionListener(new IActionListener()
        {
          public void actionPerformed(ActionEvent event)
          {
            Anything any = null;
            if(event instanceof MyActionEvent)
              any = ((MyActionEvent)event).getAnything();
            MVActionLabel label = (MVActionLabel)event.getSource();
            ULCIcon icon = label.getIcon();
            if(icon == unselected_icon)
              label.setIcon(selected_icon);
            else
              label.setIcon(unselected_icon);
            System.out.println("Action-Event");
          }
        });

    
    contentPane.add(actionLabel);

    ULCFrame frame = new ULCFrame("Click Label Snippet");
    frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
    frame.add(contentPane);
    frame.setVisible(true);
  }

  public static void main(String[] args)
  {
    DevelopmentRunner.setApplicationClass(ActionLabelSnippet.class);
    DevelopmentRunner.main(args);
  }

  public static class MVActionLabel extends ULCLabel
  {
    
    public MVActionLabel(String text, ULCIcon icon, int horizontalAlignment)
    {
      super(text, icon, horizontalAlignment);
    }

    public MVActionLabel(String text, int horizontalAlignment)
    {
      super(text, horizontalAlignment);
    }

    public MVActionLabel(String text)
    {
      super(text);
    }

    public MVActionLabel(ULCIcon icon, int horizontalAlignment)
    {
      super(icon, horizontalAlignment);
    }

    public MVActionLabel(ULCIcon icon)
    {
      super(icon);
    }

    public MVActionLabel()
    {
      super();
    }

    public void addActionListener(IActionListener listener)
    {
      super.internalAddListener(IUlcEventConstants.ACTION_EVENT, listener);
    }

    public void removeActionListener(IActionListener listener)
    {
      internalRemoveListener(IUlcEventConstants.ACTION_EVENT, listener);
    }

//    public void addSelectionListener(ISelectionChangedListener listener)
//    {
//      internalAddListener(IUlcEventConstants.SELECTION_CHANGED_EVENT, listener);
//    }
//    public void removeSelectionListener(ISelectionChangedListener listener)
//    {
//      internalRemoveListener(IUlcEventConstants.SELECTION_CHANGED_EVENT, listener);
//    }

    protected void handleEvent(int listenerType, int eventId, Anything args)
    {
      if (listenerType == IUlcEventConstants.ACTION_EVENT)
      {
        distributeToListeners(new MyActionEvent(this, null,args));
      }
//      else if (listenerType == IUlcEventConstants.SELECTION_CHANGED_EVENT)
//      {
//        distributeToListeners(new SelectionChangedEvent(this));
//      }
      else
      {
        super.handleEvent(listenerType, eventId, args);
      }
    }

    protected String typeString()
    {
      return UIActionLabel.class.getName();
    }
  }

  public static class UIActionLabel extends UILabel
  {
    public void restoreState(Anything args)
    {
      super.restoreState(args);

      getBasicComponent().addMouseListener(new ActionHandler());
    }

    private class ActionHandler extends MouseAdapter
    {

      public void mouseClicked(MouseEvent event)
      {
        if (isIconFocused(event))
        {
          Anything any = new Anything();
          any.put("pointX",event.getX());
          sendOptionalEventULC(IUlcEventConstants.ACTION_EVENT, IUlcEventConstants.ACTION_PERFORMED,any);
//          sendOptionalEventULC(IUlcEventConstants.SELECTION_CHANGED_EVENT, IUlcEventConstants.SELECTION_CHANGED);
        }
      }
      private boolean isIconFocused(MouseEvent event)
      {
        Object source = event.getSource();
        UiJDnDLabel label = (UiJDnDLabel)event.getSource();
        int horizontalTextPos = label.getHorizontalTextPosition() +1;
        int x = event.getX()-1;
        if (x <= horizontalTextPos)
        {
//          System.out.println("Maus-Zeiger: " + x + "  Textposition: " + horizontalTextPos);
          return true;
        }
        return false;
      }
    }
  }
}
