/* $Id: ResourceSelectionState.java 125 2011-03-09 09:49:51Z ohura $ */
package smart_gs.drawing_tool.state;

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

import smart_gs.drawing_tool.SpreadCanvas;
import smart_gs.logical.GSResource;
import smart_gs.logical.Region;
import smart_gs.swingui.GSMouseEvent;
import smart_gs.util.ResourceNotify;

public class ResourceSelectionState extends DefaultState{

	private static DefaultState singleton = new ResourceSelectionState();
	private List<ResourceNotify> elements;
	
	public ResourceSelectionState() {
		super();
		elements = new ArrayList<ResourceNotify>();
	}
	public static DefaultState getInstance(){
		return singleton;
	}
	public void addNotifyElement(ResourceNotify element){
		this.elements.add(element);
	}
	@Override
	public void mousePressed(GSMouseEvent e, SpreadCanvas canvas) {
		List<Region> regions = canvas.getSpread().getRegions();
		for(int i=0;i<regions.size();i++){
			if(regions.get(i).contains(e.getPoint2D())){
				this.notifySelectedResource(regions.get(i));
				return;
			}
		}
		super.mousePressed(e, canvas);
	}
	@Override
	public void mouseClicked(GSMouseEvent e, SpreadCanvas canvas) {
		super.mouseClicked(e, canvas);
	}
	private void notifySelectedResource(GSResource resource){
		for(int i=0;i<this.elements.size();i++){
			this.elements.get(i).notifyResourceSelection(resource);
		}
	}
}
