//*********************************************************************************************
//
// Author: 			Dackral Scott Phillips
//
// File: 			SWOGUI.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 javax.swing.*;
   import javax.swing.event.*;
   import java.awt.event.*;
   import java.awt.*;
   import java.util.ArrayList;
   import SWOGDesktop;
   import SWOGHelp;
   import SWOGSplashScreen;

    public class SWOGUI extends JFrame 
   {
      private static final int NO_MASK = 0;	// Needed for Menu Accelerator
      private SWOGHelp helpBrowser;				// Pointer to the Help Window
      private JDesktopPane desktop;				// Main Application Desktop
   
      private JMenuItem fmSave;					// Pointer to the Save All Menu So It Can Be Toggled On And Off
      private JMenuItem fmClose;					// Pointer to the Close Menu For the Same Reason
   
      private JMenuItem wndRefresh;				// Refresh Menu Pointer, Same Reason
      private JMenuItem wndCascade;				// Cascade Menu Pointer, Same Reason
   
      private ArrayList desktops;				// Arraylist in Which to Store Desktops
   
       public SWOGUI() 
      {
         super("SWOG - Semantic Web Ontology Generator"); // Create the Application Desktop with the given title
      
      // Set the Title Bar icon
      
         ImageIcon icon = new ImageIcon("images/swogMainIcon.gif");
         setIconImage(icon.getImage());
      
      // Make the initial screen 50 pixels smaller than the total desktop realty
      
         int inset = 50;
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
         setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);
      
      // Quit SWOG when the main application window closes
      
         addWindowListener(
                new WindowAdapter()
               {
                   public void windowActivated(WindowEvent e)
                  {
                  }
               
                   public void windowClosed(WindowEvent e)
                  {
                  }
               
                   public void windowClosing(WindowEvent e)
                  {
                     Exit();
                  }
               
                   public void windowIconified(WindowEvent e)
                  {
                  }
               
                   public void windowDeiconified(WindowEvent e)
                  {
                  }
               
                   public void windowDeactivated(WindowEvent e)
                  {
                  }
               
                   public void windowOpened(WindowEvent e)
                  {
                  }
               });
      
      // Set up the GUI
      
         desktop = new JDesktopPane();
         setContentPane(desktop);
         JMenuBar menuBar = ConstructMenuBar();  //Make the Menubar
         setJMenuBar(menuBar);
         desktop.putClientProperty("JDesktopPane.dragMode", "outline");
      
      // Prepare the Help Pane
      
         helpBrowser = null;
      
      // Prepare Arraylist to keep track of Desktops
      
         desktops = new ArrayList();
      }
   
      // Construct Various and Sundry Menus
   
       private JMenuBar ConstructMenuBar()
      {
         JMenuBar menuBar = new JMenuBar();
      
         JMenu fileMenu = ConstructFileMenu();		// File Menu
         JMenu windMenu = ConstructWindowMenu();	// Windows Menu
         JMenu setMenu = ConstructSettingsMenu();	// Settings Menu
         JMenu helpMenu = ConstructHelpMenu();		// Help Menu
      
      // Add the Menus to the MenuBar
      
         menuBar.add(fileMenu);
         menuBar.add(windMenu);
         menuBar.add(setMenu);
         menuBar.add(helpMenu);
      
      // Return the finished Menu Bar
      
         return menuBar;
      }
   
       private JMenu ConstructFileMenu()
      {
      // Define the File Menu
      
         JMenu fileMenu = new JMenu("File");
         fileMenu.setMnemonic(KeyEvent.VK_F);  //Alt F Brings It Up
      
      // Create Menu Item objects
      
         JMenuItem fmNew = new JMenuItem("New Desktop");
         fmNew.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     New();
                  }
               }
            );
         JMenuItem fmOpen = new JMenuItem("Open Desktop");
         fmOpen.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Open();
                  }
               }
            );
         fmSave = new JMenuItem("Save All");
         fmSave.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     SaveAll();
                  }
               }
            );
         fmClose = new JMenuItem("Close All");
         fmClose.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     CloseAll();
                  }
               }
            );
         JMenuItem fmExit = new JMenuItem("Exit SWOG");
         fmExit.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Exit();
                  }
               }
            );
      
      // Set Shortcuts for the Menu Items
      
         fmNew.setMnemonic(KeyEvent.VK_N);	// Alt N Brings it up
         fmOpen.setMnemonic(KeyEvent.VK_O);	// Alt O
         fmSave.setMnemonic(KeyEvent.VK_S);	// Alt S
         fmSave.setEnabled(false);				// Disable Save All by Default
         fmClose.setMnemonic(KeyEvent.VK_C);	// Alt C
         fmClose.setEnabled(false);				// Disable Close All by Default
         fmExit.setMnemonic(KeyEvent.VK_E);	// Alt E
      
      // Add Items to the File Menu
      
         fileMenu.add(fmNew);
         fileMenu.add(fmOpen);
         fileMenu.addSeparator();
         fileMenu.add(fmSave);
         fileMenu.add(fmClose);
         fileMenu.addSeparator();
         fileMenu.add(fmExit);
      
         return fileMenu;
      }
   
       private JMenu ConstructWindowMenu()
      {
      // Define the Windows Menu
      
         JMenu windMenu = new JMenu("Windows");
         windMenu.setMnemonic(KeyEvent.VK_W);	// Alt W Brings It Up
      
      // Create Menu Item objects
      
         wndRefresh = new JMenuItem("Refresh");
         wndRefresh.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Refresh();
                  }
               }
            );
         wndCascade = new JMenuItem("Cascade Windows");
         wndCascade.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Cascade();
                  }
               }
            );
      
      // Set Shortcuts for the Menu Items
      
         wndRefresh.setMnemonic(KeyEvent.VK_R);	// Alt R Bring It Up
         wndCascade.setMnemonic(KeyEvent.VK_C);	// Alt C
         wndCascade.setEnabled(false);
      
      // Add Items to the Windows Menu
      
         windMenu.add(wndRefresh);
         windMenu.add(wndCascade);
      
         return windMenu;
      }
   
       private JMenu ConstructSettingsMenu()
      {
      // Define the Setting Menu
      
         JMenu setMenu = new JMenu("Settings");
         setMenu.setMnemonic(KeyEvent.VK_S);		// Alt S Brings It Up
      
      // Create SubMenu
      
         JMenu LandFMenu = new JMenu("Look & Feel");
         LandFMenu.setMnemonic(KeyEvent.VK_L);	// Alt L
      
      // Look And Feel Radio Options
      
         ButtonGroup group = new ButtonGroup(); 
         JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem("Metal"); 
         rbMenuItem.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     try // to Change the Look and Feel to Metal
                     {
                        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                        SwingUtilities.updateComponentTreeUI(SWOGUI.this);
                     } 
                         catch (Exception  ex) 
                        {
                        }
                  }
               }
            );
         rbMenuItem.setSelected(true); 
         rbMenuItem.setMnemonic(KeyEvent.VK_M); 
         group.add(rbMenuItem); 
         LandFMenu.add(rbMenuItem); 
      
         rbMenuItem = new JRadioButtonMenuItem("Motif"); 
         rbMenuItem.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     try // to Change the Look and Feel to Motif
                     {
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                        SwingUtilities.updateComponentTreeUI(SWOGUI.this);
                     } 
                         catch (Exception  ex) 
                        {
                        }
                  }
               }
            );
         rbMenuItem.setMnemonic(KeyEvent.VK_O); 
         group.add(rbMenuItem); 
         LandFMenu.add(rbMenuItem); 
      
         rbMenuItem = new JRadioButtonMenuItem("Windows"); 
         rbMenuItem.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     try // to Change the Look and Feel to Windows
                     {
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                        SwingUtilities.updateComponentTreeUI(SWOGUI.this);
                     } 
                         catch (Exception  ex) 
                        {
                        }
                  }
               }
            );
         rbMenuItem.setMnemonic(KeyEvent.VK_W); 
         group.add(rbMenuItem); 
         LandFMenu.add(rbMenuItem); 
      
      // Add Items to the Windows Menu
      
         setMenu.add(LandFMenu);
      
         return setMenu;
      }
   
       private JMenu ConstructHelpMenu()
      {
      // Define the Help Menu
      
         JMenu helpMenu = new JMenu("Help");
         helpMenu.setMnemonic(KeyEvent.VK_H);	// ALt H Brings It Up
      
      // Create Menu Item objects
      
         JMenuItem hlpContents = new JMenuItem("Help Contents");
         hlpContents.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     Contents();
                  }
               }
            );
         JMenuItem hlpSWOG = new JMenuItem("SWOG Help");
         hlpSWOG.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     SWOG();
                  }
               }
            );
         JMenuItem hlpSW = new JMenuItem("Semantic Web Help");
         hlpSW.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     SW();
                  }
               }
            );
         JMenuItem hlpLicense = new JMenuItem("License");
         hlpLicense.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     License();
                  }
               }
            );
         JMenuItem hlpAbout = new JMenuItem("About");
         hlpAbout.addActionListener(
                new ActionListener() 
               {
                   public void actionPerformed(ActionEvent e) 
                  {
                     About();
                  }
               }
            );      
      
      // Set Shortcuts for the Menu Items
      
         hlpContents.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, NO_MASK));
      
         hlpContents.setMnemonic(KeyEvent.VK_H);	// Alt H
         hlpSWOG.setMnemonic(KeyEvent.VK_S);			// Alt S
         hlpSW.setMnemonic(KeyEvent.VK_W);			// ALt W
         hlpLicense.setMnemonic(KeyEvent.VK_L);		// Alt L
         hlpAbout.setMnemonic(KeyEvent.VK_A);		// Alt A
      
      // Add Items to the Help Menu
      
         helpMenu.add(hlpContents);
         helpMenu.add(hlpSWOG);
         helpMenu.add(hlpSW);
         helpMenu.addSeparator();
         helpMenu.add(hlpLicense);
         helpMenu.add(hlpAbout);
      
         return helpMenu;
      }
   
   // Function to create a new help browser window if one does not exist already
   
       private SWOGHelp createHelpWindow(String u)
      {
         if (helpBrowser == null)
         {
            helpBrowser = new SWOGHelp(u);
            helpBrowser.setVisible(true);
            helpBrowser.requestFocus();
         }
         else if (helpBrowser.getState() == Frame.ICONIFIED)  // If it is minimized, restore it
         {
            helpBrowser.setState(Frame.NORMAL);
            helpBrowser.requestFocus();			// Make it the focus window
         }
         else
         {
            helpBrowser.setVisible(true);
            helpBrowser.requestFocus();
         }
         return helpBrowser;
      }
   
   // File Menu/ToolBar Methods
   
       public SWOGDesktop New()	// Create a new Ontology Editor and enable all the options disabled by default
      {
         final SWOGDesktop swogdesktop = new SWOGDesktop(helpBrowser, desktops, fmSave, fmClose, wndCascade);
         desktops.add(swogdesktop);
         ((SWOGDesktop) desktops.get(desktops.lastIndexOf(swogdesktop))).setVisible(true); 
         desktop.add((SWOGDesktop) desktops.get(desktops.lastIndexOf(swogdesktop)));
         fmSave.setEnabled(true);
         fmClose.setEnabled(true);
         wndCascade.setEnabled(true);
      
      // Add Internal Window Listeners to guard against closing without saving
      
         swogdesktop.addInternalFrameListener(
                new InternalFrameAdapter()
               {
                   public void internalFrameActivated(InternalFrameEvent e)
                  {
                  }
               
                   public void internalFrameClosed(InternalFrameEvent e)
                  {
                  }
               
                   public void internalFrameClosing(InternalFrameEvent e)
                  {
                     swogdesktop.Close();
                  }
               
                   public void internalFrameDeactivated(InternalFrameEvent e)
                  {
                  }
               
                   public void internalFrameDeiconified(InternalFrameEvent e)
                  {
                  }
               
                   public void internalFrameIconified(InternalFrameEvent e)
                  {
                  }
               
                   public void internalFrameOpened(InternalFrameEvent e)
                  {
                  }
               });
         try {
            swogdesktop.setSelected(true); //Make it the focused internal window
         } 
             catch (java.beans.PropertyVetoException e) 
            {
            }
      
         return swogdesktop;
      }
   
       public void Open()
      {
         JFileChooser fc = new JFileChooser();
         String[] damlStr = new String[] {"daml"};	// Let a user choose DAML files from filter
         String[] xmlStr = new String[] {"xml"};	// Let a user choose XML files from filter
         fc.setMultiSelectionEnabled(false);			// Only One File At a Time Please!
         fc.addChoosableFileFilter(new SWOGFileFilter(damlStr, "DAML (*.daml)"));
         fc.addChoosableFileFilter(new SWOGFileFilter(xmlStr, "XML (*.xml)"));
         int returnVal = fc.showOpenDialog(this); 	// Prompt the user for the file with a dialog box
      
         if (returnVal == JFileChooser.APPROVE_OPTION) 
         { 
            File file = fc.getSelectedFile(); 
         
            // Open the file in a new window
         
            SWOGDesktop currentDesktop = New();
            currentDesktop.Open(file);
         }
      }
   
       public void SaveAll()
      {
      // Run through all open editors and save them
      
         JInternalFrame[] openWindows = desktop.getAllFrames(); 
         for (int i = 0; i < openWindows.length; i++)
         {
            ((SWOGDesktop) openWindows[i]).Save();
         }
      }
   
       public void CloseAll()
      {
      // Make Sure All Files Are Saved by running through all open editors, then close them
      
         JInternalFrame[] openWindows = desktop.getAllFrames();
      
         for (int i = 0; i < openWindows.length; i++)
         {
            if (((SWOGDesktop)openWindows[i]).isModified())
            {
               ((SWOGDesktop)openWindows[i]).checkSaveBeforeClose();
            }
         }
      
      // Reset All Necessary Variables
      
         desktops = null;
         desktops = new ArrayList();
         desktop.removeAll();
         desktop.updateUI();
         fmSave.setEnabled(false);
         fmClose.setEnabled(false);
      }
   
       public void Exit()
      {
      // Make Sure All Files Are Saved, then close the application
      
         JInternalFrame[] openWindows = desktop.getAllFrames();
      
         for (int i = 0; i < openWindows.length; i++)
         {
            if (((SWOGDesktop)openWindows[i]).isModified())
            {
               ((SWOGDesktop)openWindows[i]).checkSaveBeforeClose();
            }
         }
      
         dispose();
         System.exit(0);
      }
   
   // Window Menu Methods
   
       public void Refresh()	//Redraw all the interfaces
      {
         JInternalFrame[] openWindows = desktop.getAllFrames(); 
      
         desktop.updateUI();
      
         for (int i = 0; i < openWindows.length; i++)
         {
            openWindows[i].updateUI();
         }
      }
   
       public void Cascade()	// Cascade all open editor windows
      {
         int x = 0;
         int y = 0;
         JInternalFrame[] openWindows = desktop.getAllFrames(); 
      
         for (int i = 0; i < openWindows.length; i++)
         {
            openWindows[i].reshape(x, y, 660, 520);	//Resize each window and offset it by 30 px east and 30 px south
            x += 30;
            y += 30;
         }
      }
   
   // Help Menu/ToolBar Methods
   
       public void Contents()	// Set the Help Browser to the contents page
      {
         helpBrowser = createHelpWindow("file:///" + System.getProperty("user.dir") + "\\help\\contents.html");
      }
   
       public void SWOG()	// Set the Help Browser to the contents page, section SWOG
      {
         helpBrowser = createHelpWindow("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#SWOG");
      }
   
       public void SW()	// Set the Help Browser to the contents page, section SW (Semantic Web)
      {
         helpBrowser = createHelpWindow("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#SW");
      }
   
       public void License()	// Set the Help Browser to the contents page, section License
      {
         helpBrowser = createHelpWindow("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#License");
      }
   
       public void About()	// Set the Help Browser to the contents page, section About
      {
         helpBrowser = createHelpWindow("file:///" + System.getProperty("user.dir") + "\\help\\contents.html#About");
      }
   
       public static void main(String[] args) 
      {
      //Create a Splashscreen that displays an image with a text label
         String text = "Copyright (C) 2002 by Dackral Scott Phillips. All Rights Reserved.";
         String iconPath = "images/swogSplash.gif";
         SWOGSplashScreen intro = new SWOGSplashScreen(text, iconPath, 5000, Color.white, Color.black);
      
      // Instantiate the GUI
         SWOGUI swogui = new SWOGUI();
         swogui.setVisible(true);
      }
   }