/*
 * CameraCustomizer.java
 *
 * Created on September 28, 2000, 5:51 PM
 */

package ovt.gui;

import ovt.*;
import ovt.mag.*;
import ovt.util.*;
import ovt.event.*;
import ovt.beans.*;
import ovt.object.*;
import ovt.datatype.*;
import ovt.interfaces.*;
import ovt.object.editor.*;

import vtk.*;

import java.io.*;
import java.util.*;
import java.lang.*;
import java.beans.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 * @author  ko
 * @version 
 */


public class CameraCustomizer extends JFrame implements PropertyChangeListener, ActionListener {
    
    protected Camera cam;
    private Descriptors desc;
    protected JComboBox viewFromComboBox;
    protected JComboBox viewToComboBox;
    protected JComboBox projectionComboBox;
    
    public CameraCustomizer(Camera acam, Window parent) {
        super();
        setTitle("View Control");
        this.cam = acam;
        desc = cam.getDescriptors();
        
        viewFromComboBox = (JComboBox)((ComponentPropertyEditor)(desc.getDescriptor("viewFrom").getPropertyEditor())).getComponent();
        viewToComboBox = (JComboBox)((ComponentPropertyEditor)(desc.getDescriptor("viewTo").getPropertyEditor())).getComponent();
        
        Dimension dim = viewFromComboBox.getPreferredSize();
        dim.width = 100;
        viewFromComboBox.setPreferredSize(dim);
        viewToComboBox.setPreferredSize(dim);
        
        viewFromComboBox.addActionListener(this);
        viewToComboBox.addActionListener(this);
        
        Container cont = getContentPane();
        //create layout : new java.awt.GridLayout (4, 1, 5, 5)
        cont.setLayout(new BorderLayout(0,5));
        
        // Undo and redo buttons panel
        /* JPanel undoredoPanel = new JPanel();
        undoredoPanel.setLayout(new java.awt.GridLayout (1, 2, 10, 10));
               
        JButton undoButton = new JButton("Undo");
        undoButton.setEnabled(false);
        undoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Not already implemented :-)");
            }
        });
        undoredoPanel.add(undoButton);
        
        JButton redoButton = new JButton("Redo");
        redoButton.setEnabled(false);
        redoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Not already implemented ;-)");
            }
        });
        undoredoPanel.add(redoButton);
        
        cont.add(undoredoPanel); */
        
        // -------- views Panel -------
        
        JPanel viewsPanel = createViewsPanel();
        
        cont.add(viewsPanel, BorderLayout.NORTH);
        
        // -------- positions Panel -------
        
        JPanel positionPanel = createPositionPanel();
        
        cont.add(positionPanel, BorderLayout.CENTER);
        
        
        // ------------------- close, reset buttons ----------------
        
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridLayout (1, 2, 5, 5));
        
        JButton closeButton = new JButton("Close");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                cam.setCustomizerVisible(false);
            }
        });
        panel.add(closeButton);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                cam.setCustomizerVisible(false);
            }
        });

        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                cam.reset();
                cam.Render();
            }
        });
        panel.add(resetButton);
        
        cont.add(panel, BorderLayout.SOUTH);
        
        pack();
        //setResizable(false);
    }
    
    JPanel createViewsPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new java.awt.GridLayout(2, 2, 5, 5));
        panel.add(new JLabel("From"));
        panel.add(viewFromComboBox);
        panel.add(new JLabel("To"));
        panel.add(viewToComboBox);
        return panel;
    }
    
    JPanel createPositionPanel() {
        JPanel panel = new JPanel();
        // create layout
        panel.setLayout(new java.awt.GridLayout (3, 2, 5, 5));
        String[] list = {"r", "delta", "phi"};
        
        JTextField tf;
        BasicPropertyDescriptor pd;
        JLabel label;
        for (int i=0; i<3; i++) {
            pd = desc.getDescriptor(list[i]);
            tf = (JTextField)((ComponentPropertyEditor)(pd.getPropertyEditor())).getComponent();
            label = new JLabel(pd.getDisplayName());
            label.setAlignmentX(CENTER_ALIGNMENT);
            panel.add(label);
            panel.add(tf);
        }
        return panel;
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
        // listens to visibility change of a camera
        String propertyName = evt.getPropertyName();
        if (propertyName.equals("customizerVisible")) {
            
            boolean value = ((Boolean)evt.getNewValue()).booleanValue();
            setVisible(value);
            
        } 
    }

public void actionPerformed(ActionEvent evt) {
    if (OVTCore.isGuiPresent()) cam.Render();
}    


}