/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unibas.bartgui.view.topComponent;

import it.unibas.bartgui.egtaskdataobject.EGTaskDataObjectDataObject;
import it.unibas.bartgui.resources.R;
import it.unibas.bartgui.view.panel.editor.Dependency.DependenciesStyleContext;
import it.unibas.bartgui.view.panel.editor.Dependency.EditorButtonPanel;
import it.unibas.bartgui.view.panel.editor.Dependency.TextLineNumber;
import it.unibas.centrallookup.CentralLookup;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.Actions;
//import org.openide.awt.UndoRedo;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//it.unibas.bartgui.view.topComponent//DependenciesEditor//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "DependenciesEditorTopComponent",
        iconBase = R.IMAGE_NODE_DCS, 
        persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "it.unibas.bartgui.view.topComponent.DependenciesEditorTopComponent")
@ActionReference(path = "Menu/Window" , position = 25 )
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_DependenciesEditorAction",
        preferredID = "DependenciesEditorTopComponent"
)
@Messages({
    "CTL_DependenciesEditorAction=DependenciesEditor",
    "CTL_DependenciesEditorTopComponent=Dependencies Editor",
    "HINT_DependenciesEditorTopComponent=This is a Dependencies Editor window"
})
public final class DependenciesEditorTopComponent extends TopComponent {

    //private final UndoRedo.Manager UndoRedomanager = new UndoRedo.Manager();
    private JScrollPane scrPane;
    private JTextPane textPane;
    private TextLineNumber textLineNumber;
    private EditorButtonPanel editorButton;
    private InstanceContent content;
    private AbstractLookup lookup;
    
    public DependenciesEditorTopComponent() {
        setName(Bundle.CTL_DependenciesEditorTopComponent());
        setToolTipText(Bundle.HINT_DependenciesEditorTopComponent());
        setLayout(new BorderLayout());
        init();
        content = new InstanceContent();
        lookup = new AbstractLookup(content);
        associateLookup(lookup);
    }

    private void init()   {
        scrPane = new JScrollPane();
        editorButton = new EditorButtonPanel();
        editorButton.getSaveRButton()
                .setAction(
                Actions.forID("DependenciesNode",
                "it.unibas.bartgui.controlegt.actions.node.DependenciesNode.ReloadDependencies"));
        /*editorButton.getResetButton()
                .setAction(
                Actions.forID("DependenciesNode", 
                "it.unibas.bartgui.controlegt.actions.node.DependenciesNode.Reset"));*/
        textPane = new JTextPane(new DependenciesStyleContext());
        //textPane.getDocument().addUndoableEditListener(UndoRedomanager);
        textPane.getDocument().addDocumentListener(new DocListener());
        
        JScrollPane scrPaneText = new JScrollPane();
        scrPaneText.setViewportView(textPane);
        textLineNumber = new TextLineNumber(textPane);
        scrPaneText.setRowHeaderView(textLineNumber);
        
        //JPanel panel = new JPanel(new BorderLayout());
        //panel.add(editorButton,BorderLayout.NORTH);
        //panel.add(scrPaneText,BorderLayout.CENTER);
                
        //scrPane.setViewportView(panel);
        add(editorButton,BorderLayout.NORTH);
        add(scrPaneText,BorderLayout.CENTER);
        
    }
    /*
    @Override
    public UndoRedo getUndoRedo() {
        return UndoRedomanager;
    }*/
    /**
     * 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() {

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        textPane.setText(getDependencies());
        content.add(textPane);
    }

    @Override
    protected void componentShowing() {
        textPane.setText(getDependencies().trim());
        content.add(textPane);
    }
    
    
    
    private void initData()   {
        
    }
    
    private String getDependencies()   {
        EGTaskDataObjectDataObject dto = CentralLookup.getDefLookup().lookup(EGTaskDataObjectDataObject.class);
        if((dto != null) && (dto.getEgtask() != null))    {
            if(dto.getDependencies() != null)return dto.getDependencies().trim();
        }
        return "DCs:";
    }
    
    
    

    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
    }
    
    private void clearContent()   {
        for(JTextPane o : lookup.lookupAll(JTextPane.class))   {
            content.remove(o);
        }
    }
    
    private class DocListener implements DocumentListener   {

        @Override
        public void insertUpdate(DocumentEvent e) {
            analize();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            analize();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            analize();
        }
        
        private void analize()   {
            if(textPane.getText().isEmpty())    {
                clearContent();
            }else{
                content.add(textPane);
            }
        }
    }
}