package at.int32.sweaty.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

public abstract class Window extends Control {
	private Display display;
	private Shell shell;

	public abstract void onExit();

	public Window() {
		display = Display.getDefault();
		shell = new Shell();
	}

	@Override
	public Composite onCreate() {
		return new Composite(parent(), SWT.NONE);
	}

	public void centerOnScreen() {
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();

		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;

		shell.setLocation(x, y);
	}

	public void show() {
		create(shell);
		ctrl().setLayout(Layout.Grid.layout(1));
		ctrl().setLayoutData(Layout.Grid.data(0, true, true));

		setDefaultLayout();
		shell.open();
		shell.layout();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		onExit();
	}

	public void hide() {
		if (display != null)
			display.dispose();
	}

	public Composite raw() {
		return shell;
	}

	public void setTitle(String text) {
		shell.setText(text);
	}

	public void setSize(int x, int y) {
		shell.setSize(x, y);
	}

	public void setAppName(String text) {
		Display.setAppName(text);
	}

	private void setDefaultLayout() {
		GridLayout layout = new GridLayout(1, false);
		layout.makeColumnsEqualWidth = true;
		layout.marginBottom = 0;
		layout.marginTop = 0;
		layout.marginLeft = 0;
		layout.marginRight = 0;
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.marginWidth = 0;
		layout.marginHeight = 0;

		shell.setLayout(layout);
	}
}

@@ -50,16 +50,55 @@
 wt.SWT;%0A
+import org.eclipse.swt.graphics.Image;%0A
 import o
@@ -463,16 +463,17 @@
 Exit();%0A
+%09
 %0A%09public
@@ -480,19 +480,64 @@
  Window(
-) %7B
+String appName) %7B%0A%09%09Display.setAppName(appName);
 %0A%09%09displ
@@ -1558,53 +1558,47 @@
 tApp
-Name(String text) %7B%0A%09%09Display.setAppName(text
+Image(Image img) %7B%0A%09%09shell.setImage(img
 );%0A%09

set app name