package net.sourceforge.javydreamercsw.vm.jasperreportviewer;

import java.util.Collection;
import java.util.Iterator;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//net.sourceforge.javydreamercsw.vm.jasperreportviewer//JasperReportViewer//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "JasperReportViewerTopComponent",
        //        iconBase = "com/validation/manager/resources/icons/VMSmall.png",
        persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window",
        id = "net.sourceforge.javydreamercsw.vm.jasperreportviewer.JasperReportViewerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_JasperReportViewerAction",
        preferredID = "JasperReportViewerTopComponent")
@Messages({
    "CTL_JasperReportViewerAction=Report Viewer",
    "CTL_JasperReportViewerTopComponent=Report Viewer Window",
    "HINT_JasperReportViewerTopComponent=This is a Report Viewer window"
})
public final class JasperReportViewerTopComponent extends TopComponent
        implements LookupListener {

    private Lookup.Result<JasperPrint> result = null;

    public JasperReportViewerTopComponent() {
        initComponents();
        setName(Bundle.CTL_JasperReportViewerTopComponent());
        setToolTipText(Bundle.HINT_JasperReportViewerTopComponent());
    }

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

        reportPane = new javax.swing.JScrollPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(reportPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(reportPane, 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 reportPane;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        result = Utilities.actionsGlobalContext().lookupResult(JasperPrint.class);
        result.allItems();
        result.addLookupListener(this);
    }

    @Override
    public void componentClosed() {
        result.removeLookupListener(this);
        result = null;
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent le) {
        Lookup.Result res = (Lookup.Result) le.getSource();
        Collection instances = res.allInstances();

        if (!instances.isEmpty()) {
            Iterator it = instances.iterator();
            while (it.hasNext()) {
                JasperPrint jasperPrint = Utilities.actionsGlobalContext().lookup(JasperPrint.class);
                if (jasperPrint != null) {
                    reportPane.setViewportView(new JRViewer(jasperPrint));
                }
            }
        }
    }
}

@@ -262,81 +262,8 @@
 es;%0A
-import org.openide.awt.ActionID;%0Aimport org.openide.awt.ActionReference;%0A
 impo
@@ -979,200 +979,8 @@
 se)%0A
-@ActionID(category = %22Window%22,%0A        id = %22net.sourceforge.javydreamercsw.vm.jasperreportviewer.JasperReportViewerTopComponent%22)%0A@ActionReference(path = %22Menu/Window%22 /*, position = 333 */)%0A
 @Top

Remove action from menu.