package org.minimalj.frontend.swing;

import java.util.List;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.text.DefaultEditorKit;

import org.minimalj.frontend.page.Separator;
import org.minimalj.frontend.swing.lookAndFeel.LookAndFeelAction;
import org.minimalj.frontend.swing.lookAndFeel.PrintLookAndFeel;
import org.minimalj.frontend.swing.lookAndFeel.TerminalLargeFontLookAndFeel;
import org.minimalj.frontend.swing.lookAndFeel.TerminalLookAndFeel;
import org.minimalj.frontend.swing.toolkit.SwingClientToolkit;
import org.minimalj.frontend.toolkit.Action;
import org.minimalj.util.StringUtils;
import org.minimalj.util.resources.Resources;

public class SwingMenuBar extends JMenuBar {
	private static final long serialVersionUID = 1L;
	
	private final SwingTab tab;

	public SwingMenuBar(SwingTab tab) {
		super();
		this.tab = tab;

		add(createFileMenu());
		add(createEditMenu());
		add(createViewMenu());
		add(createWindowMenu());
	}
	
	private JMenu createFileMenu() {
		JMenu menu = menu("file");
		
		menu.add(new JMenuItem(tab.frame.closeWindowAction));
		menu.add(new JMenuItem(tab.closeTabAction));
		menu.addSeparator();
		menu.add(new JMenuItem(tab.frame.exitAction));
		return menu;
	}
	
	private JMenu createEditMenu() {
		JMenu menu = menu("edit");
		menu.add(new JMenuItem(SwingResourceAction.initProperties(new DefaultEditorKit.CutAction(), "cut")));
		menu.add(new JMenuItem(SwingResourceAction.initProperties(new DefaultEditorKit.CopyAction(), "copy")));
		menu.add(new JMenuItem(SwingResourceAction.initProperties(new DefaultEditorKit.PasteAction(), "paste")));
		return menu;
	}
	
	private JMenu createViewMenu() {
		JMenu menu = menu("view");
		menu.add(new JMenuItem(tab.previousAction));
		menu.add(new JMenuItem(tab.nextAction));
		menu.add(new JMenuItem(tab.refreshAction));
//		menu.addSeparator();
//		menu.add(new JMenuItem(tab.menuItemToolBarVisible));
		menu.addSeparator();
		menu.add(createLookAndFeeldMenu());
		return menu;
	}

	private JMenu createLookAndFeeldMenu() {
		JMenu menu = menu("lookAndFeel");
		menu.add(new JMenuItem(new LookAndFeelAction("system")));
		menu.add(new JMenuItem(new LookAndFeelAction("highContrast", TerminalLookAndFeel.class.getName())));
		menu.add(new JMenuItem(new LookAndFeelAction("highContrastLarge", TerminalLargeFontLookAndFeel.class.getName())));
		menu.add(new JMenuItem(new LookAndFeelAction("print", PrintLookAndFeel.class.getName())));
		return menu;
	}

	private JMenu createWindowMenu() {
		JMenu menu = menu("window");
		menu.add(new JMenuItem(tab.frame.newWindowAction));
		menu.add(new JMenuItem(tab.frame.newTabAction));
		return menu;
	}

	//
	
	private JMenu menu(String resourceName) {
		String text = Resources.getString("Menu." + resourceName);
		JMenu menu = new JMenu(text);
		Integer mnemonic = SwingResourceAction.getKeyCode("Menu." + resourceName + ".mnemonic");
		if (mnemonic != null) {
			menu.setMnemonic(mnemonic);
		} else if (!StringUtils.isEmpty(text)) {
			menu.setMnemonic(text.charAt(0));
		}
		return menu;
	}
	
	private JMenu menu(String type, List<Action> actions) {
		JMenu subMenu = menu(type);
		addActions(subMenu, actions);
		return subMenu;
	}
	
	public static void addActions(JMenu menu, List<Action> actions) {
		for (Action action : actions) {
			if (action instanceof org.minimalj.frontend.page.ActionGroup) {
				org.minimalj.frontend.page.ActionGroup actionGroup = (org.minimalj.frontend.page.ActionGroup) action;
				JMenu subMenu = new JMenu(SwingClientToolkit.adaptAction(action));
				addActions(subMenu, actionGroup.getItems());
				menu.add(subMenu);
			} else if (action instanceof Separator) {
				menu.addSeparator();
			} else {
				menu.add(new JMenuItem(SwingClientToolkit.adaptAction(action)));
			}
		}
	}
}	

@@ -899,28 +899,30 @@
 %09%09add(create
-File
+Window
 Menu());%0A%09%09a
@@ -972,35 +972,8 @@
 ));%0A
-%09%09add(createWindowMenu());%0A
 %09%7D%0A%09
@@ -998,12 +998,14 @@
 eate
-File
+Window
 Menu
@@ -1034,12 +1034,14 @@
 nu(%22
-file
+window
 %22);%0A
@@ -1082,19 +1082,147 @@
 ame.
-closeWindow
+newWindowAction));%0A%09%09menu.add(new JMenuItem(tab.frame.closeWindowAction));%0A%09%09menu.addSeparator();%0A%09%09menu.add(new JMenuItem(tab.frame.newTab
 Acti
@@ -2602,199 +2602,8 @@
 %09%7D%0A%0A
-%09private JMenu createWindowMenu() %7B%0A%09%09JMenu menu = menu(%22window%22);%0A%09%09menu.add(new JMenuItem(tab.frame.newWindowAction));%0A%09%09menu.add(new JMenuItem(tab.frame.newTabAction));%0A%09%09return menu;%0A%09%7D%0A%0A
 %09//%0A

replace file menu with window menu. Nothing file related