package de.prob2.ui.menu;

import java.io.File;
import java.io.IOException;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import de.codecentric.centerdevice.MenuToolkit;
import de.prob.scripting.Api;
import de.prob2.ui.events.OpenFileEvent;
import de.prob2.ui.modelchecking.ModelcheckingView;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.Window;

@Singleton
public class MenuController extends MenuBar {

	private EventBus bus;
	private Scene mcheckScene;

	@FXML
	private void handleOpen(ActionEvent event) {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Open File");
		fileChooser.getExtensionFilters().addAll(
				//new ExtensionFilter("All Files", "*.*"),
				new ExtensionFilter("Classical B Files", "*.mch", "*.ref", "*.imp")
		// new ExtensionFilter("EventB Files", "*.eventb", "*.bum", "*.buc"),
		// new ExtensionFilter("CSP Files", "*.cspm")
		);
		bus.post(fileChooser);
	}
	
	@FXML
	private void handleModelCheck(ActionEvent event) {
		Window stage = this.getScene().getWindow();
		Stage mcheckStage = new Stage();
        mcheckStage.setTitle("Model Check");
        mcheckStage.initOwner(stage);
		mcheckStage.setScene(mcheckScene);
        mcheckStage.showAndWait();
	}

	@Subscribe
	public void showFileDialogHandler(FileChooser chooser) {
		Window stage = this.getScene().getWindow();
		File selectedFile = chooser.showOpenDialog(stage);
		if (selectedFile != null) {
			String extensionFilter = chooser.getSelectedExtensionFilter().getDescription();
			bus.post(new OpenFileEvent(selectedFile, extensionFilter));
		}
	}

	@Inject
	public MenuController(FXMLLoader loader, Api api, EventBus bus, ModelcheckingView mcheckController) {
		this.bus = bus;
		this.mcheckScene = new Scene(mcheckController);
		try {
			loader.setLocation(getClass().getResource("menu.fxml"));
			loader.setRoot(this);
			loader.setController(this);
			loader.load();
		} catch (IOException e) {
			e.printStackTrace();
		}
		bus.register(this);
		
		/*
		if (System.getProperty("os.name", "").toLowerCase().contains("mac")) {
			// Mac-specific menu stuff
			this.setUseSystemMenuBar(true);
			MenuToolkit tk = MenuToolkit.toolkit();
			
			// Create Mac-style application menu
			Menu applicationMenu = tk.createDefaultApplicationMenu("ProB 2");
			this.getMenus().add(0, applicationMenu);
			tk.setApplicationMenu(applicationMenu);
			
			// Move About menu item from Help to application menu
			Menu helpMenu = this.getMenus().get(this.getMenus().size()-1);
			MenuItem aboutItem = helpMenu.getItems().get(helpMenu.getItems().size()-1);
			aboutItem.setText("About ProB 2");
			helpMenu.getItems().remove(aboutItem);
			applicationMenu.getItems().set(0, aboutItem);
			
			// Create Mac-style Window menu
			Menu windowMenu = new Menu("Window");
			windowMenu.getItems().addAll(
				tk.createMinimizeMenuItem(),
				tk.createZoomMenuItem(),
				tk.createCycleWindowsItem(),
				new SeparatorMenuItem(),
				tk.createBringAllToFrontItem(),
				new SeparatorMenuItem()
			);
			tk.autoAddWindowMenuItems(windowMenu);
			this.getMenus().add(this.getMenus().size()-1, windowMenu);
			
			// Make this the global menu bar
			tk.setGlobalMenuBar(this);
		}
		*/
	}

}

@@ -400,16 +400,69 @@
 ngView;%0A
+import javafx.beans.property.ReadOnlyObjectProperty;%0A
 import j
@@ -482,24 +482,24 @@
 ctionEvent;%0A
-
 import javaf
@@ -971,16 +971,40 @@
 ckScene;
+%0A%09private Window window;
 %0A%0A%09@FXML
@@ -1515,54 +1515,8 @@
 ) %7B%0A
-%09%09Window stage = this.getScene().getWindow();%0A
 %09%09St
@@ -1621,21 +1621,27 @@
 itOwner(
-stage
+this.window
 );%0A%09%09mch
@@ -1785,54 +1785,8 @@
 ) %7B%0A
-%09%09Window stage = this.getScene().getWindow();%0A
 %09%09Fi
@@ -1826,21 +1826,27 @@
 nDialog(
-stage
+this.window
 );%0A%09%09if 
@@ -2022,16 +2022,346 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09@FXML%0A%09public void initialize() %7B%0A%09%09this.sceneProperty().addListener(observable -%3E %7B%0A%09%09%09Scene scene = ((ReadOnlyObjectProperty%3CScene%3E)observable).get();%0A%09%09%09if (scene != null) %7B%0A%09%09%09%09scene.windowProperty().addListener(observable1 -%3E %7B%0A%09%09%09%09%09this.window = ((ReadOnlyObjectProperty%3CWindow%3E)observable1).get();%0A%09%09%09%09%7D);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D
 %0A%0A%09@Inje
@@ -2761,13 +2761,8 @@
 %0A%09%09%0A
-%09%09/*%0A
 %09%09if
@@ -3958,17 +3958,12 @@
 s);%0A
+
 %09%09%7D%0A
-%09%09*/%0A
 %09%7D%0A%0A

Fix the Mac menu and file dialog issue