/*
 * This file is part of libbluray
 * Copyright (C) 2010  William Hahne
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package org.havi.ui;

import org.videolan.GUIManager;
import org.videolan.BDJXletContext;

public class HSceneFactory extends Object {
    private HSceneFactory() {
    }

    public static HSceneFactory getInstance() {
        BDJXletContext context = BDJXletContext.getCurrentContext();
        if (context != null) {
            if (context.getSceneFactory() == null) {
                context.setSceneFactory(new HSceneFactory());
            }
            return context.getSceneFactory();
        }

        org.videolan.Logger.getLogger("HSceneFactory").error("getInstance(): no context at " + org.videolan.Logger.dumpStack());

        return null;
    }

    public HSceneTemplate getBestSceneTemplate(HSceneTemplate template) {
        org.videolan.Logger.unimplemented("HSceneFactory", "getBestSceneTemplate");
        return null;
    }

    public HScene getBestScene(HSceneTemplate template) {
        return getDefaultHScene();
        // TODO
    }

    public HSceneTemplate resizeScene(HScene scene, HSceneTemplate template)
            throws IllegalStateException
    {
        org.videolan.Logger.unimplemented("HSceneFactory", "resizeScene");
        return template;
    }

    public HScene getDefaultHScene(HScreen screen)
    {
        synchronized(HSceneFactory.class) {
            if (defaultHScene == null) {
                defaultHScene = new HScene();
                defaultHScene.setLocation(0, 0);
                defaultHScene.setSize(GUIManager.getInstance().getWidth(), GUIManager.getInstance().getHeight());
                GUIManager.getInstance().add(defaultHScene);
            }
        }

        return defaultHScene;
    }

    public HScene getDefaultHScene()
    {
        synchronized(HSceneFactory.class) {
            if (defaultHScene == null) {
                defaultHScene = new HScene();
                defaultHScene.setLocation(0, 0);
                defaultHScene.setSize(GUIManager.getInstance().getWidth(), GUIManager.getInstance().getHeight());
                GUIManager.getInstance().add(defaultHScene);
            }
        }

        return defaultHScene;
    }

    public HScene getFullScreenScene(HGraphicsDevice device) {
        return getDefaultHScene();
/*
        HScene FullScreenScene = new HScene();
        FullScreenScene.setLocation(0, 0);
        FullScreenScene.setSize(GUIManager.getInstance().getWidth(), GUIManager.getInstance().getHeight());
        GUIManager.getInstance().add(FullScreenScene);
        return FullScreenScene;
*/
    }

    public void dispose(HScene scene) {
        GUIManager.getInstance().remove(scene);
    }

    public void dispose() {
        synchronized(HSceneFactory.class) {
            dispose(defaultHScene);
            defaultHScene = null;
        }
    }

    private HScene defaultHScene = null;
}

@@ -835,16 +835,44 @@
 Context;
+%0Aimport org.videolan.Logger;
 %0A%0Apublic
@@ -3337,24 +3337,337 @@
 ne scene) %7B%0A
+%0A        if (scene == null) %7B%0A            logger.error(%22null HScene%22);%0A            return;%0A        %7D%0A        if (defaultHScene == null) %7B%0A            logger.error(%22no HScene created%22);%0A            return;%0A        %7D%0A%0A        if (!scene.equals(defaultHScene)) %7B%0A            logger.error(%22wrong HScene%22);%0A        %7D%0A%0A
         GUIM
@@ -3698,24 +3698,54 @@
 ove(scene);%0A
+        defaultHScene = null;%0A
     %7D%0A%0A    p
@@ -3851,42 +3851,8 @@
 e);%0A
-            defaultHScene = null;%0A
     
@@ -3904,11 +3904,102 @@
  = null;
+%0A%0A    private static final Logger logger = Logger.getLogger(HSceneFactory.class.getName());
 %0A%7D%0A

Fix HSceneFactory.dispose(HScene). Never return disposed scene from factory. Fixes Terminator Salvation.