package org.bladecoder.engineeditor.ui.components;

import java.io.File;

import javax.swing.JFileChooser;

import org.bladecoder.engineeditor.Ctx;

import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class FileInputPanel extends InputPanel {
	
	private File cd;
	private File selected;
	
	private boolean dirOnly = false;
	
	public FileInputPanel(Skin skin, String title, String desc, boolean dirOnly) {
		this(skin, title, desc, Ctx.project.getProjectDir() != null ? Ctx.project.getProjectDir() : new File("."), dirOnly);
	}

	public FileInputPanel(Skin skin, String title, String desc, File current, boolean dOnly) {
		super(skin, title, desc, new TextButton("Select file", skin), null);
		
		this.cd = current;
		this.dirOnly = dOnly;

//		((TextField) getField()).setEditable(false);

		((TextButton) getField()).addListener(new ClickListener() {
			public void clicked (InputEvent event, float x, float y) {
				JFileChooser chooser = new JFileChooser(cd);			
				
				if(dirOnly) {
					chooser.setDialogTitle("Select folder");
					chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				} else {
					chooser.setDialogTitle("Select file");
				}
				
				chooser.setMultiSelectionEnabled(false);

				if (chooser.showDialog(null, "Ok") == JFileChooser.APPROVE_OPTION) {
					((TextButton) getField()).setText(chooser.getSelectedFile().getAbsolutePath());
					selected = cd = chooser.getSelectedFile();
				}				
			}		
		});
	}
	
	public File getFile() {
		return selected;
	}
	
	@Override
	public String getText() {
		if(selected != null)
			return selected.getAbsolutePath();
		else 
			return "";
	}
}

@@ -1784,10 +1784,118 @@
  %22%22;%0A%09%7D%0A
+%09%0A%09@Override%0A%09public void setText(String text) %7B%0A%09%09super.setText(text);%0A%09%09selected=cd= new File(text);%0A%09%7D%0A%09%0A
 %7D%0A

fix settext method