/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.sparkimpl.plugin.emoticons;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.text.BadLocationException;

import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.RolloverButton;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.ui.ChatInputEditor;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomClosingListener;
import org.jivesoftware.spark.ui.ChatRoomListener;
import org.jivesoftware.spark.ui.themes.ThemePreference;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.emoticons.EmoticonUI.EmoticonPickListener;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

/**
 * Adds an EmoticonPickList to each ChatRoom.
 */
public class EmoticonPlugin implements Plugin, ChatRoomListener {

	private EmoticonManager emoticonManager;
	private ChatManager chatManager;

	public void initialize() {
		emoticonManager = EmoticonManager.getInstance();
		chatManager = SparkManager.getChatManager();
		addChatRoomListener();
	}

	/**
	 * Listen for rooms opening to add emoticon picker.
	 */
	private void addChatRoomListener() {
		// Adds the listener
		chatManager.addChatRoomListener(this);

		// Add Preferences
		SparkManager.getPreferenceManager()
				.addPreference(new ThemePreference());
	}

	public void chatRoomOpened(final ChatRoom room) {
		// Check to see if emoticons are enabled.
		if (!SettingsManager.getLocalPreferences().areEmoticonsEnabled()) {
			return;
		}

		// final String activeEmoticonSetName =
		// emoticonManager.getActiveEmoticonSetName();

		emoticonManager = EmoticonManager.getInstance();
		Collection<String> emoticonPacks = null;
		emoticonPacks = emoticonManager.getEmoticonPacks();

		if (emoticonPacks != null) {

			// Add Emoticon button
			ImageIcon icon = null;
			final RolloverButton emoticonPicker;
			icon = new ImageIcon(getClass().getClassLoader().getResource(
					"images/smileBtn.png"));

			if (icon == null) {
				emoticonPicker = new RolloverButton("Smiley's", icon);

			} else {
				emoticonPicker = new RolloverButton(icon);
			}

			room.getEditorBar().add(emoticonPicker);

			emoticonPicker.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					// Show popup
					final JPopupMenu popup = new JPopupMenu();
					EmoticonUI emoticonUI = new EmoticonUI();
					emoticonUI
							.setEmoticonPickListener(new EmoticonPickListener() {
								public void emoticonPicked(String emoticon) {
									try {
										popup.setVisible(false);
										final ChatInputEditor editor = room
												.getChatInputEditor();
										String currentText = editor.getText();
										if (currentText.length() == 0
												|| currentText.endsWith(" ")) {
											room.getChatInputEditor()
													.insertText(emoticon + " ");
										} else {
											room.getChatInputEditor()
													.insertText(
															" " + emoticon
																	+ " ");
										}
										room.getChatInputEditor()
												.requestFocus();
									} catch (BadLocationException e1) {
										Log.error(e1);
									}

								}
							});

					popup.add(emoticonUI);
					popup.show(emoticonPicker, e.getX(), e.getY());
				}
			});

			room.addClosingListener(new ChatRoomClosingListener() {
				public void closing() {
					room.getEditorBar().remove(emoticonPicker);
				}
			});
		}
	}

	public void chatRoomLeft(ChatRoom room) {
	}

	public void chatRoomClosed(ChatRoom room) {
	}

	public void chatRoomActivated(ChatRoom room) {
	}

	public void userHasJoined(ChatRoom room, String userid) {
	}

	public void userHasLeft(ChatRoom room, String userid) {
	}

	public void shutdown() {

	}

	public boolean canShutDown() {
		return false;
	}

	public void uninstall() {
		// Do nothing.
	}

}

@@ -358,16 +358,37 @@
 eEvent;%0A
+import java.net.URL;%0A
 import j
@@ -2268,34 +2268,8 @@
 ton%0A
-%09%09%09ImageIcon icon = null;%0A
 %09%09%09f
@@ -2311,100 +2311,298 @@
 %0A%09%09%09
-icon = new ImageIcon(getClass().getClassLoader().getResource(%0A%09%09%09%09%09%22images/smileBtn.png%22));%0A
+%0A%09%09%09final String activeEmoticonSetName = emoticonManager.getActiveEmoticonSetName();%0A%09%09%09final Emoticon smileEmoticon = emoticonManager.getEmoticon(activeEmoticonSetName, %22:)%22);%0A%09%09%09URL smileURL = emoticonManager.getEmoticonURL(smileEmoticon);%0A%09%09%09ImageIcon icon = new ImageIcon(smileURL);%0A%09%09%09
 %0A%09%09%09
@@ -3232,29 +3232,16 @@
 r = room
-%0A%09%09%09%09%09%09%09%09%09%09%09%09
 .getChat
@@ -3343,29 +3343,17 @@
 h() == 0
-%0A%09%09%09%09%09%09%09%09%09%09%09%09
+ 
 %7C%7C curre
@@ -3408,38 +3408,24 @@
 nputEditor()
-%0A%09%09%09%09%09%09%09%09%09%09%09%09%09
 .insertText(
@@ -3526,56 +3526,23 @@
 ext(
-%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%22 %22 + emoticon%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09
+%22 %22 + emoticon 
 + %22 
@@ -3592,29 +3592,16 @@
 Editor()
-%0A%09%09%09%09%09%09%09%09%09%09%09%09
 .request

fix patch from walter.ebeling