package imagej.plugin.ij2;

import imagej.Log;
import imagej.plugin.MenuEntry;
import imagej.plugin.PluginEntry;
import imagej.plugin.PluginFinder;

import java.util.ArrayList;
import java.util.List;

import net.java.sezpoz.Index;
import net.java.sezpoz.IndexItem;

import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service=PluginFinder.class)
public class Ij2PluginFinder implements PluginFinder {

	@Override
	public void findPlugins(List<PluginEntry> plugins) {
		// use SezPoz to discover available IJ2 plugins
		for (final IndexItem<Plugin, IPlugin> item :
			Index.load(Plugin.class, IPlugin.class))
		{
			final String pluginClass = item.className();
			final List<MenuEntry> menuPath = new ArrayList<MenuEntry>();

			// parse menu path from annotations
			final Menu[] menu = item.annotation().menu();
			if (menu.length > 0) {
				parseMenuPath(menuPath, menu);
			}
			else {
				// parse menuPath attribute
				final String path = item.annotation().menuPath();
				parseMenuPath(menuPath, path);
			}

			final String arg = "";
			final PluginEntry pluginEntry =
				new PluginEntry(pluginClass, menuPath, arg);
			plugins.add(pluginEntry);
			Log.debug("Found plugin: " + pluginEntry);
		}
	}

	private void parseMenuPath(final List<MenuEntry> menuPath,
		final Menu[] menu)
	{
		for (int i = 0; i < menu.length; i++) {
			final String name = menu[i].label();
			final double weight = menu[i].weight();
			final char mnemonic = menu[i].mnemonic();
			final String accelerator = menu[i].accelerator();
			final String icon = menu[i].icon();				
			menuPath.add(new MenuEntry(name, weight, mnemonic, accelerator, icon));
		}
	}

	private void parseMenuPath(final List<MenuEntry> menuPath,
		final String path)
	{
		final String[] menuPathTokens = path.split(">");
		for (String token : menuPathTokens) menuPath.add(new MenuEntry(token));
	}

}

@@ -474,16 +474,61 @@
 gins) %7B%0A
+%09%09Log.debug(%22Searching for IJ2 plugins...%22);%0A
 %09%09// use

Add debug statement indicating initiation of plugin search.