
package ca.ucalgary.seahawk.gui;

import ca.ucalgary.seahawk.util.DataFlowRecorder;

import org.biomoby.client.MobyRequestEventHandler;
import org.biomoby.shared.data.MobyContentInstance;
import org.biomoby.shared.data.MobyDataInstance;
import org.biomoby.shared.data.MobyDataObject;
import org.biomoby.shared.data.MobyDataObjectSet;
import org.biomoby.shared.data.MobyDataUtils;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.xml.transform.Transformer;
import javax.xml.parsers.DocumentBuilder;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.TreeMap;

/**
 * Special text display tab in Seahawk for the Seahawk help file.  Ensures that
 * links and services are launched in new tabs so that the help doesn't disappear.
 */

public class MobyContentHelpPane extends MobyContentPane{

    public static final String HELP_TAB_ICON_RESOURCE = "ca/ucalgary/seahawk/resources/images/help_tab.gif";
    public static final String HELP_HTML_RESOURCE = "ca/ucalgary/seahawk/resources/MobyContentGUIHelp.html";
    public final static String HELP_TAB_NAME = "Help Page";
    public final static Color HELP_TAB_COLOR = Color.blue;

    private static ImageIcon helpTabIcon;
    private static URL helpHTMLURL;
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(MobyContentHelpPane.class);
    public MobyContentHelpPane(MobyContentGUI cGUI, MobyServicesGUI sGUI, JTabbedPane parentComponent, 
			       DataFlowRecorder recorder, JLabel statusBar){
	super(cGUI, sGUI, parentComponent, recorder, statusBar);

	// So JFCUnit tests can find it
	setName(HELP_TAB_NAME);

	ClassLoader cl = getClass().getClassLoader();
	if(cl == null){
	    cl = ClassLoader.getSystemClassLoader();
	}
    	if(helpTabIcon == null){
	    URL u = cl.getResource(HELP_TAB_ICON_RESOURCE);
	    if(u == null){
		logger.warn("Could not find icon resource " + HELP_TAB_ICON_RESOURCE);
	    }
	    else{
		helpTabIcon = new ImageIcon(u);
	    }
	}

	if(helpHTMLURL == null){
	    helpHTMLURL = cl.getResource(HELP_HTML_RESOURCE);
	    if(helpHTMLURL == null){
		JOptionPane.showMessageDialog(null, "Could not find help file resource " + HELP_HTML_RESOURCE);
	    }
	}

	gotoURL(helpHTMLURL, false);
	editorPane.scrollToReference("start");  // use anchor at top of doc: pane scrolls to bottom normally...
    }

    /** Sets up icons, tab title, etc. */
    public void init(){
	int index = tabbedPane.indexOfComponent(this);
	tabbedPane.setIconAt(index, helpTabIcon);
	tabbedPane.setForegroundAt(index, HELP_TAB_COLOR);	
	tabbedPane.setTitleAt(index, HELP_TAB_NAME);
    }

    /** history is disabled for the help */ 
    public boolean canGoBack(){
	return false;
    }
    
    /** history is disabled for the help */ 
    public boolean canGoForward(){
	return false;
    }

    /** filter/search is disabled for the help, not implemented as a text search function, for consistency  */ 
    public boolean canFilter(){
	return false;
    }

    /**
     * If a service is requested from a link on this pane, the object returned here
     * will get the callback.  Normally, the reference is to "this", as new responses should
     * load in the same pane as the current data.  But this method returns null, meaning that 
     * MobyServiceGUI's default handler should be used.
     *
     * @return null
     */
    protected MobyRequestEventHandler getDefaultHandler(){
	return null;
    }
}
