package beaform.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import beaform.gui.debug.ClearDbEvent;
import beaform.gui.debug.FillDbEvent;
import beaform.gui.debug.ListFormulasEvent;
import beaform.gui.search.NewSearchWindowAction;


/**
 * This class represents the main user interface.
 * @author Steven
 *
 */
public class MainGUI {

	/** A logger */
	private static final Logger LOG = LoggerFactory.getLogger(MainGUI.class);

	/** The main instance of the GUI */
	private static MainGUI instance;

	/** The main frame (or window) */
	private static JFrame frm = new JFrame("BeaForm");

	/** The menu */
	private final JMenuBar menu = new JMenuBar();

	/** The main panel */
	private final MainPanel panel = new MainPanel();

	private void init(){

		//Create pane and add components
		createMenu(this.menu);

		//Putting components in place
		frm.setJMenuBar(this.menu);

		LOG.info("end init");
	}

	/**
	 * This method will replace the current active window.
	 *
	 * @param comp The new window to display
	 */
	public void replaceWindow(final Component comp) {
		this.panel.replaceWindow(comp);
	}

	private JPanel getPanel() {
		return this.panel;
	}

	private void createMenu(final JMenuBar menu) {
		menu.add(createNewMenu());
		menu.add(createHelpMenu());
		menu.add(createDebugMenu());
	}

	private JMenu createNewMenu() {
		final JMenu newMenuItem = new JMenu("New...");

		final JMenuItem search = new JMenuItem("Search");
		newMenuItem.add(search);
		search.addActionListener(new NewSearchWindowAction());

		final JMenuItem add = new JMenuItem("Add");
		newMenuItem.add(add);
		add.addActionListener(new NewAddWindowAction());

		return newMenuItem;
	}

	private JMenu createHelpMenu() {
		final JMenu helpmenu = new JMenu("Help");

		final JMenuItem about = new JMenuItem("About...");
		helpmenu.add(about);
		about.addActionListener(new AboutLaunchAction());

		return helpmenu;
	}

	private JMenu createDebugMenu() {
		final JMenu debugMenuItem = new JMenu("Debug");

		final JMenuItem dbgAllFormulas = new JMenuItem("List all formulas");
		debugMenuItem.add(dbgAllFormulas);
		dbgAllFormulas.addActionListener(new ListFormulasEvent());

		final JMenuItem dbgFill = new JMenuItem("Fill DB");
		debugMenuItem.add(dbgFill);
		dbgFill.addActionListener(new FillDbEvent());

		final JMenuItem dbgClear = new JMenuItem("Clear DB");
		debugMenuItem.add(dbgClear);
		dbgClear.addActionListener(new ClearDbEvent());

		return debugMenuItem;
	}

	/**
	 * Gets the (current) instance.
	 * @return the instance of this window
	 */
	public static MainGUI getInstance() {
		if (instance == null) {
			throw new IllegalStateException("The MainGUI should be initialized before trying to get the instance");
		}
		return instance;
	}

	public static void createAndShowGUI() {
		//Set the look and feel.
		try{
			// Using OpenJDK, there is a bug that causes the
			// application to freeze when using a GTK look and feel.
			final boolean crossplatform = Boolean.getBoolean("beaform.useCrossPlatformLookAndFeel");
			if (crossplatform) {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			}
			else {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
		}
		catch (Exception e){
			if (LOG.isErrorEnabled()) {
				LOG.error("Look'n feel: " + e.getMessage());
			}
		}

		//Make sure we have nice window decorations.
		JFrame.setDefaultLookAndFeelDecorated(true);

		//Create and set up the window.
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final MainGUI app = new MainGUI();
		app.init();
		frm.add(app.getPanel());
		frm.setLocation(150, 150);

		//Display the window.
		//frm.pack();
		frm.setSize(600, 400);
		frm.setVisible(true);

		instance = app;
	}

	/**
	 * This action lets one launch the 'about' window.
	 *
	 * @author steven
	 *
	 */
	public static final class AboutLaunchAction implements ActionListener {

		/**
		 * Invoked when the action is triggered.
		 *
		 * @param the event object
		 */
		@Override
		public void actionPerformed(final ActionEvent event){
			javax.swing.SwingUtilities.invokeLater(new About());
		}
	}

}
@@ -876,73 +876,8 @@
 );%0A%0A
-%09/** The menu */%0A%09private final JMenuBar menu = new JMenuBar();%0A%0A
 %09/**
@@ -972,101 +972,45 @@
 ()%7B%0A
-%0A
 %09%09
-//Create pane and add components%0A%09%09createMenu(this.menu);%0A%0A%09%09//Putting components in place
+final JMenuBar menu = createMenu();
 %0A%09%09f
@@ -1024,21 +1024,16 @@
 MenuBar(
-this.
 menu);%0A%0A
@@ -1042,12 +1042,13 @@
 LOG.
-info
+debug
 (%22en
@@ -1325,20 +1325,24 @@
 private 
-void
+JMenuBar
  createM
@@ -1345,16 +1345,22 @@
 ateMenu(
+) %7B%0A%09%09
 final JM
@@ -1370,19 +1370,34 @@
 Bar menu
-) %7B
+ = new JMenuBar();
 %0A%09%09menu.
@@ -1478,16 +1478,32 @@
 Menu());
+%0A%0A%09%09return menu;
 %0A%09%7D%0A%0A%09pr

Make the menu code more confined