package net.sourceforge.javydreamercsw.client.ui.components.project.viewer;

import com.validation.manager.core.db.Project;
import com.validation.manager.core.db.Requirement;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.swing.JComponent;
import net.sourceforge.javydreamercsw.client.ui.components.project.viewer.scene.HierarchyScene;
import net.sourceforge.javydreamercsw.client.ui.nodes.SubProjectChildFactory;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//net.sourceforge.javydreamercsw.client.ui.components.project//ProjectViewer//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "ProjectViewerTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_ProjectViewerAction",
        preferredID = "ProjectViewerTopComponent"
)
@Messages({
    "CTL_ProjectViewerAction=Project Viewer",
    "CTL_ProjectViewerTopComponent=Project Viewer",
    "HINT_ProjectViewerTopComponent=This is a Project Viewer window"
})
public final class ProjectViewerTopComponent extends TopComponent
        implements ExplorerManager.Provider, LookupListener {

    private final ExplorerManager em = new ExplorerManager();
    private SubProjectChildFactory projectFactory;
    private Lookup.Result<Project> result = null;
    private Lookup.Result<Requirement> result2 = null;
    private final HierarchyScene scene;
    private final JComponent myView;
    private Node root = null;
    private static final Logger LOG
            = Logger.getLogger(ProjectViewerTopComponent.class.getSimpleName());

    public ProjectViewerTopComponent() {
        initComponents();
        setName(Bundle.CTL_ProjectViewerTopComponent());
        setToolTipText(Bundle.HINT_ProjectViewerTopComponent());
        scene = new HierarchyScene();
        myView = scene.createView();
        hierarchyPane.setViewportView(myView);
//        hierarchyPane.add(scene.createSatelliteView(), BorderLayout.WEST);
//        associateLookup(Lookups.fixed(
//                // exposed TopComponent,
//                //   and SatelliteViewProvider, BirdViewProvider interfaces, too
//                (ProjectViewerTopComponent) this,
//                getActionMap() // do not forget expose ActionMap
//                , scene // if some object needs it and if it is final
//                ,getExplorerManager()
//        ));
        associateLookup(ExplorerUtils.createLookup(getExplorerManager(),
                getActionMap()));
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return em;
    }

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

        jSplitPane1 = new javax.swing.JSplitPane();
        projectPane = new OutlineView();
        hierarchyPane = new BeanTreeView();

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setLeftComponent(projectPane);
        jSplitPane1.setRightComponent(hierarchyPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane hierarchyPane;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JScrollPane projectPane;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        result = Utilities.actionsGlobalContext().lookupResult(Project.class);
        result.allItems();
        result.addLookupListener(this);
        result2 = Utilities.actionsGlobalContext().lookupResult(Requirement.class);
        result2.allItems();
        result2.addLookupListener(this);
    }

    @Override
    public void componentClosed() {
        result.removeLookupListener(this);
        result = null;
        result2.removeLookupListener(this);
        result2 = 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()) {
                Object item = it.next();
                if (item instanceof Project) {
                    Project p = (Project) item;
                    projectFactory = new SubProjectChildFactory(p);
                    root = new AbstractNode(Children.create(projectFactory, true));
                    root.setDisplayName(p.getName());
                    getExplorerManager().setRootContext(root);
                } else if (item instanceof Requirement) {
                    Requirement req = (Requirement) item;
                    scene.clear();
                    LOG.info(req.getUniqueId());
                    scene.addRequirement(req);
                }
            }
        }
    }
}

@@ -1321,18 +1321,16 @@
     
-//
 iconBase
 =%22SE
@@ -1329,34 +1329,65 @@
 Base
-=%22SET/PATH/TO/ICON/HERE
+ = %22net/sourceforge/javydreamercsw/client/ui/VSmall.png
 %22,
- 
 %0A   
@@ -1438,14 +1438,13 @@
 NCE_
-ALWAYS
+NEVER
 %0A)%0A@
@@ -2717,497 +2717,8 @@
 w);%0A
-//        hierarchyPane.add(scene.createSatelliteView(), BorderLayout.WEST);%0A//        associateLookup(Lookups.fixed(%0A//                // exposed TopComponent,%0A//                //   and SatelliteViewProvider, BirdViewProvider interfaces, too%0A//                (ProjectViewerTopComponent) this,%0A//                getActionMap() // do not forget expose ActionMap%0A//                , scene // if some object needs it and if it is final%0A//                ,getExplorerManager()%0A//        ));%0A
     

Fix persistence and add icon.