package vermilion;

import java.awt.Font;
import relative.*;

import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * A single class plugin which shows notes for the composotion.
 */
public class DocumentNotesPlugin extends PluginClassVermilion
{
    private JPanel panel;
    private String origStr;
    private JTextArea notesField;

    /**
     * The type for this plugin is 'STARTUP'.
     */
    @Override
    public String getType()
    {
        return "STARTUP";
    }

    /**
     * The name for this plugin is 'Router'.
     */
    @Override
    public String getName()
    {
        return "DocNotes";
    }

    /**
     * The initialise method of this plugin creates the interface tab.
     */
    @Override
    public void initialise()
    {
        panel = new JPanel();
        Main.window.addTab("Notes", panel);

        RelativeLayout layout = new RelativeLayout(null);
        panel.setLayout(layout);

        notesField = new JTextArea("Bachowny");
        notesField.setWrapStyleWord(true);
        notesField.setLineWrap(true);
        panel.add(notesField, new RelativePlacement(10, 10, 980, 980));
        layout.registerForFontSizeChanges(notesField, 1.5f, "Courier New", Font.BOLD);
    }

    /**
     * The deinitialise method for this plugin removes the interface tab and
     * clears the audio chain, stopping audio processing.
     */
    @Override
    public void deinitialise()
    {
        Main.window.removeTab(panel);
    }

    /**
     * The documentSaving method for this plugin copies the working audio chain
     * to a UsefulFileObject representation.
     */
    @Override
    public void documentSaving()
    {
        String newStr = notesField.getText();

        if (newStr.equals(origStr)) return;

        origStr = newStr;
        UsefulFileObjectSimple notesObj = new UsefulFileObjectSimple(origStr);
        Main.document.setValue("DocNotes", notesObj);
    }

    /**
     * The documentLoaded method of this plugin reads from the composition,
     * creating the machines and wires.
     */
    @Override
    public void documentLoaded()
    {
        origStr = "";
        
        UsefulFileObjectSimple notesObj = (UsefulFileObjectSimple) Main.document.getValue("DocNotes");
        if (notesObj != null) origStr = (notesObj).strValue;

        notesField.setText(origStr);
    }



    /**
     * Processes messages from the system composer (if any ever exist).
     * @param message A message from another system component.
     */
    @Override
    public void processMessage(String message, Object msgVal) {}

}