package com.digero.maestro;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.UIManager;

import com.digero.common.util.Version;
import com.digero.maestro.view.ProjectFrame;

public class MaestroMain {
	public static final String APP_NAME = "LOTRO Maestro";
	public static final String APP_URL = "http://lotro.acasylum.com/maestro/";
	public static final Version APP_VERSION = new Version(1, 0, 0);

	private static ProjectFrame mainWindow = null;

	public static void main(String[] args) throws Exception {
		System.setProperty("sun.sound.useNewAudioEngine", "true");

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e) {}

		mainWindow = new ProjectFrame();
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainWindow.setVisible(true);
		openSongFromCommandLine(args);
		try {
			ready();
		}
		catch (UnsatisfiedLinkError err) {
			// Ignore (we weren't started via WinRun4J)
		}
	}

	/** Tells the WinRun4J launcher that we're ready to accept activate() calls. */
	public static native void ready();

	/** A new activation from WinRun4J (a.k.a. a file was opened) */
	public static void activate(String[] args) {
		openSongFromCommandLine(args);
	}

	public static void execute(String cmdLine) {
		openSongFromCommandLine(new String[] {
			cmdLine
		});
	}

	private static void openSongFromCommandLine(String[] args) {
		if (mainWindow == null)
			return;

		int state = mainWindow.getExtendedState();
		if ((state & JFrame.ICONIFIED) != 0)
			mainWindow.setExtendedState(state & ~JFrame.ICONIFIED);

		if (args.length > 0) {
			File file = new File(args[0]);
			if (file.exists())
				mainWindow.openSong(file);
		}
	}

	/** @deprecated Use isNativeVolumeSupported() instead. */
	public static native boolean isVolumeSupported();

	public static boolean isNativeVolumeSupported() {
		try {
			return isVolumeSupported();
		}
		catch (UnsatisfiedLinkError err) {
			return false;
		}
	}

	public static native float getVolume();

	public static native void setVolume(float volume);

	public static void onVolumeChanged() {
		if (mainWindow != null)
			mainWindow.onVolumeChanged();
	}
}

@@ -76,16 +76,52 @@
 Frame;%0D%0A
+import javax.swing.SwingUtilities;%0D%0A
 import j
@@ -143,16 +143,16 @@
 nager;%0D%0A
-
 %0D%0Aimport
@@ -539,16 +539,22 @@
 id main(
+final 
 String%5B%5D
@@ -853,24 +853,99 @@
 ON_CLOSE);%0D%0A
+%09%09SwingUtilities.invokeAndWait(new Runnable() %7B%0D%0A%09%09%09public void run() %7B%0D%0A%09%09
 %09%09mainWindow
@@ -964,16 +964,18 @@
 true);%0D%0A
+%09%09
 %09%09openSo
@@ -1000,16 +1000,29 @@
 args);%0D%0A
+%09%09%09%7D%0D%0A%09%09%7D);%0D%0A
 %09%09try %7B%0D
@@ -1354,16 +1354,22 @@
 ctivate(
+final 
 String%5B%5D
@@ -1374,24 +1374,97 @@
 %5B%5D args) %7B%0D%0A
+%09%09SwingUtilities.invokeLater(new Runnable() %7B%0D%0A%09%09%09public void run() %7B%0D%0A%09%09
 %09%09openSongFr
@@ -1481,24 +1481,37 @@
 ine(args);%0D%0A
+%09%09%09%7D%0D%0A%09%09%7D);%0D%0A
 %09%7D%0D%0A%0D%0A%09publi

Make sure songs opened by second instances of Maestro are opened on the Swing UI thread