//----------------------------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 javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
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 JavaTextPanel extends JavaPanel
{
	//-------------------------------------------
	private static Dimension textFieldDimension = new Dimension(200, 20);
	public static int DTDTextPanel_SMALL = 0;
	public static int DTDTextPanel_MEDIUM = 1;
	public static int DTDTextPanel_LARGE = 2;
  //-------------------------------------------
  protected JLabel java_label=null;
  protected JTextField java_field=null;
  
	//-------------------------------------------
	public JavaTextPanel(XPDLComponent myOwner)
	{
		this(myOwner, JavaPanel.BOX_LAYOUT, false, false);
	}
	public JavaTextPanel(XPDLComponent myOwner, int layout, boolean isVertical, boolean isPasswordField)
	{
		super(myOwner, 3, "", layout, isVertical, false);
		java_label = new JLabel(myOwner.getLabel() + ": ");
		java_label.setAlignmentX(Component.LEFT_ALIGNMENT);
		java_label.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		java_label.setHorizontalAlignment(SwingConstants.RIGHT);
		
		if (!isPasswordField)
		{
			java_field = new JTextField();
		}
		else
		{
			java_field = new JPasswordField();
		}
		java_field.setText(myOwner.toString());
		java_field.setAlignmentX(Component.LEFT_ALIGNMENT);
		java_field.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		java_field.setMinimumSize(new Dimension(textFieldDimension));
		java_field.setMaximumSize(new Dimension(textFieldDimension));
		java_field.setPreferredSize(new Dimension(textFieldDimension));
		java_field.setEnabled(!myOwner.isReadOnly());
		Dimension minSize = new Dimension(5, 10);
		Dimension prefSize = new Dimension(100, 10);
		Dimension maxSize = new Dimension(Short.MAX_VALUE, 10);
	
		add(Box.createHorizontalGlue());
		add(java_label);
		add(java_field);
	}
	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());
	}
}
