package extractor.ui.EXmethodUI;

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

import extractor.extractMethod.MethodExtractor;

public class EXmethodUI extends JFrame 
                      implements ListSelectionListener {
    private JList list;
    private DefaultListModel listModel;

	 private static final String moveupString = "Move Up";
    private static final String movedownString = "Move Down";
    private static final String setString = "Set Parameter";    

	 private JButton moveupButton;
    private JButton movedownButton;
    private JButton setButton;
    private JTextField methodField;
	 private JTextField returnField;
    MethodExtractor me = null;
	 JFrame editPara = null;
    JTextField jfPara = null;
	
    public EXmethodUI() {
        super("Extract Method");

        listModel = new DefaultListModel();
        //분석기 코드
        //분석기에서 전달 받은 getParameter()값을 display
        listModel.addElement("String x"); 
        listModel.addElement("String y");
        listModel.addElement("String Friend");
        listModel.addElement("String Camp");

		 TitledBorder methodTitle = new TitledBorder(
		 								BorderFactory.createEtchedBorder(),"Method Name");
		 methodTitle.setTitleFont(new Font("SansSerif", Font.BOLD, 13));
        
        //Create a methodpanel that uses FlowLayout (the default).
	   	 methodField = new JTextField(30);
	   	 JPanel methodPane = new JPanel();
        methodPane.add(methodField);
        methodPane.setBorder(methodTitle);		 
         
        //Create the list and put it in a scroll pane
        list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);
        JScrollPane listScrollPane = new JScrollPane(list);

        moveupButton = new JButton(moveupString);
        moveupButton.setActionCommand(moveupString);
        moveupButton.addActionListener(new MoveUpListener());

        movedownButton = new JButton(movedownString);
        movedownButton.setActionCommand(movedownString);
        movedownButton.addActionListener(new MoveDownListener());

        setButton = new JButton(setString);        
        setButton.setActionCommand(setString);
        setButton.addActionListener(new SetNameListener());
        
        JLabel nullLabel = new JLabel();

        String name = listModel.getElementAt(
                              list.getSelectedIndex()).toString();

        //Create a panel that uses FlowLayout (the default).
        JPanel buttonPane = new JPanel();
        //buttonPane.setBounds(1,1,2,2);
		 buttonPane.setLayout(new GridLayout(5,1,10,7));
        
		 buttonPane.add(nullLabel);
        buttonPane.add(moveupButton);
        buttonPane.add(movedownButton);
        buttonPane.add(setButton);

        //Create a panel for parameters
        JPanel paraPane = new JPanel(new BorderLayout());
        
        paraPane.add(listScrollPane, BorderLayout.CENTER);
        paraPane.add(buttonPane, BorderLayout.EAST);
        
        TitledBorder paraTitle = new TitledBorder(
		 								BorderFactory.createEtchedBorder(),"Parameters");
		 paraTitle.setTitleFont(new Font("SansSerif", Font.BOLD, 13));
        paraPane.setBorder(paraTitle);		 
		 
        //return value textfield
        TitledBorder returnTitle = new TitledBorder(
		 								BorderFactory.createEtchedBorder(),"Return Value");
		 returnTitle.setTitleFont(new Font("SansSerif", Font.BOLD, 13));

        returnField = new JTextField(30);
	   	 JPanel returnPane = new JPanel();
        returnPane.add(returnField);
        returnPane.setBorder(returnTitle);		 
        
        //ok and cancel button
        JButton okButton = new JButton("OK");
        JButton cancelButton = new JButton("Cancel");
        
		 okButton.addActionListener(new OKListener());
		 cancelButton.addActionListener(new CancelListener());
        
		 JPanel okcancelPanel = new JPanel();		 
		 
		 okcancelPanel.add(okButton);
		 okcancelPanel.add(cancelButton);
		
		 // Create a panel for method name and parameter
		 JPanel jp = new JPanel();
		 jp.setLayout(new BorderLayout());
        jp.add(methodPane, BorderLayout.NORTH);
        jp.add(paraPane, BorderLayout.CENTER);

        Container contentPane = getContentPane();
        //contentPane.add(methodPane, BorderLayout.NORTH);
        //contentPane.add(listScrollPane, BorderLayout.CENTER);
        //contentPane.add(buttonPane, BorderLayout.EAST);
        //contentPane.add(okcancelPanel);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(jp, BorderLayout.NORTH);
        contentPane.add(returnPane, BorderLayout.CENTER);      
        contentPane.add(okcancelPanel, BorderLayout.SOUTH);
    }

    //This listener is shared by the text field and the moveup button
    class MoveUpListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            int index = list.getSelectedIndex();

            if((index != 0) && ( index != -1)){
            		String temp = (String)listModel.getElementAt(index);
            		listModel.removeElementAt(index);            
            		listModel.insertElementAt(temp, index-1);
            		list.setSelectedIndex(index-1);            
            }
        }
    }

    //This listener is shared by the text field and the setName button
    class SetNameListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
			int index = list.getSelectedIndex();

			editPara = new JFrame();
			JPanel pane = new JPanel();
			jfPara = new JTextField(20);
			
			JPanel buttonPane = new JPanel();
			
			JButton addB = new JButton("Add");
			JButton editB = new JButton("Edit");
			JButton cancelB = new JButton("Cancel");
			
			jfPara.setText((String)list.getSelectedValue());
			pane.add(jfPara, BorderLayout.NORTH);

			addB.addActionListener(new addBListener());
			editB.addActionListener(new editBListener());
			cancelB.addActionListener(new cancelBListener());

			buttonPane.add(addB);
			buttonPane.add(editB);
			buttonPane.add(cancelB);
			
			editPara.getContentPane().setLayout(new BorderLayout());
			editPara.getContentPane().add(pane, BorderLayout.NORTH);
			editPara.getContentPane().add(buttonPane, BorderLayout.CENTER);
			
			
			editPara.pack();
        	editPara.setVisible(true);	
        }
    }

    
    class MoveDownListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            int index = list.getSelectedIndex();
            int listSize = listModel.getSize();

            if((index != listSize-1) && ( index != -1)){
            		String temp = (String)listModel.getElementAt(index);
            		listModel.removeElementAt(index);            
            		listModel.insertElementAt(temp, index+1);
            		list.setSelectedIndex(index+1);
            }            
        }
    }
    
    class OKListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	Enumeration  enum = listModel.elements();
     		String[] para = new String[10];     
     		int numOfpara = 0;

 		   me = new MethodExtractor();

 		   // Get the value of the method textfield and pass it to the MethodExtractor
			String methodName = methodField.getText();
     		me.setMethodName(methodName);
     		
        	// Get the values of the list and pass them to the MethodExtractor
     		while (enum.hasMoreElements()) {
       		//System.out.println((String)enum.nextElement());
       		para[numOfpara] = (String)enum.nextElement();
       		numOfpara++;
 		    }
 		   me.setPara(para, numOfpara);
 		   
 		   // Get the value of the return textfield and pass it to the MethodExtractor
			String returnValue = returnField.getText();
     		me.setReturnValue(returnValue);
       }
    }

    class CancelListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
			setVisible(false);
        }
    }

    class addBListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	      		listModel.addElement(jfPara.getText());
	      		editPara.setVisible(false);	         
        }
    }
    
    class editBListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int index = list.getSelectedIndex();

            if( index != -1 ){
            		listModel.setElementAt(jfPara.getText(), index);
	         }
        	  editPara.setVisible(false);	         
        }
    }

    class cancelBListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	editPara.setVisible(false);
        }
    }
    
    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() == false) {

            if (list.getSelectedIndex() == -1) {
            //No selection, disable movedown button.
                movedownButton.setEnabled(false);
            } else {
            //Selection, update text field.
                movedownButton.setEnabled(true);
                String name = list.getSelectedValue().toString();
            }
        }
    }

    public static void main(String s[]) {
        JFrame frame = new EXmethodUI();

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.pack();
        frame.setVisible(true);
    }
}
	
