/*
 * MMCDHTMLPane.java
 * 
 * Created on 25 �ubat 2005 Cuma, 10:48
 */

package com.isoft.mmcd.skin.widgets ;

import java.awt.* ;
import java.awt.datatransfer.DataFlavor ;
import java.awt.datatransfer.StringSelection ;
import java.awt.datatransfer.Transferable ;
import java.awt.datatransfer.UnsupportedFlavorException ;
import java.awt.im.InputContext ;

import javax.swing.* ;

import com.isoft.mmcd.utils.* ;
import com.isoft.mmcd.utils.cfgreader.* ;
import com.isoft.mmcd.skin.* ;
import com.isoft.mmcd.exceptions.* ;

import java.io.* ;

import javax.swing.text.JTextComponent ;
import javax.swing.text.html.* ;

/**
 * 
 * @author Fatih ATAK
 */
public class MMCDHTMLPane extends JEditorPane implements java.io.Serializable, Widget, ResizableWidget, ScrollableWidget
{
    public static final int MAX_PASTE_LENGTH = 1024 ;

    protected String helpId = "" ;

    protected String widgetName = "" ;

    protected String popupMenuName = null ;

    protected DynamicWidgetBounds dwbounds = null ;

    protected MMCDScrollPane scrollPane = null ;

    protected String docBase = null ;

    /** Creates a new instance of MMCDHTMLPane */
    public MMCDHTMLPane( )
    {
        super( ) ;
        setScrollPane( new MMCDScrollPane( this ) ) ;
    }

    public boolean init( MCCompoundProperty property ) throws MMCDException
    {
        setAutoscrolls( true ) ;
        // setBorder( BorderFactory.createEmptyBorder( ) ) ;
        try
        {
            if( property.getAtomicProperty( "$html" ) != null )
            {
                if( "true".equals( property.getAtomicProperty( "$html" ).getValue( ) ) )
                {
                    setContentType( "text/html" ) ;
                    setEditable( false ) ;
                }
            }
            if( property.getAtomicProperty( "$editablehtml" ) != null )
            {
                setContentType( "text/html" ) ;
                setEditable( property.getAtomicProperty( "$editablehtml" ).getBooleanValue( ) ) ;
            }

            if( property.getAtomicProperty( "$docbase" ) != null )
                docBase = property.getAtomicProperty( "$docbase" ).getValue( ) ;

            if( property.getAtomicProperty( "$file" ) != null )
            {
                MCProperty p = property.getAtomicProperty( "$file" ) ;
                if( p.getNumValues( ) > 1 )
                    setHtml( FileUtils.loadFileAsString( p.getInputWrapper( ).getReader( ) ) ) ;
                else
                    setPage( p.getValue( ) ) ;
            }

            if( property.getAtomicProperty( "$url" ) != null )
            {
                setPage( property.getAtomicProperty( "$url" ).getValue( ) ) ;
            }
        }
        catch( Exception e )
        {
            e.printStackTrace( ) ;
            throw new MMCDException( e ) ;
        }
        return true ;
    }

    public synchronized void setHtml( String html )
    {
        try
        {
            StringReader sr = new StringReader( html ) ;
            // System.err.println( "HTML:[" + ( html.length( ) > 30 ? html.substring( 0, 30 ) : html ) + "]" ) ;
            setContentType( "text/html" ) ;
            // System.err.println( "Line.xx" ) ;
            // setEditable( false ) ;
            // System.err.println( "Line.yy" ) ;
            setEditorKit( getEditorKitForContentType( "text/html" ) ) ;
            setDocument( getEditorKit( ).createDefaultDocument( ) ) ;
            System.err.println( "Token Threshold:" + ( ( HTMLDocument )getDocument( ) ).getTokenThreshold( ) ) ;
            ( ( HTMLDocument )getDocument( ) ).setTokenThreshold( Integer.MAX_VALUE ) ;
            // setDocument( new HTMLDocument( ) ) ;
            // System.err.println( "Line.zz" ) ;
            if( docBase != null )
                ( ( HTMLDocument )getDocument( ) ).setBase( new File( docBase ).toURL( ) ) ;

            getEditorKit( ).read( sr, getDocument( ), 0 ) ;
            // System.err.println( "Line.ww" ) ;
        }
        catch( Exception e )
        {
            e.printStackTrace( ) ;
        }
        catch( Throwable t )
        {
            t.printStackTrace( ) ;
            if( t instanceof Error )
                throw ( Error )t ;
        }

    }

    public String getWidgetName( )
    {
        return widgetName ;
    }

    public void setWidgetName( String widgetName )
    {
        this.widgetName = widgetName ;
    }

    public DynamicWidgetBounds getDynamicWidgetBounds( )
    {
        if( getScrollPane( ) != null )
            return getScrollPane( ).getDynamicWidgetBounds( ) ;

        return dwbounds ;
    }

    public void setDynamicWidgetBounds( DynamicWidgetBounds dwb )
    {
        if( getScrollPane( ) != null )
        {
            getScrollPane( ).setDynamicWidgetBounds( dwb ) ;
        }
        else
        {
            this.dwbounds = dwb ;
        }
    }

    public void resizeWidget( Rectangle newParentBounds )
    {
        if( getScrollPane( ) != null )
        {
            getScrollPane( ).resizeWidget( newParentBounds ) ;
        }
        else
        {
            getDynamicWidgetBounds( ).calculateBounds( newParentBounds ) ;
            setBounds( getDynamicWidgetBounds( ).getBounds( ) ) ;
            setPreferredSize( new Dimension( getDynamicWidgetBounds( ).getBounds( ).width, getDynamicWidgetBounds( ).getBounds( ).height ) ) ;
        }
    }

    public MMCDScrollPane getScrollPane( )
    {
        return scrollPane ;
    }

    public void setScrollPane( MMCDScrollPane scrollPane )
    {

        this.scrollPane = scrollPane ;
    }

    public void setVisible( boolean visible )
    {
        super.setVisible( visible ) ;
        if( getScrollPane( ) != null )
            getScrollPane( ).setVisible( visible ) ;

    }

    public void setInheritsPopupMenu( boolean flag )
    {
        super.setInheritsPopupMenu( flag ) ;
        if( getScrollPane( ) != null )
            getScrollPane( ).setInheritsPopupMenu( flag ) ;
    }

    public void setComponentPopupMenu( JPopupMenu popup )
    {
        super.setComponentPopupMenu( popup ) ;
        if( getScrollPane( ) != null )
            getScrollPane( ).setComponentPopupMenu( popup ) ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#getPopUpMenuName()
     */
    public String getPopUpMenuName( )
    {
        return this.popupMenuName ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#setPopUpMenuName(java.lang.String)
     */
    public void setPopUpMenuName( String popupMenuName )
    {
        this.popupMenuName = popupMenuName ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#setHelpId(java.lang.String)
     */
    public void setHelpId( String helpId )
    {
        this.helpId = helpId ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#getHelpId()
     */
    public String getHelpId( )
    {
        return this.helpId ;
    }

    public void copy( )
    {
        StringSelection ss = new StringSelection( getAccessibleContext( ).getAccessibleText( ).getSelectedText( ) ) ;
        Toolkit.getDefaultToolkit( ).getSystemClipboard( ).setContents( ss, null ) ;
    }

    public void paste( )
    {
        {
            try
            {
                Transferable t = Toolkit.getDefaultToolkit( ).getSystemClipboard( ).getContents( null ) ;
                DataFlavor flavor = getFlavor( t.getTransferDataFlavors( ) ) ;

                if( flavor != null )
                {
                    InputContext ic = getInputContext( ) ;
                    if( ic != null )
                    {
                        ic.endComposition( ) ;
                    }
                    try
                    {
                        String data = ( String )t.getTransferData( flavor ) ;

                        ( ( JTextComponent )comp ).replaceSelection( data ) ;
                    }
                    catch( UnsupportedFlavorException ufe )
                    {
                    }
                    catch( IOException ioe )
                    {
                    }
                }

                if( getText( ).length( ) + s.length( ) > MAX_PASTE_LENGTH )
                {
                    ss = new StringSelection( s.substring( 0, MAX_PASTE_LENGTH - getText( ).length( ) ) ) ;
                    Toolkit.getDefaultToolkit( ).getSystemClipboard( ).setContents( ss, null ) ;
                }
            }
            catch( Exception e )
            {
                ThrowableHandler.getInstance( ).addThrowable( e, ThrowableHandler.S_WARNING ) ;
            }
        }
        super.paste( ) ;
    }

    private DataFlavor getFlavor( DataFlavor[ ] flavors )
    {
        if( flavors != null )
        {
            for( int counter = 0; counter < flavors.length; counter++ )
            {
                if( flavors[ counter ].equals( DataFlavor.stringFlavor ) )
                {
                    return flavors[ counter ] ;
                }
            }
        }
        return null ;
    }

}