/* $Id: ColleagueCheckBox.java 268 2011-07-19 10:00:29Z shayashi $ */
package smart_gs.image_search.normal.swingui;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;

import smart_gs.image_search.logical.SearchResult;

public class ColleagueCheckBox extends JCheckBox implements ItemListener {
    SearchResultViewer parent;
    SearchResult result;
    AbstractSearchResultPanel mediator;

    public ColleagueCheckBox(String string, SearchResultViewer parent,
	    SearchResult searchResult) {
	// TODO Auto-generated constructor stub
	super(string);
	this.parent = parent;
	this.result = searchResult;
    }
    
    public void setMediator(AbstractSearchResultPanel mediator) {
	this.mediator = mediator;
    }

    public void setColleagueEnabled(boolean enabled) {
	setEnabled(enabled);
    }

    public void itemStateChanged(ItemEvent arg0) {
	// TODO Auto-generated method stub
	    /*
	     * 20071111 田村
	     *  "Remove" にチェックが入ったときには反応しないように
	     */
	    if (!parent.getRemovedResults().contains(result)) {
		if(arg0.getStateChange() == ItemEvent.SELECTED){
			parent.getSelectedResults().add(result);
			parent.getBucket().add(result.toQuery());
			//変更　田村
			parent.updateBucket(false);
			//mediator.colleagueChanged();
		}else{
			if(parent.getSelectedResults().contains(result)){
		//		parent.getBucket().remove(parent.getSelectedResults().indexOf(result));
//				parent.getSelectedResults().remove(result);
				/*
				 * 変更
				 */
				parent.getBucket().remove(parent.getSelectedResults().indexOf(result) + 1);
				parent.getSelectedResults().remove(result);
				parent.updateBucket(true);
				
			}
		}
	    }

    }

}
