/* HistoryController */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
import com.redbugz.macpaf.Individual;

public class HistoryController implements NSMenu.MenuValidation {
	private static Logger log = Logger.getLogger(HistoryController.class);
	
	private List rememberedIndividuals = new ArrayList(10);
	private LinkedList recentIndividuals = new LinkedList();

	private MyDocument document;

	private int recentIndividualsListMax = 10;
	
    public HistoryController(MyDocument doc) {
    	document = doc;
			for (int i = 0; i < 10; i++) {
	    		rememberedIndividuals.add(i, Individual.UNKNOWN);
	    	}
	}

	public HistoryController() {
		// TODO Auto-generated constructor stub
	}

	public void forgetIndividual(Individual individual) {
    	log.debug("HistoryController.forgetIndividual():"+individual);
    }

    public void recallIndividual(int index) {
    	log.debug("HistoryController.recallIndividual():"+index);
    }

    public void recallLastFoundIndividual() {
    	log.debug("HistoryController.recallLastFoundIndividual():");
    }

    public void recallLastSavedIndividual() {
    	log.debug("HistoryController.recallLastSavedIndividual():");
    }

    public void rememberIndividual(Individual individual) {
    	log.debug("HistoryController.rememberIndividual():"+individual);
    	int availIndex = getNextAvailableIndex();
    	if (availIndex >= 0) {
    		rememberedIndividuals.set(availIndex, individual);
    	} else {
    		log.warn("HistoryController.rememberIndividual(): Could not rememberIndividual because list is full. Ind:"+individual);
    	}
	}
    
	private int getNextAvailableIndex() {
		for (int i = 0; i < rememberedIndividuals.size(); i++) {			
			if (rememberedIndividuals.get(i) instanceof Individual.UnknownIndividual) {
				return i;
			}
		}
		return -1;
	}
	
	public void addToRecentIndividualList(Individual individual) {
		if (!(individual instanceof Individual.UnknownIndividual)) {
			recentIndividuals.addFirst(individual);
			trimRecentIndividualList();
		}
	}

	private void trimRecentIndividualList() {
		while (recentIndividuals.size() > recentIndividualsListMax) {
			recentIndividuals.removeLast();
		}
	}
	
	public Individual getRecentIndividual(int index) {
		Individual recent = Individual.UNKNOWN;
		try {
			recent = (Individual) recentIndividuals.get(index);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recent;
	}

	public boolean validateMenuItem(_NSObsoleteMenuItemProtocol menuItem) {
		log.debug("HistoryController.validateMenuItem():"+menuItem);
		boolean result = false;
		switch (menuItem.tag()) {
		case 101:
			result = true;
			if (getNextAvailableIndex() < 0) {
				result = false;
			} //else if (NSDocumentController.sharedDocumentController().currentDocument())
				//menuItem.
			break;
		case 102:
		case 110:
		case 111:
			result = true;
			break;
		default:
			result = true;
			if (menuItem.tag() > 0 && (rememberedIndividuals.get(menuItem.tag()-1) instanceof Individual.UnknownIndividual)) {
				result = false;
			}
			break;
		}
		log.debug("validateMenuItem done, tag:"+menuItem.tag()+" result:"+result);
		return result;
	}

	public List getRecentIndividualList() {
		return recentIndividuals;
	}

	public void setRecentIndividualListSize(int newSize) {
		recentIndividualsListMax = newSize;
		trimRecentIndividualList();
	}
}

@@ -2685,16 +2685,34 @@
 Item) %7B%0A
+%09%09return false;%0A//
 %09%09log.de
@@ -2766,16 +2766,18 @@
 uItem);%0A
+//
 %09%09boolea
@@ -2794,16 +2794,18 @@
  false;%0A
+//
 %09%09switch
@@ -2824,16 +2824,18 @@
 ag()) %7B%0A
+//
 %09%09case 1
@@ -2838,16 +2838,18 @@
 se 101:%0A
+//
 %09%09%09resul
@@ -2854,24 +2854,26 @@
 ult = true;%0A
+//
 %09%09%09if (getNe
@@ -2894,24 +2894,26 @@
 ex() %3C 0) %7B%0A
+//
 %09%09%09%09result =
@@ -2916,24 +2916,26 @@
 lt = false;%0A
+//
 %09%09%09%7D //else 
@@ -3005,16 +3005,18 @@
 ment())%0A
+//
 %09%09%09%09//me
@@ -3023,16 +3023,18 @@
 nuItem.%0A
+//
 %09%09%09break
@@ -3035,16 +3035,18 @@
 %09break;%0A
+//
 %09%09case 1
@@ -3049,16 +3049,18 @@
 se 102:%0A
+//
 %09%09case 1
@@ -3063,16 +3063,18 @@
 se 110:%0A
+//
 %09%09case 1
@@ -3077,16 +3077,18 @@
 se 111:%0A
+//
 %09%09%09resul
@@ -3097,16 +3097,18 @@
 = true;%0A
+//
 %09%09%09break
@@ -3109,16 +3109,18 @@
 %09break;%0A
+//
 %09%09defaul
@@ -3122,16 +3122,18 @@
 efault:%0A
+//
 %09%09%09resul
@@ -3142,16 +3142,18 @@
 = true;%0A
+//
 %09%09%09if (m
@@ -3258,24 +3258,26 @@
 ividual)) %7B%0A
+//
 %09%09%09%09result =
@@ -3284,21 +3284,25 @@
  false;%0A
+//
 %09%09%09%7D%0A
+//
 %09%09%09break
@@ -3303,20 +3303,24 @@
 %09break;%0A
+//
 %09%09%7D%0A
+//
 %09%09log.de
@@ -3388,16 +3388,18 @@
 esult);%0A
+//
 %09%09return

fix problem that seems to cause pre-Tiger systems to freeze up the menu bar