//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.editor.panels;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.huihoo.god4j.editor.XPDLComponent;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaLocationPanel extends JavaPanel
{
	private static Dimension locationDimension = new Dimension(175, 20);
	private static Dimension fileButtonDimension = new Dimension(25, 20);
	
	//-------------------------------------------
	protected JLabel java_label=null;
	protected JTextField java_field=null;
  protected JButton    java_button=null;
	//-------------------------------------------
	
	public JavaLocationPanel(XPDLComponent myOwner)
	{
		this(myOwner, JavaPanel.BOX_LAYOUT);
	}
	public JavaLocationPanel(XPDLComponent myOwner, int layout)
	{
		this(myOwner, JavaPanel.BOX_LAYOUT, -1);
	}
	public JavaLocationPanel(XPDLComponent myOwner, int layout, final int filteringMode)
	{
		super(myOwner, 4, "", layout, false, false);
		java_label = new JLabel(myOwner.getLabel() + ": ");
		java_label.setAlignmentX(Component.LEFT_ALIGNMENT);
		java_label.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		java_label.setHorizontalAlignment(SwingConstants.RIGHT);
		//java_label.setMaximumSize(new Dimension(Short.MAX_VALUE,10));
		
		java_field = new JTextField();
		if(myOwner.getValue()!=null)
		{
		  java_field.setText(myOwner.getValue().toString());
    }
		java_field.setAlignmentX(Component.LEFT_ALIGNMENT);
		java_field.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		java_field.setMinimumSize(new Dimension(locationDimension));
		java_field.setMaximumSize(new Dimension(locationDimension));
		java_field.setPreferredSize(new Dimension(locationDimension));
		java_field.setEnabled(!myOwner.isReadOnly());
		
		java_button = new JButton("...");
		java_button.setAlignmentX(Component.LEFT_ALIGNMENT);
		java_button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		java_button.setMinimumSize(new Dimension(fileButtonDimension));
		java_button.setMaximumSize(new Dimension(fileButtonDimension));
		java_button.setPreferredSize(new Dimension(fileButtonDimension));
		java_button.setEnabled(!myOwner.isReadOnly());
		
		add(Box.createHorizontalGlue());
		add(java_label);
		add(java_field);
		add(java_button);
		java_button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				String fileName = "";
				String message = sm.getString("DialogChooseFile");
			//	fileName = XPDL.dialog(getDialog(), message, 0, filteringMode, null);
				if (fileName != null && fileName.length() > 0)
				{
					java_field.setText(fileName);
				}
			}
		});
	}
	public boolean checkRequired()
	{
		if (isEmpty() && getXPDLComponent().isRequired() && !getXPDLComponent().isReadOnly())
		{
			JavaPanel.defaultErrorMessage(this.getDialog(),java_label.getText());
			java_field.requestFocus();
			return false;
		}
		return true;
	}
	public boolean isEmpty()
	{
		return getText().trim().equals("");
	}

	public String getText()
	{
		return java_field.getText();
	}
	
	public void applyChanges()
	{
		getXPDLComponent().setValue(getText());
	}
}