/* $Id: RegionSelectionState.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.Region;
import smart_gs.swingui.GSMouseEvent;


public class RegionSelectionState extends DefaultState{
	
	private static DefaultState singleton = new RegionSelectionState();
	private List<RegionNotify> elements;
	
	public RegionSelectionState() {
		super();
		elements = new ArrayList<RegionNotify>();
	}
	public static DefaultState getInstance(){
		return singleton;
	}
	public void addNotifyElement(RegionNotify 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.notifySelectedRegion(regions.get(i));
				return;
			}
		}
		super.mousePressed(e, canvas);
	}
	@Override
	public void mouseClicked(GSMouseEvent e, SpreadCanvas canvas) {
		super.mouseClicked(e, canvas);
	}
	private void notifySelectedRegion(Region region){
		for(int i=0;i<this.elements.size();i++){
			this.elements.get(i).notifyRegionSelection(region);
		}
	}
}
