Package org.javwer

Source Code of org.javwer.StatusLogManager$StatusTab

package org.javwer;

import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.jivesoftware.smack.packet.Message;


public class StatusLogManager {
    JavwerManager parent;
    StatusTab tab;
    FileWriter file;
    String filename;
    StatusLogManager( JavwerManager parent ) {
        this.parent = parent;
        tab = null;
        filename = parent.getJavwerHomeDir() + "/status_log.html";
        try {
            file = new FileWriter( filename, true );
        } catch ( Exception e ) {
            addMessage( "Could not open the File '" + filename + " in 'rw'-Mode<br />", "Javwer", JVW.MSG.ERROR );
        }
//        file.
    }
   
    public StatusLogManager.StatusTab show() {
        if( tab == null ) {
            tab = new StatusTab( parent.getJChat(), filename );
        }
        parent.getJChat().open();
        parent.getJChat().getShell().forceActive();
        parent.getJChat().getTabFolder().setSelection( tab.getTabItem() );
        return tab;
    }
    public void close() {
 
            try {
            file.close();
            } catch( Exception ex ) { System.err.println("#asldkjfasd"); }

    }
   public void addMessage( String f_text, String nick, int source ) {
        //System.out.println("t�lkj�lkab: " );
        final String text = f_text.replaceAll( "&", "&amp;" ).replaceAll( "<", "&lt;" ).replaceAll( ">", "&gt;" ).replaceAll( "\n", "<br />" );       
        Calendar cal = Calendar.getInstance();
        DateFormat formater = DateFormat.getTimeInstance( DateFormat.MEDIUM );
        String currentString = "<span class=\"timestamp\">[" + formater.format( cal.getTime() ) + "]</span> ";
        switch ( source ) {
            case JVW.MSG.ME: //from me
                currentString = currentString + "<span class=\"me_text\">Your new status " + text + "</span><br />\n";
                break;
            case JVW.MSG.CONTACT: //from contact
                currentString = currentString + "<span class=\"contact_name\">" + nick + ":</span><span class=\"contact_text\"> " + text + "</span><br />\n";
                break;
            case JVW.MSG.ERROR: //error
                currentString = currentString + "<span class=\"error\">Error: " + text + "</span><br />\n";
                break;
            case JVW.MSG.STATUS: //status
                currentString = currentString + "<span class=\"status\">" + nick + "s new status: " + text + "</span><br />\n";
                break;
            default:
                currentString += "Unexpected Event<br />";
        }
        if ( file != null ) {
            try {
                file.write( currentString );
                file.flush();
                //file.close();
            } catch ( IOException e ) {
                currentString += "Unable to write to Log-File<br />\n";
            }
        }
        if( tab != null ) {
            tab.addMessage( currentString );
        }
    }
   
    public class StatusTab extends JChatTab {
        private Browser browser;
        private String textString;
       
        StatusTab( JChat parent, String filename ) {
            tabTitle = "[Status]";
            tabName = tabTitle;
            type = Message.Type.ERROR;
            this.filename = filename;
            this.parent = parent;
            createGUI();
        }
       
        public void close() {
            tabItem.dispose();
            tab = null;
            parent.closeChatTab( this );           
        }
       
        private void createGUI() {
            tabItem = new CTabItem( parent.getTabFolder(), SWT.NONE );
            tabItem.setText( tabTitle );
            final Composite composite = new Composite( parent.getTabFolder(), SWT.NONE );
            composite.setLayout( new FillLayout() );
           
            browser = new Browser( composite, SWT.NONE );
            //browser.setText( "" );
            tabItem.setControl( composite );
            textString = "<html><head><link rel=\"stylesheet\" href=\"" + parent.getJavwerManager().getSettingsManager().getStringSetting( "style" ) + "\" type=\"text/css\"></head><body>";

            try {
                RandomAccessFile tmp = new RandomAccessFile( filename, "r" );
                String line;
                int maxLength = 10000;
                if( tmp.length() > maxLength ) {
                    tmp.skipBytes( (int)tmp.length() - maxLength );
                    tmp.readLine();
                }
               
                while ( (line = tmp.readLine()) != null )
                    textString = textString + line;
                tmp.close();
                if( !parent.getJavwerManager().getSettingsManager().getBooleanSetting( "show_timestamp" ) )
                    textString = textString.replaceAll( "class=\"timestamp\"", "class=\"hidden\"" );
            } catch ( Exception e ) {
                System.err.println("could now oasdf" + e + "\n" + e.getMessage());
               
            }
            browser.setText( textString );
           
            browser.addProgressListener( new ProgressListener() {
                public void changed( ProgressEvent e ) {}
                public void completed( ProgressEvent e ) {
                    browser.execute( "window.scrollBy( 0, document.height )" );
                }
            });
            //browser.setText( textString + "roflasdlfjalkjlkjlkjlkjf</body></html>" );
            parent.getShell().addDisposeListener( new DisposeListener() {
                public void widgetDisposed( DisposeEvent arg0 ) {
                    tab = null;
                }
            });
            parent.getJavwerManager().getDisplay().syncExec( new Runnable() {
                public void run() {
                    tabItem.addDisposeListener( new DisposeListener() {
                        public void widgetDisposed( DisposeEvent e ) {
                            close();
                        }
                    });
                }
            });
        }
       
        public void addMessage( String text ) {
            //System.out.println( textString );
            if( parent.getJavwerManager().getSettingsManager().getBooleanSetting( "image_smilies" ) )
                text = parent.getJavwerManager().getGraphicsManager().replaceSmilies( text );
            if( !parent.getJavwerManager().getSettingsManager().getBooleanSetting( "show_timestamp" ) )
                text = text.replaceAll( "class=\"timestamp\"", "class=\"hidden\"" );
            textString += text;
            parent.getJavwerManager().getDisplay().syncExec( new Runnable() {
                public void run() {
                    browser.setText( textString );
                }
            });
        }
       
        public CTabItem getTabItem() {
            return tabItem;
        }

        public void addMessage( String text, int source ) {
            // TODO Auto-generated method stub
           
        }

        public void addMessage( Message message ) {
            // TODO Auto-generated method stub
           
        }
       
      
    }

}
TOP

Related Classes of org.javwer.StatusLogManager$StatusTab

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.