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

package com.jaspersoft.ireport.designer.errorhandler;

import java.awt.BorderLayout;
import java.io.Serializable;
import java.util.logging.Logger;
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.
 */
final public class ErrorHandlerTopComponent extends TopComponent {

    private static ErrorHandlerTopComponent instance;
    
    private ErrorHandlerPanel errorHandlerPanel = null;
    
    /** path to the icon used by the component and its open action */
    static final String ICON_PATH = "com/jaspersoft/ireport/designer/resources/errorhandler/error.png";

    private static final String PREFERRED_ID = "ErrorHandlerTopComponent";

    private ErrorHandlerTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(ErrorHandlerTopComponent.class, "CTL_ErrorHandlerTopComponent"));
        setToolTipText(NbBundle.getMessage(ErrorHandlerTopComponent.class, "HINT_ErrorHandlerTopComponent"));
        setIcon(Utilities.loadImage(ICON_PATH, true));
        
        errorHandlerPanel = new ErrorHandlerPanel();
        add(errorHandlerPanel, BorderLayout.CENTER);
    }

    public void refreshErrors() {
        errorHandlerPanel.refreshErrors();
    }

    /** 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() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // 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 ErrorHandlerTopComponent getDefault() {
        if (instance == null) {
            instance = new ErrorHandlerTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the ErrorHandlerTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized ErrorHandlerTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(ErrorHandlerTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof ErrorHandlerTopComponent) {
            return (ErrorHandlerTopComponent) win;
        }
        Logger.getLogger(ErrorHandlerTopComponent.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;
    }

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

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