/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netbeans.cubeon.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import java.io.IOException;
import java.util.List;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.cubeon.tasks.spi.Extension;
import org.netbeans.cubeon.tasks.spi.task.TaskEditorProvider;
import org.netbeans.cubeon.tasks.spi.task.TaskEditorProvider.EditorAttributeHandler;
import org.netbeans.cubeon.tasks.spi.task.TaskElement;
import org.netbeans.cubeon.tasks.spi.task.TaskElementChangeAdapter;
import org.netbeans.cubeon.tasks.spi.repository.TaskRepository;
import org.netbeans.cubeon.ui.internals.TaskElementNode;
import org.openide.cookies.SaveCookie;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
final class TaskEditorTopComponent extends TopComponent implements SaveCookie, ChangeListener {

    private static final String PREFERRED_ID = "TaskEditorTopComponent";

    private TaskElement element;
    private final TaskElementNode editorNode;
    private final EditorAttributeHandler eah;
    private final Extension extension;
    private final TaskElementChangeAdapter changeAdapter;


    TaskEditorTopComponent(final TaskElement element) {
        this.element = element;

        initComponents();
        jToolBar1.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
        Lookup lookup = element.getLookup();

        TaskEditorProvider editorProvider = lookup.lookup(TaskEditorProvider.class);
        eah = editorProvider.createEditorAttributeHandler();

        setName(eah.getName());
        extension = element.getLookup().lookup(Extension.class);
        setIcon(element.getImage());
        TaskRepository taskRepository = element.getTaskRepository();

        lblHeader.setIcon(new ImageIcon(taskRepository.getImage()));
        lblHeader.setText(eah.getDisplayName());
        base.add(eah.getComponent(), BorderLayout.CENTER);

        List<Action> actions = eah.getActions();
        for (Action action : actions) {
            if (action == null) {
                jToolBar1.addSeparator();
            } else {
                JButton button = new JButton(action);
                button.setText(null);
                button.setOpaque(false);
                jToolBar1.add(button,0);
            }
        }

        setActivatedNodes(new Node[]{editorNode = TaskElementNode.createNode(element, this)});
        eah.addChangeListener(this);
        changeAdapter = new TaskElementChangeAdapter() {

            @Override
            public void nameChenged() {
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        setName(eah.getName());
                        lblHeader.setText(eah.getDisplayName());
                    }
                });

            }

            @Override
            public void typeChenged() {
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        setName(eah.getName());
                        setIcon(element.getImage());
                    }
                });

            }
        };
        extension.add(changeAdapter);
    }

    /** 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.JSeparator separator = new javax.swing.JSeparator();
        base = new javax.swing.JPanel();
        header = new javax.swing.JPanel();
        lblHeader = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        base.setBackground(new java.awt.Color(255, 255, 255));
        base.setLayout(new java.awt.BorderLayout());

        header.setBackground(new java.awt.Color(255, 255, 255));
        header.setMaximumSize(new java.awt.Dimension(32767, 34));
        header.setMinimumSize(new java.awt.Dimension(0, 34));
        header.setPreferredSize(new java.awt.Dimension(0, 34));

        lblHeader.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        lblHeader.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/netbeans/cubeon/ui/repository.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblHeader, "Task Element Name"); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setOpaque(false);
        jToolBar1.setPreferredSize(new java.awt.Dimension(49, 23));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/netbeans/cubeon/ui/refresh.png"))); // NOI18N
        jButton1.setToolTipText(NbBundle.getMessage(TaskEditorTopComponent.class, "TaskEditorTopComponent.jButton1.toolTipText","-")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setOpaque(false);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        org.jdesktop.layout.GroupLayout headerLayout = new org.jdesktop.layout.GroupLayout(header);
        header.setLayout(headerLayout);
        headerLayout.setHorizontalGroup(
            headerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, headerLayout.createSequentialGroup()
                .addContainerGap()
                .add(lblHeader, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        headerLayout.setVerticalGroup(
            headerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(lblHeader, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
            .add(jToolBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
        );

        base.add(header, java.awt.BorderLayout.NORTH);

        add(base, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        eah.refresh();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel base;
    private javax.swing.JPanel header;
    private javax.swing.JButton jButton1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblHeader;
    // End of variables declaration//GEN-END:variables

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_NEVER;
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    public void save() throws IOException {
        eah.save();
        editorNode.setModified(false);
    }

    public void stateChanged(ChangeEvent e) {
        editorNode.setModified(true);
    }

    @Override
    protected void componentClosed() {
        extension.remove(changeAdapter);
        try {
            editorNode.destroy();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

@@ -7154,16 +7154,57 @@
 fresh();
+ %0A        editorNode.setModified(false);%0A
 %0A    %7D//

reset modified tag