/**
Khalid
*/
package org.sikuli.slides.sikuli;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.sikuli.api.ScreenRegion;
import org.sikuli.slides.listeners.GlobalMouseListeners;
import org.sikuli.slides.utils.Constants;

public class SlideTutorial {
	private ScreenRegion targetRegion;
	private Constants.DesktopEvent desktopEvent;
	
	public SlideTutorial(ScreenRegion targetRegion, Constants.DesktopEvent desktopEvent){
		this.targetRegion=targetRegion;
		this.desktopEvent=desktopEvent;
	}
	
	public void performTutorialSlideAction() {
        SikuliController.displayBox(targetRegion);
        System.out.print("Waiting for user to pefrom "+this.desktopEvent.toString()+" on target.");
		try {
        	if(!GlobalScreen.isNativeHookRegistered()){
        		GlobalScreen.registerNativeHook();
        	}
        	GlobalMouseListeners globalMouseListener=new GlobalMouseListeners(targetRegion,desktopEvent);
            GlobalScreen.getInstance().addNativeMouseListener(globalMouseListener);
            final ExecutorService service=Executors.newFixedThreadPool(1);
            final Future<Boolean>task=service.submit(globalMouseListener);
            
            performUserAction(task);
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem running the tutorial mode.");
            System.err.println(ex.getMessage());
            System.exit(1);
        }

		
	}
	
	private void performUserAction(final Future<Boolean>task){
		try{
			if(task.get()==true){
			// passed and add another task
			System.out.println(desktopEvent.toString()+ " performed!");
			return;
			}
		}
        catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}
}

@@ -45,348 +45,406 @@
 li;%0A
-%0A
 import 
-java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport org.jnativehook.GlobalScreen;%0Aimport org.jnativehook.NativeHookException;%0Aimport org.sikuli.api.ScreenRegion;%0Aimport org.sikuli.slides.listeners.GlobalMouseListener
+org.jnativehook.GlobalScreen;%0Aimport org.jnativehook.NativeHookException;%0Aimport org.sikuli.api.ScreenRegion;%0Aimport org.sikuli.api.visual.Canvas;%0Aimport org.sikuli.api.visual.DesktopCanvas;%0Aimport org.sikuli.slides.listeners.GlobalKeyboardListeners;%0Aimport org.sikuli.slides.listeners.GlobalMouseListeners;%0Aimport org.sikuli.slides.shapes.SlideShape;%0Aimport org.sikuli.slides.utils.Constant
 s;%0Ai
@@ -482,16 +482,29 @@
 onstants
+.DesktopEvent
 ;%0A%0Apubli
@@ -609,16 +609,48 @@
 pEvent;%0A
+%09private SlideShape slideShape;%0A
 %09%0A%09publi
@@ -691,16 +691,39 @@
 tRegion,
+ SlideShape slideShape,
  Constan
@@ -787,16 +787,46 @@
 Region;%0A
+%09%09this.slideShape=slideShape;%0A
 %09%09this.d
@@ -904,40 +904,57 @@
 ) %7B%0A
-        SikuliController.display
+%09%09Canvas canvas=new DesktopCanvas();%0A%09%09canvas.add
 Box(
@@ -992,16 +992,18 @@
 ut.print
+ln
 (%22Waitin
@@ -1008,16 +1008,20 @@
 ing for 
+the 
 user to 
@@ -1063,16 +1063,32 @@
 ()+%22 on 
+the highlighted 
 target.%22
@@ -1219,20 +1219,92 @@
     
-%09GlobalMouse
+    if(this.desktopEvent==DesktopEvent.KEYBOARD_TYPING)%7B%0A            %09GlobalKeyboard
 List
@@ -1311,29 +1311,32 @@
 eners global
-Mouse
+Keyboard
 Listener=new
@@ -1338,29 +1338,32 @@
 r=new Global
-Mouse
+Keyboard
 Listeners(ta
@@ -1373,16 +1373,37 @@
 tRegion,
+slideShape.getText(),
 desktopE
@@ -1421,16 +1421,17 @@
         
+%09
 GlobalSc
@@ -1458,21 +1458,19 @@
 ddNative
-Mouse
+Key
 Listener
@@ -1472,29 +1472,32 @@
 tener(global
-Mouse
+Keyboard
 Listener);%0A 
@@ -1511,761 +1511,567 @@
     
-final ExecutorService service=Executors.newFixedThreadPool(1);%0A            final Future%3CBoolean%3Etask=service.submit(globalMouseListener);%0A            %0A            performUserAction(task);%0A        %7D%0A        catch (NativeHookException ex) %7B%0A            System.err.println(%22There was a problem running the tutorial mode.%22);%0A            System.err.println(ex.getMessage());%0A            System.exit(1);%0A        %7D%0A%0A%09%09%0A%09%7D%0A%09%0A%09private void performUserAction(final Future%3CBoolean%3Etask)%7B%0A%09%09try%7B%0A%09%09%09if(task.get()==true)%7B%0A%09%09%09// passed and add another task%0A%09%09%09System.out.println(desktopEvent.toString()+ %22 performed!%22);%0A%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A        catch (InterruptedException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (ExecutionException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D
+%09canvas.displayWhile(globalKeyboardListener);%0A            %7D%0A            else%7B%0A            %09GlobalMouseListeners globalMouseListener=new GlobalMouseListeners(targetRegion,desktopEvent);%0A            %09GlobalScreen.getInstance().addNativeMouseListener(globalMouseListener);%0A            %09canvas.displayWhile(globalMouseListener);%0A            %7D%0A        %7D%0A        catch (NativeHookException ex) %7B%0A            System.err.println(%22There was a problem running the tutorial mode.%22);%0A            System.err.println(ex.getMessage());%0A            System.exit(1);%0A        %7D%0A
 %0A%09%09%0A

add global mouse and keyboard actions to tutorial mode