//
// Triple Play - utilities for use in PlayN-based games
// Copyright (c) 2011-2013, Three Rings Design, Inc. - All rights reserved.
// http://github.com/threerings/tripleplay/blob/master/LICENSE

package tripleplay.platform;

import java.awt.Dimension;
import java.awt.Canvas;
import javax.swing.JFrame;

import org.lwjgl.opengl.Display;

import playn.core.Keyboard;
import playn.java.JavaPlatform;
import react.Value;
import react.ValueView;
import tripleplay.ui.Field;

/**
 * Implements Java-specific TriplePlay services.
 */
public class JavaTPPlatform extends TPPlatform
{
    /** Registers the IOS TriplePlay platform. */
    public static JavaTPPlatform register (JavaPlatform platform, JavaPlatform.Config config) {
        JavaTPPlatform instance = new JavaTPPlatform(platform, config);
        TPPlatform.register(instance);
        return instance;
    }

    protected JavaTPPlatform (JavaPlatform platform, JavaPlatform.Config config) {
        _platform = platform;

        _frame = new JFrame("Game");
        _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Canvas canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(config.width, config.height));
        _frame.getContentPane().add(canvas);

        // NOTE: This order is important. Resizability changes window decorations on some
        // platforms/themes and we need the packing to happen last to take that into account.
        _frame.setResizable(false);
        _frame.setVisible(true);
        _frame.pack();

        try {
            Display.setParent(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override public boolean hasNativeTextFields () {
        return true;
    }

    @Override public NativeTextField createNativeTextField (Field field) {
        return new JavaNativeTextField(_frame.getLayeredPane(), field);
    }

    @Override public void setVirtualKeyboardController (VirtualKeyboardController ctrl) {
        // nada, no virtual keyboard
    }

    @Override public void setVirtualKeyboardListener (Keyboard.Listener listener) {
        // nada, no virtual keyboard
    }

    @Override public ValueView<Boolean> virtualKeyboardActive () {
        return _false;
    }

    /**
     * Sets the title of the window.
     *
     * @param title the window title
     */
    public void setTitle (String title) {
        _frame.setTitle(title);
    }

    /** The Java platform with which this TPPlatform was registered. */
    protected JavaPlatform _platform;

    protected JFrame _frame;

    protected final Value<Boolean> _false = Value.create(false);
}

@@ -222,16 +222,40 @@
 tform;%0A%0A
+import java.awt.Canvas;%0A
 import j
@@ -285,32 +285,32 @@
 rt java.awt.
-Canvas
+Image
 ;%0A
+%0A
 import javax
@@ -359,16 +359,43 @@
 splay;%0A%0A
+import playn.core.Asserts;%0A
 import p
@@ -414,16 +414,45 @@
 yboard;%0A
+import playn.java.JavaImage;%0A
 import p
@@ -519,16 +519,16 @@
 ueView;%0A
-
 import t
@@ -548,16 +548,97 @@
 Field;%0A%0A
+import com.google.common.base.Function;%0Aimport com.google.common.collect.Lists;%0A%0A
 /**%0A * I
@@ -2581,24 +2581,564 @@
 le);%0A    %7D%0A%0A
+    /**%0A     * Sets the window icon.%0A     *%0A     * Takes icons of different sizes, preferring earlier ones in case of duplicate sizes.%0A     */%0A    public void setIcon (playn.core.Image... icons) %7B%0A        Asserts.check(icons.length %3E 0);%0A        _frame.setIconImages(Lists.transform(Lists.newArrayList(icons),%0A            new Function%3Cplayn.core.Image, java.awt.Image%3E() %7B%0A                public Image apply (playn.core.Image input) %7B%0A                    return ((JavaImage)input).bufferedImage();%0A                %7D%0A            %7D));%0A    %7D%0A%0A
     /** The 

Allow setting the window title on java.