// file: BugQuery.java
//

// import java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.border.*;

/**
 * Handles bug queries, is an extension of JPanel
 */
public class BugQuery extends JPanel {

    // ---------------------------------------------------
    //
    // declare class member data
    //
    // ---------------------------------------------------
  
    /**
     * container for entering new text to be added to the tree
     */
    public static JEditorPane queryEntry = null;
    
    /**
     * put a jpanel in for moutning to help with border creation
     */
    public static JScrollPane  mountingPanel = null;

    /**
     * TitledBorder variable used
     */
    public static TitledBorder border_d = null;
    
    /**
     * static variable assigned the value "Query"
     */
    public static final String TITLE_START = "Query";
    
    /**
     * static variable used to store query in a String
     */
    private static String query_string_d = "";

    /**
     * static variable used to store the query number
     */
    private static int query_num_d = -1;

    /**
     * static variable used to store whether or not query has been modifyied
     */
    private static boolean query_mod_d = false;
    
    // ---------------------------------------------------
    //
    // declare class constructors
    //
    // ---------------------------------------------------

    /**
     * default class constructor
     *
     * @param varmit VarmintPanel variable to initialize
     */
    public BugQuery(VarmintPanel varmint) {

	// intitalize the Editor Pane to plain text and to have nothing 
	// currently displayed in it and to a specific size
	//
	queryEntry = new JEditorPane("text/plain", 
				     VarmintPanel.DEFAULT_QUERY);

	// set the font that will be used in query entry
	//
	queryEntry.setFont(varmint.newFont);

	// create the mounting panel
	//
	mountingPanel = new JScrollPane(queryEntry);
	
	// set the sizes for the mounting panel.  The minimum size keeps the
	// box from disappearing or becoming nothing but a scroll bar if the 
	// user shrinks the VarmintPanel too small.  The preferred size sets
	// the default.
	//
	mountingPanel.setMinimumSize(new Dimension(100,100));
	mountingPanel.setPreferredSize(new Dimension(770,140));

	// add a key listener to the query entry panel allowing users to 
	// execute queries and perform other task from within this window
	//
	queryEntry.addKeyListener(varmint);

	// set the border of the scroll pane
	//
	border_d =  BorderFactory.
	    createTitledBorder(BorderFactory.createLineBorder(Color.black), 
			       "Query", TitledBorder.LEFT, 
			       TitledBorder.ABOVE_TOP, 
			       varmint.newFont, 
			       Color.black);
	mountingPanel.setBorder(border_d);

	updateTitle();
    }

    /**
     * Sets the query string and query number.
     * Assigns false to query_mod_d.
     * @param query String value assigned to query_string_d
     * @param num int value assigned to query_num_d
     */
    public static void setQueryTitle(String query, int num) {
	query_string_d = query;
	query_num_d = num;
	query_mod_d = false;
    }

    /**
     * Sets the query string
     * @param query String to assign query_string_d
     */
    public static void setQueryString(String query) {
	query_string_d = query;
    }
    /**
     * Sets the query number
     * @param num int value assigned to query_num_d
     */
    public static void setQueryNum(int num) {
	query_num_d = num;
    }

    /**
     * Sets the query mod boolean
     * @param flag value assigned to query_mod_d
     */
    public static void setQueryModify(boolean flag) {

	query_mod_d = flag;

    }

    /**
     * Resets the title. New queries are added.
     */
    public static void updateTitle() {
	
	String s = "";
	if (query_mod_d) {
	    s = TITLE_START+": (*) ";
	} else {
	    s = TITLE_START+": ";
	}
	s = s+query_string_d;
	
	if (query_num_d >= 0) {
	    s=s+" ("+query_num_d+" results)";
	}
	
	border_d.setTitle(s);
    }
}
