/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2012 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.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.ui.swing;

import java.awt.Color;
import java.awt.Graphics;
import java.util.HashMap;

import imagej.plugin.Plugin;
import imagej.service.AbstractService;
import imagej.service.Service;
import imagej.tool.IconDrawer;
import imagej.tool.IconService;
import imagej.tool.Tool;
import imagej.util.ColorRGB;

import javax.swing.AbstractButton;

/**
 * 
 * @author Barry DeZonia
 *
 */
@Plugin(type = Service.class)
public class SwingIconService extends AbstractService implements IconService {

	private HashMap<Tool,AbstractButton> buttonMap = new HashMap<Tool, AbstractButton>();
	
	@Override
	public IconDrawer acquireDrawer(Tool tool) {
		return new SwingIconDrawer(tool);
	}
	
	public void registerButton(Tool tool, AbstractButton button) {
		buttonMap.put(tool,button);
	}
	
	private class SwingIconDrawer implements IconDrawer {
		private Tool tool;
		private AbstractButton button;
		private Graphics graphics;
		
		public SwingIconDrawer(Tool tool) {
			this.tool = tool;
			this.button = buttonMap.get(tool);
			this.graphics = button.getGraphics();
		}
		
		@Override
		public int getIconRectangleWidth() {
			return button.getWidth();
		}

		@Override
		public int getIconRectangleHeight() {
			return button.getHeight();
		}

		@Override
		public void setIconPixel(int x, int y, ColorRGB color) {
			Color awtColor = new Color(color.getRed(), color.getGreen(), color.getBlue());
			graphics.setColor(awtColor);
			graphics.drawLine(x, y, x, y);
		}
		
	}

}

@@ -2324,16 +2324,47 @@
 vice %7B%0A%0A
+%09// -- instance variables --%0A%09%0A
 %09private
@@ -2444,16 +2444,48 @@
 n%3E();%0A%09%0A
+%09// -- IconService methods --%0A%09%0A
 %09@Overri
@@ -2574,16 +2574,53 @@
 );%0A%09%7D%0A%09%0A
+%09// -- SwingIconService methods --%0A%09%0A
 %09public 
@@ -2710,16 +2710,44 @@
 );%0A%09%7D%0A%09%0A
+%09// -- private helpers --%0A%09%0A
 %09private
@@ -2797,29 +2797,8 @@
 r %7B%0A
-%09%09private Tool tool;%0A
 %09%09pr
@@ -2900,29 +2900,8 @@
 ) %7B%0A
-%09%09%09this.tool = tool;%0A
 %09%09%09t
@@ -2937,21 +2937,220 @@
 ol);%0A%09%09%09
-this.
+if (button == null)%0A%09%09%09%09throw new IllegalArgumentException(%0A%09%09%09%09%09%22There is no button associated with the specified tool%22);%0A%09%09%09// TODO - eliminate this ref. But experiment below doesn't draw correctly.%0A%09%09%09
 graphics
@@ -3424,16 +3424,20 @@
 olor) %7B%0A
+%09%09%09%0A
 %09%09%09Color
@@ -3447,17 +3447,22 @@
 tColor =
- 
+%0A%09%09%09%09%09
 new Colo
@@ -3515,16 +3515,208 @@
 lue());%0A
+%09%09%09%0A%09%09%09// TODO this would be nice but doesn't work%0A%09%09%09//button.getGraphics().setColor(awtColor);%0A%09%09%09//button.getGraphics().drawLine(x, y, x, y);%0A%09%09%09%0A%09%09%09// But using an old reference does work%0A
 %09%09%09graph

Document a design decision