//----------------------------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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
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 JavaMultiLineTextPanel extends JavaPanel
{
	public static Dimension textAreaDimension = new Dimension(200, 60);
	public JavaMultiLineTextPanel(XPDLComponent myOwner)
	{
		this(myOwner, JavaPanel.BOX_LAYOUT);
	}
	public JavaMultiLineTextPanel(XPDLComponent myOwner, int layout)
	{
		this(myOwner, layout, false, false, true);
	}
	public JavaMultiLineTextPanel(XPDLComponent myOwner, int layout, boolean isVertical, boolean bigPanel, boolean wrapLines)
	{
		super(myOwner, 3, "", layout, isVertical, false);
		JScrollPane jsp = new JScrollPane();
		jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
		jsp.setAlignmentY(Component.TOP_ALIGNMENT);
		JLabel jl = new JLabel(myOwner.getLabel() + ": ");
		jl.setAlignmentX(Component.LEFT_ALIGNMENT);
		jl.setAlignmentY(Component.TOP_ALIGNMENT);
		jl.setHorizontalAlignment(SwingConstants.RIGHT);
		JTextArea jta = new JTextArea();
		jta.setTabSize(4);
		jta.setText(myOwner.toString());
		jta.getCaret().setDot(0);
		jta.setLineWrap(wrapLines);
		jta.setWrapStyleWord(wrapLines);
		jta.setAlignmentX(Component.LEFT_ALIGNMENT);
		jta.setAlignmentY(Component.TOP_ALIGNMENT);
		jta.setEnabled(!myOwner.isReadOnly());
		jsp.setViewportView(jta);
		jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
		jsp.setAlignmentY(Component.TOP_ALIGNMENT);
		if (!bigPanel)
		{
			jsp.setPreferredSize(new Dimension(textAreaDimension));
		}
		else
		{
			jsp.setPreferredSize(new Dimension(400, 400));
		}
		add(Box.createHorizontalGlue());
		add(jl);
		add(jsp);
	}
	public boolean checkRequired()
	{
		if (isEmpty() && getXPDLComponent().isRequired() && !getXPDLComponent().isReadOnly())
		{
			JavaPanel.defaultErrorMessage(this.getDialog(), ((JLabel) getComponent(1)).getText());
			JScrollPane jsp = (JScrollPane) getComponent(2);
			JViewport jvp = (JViewport) jsp.getComponent(0);
			((JTextArea) jvp.getComponent(0)).requestFocus();
			return false;
		}
		return true;
	}
	public boolean isEmpty()
	{
		return getText().trim().equals("");
	}
	public String getText()
	{
		JScrollPane jsp = (JScrollPane) getComponent(2);
		JViewport jvp = (JViewport) jsp.getComponent(0);
		return ((JTextArea) jvp.getComponent(0)).getText();
	}
	
	public void applyChanges()
	{
		getXPDLComponent().setValue(getText());
	}
}
