//----------------------------------------------------------------------------//
//                                                                            //
//                              S t e p M e n u                               //
//                                                                            //
//  Copyright (C) Herve Bitteur 2000-2009. All rights reserved.               //
//  This software is released under the GNU General Public License.           //
//  Please contact users@audiveris.dev.java.net to report bugs & suggestions. //
//----------------------------------------------------------------------------//
//
package omr.step;

import omr.log.Logger;

import omr.sheet.Sheet;
import omr.sheet.ui.SheetsController;

import omr.util.BasicTask;
import omr.util.Implement;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

/**
 * Class <code>StepMenu</code> encapsulates the user interface needed to deal
 * with application steps.  Steps are represented by menu items, each one being
 * a check box, to indicate the current status regarding the execution of the
 * step (done or not done).
 *
 * @author Herv&eacute; Bitteur
 * @version $Id$
 */
public class StepMenu
{
    //~ Static fields/initializers ---------------------------------------------

    /** Usual logger utility */
    private static final Logger logger = Logger.getLogger(StepMenu.class);

    //~ Instance fields --------------------------------------------------------

    /** The concrete UI menu */
    private final JMenu menu;

    //~ Constructors -----------------------------------------------------------

    //----------//
    // StepMenu //
    //----------//
    /**
     * Generates the menu to be inserted in the application menu hierarchy.
     *
     * @param menu the hosting menu, or null
     */
    public StepMenu (JMenu menu)
    {
        if (menu == null) {
            menu = new JMenu();
        }

        // List of Steps classes in proper order
        for (Step step : Step.values()) {
            menu.add(new StepItem(step));
        }

        // Listener to modify attributes on-the-fly
        menu.addMenuListener(new MyMenuListener());

        this.menu = menu;
    }

    //~ Methods ----------------------------------------------------------------

    //---------//
    // getMenu //
    //---------//
    /**
     * Report the concrete UI menu
     *
     * @return the menu entity
     */
    public JMenu getMenu ()
    {
        return menu;
    }

    //~ Inner Classes ----------------------------------------------------------

    //------------//
    // StepAction //
    //------------//
    /**
     * Action to be performed when the related step item is selected
     */
    private static class StepAction
        extends AbstractAction
    {
        //~ Instance fields ----------------------------------------------------

        // The related step
        final Step step;

        //~ Constructors -------------------------------------------------------

        public StepAction (Step step)
        {
            super(step.toString());
            this.step = step;
            putValue(SHORT_DESCRIPTION, step.description);
        }

        //~ Methods ------------------------------------------------------------

        @Implement(AbstractAction.class)
        public void actionPerformed (ActionEvent e)
        {
            final Sheet sheet = SheetsController.selectedSheet();
            new BasicTask() {
                    @Override
                    protected Void doInBackground ()
                        throws Exception
                    {
                        step.performUntil(sheet, null);

                        return null;
                    }

                    @Override
                    protected void finished ()
                    {
                        // Select the assembly tab related to the target step
                        if (sheet != null) {
                            sheet.getAssembly()
                                 .selectTab(step);
                        }
                    }
                }.execute();
        }
    }

    //----------//
    // StepItem //
    //----------//
    /**
     * Class <code>StepItem</code> implements a menu item linked to a given step
     */
    private static class StepItem
        extends JCheckBoxMenuItem
    {
        //~ Constructors -------------------------------------------------------

        //----------//
        // StepItem //
        //----------//
        public StepItem (Step step)
        {
            super(new StepAction(step));
        }

        //~ Methods ------------------------------------------------------------

        //--------------//
        // displayState //
        //--------------//
        public void displayState (Sheet sheet)
        {
            StepAction action = (StepAction) getAction();

            if (sheet == null) {
                setState(false);
                action.setEnabled(false);
            } else {
                boolean done = action.step.isMandatory
                               ? sheet.getSheetSteps()
                                      .isDone(action.step) : false;
                setState(done);
                setEnabled(!done);
            }
        }
    }

    //----------------//
    // MyMenuListener //
    //----------------//
    /**
     * Class <code>MyMenuListener</code> is triggered when the whole sub-menu
     * is entered. This is done with respect to currently displayed sheet. The
     * steps already done are flagged as such.
     */
    private class MyMenuListener
        implements MenuListener
    {
        //~ Methods ------------------------------------------------------------

        @Implement(MenuListener.class)
        public void menuCanceled (MenuEvent e)
        {
        }

        @Implement(MenuListener.class)
        public void menuDeselected (MenuEvent e)
        {
        }

        @Implement(MenuListener.class)
        public void menuSelected (MenuEvent e)
        {
            Sheet sheet = SheetsController.selectedSheet();

            for (int i = 0; i < menu.getItemCount(); i++) {
                JMenuItem menuItem = menu.getItem(i);

                if (menuItem instanceof StepItem) {
                    StepItem item = (StepItem) menuItem;

                    // Adjust the status for each step
                    item.displayState(sheet);
                }
            }
        }
    }
}

@@ -5096,23 +5096,12 @@
     
-boolean done = 
+if (
 acti
@@ -5119,23 +5119,20 @@
 andatory
-%0A      
+) %7B%0A
         
@@ -5147,14 +5147,28 @@
     
-     ?
+final boolean done =
  she
@@ -5224,16 +5224,24 @@
         
+  
+      
 .isDone(
@@ -5256,18 +5256,15 @@
 tep)
- : false;%0A
+;%0A%0A    
     
@@ -5307,16 +5307,20 @@
         
+    
 setEnabl
@@ -5326,24 +5326,142 @@
 led(!done);%0A
+                %7D else %7B%0A                    setState(false);%0A                    setEnabled(true);%0A                %7D%0A
             

Handle non-mandatory steps specially in the StepMenu