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

package cz.karry.svgview;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.Document;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
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 class SVGViewTopComponent extends TopComponent {

    private static SVGViewTopComponent 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 = "SVGViewTopComponent";

    private SVGViewTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(SVGViewTopComponent.class, "CTL_SVGViewTopComponent"));
        setToolTipText(NbBundle.getMessage(SVGViewTopComponent.class, "HINT_SVGViewTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        

        try {
            InputStream svgInputStream = getClass().getResourceAsStream("/cz/karry/svgview/controller.svg");

            String parserClassName = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parserClassName);

            // I get exception on this line
            org.w3c.dom.Document doc = f.createDocument(parserClassName, svgInputStream);

            svgInputStream.close();
            jSVGCanvas1.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); // nutne pro updatovani pri upravach dokumentu (volani getUpdateManager...)
            jSVGCanvas1.setDocument( doc);

            if (doc.getDocumentElement().getAttribute("viewBox").isEmpty()) {
                Logger.getLogger(this.getClass().getName()).warning("Input SVG file has't set viewBox. Disable autoresize!");
            }
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }    
    }

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

        jSVGCanvas1 = new org.apache.batik.swing.JSVGCanvas();

        javax.swing.GroupLayout jSVGCanvas1Layout = new javax.swing.GroupLayout(jSVGCanvas1);
        jSVGCanvas1.setLayout(jSVGCanvas1Layout);
        jSVGCanvas1Layout.setHorizontalGroup(
            jSVGCanvas1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jSVGCanvas1Layout.setVerticalGroup(
            jSVGCanvas1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

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


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.apache.batik.swing.JSVGCanvas jSVGCanvas1;
    // 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 SVGViewTopComponent getDefault() {
        if (instance == null) {
            instance = new SVGViewTopComponent();
        }
        return instance;
    }

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