package org.tomighty;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.tomighty.bus.Bus;
import org.tomighty.bus.Subscriber;
import org.tomighty.bus.messages.ChangeState;
import org.tomighty.states.InitialState;
import org.tomighty.util.Images;
import org.tomighty.util.New;

public class Tomighty {
	
	private JFrame window;
	private JPanel contentPane;
	private Logger logger;
	private TrayIcon trayIcon;
	private SystemTray tray;

	public Tomighty() throws AWTException {
		logger = Logger.getLogger(getClass().getName());
		
		contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		contentPane.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
		
		window = new JFrame("Tomighty");
		window.setAlwaysOnTop(true);
		window.setContentPane(contentPane);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLocation(100, 100);
		window.setResizable(false);
		window.setSize(150, 100);
		window.setUndecorated(true);
		
		trayIcon = new TrayIcon(Images.get("/tomato-16x16.png"));
		trayIcon.addMouseListener(new TrayClick());
		trayIcon.setToolTip("Click to show/hide Tomighty");
		
		tray = SystemTray.getSystemTray();
		tray.add(trayIcon);
		
		MenuItem closeItem = new MenuItem("Close");
		PopupMenu popupMenu = new PopupMenu();
		popupMenu.add(closeItem);
		closeItem.addActionListener(new Exit());
		trayIcon.setPopupMenu(popupMenu);
	}
	
	public void start() {
		Bus.subscribe(new StateSwitch(), ChangeState.class);
		render(InitialState.class);
		trayIcon.displayMessage(null, "Click here to show Tomighty", MessageType.NONE);
	}
	
	private void render(Class<? extends State> stateClass) {
		State state = New.instanceOf(stateClass);
		Component component;
		try {
			component = state.render();
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Failed to render state: "+state, e);
			return;
		}
		contentPane.removeAll();
		contentPane.add(component, BorderLayout.CENTER);
		contentPane.validate();
	}

	private class StateSwitch implements Subscriber<ChangeState> {
		@Override
		public void receive(ChangeState message) {
			Class<? extends State> stateClass = message.getStateClass();
			render(stateClass);
		}
	}
	
	private class TrayClick extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			if(e.getButton() == MouseEvent.BUTTON1) {
				window.setVisible(!window.isVisible());
			} else if(e.getButton() == MouseEvent.BUTTON2) {
				
			}
		}
	}
	
	private class Exit implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
	
	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new Tomighty().start();
	}

}

@@ -2989,67 +2989,8 @@
 );%0D%0A
-%09%09%09%7D else if(e.getButton() == MouseEvent.BUTTON2) %7B%0D%0A%09%09%09%09%0D%0A
 %09%09%09%7D

Remove dead code