package de.mycrobase.ssim.ed.helper;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import com.jme3.app.SimpleApplication;

import de.mycrobase.ssim.ed.GameMode;
import de.mycrobase.ssim.ed.GameModeListener;
import de.mycrobase.ssim.ed.SSimApplication;
import de.mycrobase.ssim.ed.mission.Mission;
import de.mycrobase.ssim.ed.settings.SettingsManager;

public abstract class SteppedSSimApplication extends SimpleApplication implements SSimApplication {
    
    private Thread caller;
    
    private ScheduledExecutorService executor;
    
    public SteppedSSimApplication() {
        caller = Thread.currentThread();
    }
    
    @Override
    public void simpleInitApp() {
        executor = Executors.newScheduledThreadPool(2);
    }
    
    @Override
    public void simpleUpdate(float tpf) {
        super.simpleUpdate(tpf);
        
        pause();
    }

    @Override
    public void destroy() {
        super.destroy();
        
        // shutdown all (non daemon) worker pool threads
        executor.shutdown();
    }

    @Override
    public ScheduledExecutorService getExecutor() {
        return executor;
    }
    
    @Override
    public SettingsManager getSettingsManager() {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public float getSpeed() {
        return speed;
    }
    
    @Override
    public void setSpeed(float speed) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public GameMode getCurrentMode() {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void addGameModeListener(GameModeListener lis) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void removeGameModeListener(GameModeListener lis) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void doGameInit(Mission mission) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void doGamePause() {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void doGameResume() {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void doGameExit() {
        throw new UnsupportedOperationException();
    }
    
    /**
     * Gets called from controller/JUnit thread!
     */
    public void waitFor() {
        speed = 1;
        try {
            while(true) { Thread.sleep(1000); }
        } catch(InterruptedException ex) {
            // OK let's continue
        }
    }
    
    private void pause() {
        speed = 0;
        // and notify the boss
        caller.interrupt();
    }
}

@@ -31,16 +31,67 @@
 elper;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -214,16 +214,52 @@
 ication;
+%0Aimport com.jme3.app.state.AppState;
 %0A%0Aimport
@@ -666,24 +666,176 @@
 cutor;%0A    %0A
+    protected GameMode currentMode = GameMode.Running;%0A    protected List%3CGameModeListener%3E gameModeListeners = new ArrayList%3CGameModeListener%3E();%0A    %0A
     public S
@@ -856,24 +856,56 @@
 ication() %7B%0A
+        super(new AppState%5B0%5D);%0A
         call
@@ -1836,49 +1836,26 @@
     
-throw new UnsupportedOperationException()
+return currentMode
 ;%0A  
@@ -1949,50 +1949,96 @@
     
-throw new UnsupportedOperationException();
+if(!gameModeListeners.contains(lis)) %7B%0A            gameModeListeners.add(lis);%0A        %7D
 %0A   
@@ -2134,50 +2134,98 @@
     
-throw new UnsupportedOperationException();
+if(gameModeListeners.contains(lis)) %7B%0A            gameModeListeners.remove(lis);%0A        %7D
 %0A   

Upgrade SteppedSSimApplication to keep tests running