import javax.swing.*; 
import java.awt.event.*;



public class SwEdit extends JTextField
{
  final static String NO_DEFINED = EUIConst.sNO_DEFINED;//"No defined";  //UWAGA: musi sie zgadzac 
   JLabel m_swLabel=null;                          // ze stringiem NO_DEFINED w 
                                                  //programie ktory uzywa SwEdit
   boolean m_bIntegerOnly = false;
   boolean m_bFloatOnly = false;
   
   float m_fValue = 0.0f;
   int m_nValue = 0;
   
   int  m_nDownRange = Integer.MIN_VALUE,
	    m_nUpRange = Integer.MAX_VALUE;
   float m_fDownRange = Integer.MIN_VALUE,
	     m_fUpRange = Integer.MAX_VALUE;   
   
   public SwEdit()
   {
       addFocusListener( new EditFocusListener() );
   }

   public SwEdit(String sText)
	{
		super(sText);
        addFocusListener( new EditFocusListener() );        
	}


  public SwEdit(String sText, String sLabel)
  {
  		super(sText);
		if(sLabel!=null)
	  	     m_swLabel = new JLabel(sLabel);
       addFocusListener( new EditFocusListener() );        
  }
	
	public SwEdit(JPanel swPane)
	{
	  if(swPane !=null)
	  {
          JPanel swSubPane = SwWndUtil.MakeBoxPanel(BoxLayout.X_AXIS);				  
		  swSubPane.add(this);
		  swPane.add(swSubPane);
	  }		
       addFocusListener( new EditFocusListener() );      
	}
	public SwEdit(String sLabel, JPanel swPane)
	{
	  if(sLabel!=null)
	  	m_swLabel = new JLabel(sLabel);
	  if(swPane !=null)
	  {
      JPanel swSubPane = SwWndUtil.MakeBoxPanel(BoxLayout.X_AXIS);				  
		  if(m_swLabel!=null)
			  swSubPane.add(m_swLabel);
		  swSubPane.add(this);
		  swPane.add(swSubPane);
	  }
       addFocusListener( new EditFocusListener() );      
	}
    
    
    public SwEdit(String sLabel, JPanel swPane, InputMethodListener cList)
    {
	  if(sLabel!=null)
	  	m_swLabel = new JLabel(sLabel);
	  if(swPane !=null)
	  {
      JPanel swSubPane = SwWndUtil.MakeBoxPanel(BoxLayout.X_AXIS);				  
		  if(m_swLabel!=null)
			  swSubPane.add(m_swLabel);
		  swSubPane.add(this);
		  swPane.add(swSubPane);
	  }
       addFocusListener( new EditFocusListener() );              
       addInputMethodListener(cList);
    }
    
    boolean CheckValid()
    {
       return CheckValid(true);
    }
	boolean CheckValid(boolean bMessages)
	throws java.lang.NumberFormatException
	{
		try{
			if( getText().equals(NO_DEFINED))
				return true;
            if(m_bIntegerOnly)
	    	{
		     	m_nValue = Integer.parseInt(getText());
				if(m_nValue<m_nDownRange || m_nValue>m_nUpRange)
				{
                    if(bMessages)
					    JOptionPane.showMessageDialog(null, "Range of one edit area among the values is not correct ("+m_nValue+")");
					return false;
				}
		    }
            if(m_bFloatOnly)
		    {
    		 	m_fValue = Float.parseFloat(getText());
				if(m_fValue<m_fDownRange || m_fValue>m_fUpRange)
				{
                    if(bMessages)
					    JOptionPane.showMessageDialog(null, "Range of one edit area among the values is not correct ("+m_fValue+")");
					return false;
				}				
		    }
		    return true;
		}catch (java.lang.NumberFormatException e)
		{
            if(bMessages)
			    JOptionPane.showMessageDialog(null, "Number format is not correct("+getText()+")" );
			return false;
		}
	}
    
	
	void PutToPanel(JPanel swPane)
	{
	   JPanel swSubPane = SwWndUtil.MakeBoxPanel(BoxLayout.X_AXIS);
	   if(m_swLabel!=null)
	      swSubPane.add(m_swLabel); 
         swSubPane.add(this);	   
	   swPane.add(swSubPane);	
	}
	/** ustawia wczytywanie samych liczb	 */
	void SetFloatOnly()
	{
       m_bFloatOnly = true;		
	}
	void SetRange(int nDown, int nUp)
	{
	  m_nDownRange = nDown;
	  m_nUpRange = nUp;	  
	}
	void SetRange(float fDown, float fUp)
	{
	  m_fDownRange = fDown;
	  m_fUpRange = fUp;	  
	}	
	void SetIntegerOnly()
	{
       m_bIntegerOnly = true;		
	}	
	
	float GetFValue()
	{
	    return m_fValue;	
	}
	int GetIValue()
	{
	  return m_nValue;	
	}	
    class EditFocusListener  implements  FocusListener
    {
        public void focusGained(FocusEvent e)   
        {
            select(0,getDocument().getLength());
        }
        public void focusLost(FocusEvent e)   
        {
        }
    }

}