/**
 *#########################################################################
 *
 * A component of the Gatherer application, part of the Greenstone digital
 * library suite from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * Copyright (C) 2006 New Zealand Digital Library Project
 *
 * <BR><BR>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * <BR><BR>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * <BR><BR>
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *########################################################################
 */
package org.greenstone.gatherer.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.LocalLibraryServer;
import org.greenstone.gatherer.collection.Collection;
import org.greenstone.gatherer.util.StaticStrings;


public class PreviewButton 
    extends GLIButton {


    static final public int HOME_PAGE = 1;
    static final public int SEARCH_PAGE = 2;
    static final public int CLASSIFIER_PAGE = 3;
    static final public int DOCUMENT_PAGE = 4;

    private String preview_address;
    private int page_type = HOME_PAGE;
    private boolean variable_preview = false;
    
    private PreviewButtonOwner owner = null;
    public PreviewButton() {
	super();
	PreviewButtonListener pbl = new PreviewButtonListener();
	addActionListener(pbl);
    }
    public PreviewButton(String text, String tooltip) {
	super(text, tooltip);
	PreviewButtonListener pbl = new PreviewButtonListener();
	addActionListener(pbl);
    }

    public void setOwner(PreviewButtonOwner o) {
	owner = o;
    }
    public void setVariablePreview(boolean vp) {
	variable_preview = vp;
    }
    protected void configureHomeURL() {
	// set up the home page for the current collection
	Collection current_collection = Gatherer.c_man.getCollection();
	if (Gatherer.GS3) {
	    // don't do anything fancy here
	    preview_address = Configuration.library_url.toString() + Configuration.getServletPath()+ "?a=p&sa=about&c=" + current_collection.getName()+"&l="+Configuration.getLanguage();
	    return;
	}
	preview_address = Configuration.library_url.toString()+"?c="+current_collection.getName()+"&l="+Configuration.getLanguage();

	String main_args = "&a=p&p=about";
	/* this code is for switching the preview page depending on what 
	    is currently active. but it is not complete yet
	String main_args = null;
	String params = null;
	if (!variable_preview || owner == null) {
	    main_args = "&a=p&p=about";
	} else {
	    page_type = owner.getPageType();
	    params = owner.getPageParams();
	    
	    switch (page_type) {
	    case SEARCH_PAGE:
		main_args = "&a=q&q="+params;
		break;
	    case CLASSIFIER_PAGE:
		main_args = "&a=d&cl="+params;
		break;
	    case DOCUMENT_PAGE:
		  main_args = "&a=d&d="+params;
		break;
	    case HOME_PAGE:
	    default:
		main_args = "&a=p&p=about";
		
		
	    }
	    

	}
	*/
	preview_address += main_args+StaticStrings.TIMESTAMP_ARGUMENT + System.currentTimeMillis();
	
    }
    private class PreviewButtonListener
	implements ActionListener {

	public void actionPerformed(ActionEvent event) {
	    Gatherer.c_man.saveCollection();
	    
	    configureHomeURL();
	    // check that the local library server is still running  - doesn't do anything if its not supposed to be running
	    LocalLibraryServer.checkServerRunning();
	    
	    Gatherer.spawnBrowser(preview_address);

	}

    }

}
