/**
  *
  * Simple Image
  * PropertyFrame
  * for ProVisu
  *
  *
  * @author Peter Beck
  */

package de.p_b_e.provisu.components.onlyanimage;

//import de.p_b_e.provisu.*;
import de.p_b_e.provisu.interfaces.*;

import de.p_b_e.utilities.Utility;
import de.p_b_e.utilities.config.Settings;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;



public class PropertyFrame extends JFrame
  implements ActionListener, MouseListener, ChangeListener, ComponentListener {

   private JFrame propertiesFrame;
   private JTextField propImage;
   private JCheckBox propTransparent;
   private JCheckBox propShowImage;
   private JSpinner propXPos;
   private JSpinner propYPos;
   private JSpinner propWidth;
   private JSpinner propHeight;

   private OnlyAnImage onlyAnImage;
   private Settings settings;
   private String address;


  /* ********************************************************************* */
  /* **************      Constructors   ********************************** */
  /* ********************************************************************* */
  /**
   *  constructor for instances
   */
   public PropertyFrame(OnlyAnImage aimage, Settings sets)
   {
       onlyAnImage = aimage;
       settings = sets;
       address = onlyAnImage.getAddress();
       init();
       
   } // end constructor
   

  /* ********************************************************************* */
  /* **************      init   ****************************************** */
  /* ********************************************************************* */
  /**
   *  intit and show PropertyFrame
   */
   public void init (){

      // *** init Properties Fields *******************************************

      ResourceBundle messages = settings.getMessages();
      
      propImage = new JTextField(settings.getProperty(address+"Image",""));

      propTransparent = new JCheckBox();
      propShowImage = new JCheckBox();
      
      String isTransparent = settings.getProperty(address+"Transparent","FALSE");
      if (isTransparent.equals("FALSE")){
         propTransparent.setSelected(false);
         propShowImage.setEnabled(false);
      }
      else{
         propTransparent.setSelected(true);
         propShowImage.setEnabled(true);
      }

      String isShowImage = settings.getProperty(address+"ShowImage","FALSE");
      if (isShowImage.equals("FALSE"))
         propShowImage.setSelected(false);
      else
         propShowImage.setSelected(true);

      propXPos = new  JSpinner(new SpinnerNumberModel(1,null,null,1));
      propYPos = new  JSpinner(new SpinnerNumberModel(1,null,null,1));
      propWidth = new  JSpinner(new SpinnerNumberModel(1,null,null,1));
      propHeight = new  JSpinner(new SpinnerNumberModel(1,null,null,1));

      Rectangle bounds = onlyAnImage.getBounds();
      propXPos.setValue(bounds.x);
      propYPos.setValue(bounds.y);
      propWidth.setValue(bounds.width);
      propHeight.setValue(bounds.height);

      //*** generate and show PropertiesPanel  ******************************

      JPanel cp = new JPanel();
      cp.setLayout(new BorderLayout());

      JPanel dataPanel = new JPanel();
      dataPanel.setLayout(new GridLayout(7,2));

      dataPanel.add(new Label(messages.getString("Property.Image")));
      dataPanel.add(propImage);
      dataPanel.add(new Label(messages.getString("Property.Transparent")));
      dataPanel.add(propTransparent);
      dataPanel.add(new Label("   "+messages.getString("Property.ShowImage")));
      dataPanel.add(propShowImage);
      dataPanel.add(new Label(messages.getString("Property.XPos")));
      dataPanel.add(propXPos);
      dataPanel.add(new Label(messages.getString("Property.YPos")));
      dataPanel.add(propYPos);
      dataPanel.add(new Label(messages.getString("Property.Width")));
      dataPanel.add(propWidth);
      dataPanel.add(new Label(messages.getString("Property.Height")));
      dataPanel.add(propHeight);

      onlyAnImage.addComponentListener(this);
      propImage.addActionListener(this);
      propImage.addMouseListener(this);
      propTransparent.addActionListener(this);
      propShowImage.addActionListener(this);
      propXPos.addChangeListener(this);
      propYPos.addChangeListener(this);
      propWidth.addChangeListener(this);
      propHeight.addChangeListener(this);

      cp.add(dataPanel, BorderLayout.CENTER);

      Box northPanel = Box.createVerticalBox();
      JLabel label = new JLabel("<html><b>"+address+"<html>");
      label.setHorizontalAlignment(JTextField.CENTER);
      northPanel.add(Box.createVerticalStrut(5),BorderLayout.NORTH);
      northPanel.add(label,BorderLayout.NORTH);
      northPanel.add(Box.createVerticalStrut(5),BorderLayout.NORTH);
      cp.add(northPanel,BorderLayout.NORTH);

      // create JFrame
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            onlyAnImage.removeComponentListener(PropertyFrame.this);
            onlyAnImage.disposePropertyFrame();
         }
      });
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE );

      setTitle(messages.getString("Property.Title"));
      add(cp);
      pack();
      Image icon = new ImageIcon(this.getClass().getResource("/de/p_b_e/provisu/resources/homeVisuPic.gif")).getImage();
      setIconImage(icon);
      setResizable(false);
//      setAlwaysOnTop(true);
      setLocationRelativeTo(null);
      setVisible(true);

   } // end init()


  /* ********************************************************************* */
  /* **************      actionPerformed     ***************************** */
  /* ********************************************************************* */
  public void actionPerformed(ActionEvent evt) {

    Object source = evt.getSource();

      if (source == propImage) {
        if (propImage.getText().equals("")){
           settings.remove(address+"Image");
           onlyAnImage.setImage(null);
        }
        else {
           try{
//             onlyAnImage.setImage(ImageIO.read(new File(propImage.getText())));
             onlyAnImage.setImage(new ImageIcon(propImage.getText()).getImage());
           }
           catch (Exception e) { }
           settings.setProperty(address+"Image",propImage.getText());
        }
      }


      else if (source == propTransparent) {
        if (propTransparent.isSelected()){
          propShowImage.setEnabled(true);
          onlyAnImage.setOpaque(false);
          settings.setProperty(address+"Transparent","TRUE");
        }
        else {
          propShowImage.setEnabled(false);
          onlyAnImage.setOpaque(true);
          settings.remove(address+"Transparent");
        }
        onlyAnImage.repaint();
      }


      else if (source == propShowImage) {
        if (propShowImage.isSelected()){
          settings.setProperty(address+"ShowImage","TRUE");
        }
        else {
          settings.remove(address+"ShowImage");
        }
        onlyAnImage.repaint();
      }


  } // end actionPerformed()


  /* ********************************************************************* */
  /* **************      MouseListener Events    ************************* */
  /* ********************************************************************* */
  public void mouseReleased(MouseEvent evt){}
  public void mouseExited(MouseEvent evt){}
  public void mouseEntered(MouseEvent evt){}
  public void mousePressed(MouseEvent evt){}
  public void mouseClicked(MouseEvent evt){
    Object source = evt.getSource();
    if (evt.getClickCount() == 2){
      // doubble click in propImage -> open JFileChoser
      if (source==propImage){
        ResourceBundle messages = settings.getMessages();
        JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
        chooser.setDialogTitle(messages.getString("FileChooser.Background.Title"));
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
          messages.getString("FileChooser.Image.FileNameExtension"), "jpg", "gif", "png");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {

          String relativePath = "";
          try {
             relativePath = Utility.findRelativePath(System.getProperty("user.dir"),chooser.getSelectedFile().getPath());   //new File("./").getPath(),chooser.getSelectedFile().getPath());
             propImage.setText(relativePath);
//               onlyAnImage.setImage(ImageIO.read(new File(propImage.getText())));
             onlyAnImage.setImage(new ImageIcon(propImage.getText()).getImage());
             settings.setProperty(address+"Image",relativePath);
           }
           catch (Exception e){ }
        }
      }
    }
  } // end mouseClicked()


   /* ********************************************************************* */
  /* **************      ChangeListener      ***************************** */
  /* ********************************************************************* */
  public void stateChanged(ChangeEvent e) {
    Object source = e.getSource();
    if ( (source == propXPos ||  source == propYPos || source == propWidth || source == propHeight ) && this.isFocused() ){


         onlyAnImage.setBounds( propXPos.getValue().hashCode(),
                    propYPos.getValue().hashCode(),
                    propWidth.getValue().hashCode(),
                    propHeight.getValue().hashCode());
         onlyAnImage.revalidate();
         
         settings.setProperty(address+"X",propXPos.getValue().hashCode());
         settings.setProperty(address+"Y",propYPos.getValue().hashCode());
         settings.setProperty(address+"Width",propWidth.getValue().hashCode());
         settings.setProperty(address+"Height",propHeight.getValue().hashCode());

    }
  } // end stateChanged()

  /* ********************************************************************* */
  /* **************      ComponentListener      ***************************** */
  /* ********************************************************************* */
  public void componentHidden(ComponentEvent e) {}
  public void componentShown(ComponentEvent e){}
  public void componentMoved(ComponentEvent e){
     if (!this.isFocused()) {
       Point pos = onlyAnImage.getLocation();
       propXPos.setValue(pos.x);
       propYPos.setValue(pos.y);
     }
  }
  public void componentResized(ComponentEvent e) {
     if (!this.isFocused()) {
       Dimension dim = onlyAnImage.getSize();
       propWidth.setValue(dim.width);
       propHeight.setValue(dim.height);
     }
  }


} // end class PropertyFrame

