/*-
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2018 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package net.imagej.legacy.plugin;

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

import net.imagej.ImageJService;

import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.CompletionProvider;
import org.scijava.plugin.AbstractPTService;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.PluginInfo;
import org.scijava.service.Service;

/**
 * This service manages extensions to the auto-complete pull down of the script
 * editor.
 * 
 * @author Robert Haase
 */
@Plugin(type = Service.class)
public class MacroExtensionAutoCompletionService extends
	AbstractPTService<MacroExtensionAutoCompletionPlugin> implements ImageJService
{

	private final HashMap<String, PluginInfo<MacroExtensionAutoCompletionPlugin>> macroExtensionAutoCompletionPlugins =
		new HashMap<>();

	boolean initialized = false;

	private void initializeService() {
		if (initialized) {
			return;
		}
		for (final PluginInfo<MacroExtensionAutoCompletionPlugin> info : getPlugins()) {
			String name = info.getName();
			if (name == null || name.isEmpty()) {
				name = info.getClassName();
			}
			macroExtensionAutoCompletionPlugins.put(name, info);
		}
		initialized = true;
	}

	public List<BasicCompletion> getCompletions(
		final CompletionProvider completionProvider)
	{
		initializeService();
		final ArrayList<BasicCompletion> completions = new ArrayList<>();
		for (final String key : macroExtensionAutoCompletionPlugins.keySet()) {
			final PluginInfo<MacroExtensionAutoCompletionPlugin> info =
				macroExtensionAutoCompletionPlugins.get(key);

			final List<BasicCompletion> list = pluginService().createInstance(info)
				.getCompletions(completionProvider);
			completions.addAll(list);

		}
		return completions;
	}

	@Override
	public Class<MacroExtensionAutoCompletionPlugin> getPluginType() {
		return MacroExtensionAutoCompletionPlugin.class;
	}
}

@@ -2468,360 +2468,8 @@
 e;%0A%0A
-%09private void initializeService() %7B%0A%09%09if (initialized) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09for (final PluginInfo%3CMacroExtensionAutoCompletionPlugin%3E info : getPlugins()) %7B%0A%09%09%09String name = info.getName();%0A%09%09%09if (name == null %7C%7C name.isEmpty()) %7B%0A%09%09%09%09name = info.getClassName();%0A%09%09%09%7D%0A%09%09%09macroExtensionAutoCompletionPlugins.put(name, info);%0A%09%09%7D%0A%09%09initialized = true;%0A%09%7D%0A%0A
 %09pub
@@ -2510,16 +2510,16 @@
 etions(%0A
+
 %09%09final 
@@ -2560,18 +2560,36 @@
 der)%0A%09%7B%0A
-
 %09%09
+if (!initialized) 
 initiali
@@ -3165,11 +3165,367 @@
 lass;%0A%09%7D
+%0A%0A%09private synchronized void initializeService() %7B%0A%09%09if (initialized) return;%0A%09%09for (final PluginInfo%3CMacroExtensionAutoCompletionPlugin%3E info : getPlugins()) %7B%0A%09%09%09String name = info.getName();%0A%09%09%09if (name == null %7C%7C name.isEmpty()) %7B%0A%09%09%09%09name = info.getClassName();%0A%09%09%09%7D%0A%09%09%09macroExtensionAutoCompletionPlugins.put(name, info);%0A%09%09%7D%0A%09%09initialized = true;%0A%09%7D
 %0A%7D%0A

Make initialization logic thread-safe