/*
 * Copyright 2014-2015 Pawel Pastuszak
 *
 * This file is part of VisEditor.
 *
 * VisEditor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VisEditor 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VisEditor.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.kotcrab.vis.editor.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglCanvas;
import com.kotcrab.vis.editor.App;
import com.kotcrab.vis.editor.Editor;
import com.kotcrab.vis.editor.util.Log;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Set;

public class EditorFrame extends JFrame {
	private Editor editor;

	public EditorFrame () {
		setTitle("VisEditor");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing (WindowEvent e) {
				performEditorExit();
			}
		});


		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
		config.width = 1280;
		config.height = 720;
		config.backgroundFPS = -1;

		editor = new Editor(this);

		LwjglCanvas editorCanvas = new LwjglCanvas(editor, config);
		Canvas canvas = editorCanvas.getCanvas();
		canvas.setSize(1280, 720);

		getContentPane().add(canvas, BorderLayout.CENTER);

		pack();
		setLocationRelativeTo(null);
	}

	public static void main (String[] args) {
		App.init();

		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run () {
				new EditorFrame().setVisible(true);
			}
		});
	}

	/**
	 * Performs editor exit, if editor is still running, this will cause to display "Do you really want to exit?" dialog in editor window.
	 * If editor LibGDX thread died, for example. after uncaught GdxRuntimeException this will simply kill app.
	 */
	private void performEditorExit () {
		Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

		for (Thread thread : threadSet) {
			if (thread.getName().contains("LWJGL Timer")) {
				editor.requestExit();
				return;
			}
		}

		Log.fatal("Editor LibGDX thread is not running, performing force exit.");
		Log.dispose();
		System.exit(-1);
	}

	@Override
	public void dispose () {
		super.dispose();
		Gdx.app.exit();
	}
}

@@ -1046,16 +1046,98 @@
 JFrame;%0A
+import javax.swing.UIManager;%0Aimport javax.swing.UnsupportedLookAndFeelException;%0A
 import j
@@ -2095,16 +2095,248 @@
 nit();%0A%0A
+%09%09try %7B%0A%09%09%09UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());%0A%09%09%7D catch (ClassNotFoundException %7C InstantiationException %7C UnsupportedLookAndFeelException %7C IllegalAccessException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%0A
 %09%09EventQ

Set system LAF