/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.tutorial;

import com.togethersoft.openapi.ide.IdeContext;
import com.togethersoft.openapi.ide.IdeScript;
import com.togethersoft.openapi.ide.message.IdeMessageManager;
import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;
import com.togethersoft.openapi.ide.progress.IdeProgressIndicatorAccess;
import com.togethersoft.openapi.ide.progress.IdeProgressIndicator;
import com.togethersoft.openapi.ide.progress.IdeProgress;

/**
 * This module shows how to use Together's progress indicator.
 *
 * You can use the java.text.MessageFormat's format in
 * the ProgressIndicator.start method to produce a nice-looking output.
 *
 * @author TogetherSoft
 */
public class Lesson08 implements IdeScript
{
   /**
    * Runs this module.
    *
    * @param context the IdeContext instance containing
    * the selection information at the moment the module was called.
    */
    public void run(IdeContext context)
    {
        // printing information message
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Lesson08 module: started");

        // getting progress indicator access
        IdeProgressIndicator progressIndicator =
            IdeProgressIndicatorAccess.getProgressIndicator();

        // start progress indicator
        IdeProgress progress =
            progressIndicator.start("myID", true, 70,
            "Step {0, number, integer} of {1, number, integer}. {3, number, percent} completed. ({2})");

        // cycle for show how progress indicator works
        for (int i = 0; i < 70; i++)
        {
            // if user canceled process
            if (progress.isCancelRequested())
            {
                break;
            }
            try
            {
                Thread.sleep(600);
            }
            catch(Exception e)
            { }

            // setting count
            progress.setCount(i);
            if (i % 10 == 0)
            {
                // setting new description
                progress.setStep(getDescription(i / 10));
            }
        }

        // stop progress indicator
        progress.stop();

        // printing final information message
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Lesson08 module: finished");
    }

    /**
     * Returns string description for progress indicator steps
     *
     * @param stage number of step description
     * @return string step description
     */
    private String getDescription(int stage)
    {
        switch (stage)
        {
            case 0:
                return "You can stop this demo by pressing Cancel button";
            case 1:
                return "Indicator supports java.text.MessageFormat's syntax";
            case 2:
                return "Use it to create nice-looking output messages";
            case 3:
                return "You can create cancelable/non-cancelable indicators";
            case 4:
                return "You can create delayed indicators (see API docs)";
            case 5:
                return "Together's progress indicator is very flexible!";
            case 6:
                return "See ide.progress package description in API docs";
        }
        return "";
    }
}




