/*
 * Copyright 2014 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.plugin.idea.service;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;

/**
 * IntelliJ service utilities.
 *
 * @author Adam Wyłuda
 */
public class ServiceHelper
{
    private ServiceHelper()
    {
    }

    public static ForgeService getForgeService()
    {
        return ServiceManager.getService(ForgeService.class);
    }

    /**
     * Makes sure that Furnace is loaded and then executes given callback on IntelliJ UI thread.
     */
    public static void loadFurnaceAndRun(Runnable callback)
    {
        if (getForgeService().isLoaded())
        {
            runOnUIThread(callback);
        }
        else
        {
            getForgeService().startAsync();
            startForgeInitTask();
            runAfterFurnaceLoaded(callback);
        }
    }

    /**
     * Starts Forge initiation background task.
     */
    private static void startForgeInitTask()
    {
        // TODO Implement startForgeInitTask()
    }

    /**
     * Binds given action to Furnace lifecycle.
     */
    private static void runAfterFurnaceLoaded(Runnable callback)
    {
        // TODO Implement runAfterFurnaceLoaded()
    }

    /**
     * Runs provided Runnable on Swing dispatch thread.
     */
    private static void runOnUIThread(Runnable runnable)
    {
        if (ApplicationManager.getApplication().isDispatchThread())
        {
            runnable.run();
        }
        else
        {
            ApplicationManager.getApplication().invokeLater(runnable);
        }
    }
}

@@ -334,16 +334,192 @@
 Manager;
+%0Aimport com.intellij.openapi.progress.ProgressIndicator;%0Aimport com.intellij.openapi.progress.Task;%0Aimport org.jboss.forge.furnace.Furnace;%0A%0Aimport java.util.concurrent.Future;
 %0A%0A/**%0A *
@@ -1083,16 +1083,41 @@
         
+ Future%3CFurnace%3E future =
  getForg
@@ -1176,45 +1176,16 @@
 ask(
-);%0A            runAfterFurnaceLoaded(
+future, 
 call
@@ -1319,189 +1319,598 @@
 ask(
-)%0A    %7B%0A        // TODO Implement startForgeInitTask(
+final Future%3CFurnace%3E future, final Runnable callback)%0A    %7B%0A        new Task.Backgroundable(null, %22Starting Forge%22, true)%0A        %7B%0A            public void run(ProgressIndicator indicator
 )%0A    
-%7D%0A%0A
     
-/**
+    %7B
 %0A     
-* Binds given action to Furnace lifecycle.%0A     */%0A    private static void runAfterFurnaceLoaded(Runnable 
+           indicator.setText(%22Loading Furnace%22);%0A                indicator.setFraction(0.0);%0A                try%0A                %7B%0A                    future.get();%0A                %7D%0A                catch (Exception e)%0A                %7B%0A                    throw new RuntimeException(e);%0A                %7D%0A                indicator.setFraction(1.0);%0A%0A                runOnUIThread(
 call
@@ -1906,38 +1906,33 @@
 Thread(callback)
-%0A    %7B
+;
 %0A        // TODO
@@ -1928,49 +1928,62 @@
     
-// TODO Implement runAfterFurnaceLoaded
+    %7D%0A        %7D.setCancelText(%22Stop loading%22).queue
 ()
+;
 %0A   

Implement ServiceHelper.loadFurnaceAndRun()