/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.karry.dop.nbmodule.dbg.ui;

import java.awt.Color;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.Utilities;

/**
 * Top component which displays something.
 */
public final class DopDbgOutTopComponent extends TopComponent {

    private static DopDbgOutTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";

    private static final String PREFERRED_ID = "DopDbgOutTopComponent";
    
    private DefaultStyledDocument document;
    private Style messageStyle = new Style();
    private Style errorStyle = new Style();

    private DopDbgOutTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(DopDbgOutTopComponent.class, "CTL_DopDbgOutTopComponent"));
        setToolTipText(NbBundle.getMessage(DopDbgOutTopComponent.class, "HINT_DopDbgOutTopComponent"));

        
        StyleConstants.setForeground( messageStyle, Color.BLACK );        
        StyleConstants.setForeground( errorStyle, Color.RED );        
        document = new DefaultStyledDocument();
        outTextArea.setDocument(document);
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }
    
    public void addMessage(String text){
        try {
            document.insertString(document.getLength(), text, messageStyle);
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    
    public void addError(String text){
        try {
            document.insertString(document.getLength(), text, errorStyle);
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    
    
    
    public void clear(){
        outTextArea.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        outTextArea = new javax.swing.JTextPane();

        jScrollPane2.setViewportView(outTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane outTextArea;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized DopDbgOutTopComponent getDefault() {
        if (instance == null) {
            instance = new DopDbgOutTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the DopDbgOutTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized DopDbgOutTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(DopDbgOutTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof DopDbgOutTopComponent) {
            return (DopDbgOutTopComponent) win;
        }
        Logger.getLogger(DopDbgOutTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace() {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    private class Style extends SimpleAttributeSet{}

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

        public Object readResolve() {
            return DopDbgOutTopComponent.getDefault();
        }
    }
}
