/**
 * Creates a GUI form which allows definitions to be
 * retrieved for terms in a listbox. The data can either
 * be from a database or from a Vector of TermRows.
 * 
 * @author James McCabe
 * @version 0.1 
 */
 
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import java.util.Vector;
import java.sql.*;

import TermRow;


public class Glossary extends java.awt.Frame implements ItemListener, WindowListener
 
{
	// msql — whether or not the database server is running
	// if not use the Vector structure to stand in
	boolean msql = false;
	
	Label copyright;	
	Label instructions;
	TextField field;
	List list;
	
	TextArea definition;
	Label sql;
	Label sqlExplanation;
	
	GridBagLayout gl;
	GridBagConstraints gb;
	
	Vector v;
	
	/**
	 * The constructor creates the GUI and optionally
	 * initializes a vector containing the glossary
	 * information.
	 */
	public Glossary() 
	{
	 	System.out.print("gloss");
		this.addWindowListener(this);
		if (!msql) {
			initializeTable();
		}
		createGUI();
	}

	/**
	 * If there is no glossary database available we create
	 * an artificial one in memory using a Vector of TermRows
	 */
	protected void initializeTable() {
		v = new Vector(25,5);
		v.addElement(new TermRow("abstraction", "The essential characteristics"
		 + " of an object that distinguish it from other"
		 + " objects."));
		v.addElement(new TermRow("AWT", "The Abstract Windowing Toolkit: Java's"
		 + " basic GUI toolkit implemented in the java.awt.*"
		 + " classes"));
		v.addElement(new TermRow("beans", "In Java, re-usable components, typically"
		 + " with support for GUI building tools and serialization"));
		v.addElement(new TermRow("composition", "The process of bringing objects"
		 + " together to form a more complex object."));
		v.addElement(new TermRow("CORBA", "Common Object Request Broker Architecture:"
		 + " a language-independent distributed object architecture"));
		v.addElement(new TermRow("encapsulation", "The process of hiding all"
		 + " the (implementation) details of an object that"
		 + " do not contribute to its essential characteristics."));
		v.addElement(new TermRow("inheritance", "The process of extending a"
		 + " class by subclassing it and adding new"
		 + " characteristics to the subclass."));
		v.addElement(new TermRow("interface", "In Java, a set of methods that"
		 + " classes can implement."));
		v.addElement(new TermRow("introspection", "The mechanism that exposes"
		 + " the functionality of a component to the outside"
		 + " world."));
		v.addElement(new TermRow("Java", "An object-oriented language with strong"
		 + " support for networking and component-based"
		 + " development."));
		v.addElement(new TermRow("JFC", "Java Foundation Classes: a comprehensive"
		 + " set of GUI components and services supplementing"
		 + " the AWT"));
		v.addElement(new TermRow("MVC", "Model-View-Controller: a framework to"
		 + " support GUIs used by Smalltalk and Java's Swing."));
		v.addElement(new TermRow("persistence", "The property of an object that"
		 + " describes its ability to exist after the program"
		 + " that created it has finished."));
		v.addElement(new TermRow("polymorphism", "The ability of the same method"
		 + " to achieve different things when applied to"
		 + " objects of different types."));
		v.addElement(new TermRow("reflection", "The ability of Java code to"
		 + " examine and influence the fields and methods"
		 + " of classes at runtime."));
		v.addElement(new TermRow("RMI", "Remote Method Invocation: a Java"
		 + " networking architecture which allows the"
		 + " methods of remote objects on a server to"
		 + " be invoked by a client as if they existed"
		 + " locally"));
		v.addElement(new TermRow("serialization", "The process of converting"
		 + " an object into a serial form that is stored to"
		 + " to support object persistence."));
		v.addElement(new TermRow("three-tier", "A network architecture composed"
		 + " of three tiers: a client, an application server,"
		 + " and a data store.")); 
		v.addElement(new TermRow("UML", "Unified Modeling Language: a symbolic"
		 + " language used to model object-oriented systems."));
		v.addElement(new TermRow("XML", "Extensible Markup Language: a subset of"
		 + " SGML allowing users to define their own structured"
		 + " document formats."));
	}
	
	/**
	 * Create the form.
	 */
	protected void createGUI() {
		setTitle("Oranda Glossary");
		setBackground(Color.lightGray);
		setFont(new Font("Dialog", Font.BOLD, 12));
		copyright = new Label("Oranda Glossary Copyright (c) 1998 James McCabe");
		instructions = new Label("Select a term in the list to get its definition.");
		list = new List(10, false);
		if (!msql) {
				fillListWithTerms(list);
		} else {
			fillListWithTerms(list,"jdbc:msql://www.oranda.com:1114/Glossary");
		}
		list.addItemListener(this);
		field = new TextField(15);

		definition = new TextArea("(definition)", 7, 20, TextArea.SCROLLBARS_NONE);
		sql = new Label("SELECT...                                                                                      ");
		sqlExplanation = new Label("SQL command when a database is used:");
	
		setLayout(gl = new GridBagLayout());
		gb = new GridBagConstraints();
		gb.gridx = 1; gb.gridy = 1; gb.gridwidth = 2;
		gb.anchor = GridBagConstraints.CENTER;
		gl.setConstraints(copyright, gb); add(copyright);
		
		gb.gridx = 1; gb.gridy = 2; gb.gridwidth = 2;
		gb.anchor = GridBagConstraints.CENTER; 
		gl.setConstraints(instructions, gb); add(instructions);
		
		gb.gridx = 1; gb.gridy = 3; gb.gridheight = 2; gb.insets = new Insets(15, 0, 0, 0);
		gb.anchor = GridBagConstraints.WEST; 
		gl.setConstraints(list, gb); add(list);
		
		gb.gridx = 2; gb.gridy = 3;
		gb.anchor = GridBagConstraints.NORTHEAST; gb.insets = new Insets(15, 0, 0, 0);
		gl.setConstraints(field, gb); add(field);
	
		gb.gridx = 2; gb.gridy = 4; 
		gb.anchor = GridBagConstraints.SOUTHEAST;
		gl.setConstraints(definition, gb); add(definition);
		
		gb.gridx = 1; gb.gridy = 6; 
		gb.anchor = GridBagConstraints.CENTER; 
		gl.setConstraints(sqlExplanation, gb); add(sqlExplanation);
		
		setFont(new Font("Dialog", Font.PLAIN, 12));

		gb.gridx = 1; gb.gridy = 8; 
		gb.anchor = GridBagConstraints.CENTER; 
		gl.setConstraints(sql, gb); add(sql);
	}
	
	/**
	 * Fill the listbox with terms from a Vector
	 * of TermRows.
	 */
	protected void fillListWithTerms(List l) {
		
	 	for (int i=0;i < v.size();i++) {
			l.addItem((String)(((TermRow)(v.elementAt(i))).term));
		}	
	}
	/**
	 * Fill the listbox with terms from a msql database.
	 */
	protected void fillListWithTerms(List l, String url) {
		try {
			Class.forName("com.imaginary.sql.msql.MsqlDriver");
      		Connection con = DriverManager.getConnection(url);
      		Statement s = con.createStatement();
      		ResultSet r = s.executeQuery("SELECT Term,Definition FROM Term");    
      		while( r.next() ) {
          		System.out.println("term: " + r.getString(1) + ", definition: " +
                             r.getString(2));
          		l.addItem(r.getString(1));
      		}
      		con.close(); 
      	} catch (Exception e) {
      		e.printStackTrace();
      	}
                                  
	}
	
	/**
	 * When the user makes a listbox selection
	 * display the definition matching the item selected.
	 */
	public void itemStateChanged(ItemEvent e) {
		String term = list.getItem(list.getSelectedIndex());
		field.setText(term);
		sql.setText("SELECT Definition FROM Term WHERE Term='"
			 + term + "'");
		if (!msql) {
			definition.setText(getDefinition(term));
		} else {
			definition.setText(getDefinition(term,"jdbc:msql://shell15.ba.best.com:1114/Glossary"));
		}
	}
	
	/**
	 * Get the definition for a term from a Vector of
	 * TermRows
	 */
	private String getDefinition(String term) {
		for (int i=0;i < v.size();i++) {
			if (term.equals((((TermRow)(v.elementAt(i))).term))) {
				return ((TermRow)(v.elementAt(i))).definition;
			}
		}	
		return null;
	}
	
	/**
	 * Get the definition for a term from a remote
	 * mSQL database using a JDBC URL.
	 */
	private String getDefinition(String term, String url) {
		String definition = null;
		try {
			Class.forName("com.imaginary.sql.msql.MsqlDriver");
      		Connection con = DriverManager.getConnection(url);
      		Statement s = con.createStatement();
      		ResultSet r = s.executeQuery("SELECT Definition FROM Term WHERE Term='" + term + "'");    
      		r.next();
      		definition = r.getString(1);
      		con.close(); 
      	} catch (Exception e) {
      		e.printStackTrace();
      	}
      	return definition;
	}


	public static void main(String args[])
	{
		Glossary app = new Glossary();

		app.setSize(500,400);
		app.show();
	}

	public void windowClosing(WindowEvent e) {
		this.dispose();
	}
	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
	
}