import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

/**
 * JEditableLabel. 
 
 * This component simulates a JLabel whose text can be edited by clicking 
 * on the component. <BR>
 * This label can only accomodate text ( no icons ).<BR>
 * 
 * Copyright (C) 1999  Shazron Abdullah
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author Shazron Abdullah
 * @version March 19, 1999
 */
public class JEditableLabel extends JTextField
{
    private Border _editingBorder = null;
    private Font   _editingFont   = null;
    private Border _labelBorder   = null;
    private Font   _labelFont     = null;
    
    // flags
    private boolean _editable    = true;
    private boolean _labelOpaque = false; // label background doesn't show initially
    
    /**
     * Default constructor.
     */
    public JEditableLabel() { this( null ); }
      
    /**
     * Constructor.
     */
    public JEditableLabel( String text )
    {
        super( text );
        
        // init editing attributes to a default JTextField
        initEnabledText( new JTextField() ); 

        // init label attributes to a default JLabel
        initDisabledText( new JLabel() );
        
        // disable textfield when the focus is lost
        addFocusListener( new FocusAdapter() {
            public void focusLost( FocusEvent evt ) {
            	//System.out.println("Focus lost.");
                setEnabled( false );
            }
        });

        // enable textfield when mouse is clicked, only if it is disabled
        addMouseListener( new MouseAdapter() {
            public void mouseClicked( MouseEvent evt ) { 
                if ( ! isEnabled() ) {
                    //System.out.println("Mouse clicked.");
                    setEnabled( true );
                }
            }
         });
         
        setEnabled( false ); // initially set to disabled (i.e Label mode)
    }
    
    /**
     * Initialize the font and border of the non-editable part of this
     * component to the JComponent's font and border.
     */
    public final void initDisabledText( JComponent c ) 
    {
        setLabelFont     ( c.getFont() );
        setLabelBorder   ( c.getBorder() );
        setLabelTextColor( c.getForeground() );
    }

    /**
     * Initialize the font and border of the editable part of this
     * component to the JComponent's font and border.
     */
    public final void initEnabledText( JComponent c ) 
    {
        setEditingFont  ( c.getFont() );
        setEditingBorder( c.getBorder() );
        setForeground   ( c.getForeground() );
        setBackground   ( c.getBackground() );
    }
    
    /**
     * Sets the label's background opacity.
     * (When editing, the background is always opaque).
     */
    public final void setLabelOpaque( boolean opaque ) 
    { 
    	_labelOpaque = opaque; 
    	if ( ! isEnabled() )
    		setEnabled( false );
    }
    
    public final void setLabelFont( Font theFont )        { _labelFont = theFont; }
    public final void setLabelBorder( Border theBorder )  { _labelBorder = theBorder; }
    public final void setLabelTextColor( Color theColor ) { setDisabledTextColor( theColor ); }
    
    public final void setEditingFont( Font theFont )        { _editingFont = theFont; }
    public final void setEditingBorder( Border theBorder )  { _editingBorder = theBorder; }
    
    /**
     * Enabling this component makes the label change to a text component so
     * you can edit the text. Disabling reverts it back to a label. <BR>
     * This component will not be enabled if this component is not editable
     * ( set the component's editability through setEditable( boolean ) )
     *
     * @param enable true sets this component to TextField mode, false to
     *	Label mode.
     */
    public void setEnabled( boolean enable )
    {
        // return if it is not editable
        if ( enable && ! _editable )
        	return;
        
        super.setEnabled( enable );
        super.setEditable( enable );
        
        setOpaque ( enable ?  true          : _labelOpaque  );
        setBorder ( enable ? _editingBorder : _labelBorder  );
        setFont   ( enable ? _editingFont   : _labelFont    );
        
        if ( enable ) grabFocus(); 
        else setCaretPosition( 0 ); // clear all selections
        
        repaint();
    }
    
    /**
     * This function is where you are truly 'disabling' this component,
     * by controlling its editability. 
     *
     * @param editable true to make this component editable, false to
     * 	make it non-editable 
     */
    public void setEditable( boolean editable )
    {
    	_editable = editable;
    	
    	if ( ! _editable )
    		setEnabled( false );
    }
    
    /**
     * @return true if the Label is editable, false if not
     */
    public boolean isEditable()
    {
    	return _editable;
    }
    
} // end of class