//*********************************************************************************************
//
// Author: 			Dackral Scott Phillips
//
// File: 			SWOGHelp.java
//
//	Date:				August 5, 2K2
//
// Description:	This is a java program written to satisfy the requirements necessary to
//						fulfill my master's degree.  This program creates a Sematic Web Ontology
//						Generator (SWOG).  It's primary purpose is to give semantic web newbies
//						an introduction to the syntax.  It provides help menus for beginning
//						users as well as an advanced code viewer with highlighted syntax. 
//
//	Copyright © 2002 by Dackral Scott Phillips.  All Rights Reserved.
//
//*********************************************************************************************

   // Import Necessary Files

   import java.io.*;
   import java.net.*;
   import java.lang.*;
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.text.*;
   import javax.swing.event.*;
   import java.util.ArrayList;

    public class SWOGHelp extends JFrame implements HyperlinkListener
   {
      // Declare variables and make them class accessible
   
      private JEditorPane HTMLJEdPane;	//The Browser Window
      private URL url;						//URL to be displayed
      private String currentURLStr;		//Holds the current URL displayed
   
      private JTextField newAddressTextFld;	//Location Bar
   	
      private ArrayList backArrLst;				//Arraylist implementation of the Back Button
      private ArrayList forwardArrLst;			//Arraylist implementation of the Forward Button
   
       public SWOGHelp(String u) 
      {
         super("SWOG - Semantic Web Ontology Generator [Help]");	// Create the help window witht hte given title
      
      // Set the title bar icon
      
         ImageIcon logoIcon = new ImageIcon("images/swogHelpIcon.gif");
         setIconImage(logoIcon.getImage());
      
      // Make the Help Window 500 x 500
      
         Rectangle screenSize = new Rectangle(500, 500);
         setBounds(screenSize);
      
      // Set up the Forward and Back Button ArrayLists
      
         backArrLst = new ArrayList(10);
         forwardArrLst = new ArrayList(10);
      
      // Hide SWOGHelp when the user clicks the close button
      
         addWindowListener(
                new WindowAdapter()
               {
                   public void windowActivated(WindowEvent e)
                  {
                  }
               
                   public void windowClosed(WindowEvent e)
                  {
                  }
               
                   public void windowClosing(WindowEvent e)
                  {
                     SWOGHelp.this.setVisible(false);
                  }
               
                   public void windowIconified(WindowEvent e)
                  {
                  }
               
                   public void windowDeiconified(WindowEvent e)
                  {
                  }
               
                   public void windowDeactivated(WindowEvent e)
                  {
                  }
               
                   public void windowOpened(WindowEvent e)
                  {
                  }
               });
      
      // Set up the GUI
      
         JDesktopPane desktop = new JDesktopPane();
         setContentPane(desktop);
         JToolBar toolBar = ConstructToolBar(); 
         JMenuBar menuBar = ConstructMenuBar();
         JPanel mainPanel = new JPanel();
         mainPanel.setLayout(new BorderLayout());
         setJMenuBar(menuBar);
         getContentPane().setLayout(new BorderLayout());
         getContentPane().add(toolBar, BorderLayout.NORTH);
         getContentPane().add(mainPanel, BorderLayout.CENTER);
         desktop.putClientProperty("JDesktopPane.dragMode", "outline");
      
         try 
         {
         // Load the url of the help file we need
         
            url = new URL(u);
         
         // Create an HTML window to view the help file
         
            HTMLJEdPane = new JEditorPane(url);
            HTMLJEdPane.setEditable(false);
         
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.getViewport().add(HTMLJEdPane, BorderLayout.CENTER); //Make it Scrollable
         
            mainPanel.add(scrollPane, BorderLayout.CENTER);
         
            HTMLJEdPane.addHyperlinkListener(this);	//Allows a user to click links
         }
             catch(MalformedURLException mue) //If the URL was bad (missing http:// etc.)
            {
               JOptionPane.showMessageDialog(SWOGHelp.this, "Your URL was not in the proper format.\n" + mue, "Malformed URL Error", JOptionPane.ERROR_MESSAGE);
            }
             catch(IOException ioe)
            {
               JOptionPane.showMessageDialog(SWOGHelp.this, "There was an error in your input.\n" + ioe, "IO Error", JOptionPane.ERROR_MESSAGE);
            }
      
      // Set the URL in the Textbox to the one displayed & store the page displayed
      
         newAddressTextFld.setText(u);
         currentURLStr = newAddressTextFld.getText();
      }
   
       private JMenuBar ConstructMenuBar()	//Create the Menu Bar
      {
         JMenuBar menubar = new JMenuBar();
      
         JMenu topicsMenu = ConstructTopicsMenu();			// Topics Menu
         JMenu helpTypeMenu = ConstructHelpTypeMenu();	// Type Menu	
      
      // Add the Menus to the MenuBar
      
         menubar.add(topicsMenu);
         menubar.add(helpTypeMenu);
      
      // Return the finished Menu Bar
      
         return menubar;
      }
   
       private JMenu ConstructTopicsMenu()
      {
      // Define the Topics Menu
      
         JMenu topicsMenu = new JMenu("Topics");
         topicsMenu.setMnemonic(KeyEvent.VK_T);		//Accessible with Alt T
      
      // Create Menu Item objects
      
         JMenuItem tpContents = new JMenuItem("Contents");
         tpContents.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Contents();
                  }
               }
            );
         JMenuItem tpSWOG = new JMenuItem("SWOG");
         tpSWOG.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     SWOG();
                  }
               }
            );
         JMenuItem tpSW = new JMenuItem("Semantic Web");
         tpSW.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     SW();
                  }
               }
            );
         JMenuItem tpLicense = new JMenuItem("License");
         tpLicense.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     License();
                  }
               }
            );
         JMenuItem tpAbout = new JMenuItem("About");
         tpAbout.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     About();
                  }
               }
            );
      
      // Set Shortcuts for the Menu Items
      
         tpContents.setMnemonic(KeyEvent.VK_C);	//Alt C
         tpSWOG.setMnemonic(KeyEvent.VK_S);		//Alt S
         tpSW.setMnemonic(KeyEvent.VK_W);			//Alt W
         tpLicense.setMnemonic(KeyEvent.VK_L);	//Alt L
         tpAbout.setMnemonic(KeyEvent.VK_A);		//Alt A
      
      // Add Items to the Topics Menu
      
         topicsMenu.add(tpContents);
         topicsMenu.add(tpSWOG);
         topicsMenu.add(tpSW);
         topicsMenu.addSeparator();
         topicsMenu.add(tpLicense);
         topicsMenu.add(tpAbout);
      
         return topicsMenu;
      }
   
       private JMenu ConstructHelpTypeMenu()
      {
      // Define the Help Type Menu
      
         JMenu helpTypeMenu = new JMenu("Help Type");
         helpTypeMenu.setMnemonic(KeyEvent.VK_H);		//Alt H
      
      // Create Menu Item objects
      
         JMenuItem htContents = new JMenuItem("Contents");
         htContents.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Contents();
                  }
               }
            );
         JMenuItem htIndex = new JMenuItem("Index");
         htIndex.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Index();
                  }
               }
            );
      // Set Shortcuts for the Menu Items
      
         htContents.setMnemonic(KeyEvent.VK_C);		//Alt C
         htIndex.setMnemonic(KeyEvent.VK_I);			//Alt I
      
      // Add Items to the Help Type Menu
      
         helpTypeMenu.add(htContents);
         helpTypeMenu.add(htIndex);
      
         return helpTypeMenu;
      }
   
   // Construct the Toolbar
   
       private JToolBar ConstructToolBar()
      {
         JToolBar toolbar = new JToolBar();
         JButton button = null;
      
      // New button
      
         button = new JButton(new ImageIcon("images/Back24.gif"));
         button.setToolTipText("Back");
         button.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Back();
                  }
               });
         toolbar.add(button);
      
         button = new JButton(new ImageIcon("images/Forward24.gif"));
         button.setToolTipText("Forward");
         button.addActionListener(
                new ActionListener()
               {
                   public void actionPerformed(ActionEvent e)
                  {
                     Forward();
                  }
               });
         toolbar.add(button);
         toolbar.addSeparator(); //Separate this group of buttons from the next
      
         button = new JButton(new ImageIcon("images/Home24.gif"));
         button.setToolTipText("Home");
         button.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Home();
                  }
               });
         toolbar.add(button);
         toolbar.addSeparator(); //Separate this group of buttons from the next
            
         newAddressTextFld = new JTextField("file:///");
         newAddressTextFld.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Location();
                  }
               }
            );
         toolbar.add(newAddressTextFld);
      
         return toolbar;
      }
   
       public void hyperlinkUpdate(HyperlinkEvent hle)
      {
         if(hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
         {
         // Load cursors when a user mouses over a hyperlink
         
            Cursor cursor = HTMLJEdPane.getCursor();
            Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
            HTMLJEdPane.setCursor(waitCursor);
         
         // Change Help Pages
         
            SwingUtilities.invokeLater(new HelpLoader(HTMLJEdPane, hle.getURL(), cursor));
         
            if (hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            { 
               try 
               { 
                  checkBackSize();	//Make Sure the Back History isn't greater than 10
                  backArrLst.add(currentURLStr);	//Add the old page to it
                  HTMLJEdPane.setPage(hle.getURL());	//Change to the new page
                  newAddressTextFld.setText(hle.getURL().toExternalForm());	//Set location bar text
                  currentURLStr = newAddressTextFld.getText();	//Set the currentURL
               } 
                   catch(IOException ioe) 
                  {
                     JOptionPane.showMessageDialog(SWOGHelp.this, "Can't follow link to " + hle.getURL().toExternalForm() + "\n" + ioe, "I/O Error", JOptionPane.ERROR_MESSAGE); 
                  } 
            } 
         }
      }
   
   // Topics Menu Methods
   
       public void Contents()	//Change to the contents page
      {
         checkBackSize();	//Store the old page in Back Button History
         backArrLst.add(currentURLStr);
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
            currentURLStr = newAddressTextFld.getText();
         }
             catch (IOException ioe)
            {
            }
      }
   
       public void SWOG()	//Scroll down the contents page to the SWOG section
      {
         checkBackSize();	//Store the old page in the Back Button History
         backArrLst.add(currentURLStr);
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#SWOG");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#SWOG");
            currentURLStr = newAddressTextFld.getText();
         }
             catch (IOException ioe)
            {
            }
      }
   
       public void SW()	//Scroll down the contents page to the SW (Semantic Web) section
      {
         checkBackSize();	//Store the old page in the Back Button History
         backArrLst.add(currentURLStr); 
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#SW");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#SW");
            currentURLStr = newAddressTextFld.getText();
         }
             catch (IOException ioe)
            {
            }
      }
   
       public void License()	//Scroll down the contents page to the License section
      {
         checkBackSize();	//Store the old page in the Back Button History
         backArrLst.add(currentURLStr);
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#License");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#License");
            currentURLStr = newAddressTextFld.getText();
         }
             catch (IOException ioe)
            {
            }
      }
   
       public void About()	//Scroll down the contents page to the About section
      {
         checkBackSize();	//Store the old page in the Back Button History
         backArrLst.add(currentURLStr);
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#About");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#About");
            currentURLStr = newAddressTextFld.getText();
         }
             catch (IOException ioe)
            {
            }
      }
   
   // Type Menu Methods
   
       public void Content()	//Change to the contents page
      {
         checkBackSize();	//Store the old page in the Back Button History
         backArrLst.add(currentURLStr); 
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
            currentURLStr = newAddressTextFld.getText();
            clearForwardCache();
         }
             catch (IOException ioe)
            {
            }
      }
   
       public void Index()	//Change to the index page
      {
         checkBackSize();	//Store the old page in the Back Button History
         backArrLst.add(currentURLStr);
      
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\index.html");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\index.html");
            currentURLStr = newAddressTextFld.getText();
            clearForwardCache();
         }
             catch (IOException ioe)
            {
            }
      }
   
   // ToolBar Methods
   
       public void Back()	//Back Button Pressed
      {
         if (backArrLst.size() > 0)
         {
				//Move the current page into the forward history
            forwardArrLst.add(currentURLStr);	//Change text bar to the last page viewed
            newAddressTextFld.setText((String)backArrLst.get((backArrLst.size() - 1)));
            backArrLst.remove((backArrLst.size() - 1));	//delete the page from Back History
            try
            {
               HTMLJEdPane.setPage(newAddressTextFld.getText());	//Set the page
               currentURLStr = newAddressTextFld.getText();
            }
                catch (IOException ioe)
               {
               }
         }
      }
   
       public void Forward()	//Forward Button Pressed
      {
         checkForwardSize();	//Check the size of forward ( < 10)
         if (forwardArrLst.size() > 0)
         {
				//Move the current URL to the back button
            backArrLst.add(currentURLStr);	//Set the text bar to the page in the forward history
            newAddressTextFld.setText((String)forwardArrLst.get((forwardArrLst.size() - 1)));
            forwardArrLst.remove((forwardArrLst.size() - 1));	//remove the entry from the forward history
            try
            {
               HTMLJEdPane.setPage(newAddressTextFld.getText());	//Set the page
               currentURLStr = newAddressTextFld.getText();
            }
                catch (IOException ioe)
               {
               }
         }
      }
   
       public void Home()	//Set the page to contents
      {
         checkBackSize();	//Store the old page in the back history
         backArrLst.add(currentURLStr);
         clearForwardCache();
         try
         {
            newAddressTextFld.setText("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
            HTMLJEdPane.setPage("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
            currentURLStr = newAddressTextFld.getText();
         }
             catch (IOException ioe)
            {
            }
      }
   
       public void checkBackSize()	//History only holds 10 URLS
      {
         if (backArrLst.size() == 10)
         {
            backArrLst.remove(0);
         }
      }
   
       public void checkForwardSize()	//History only holds 10 URLS
      {
         if (backArrLst.size() == 10)
         {
            backArrLst.remove(0);
         }
      }
   
       public void clearForwardCache()	//Delete entries from Forward button.  Works like a normal browser
      {
         forwardArrLst.clear();			//Entries deleted when a user goes to a new page
      }
   
       public void Location()
      {
         try
         {
            checkBackSize();	//Store the old page in the back history & set the location to what's typed in the location bar
            backArrLst.add(currentURLStr);
            HTMLJEdPane.setPage(new URL(newAddressTextFld.getText()));
            currentURLStr = newAddressTextFld.getText();
         }
             catch(MalformedURLException mue)	//URL not properly formed (Missing http:// etc.)
            {
               JOptionPane.showMessageDialog(SWOGHelp.this, "Your URL was not in the proper format.\n" + mue, "Malformed URL Error", JOptionPane.ERROR_MESSAGE);
            }
             catch(IOException ioe)
            {
               JOptionPane.showMessageDialog(SWOGHelp.this, "There was an error in your input.\n" + ioe, "IO Error", JOptionPane.ERROR_MESSAGE);
            }
      }
   

       private class HelpLoader implements Runnable	//Changes the pages
      {
         private JEditorPane HTMLJEdPane;	//The HTML Pane
         private URL url;						//The URL to be visited
         private Cursor cursor;				//The current cursor
      
          HelpLoader(JEditorPane html, URL u, Cursor c) 
         {
            HTMLJEdPane = html;	//Set instance variables
            url = u;
            cursor = c;
         }
      
          public void run() 
         {
            if(url == null) 
            {
               HTMLJEdPane.setCursor(cursor);	//Change the cursor
               Container parent = HTMLJEdPane.getParent();
               parent.repaint();	//Redraw the HTML Pane when page is loaded
            }
            else 
            {
               Document document = HTMLJEdPane.getDocument();
               try 
               {
                  newAddressTextFld.setText(url.toExternalForm());
                  HTMLJEdPane.setPage(url);
                  currentURLStr = newAddressTextFld.getText();
               }
                   catch(IOException ioe) 
                  {
                     HTMLJEdPane.setDocument(document);
                  }
               finally
               {
                  url = null;
                  SwingUtilities.invokeLater(this);
               }
            }
         }
      }
   }