package parser.ServletParser.javaparse;

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

public class DirSelectDlg extends JDialog
{

    public Vector listData = new Vector();
	private JFileChooser jfc;
    private JList list ;
    private JButton delButton;
    private JButton doneButton;
    private JPanel  extendedFileChooser;
    private JTextField projectName;
    private JTextField projectDirectory;

	final static String includeFile   = "java";
    
	public DirSelectDlg()
	{
		super();
		setModal(true);
		setTitle( "Select Files and Directories to Reverse" );
		extendedFileChooser = makeExtendedForm();
        setContentPane(extendedFileChooser);
        pack();
        makeCenterPosition();
        this.addWindowListener( new dialogWindowClosing() );
        show();
	}
	
	public JPanel makeExtendedForm()
	{
        JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
        JPanel contentPane = new JPanel();
		jfc = new JFileChooser(System.getProperty("user.dir"));
		jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		contentPane.setLayout(new BorderLayout());
        jfc.setBorder(BorderFactory.createEtchedBorder());
        jfc.setFileFilter( new JavaFileFilter() );
		jfc.setApproveButtonText("Add");
		jfc.setApproveButtonMnemonic('A');
        contentPane.add(jfc, BorderLayout.CENTER);
				
  		JPanel panel = new JPanel();
  		panel.setLayout( new BorderLayout() );
        panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
		Border blackline;
        blackline = BorderFactory.createLineBorder(Color.black);        
        JPanel borderPanel = new JPanel();
  		borderPanel.setLayout( new BorderLayout() );
		borderPanel.setBorder( BorderFactory.createTitledBorder(blackline, "Selected Files and Directories") );
		
		list = new JList(listData);
		list.setSelectionForeground(Color.black);
		JScrollPane sp = new JScrollPane(list);
		JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.Y_AXIS));
        buttonpanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		JPanel sppanel = new JPanel();
  		sppanel.setLayout( new BorderLayout() );
        sppanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		
		delButton = new JButton("Remove");
		doneButton= new JButton("Done  ");
		delButton.setMaximumSize(new Dimension(80, 20));
		doneButton.setMaximumSize(new Dimension(80, 20));

		buttonpanel.add( delButton );
		buttonpanel.add( Box.createVerticalStrut(30) );
		buttonpanel.add( Box.createVerticalGlue() );
		buttonpanel.add( doneButton );
		sppanel.add( sp, BorderLayout.CENTER);
		panel.add( sppanel, BorderLayout.CENTER);
		panel.add( buttonpanel, BorderLayout.EAST);
		borderPanel.add(panel, BorderLayout.CENTER);
		top.add(contentPane, BorderLayout.NORTH);
		top.add(borderPanel, BorderLayout.CENTER);

		delButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				removeAction();}
		});
		doneButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				doneAction();}
		});
		jfc.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				jfcAction(e);}
		});		
        return top;
	}

	public void jfcAction(ActionEvent  e)
	{
		String command = e.getActionCommand();
		if (command.equals("ApproveSelection"))
		{
			File f = jfc.getSelectedFile();
			if( f != null )
			{
				addAction();
			}
		}
		else if (command.equals("CancelSelection"))
		{
			listData = null;
			closingWindow();
		}
	}
	
	public void addAction()
	{
		File f = jfc.getSelectedFile();
		String filename = f.getName();
		if ( filename == "" || filename == null ) return;

		if (f.isFile()) {
			int i = filename.lastIndexOf('.');
			if(i > 0 &&  i < filename.length() - 1) 
			{
				String extension =  filename.substring(i+1).toLowerCase();
				if ( includeFile.equals(extension))
				{
					if (listData.contains(f) == false) listData.add(f);
					list.setListData(listData);
					setSelectLine( listData.size() - 1 );
				} 
				else
				{
					JOptionPane.showMessageDialog(this, "자바 파일만 선택가능합니다.");
				}
			}
		} else if (f.isDirectory()) { 
			if (listData.contains(f) == false) listData.add(f);
			list.setListData(listData);
			setSelectLine( listData.size() - 1 );
		}
	}

	public void removeAction()
	{
		Object object = list.getSelectedValue();
		int index = list.getSelectedIndex();
		listData.remove(object);
		list.setListData(listData);
		setSelectLine(index);
	}

	private void setSelectLine(int selectedIndex)
	{
		int lastIndex = listData.size() - 1;
		
		if ( selectedIndex > lastIndex )
		{
			list.setSelectedIndex(lastIndex);
		}
		else
		{
			list.setSelectedIndex(selectedIndex);
		}
	}	
	
	private void doneAction()
	{
		closingWindow();
	}

	private void makeCenterPosition()
	{
		Dimension monitorSize = this.getToolkit().getScreenSize();
		Rectangle thisSize = this.getBounds();
		thisSize.x = (int)((monitorSize.width - thisSize.width)/2);
		thisSize.y = (int)((monitorSize.height - thisSize.height )/2);
		this.setBounds(thisSize);
	}

	private void closingWindow()
	{
		setVisible(false);
		dispose();
	}
	
    class dialogWindowClosing extends java.awt.event.WindowAdapter
    {
        public void windowClosing(java.awt.event.WindowEvent event)
        {
        	DirSelectDlg.this.closingWindow();
        }
    }	
	
}

class JavaFileFilter extends javax.swing.filechooser.FileFilter
{
	public boolean accept(File f) 
	{
		if(f.isDirectory()) 
		{
		   return true;
		}
		String s = f.getName();
		int i = s.lastIndexOf('.');
		if(i > 0 &&  i < s.length() - 1) 
		{
			String extension =  s.substring(i+1).toLowerCase();
			if ( DirSelectDlg.includeFile.equals(extension))
			{
			     return true;
			}
			else
			{
			  return false;
			}
		}
	    return false;
	}
	  
	public String getDescription() 
	{
		return "Java Files (*.java)";
	}
}