Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / Java / Java SE

Free "tail -f" Tool for all Platforms: Tail for Windows, Tail for Linux and Tail for Mac

Rate me:
Please Sign up or sign in to vote.
2.08/5 (8 votes)
19 Jul 2006CPOL3 min read 56K   1.1K   24  
MakeLogic Tail is a freeware similar to " tail -f " of Linux. It is Tail for Windows, Tail for Linux and Tail for Mac
/******************************************************************************
 
Copyright (C) 2006  MakeLogic

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.

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.

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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


Date		: 14th December 2005
Authors		: MakeLogic

Design		: Ujjwal
Developer	: Dharma

********************************************************************************/
 
package com.makeLogic.utils;

import com.makeLogic.Tail;
import com.makeLogic.TailMDI;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.IOException;

import java.util.EventListener;
import java.util.Hashtable;
import java.util.NoSuchElementException;
import java.util.Vector;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import com.makeLogic.utils.MLInternalFrame;

 public class FileMenu extends JMenu 
 {
	 //..Container
	 Container container;

	 //..MenuItems
	 NewMenuItem newMenuItem;
	 OpenMenuItem openMenuItem;
	 OpenProjectMenuItem openProjectMenuItem;
	 private static SaveMenuItem saveMenuItem;
	 SaveAsMenuItem saveAsMenuItem;
	 	 
	 //..Menu
	 public static RecentFilesMenuItem recentFilesMenuItem;
	 
	 //..MenuItem
	 ExitMenuItem  exitMenuItem;

	 //..Separators
	 JSeparator   separatorOne;
	 JSeparator   separatorTwo;
	 JSeparator   separatorThree;

	 //..KeyStrokes
	 KeyStroke newMenuItemKeyStroke;
	 KeyStroke openMenuItemKeyStroke;
	 KeyStroke openProjectMenuItemKeyStroke;
	 KeyStroke saveMenuItemKeyStroke;
	 KeyStroke saveAsMenuItemKeyStroke;
	 char recentFilesMenuItemKeyStroke;
	 KeyStroke exitMenuItemKeyStroke;
	 

	 //..Hashtables and Vector Declarations
     static Hashtable internalFrameOpenFileNames;
	 static Hashtable internalFrameNewFileNames;
	 static Vector recentFileEntries;

	 //..constants for Maximum File Entries and RecentFiles MenuItem Entries
	 static int maxEntriesInConfFile = 20;
	 //static int maxEntriesInRecentFilesMenu = 8;
         public static int maxEntriesInRecentFilesMenu;

	 public FileMenu(Container container)
	 {
		 //..calls super class constructor
		 super("File");

		 Trace.enter("FileMenu.FileMenu");
		 
		 //..Sets Mnemonic for FileMenu
		 setMnemonic('F');

		 //continer is assigned with argument container
		 this.container=container;
		 
		 //..Hashtable
		 internalFrameOpenFileNames = new Hashtable();
		 internalFrameNewFileNames = new Hashtable();

		 //..Vector
		 recentFileEntries=new Vector();

		 //..Create OpenMenuItem and add it to the FileMenu
		 openMenuItem = new OpenMenuItem(container);
		 add(openMenuItem);

		 add(new JSeparator());

		 //..Create New MenuItem and add it to the FileMenu
		 newMenuItem = new NewMenuItem(container);
		 add(newMenuItem);

		  //..Create OpenProject MenuItem and add it to the FileMenu       
		 openProjectMenuItem = new OpenProjectMenuItem(container);
		 add(openProjectMenuItem);
		         
		// Creates and Adds a separator
		 separatorOne = new JSeparator();
		 add(separatorOne);

         //..Create SaveAsMenuItem and add it to the FileMenu
		 //saveAsMenuItem= new SaveAsMenuItem(container);
		 
		 //..Create and Add a sepataor 
		 separatorTwo=new JSeparator();
		 
         //.. Add a RecenfFiles Menu to the FiLeMenu
		 recentFilesMenuItem=new RecentFilesMenuItem(container);
		 add(recentFilesMenuItem);

         //..Create and Add a sepataor 
		 separatorThree=new JSeparator();
		 add(separatorThree);

		 //..Create ExitMenuItem and add it to the FileMenu
		 exitMenuItem=new ExitMenuItem(container);
		 add(exitMenuItem);

		 //...Update the RecentFilesMenu Item from the files in the conf file
		 FileMenu.updateRecentFilesMenu(FileMenu.recentFilesMenuItem);

		 Trace.exit("FileMenu.FileMenu");

	 }

	 public FileMenu(Container container,KeyStroke newMenuItemKeyStroke,KeyStroke openMenuItemKeyStroke,KeyStroke saveMenuItemKeyStroke,KeyStroke saveAsMenuItemKeyStroke, char recentFilesMenuItemKeyStroke,KeyStroke exitMenuItemKeyStroke)
	 {
		 //..sending container object to the constructor
		 this(container);

		 Trace.enter("FileMenu.FileMenu");
		 
		 //..Assign the NewMenuItemKeyStroke and set the user defined KeyStroke for it
		 this.newMenuItemKeyStroke=newMenuItemKeyStroke;
		 setKeyStrokeForNewMenuItem(newMenuItemKeyStroke);

		//..Assign the openMenuItemKeyStroke and set the user defined KeyStroke for it
		 this.openMenuItemKeyStroke=openMenuItemKeyStroke;
		  setKeyStrokeForOpenMenuItem(openMenuItemKeyStroke);

		 //..Assign the openProjectMenuItemKeyStroke and set the user defined KeyStroke for it
		 this.openProjectMenuItemKeyStroke=openProjectMenuItemKeyStroke;
		 setKeyStrokeForOpenMenuItem(openProjectMenuItemKeyStroke);

		 //..Assign the SaveAsMenuItemsKeyStroke and set the user defined KeyStroke for it
		 this.saveAsMenuItemKeyStroke=saveAsMenuItemKeyStroke;
		 setKeyStrokeForSaveAsMenuItem(saveAsMenuItemKeyStroke);

		 //..Assign the RecentFilesMenuItemsKeyStroke and set the user defined KeyStroke for it
		 this.recentFilesMenuItemKeyStroke=recentFilesMenuItemKeyStroke;
		 setKeyStrokeForRecentFilesMenuItem(recentFilesMenuItemKeyStroke);

		 //..Assign the ExitMenuItemsKeyStroke and set the user defined KeyStroke for it
		 this.exitMenuItemKeyStroke=exitMenuItemKeyStroke;
		 setKeyStrokeForExitMenuItem(exitMenuItemKeyStroke);

		 //...Update the RecentFilesMenu Item from the files in the conf file
		 FileMenu.updateRecentFilesMenu(FileMenu.recentFilesMenuItem);
	 
		 Trace.exit("FileMenu.FileMenu");
	 }

	 public void setKeyStrokeForNewMenuItem(KeyStroke  newMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForNewMenuItem");
		 
		 //..sets the KeyStroke for the NewMenuItem
		 newMenuItem.setKeyStrokeForNewMenuItem(newMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForNewMenuItem");
	 }
	 
	 public void setKeyStrokeForOpenMenuItem(KeyStroke openMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForOpenMenuItem");
		 
		 //..sets the KeyStroke for the openMenuItem
		 openMenuItem.setAccelerator(openMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForOpenMenuItem");
	 }
	 
	 public void setKeyStrokeForOpenProjectMenuItem(KeyStroke openProjectMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForOpenProjectMenuItem");
		 
		 //..sets the KeyStroke for the openMenuItem
		 openProjectMenuItem.setAccelerator(openProjectMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForOpenProjectMenuItem");
	 }
	 
	 public void setKeyStrokeForSaveMenuItem(KeyStroke saveMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForSaveMenuItem");
		 
		 //..sets the KeyStroke for the SaveMenuItem
		 saveMenuItem.setAccelerator(saveMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForSaveMenuItem");
	 }
	 
	 public void setKeyStrokeForSaveAsMenuItem(KeyStroke saveAsMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForSaveAsMenuItem");
		 
		 //..sets the KeyStroke for the SaveAsMenuItem
		 saveAsMenuItem.setAccelerator(saveAsMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForSaveAsMenuItem");
	 }
	 
	 public void setKeyStrokeForRecentFilesMenuItem(char  recentFilesMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForRecentFilesMenuItem");
		 
		 //..sets the KeyStroke for the RecentFilesMenuItem
		 recentFilesMenuItem.setMnemonic(recentFilesMenuItemKeyStroke);//recentFilesMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForRecentFilesMenuItem");
	 }
	 
	 public void setKeyStrokeForExitMenuItem(KeyStroke  exitMenuItemKeyStroke)
	 {
		 Trace.enter("FileMenu.setKeyStrokeForExitMenuItem");
		 
		//..sets the KeyStroke for the ExitMenuItem
		exitMenuItem.setAccelerator(exitMenuItemKeyStroke);

		 Trace.exit("FileMenu.setKeyStrokeForExitMenuItem");
	 }

	 //.. client ActionListeners
	 public void addNewMenuItemActionListener(ActionListener newMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addNewMenuItemActionListener");
		 
		 //..adds ActionListener to the NewMenuItem
		newMenuItem.addActionListener(newMenuItemActionListener);

		 Trace.exit("FileMenu.addNewMenuItemActionListener");
	 }
	 
	 public void addOpenMenuItemActionListener(ActionListener openMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addOpenMenuItemActionListener");
		 
		 //..adds ActionListener to the OpenMenuItem
		 openMenuItem.addActionListener(openMenuItemActionListener);

		 Trace.exit("FileMenu.addOpenMenuItemActionListener");
	 }
	 public void addOpenProjectMenuItemActionListener(ActionListener openProjectMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addOpenProjectMenuItemActionListener");
		 
		 //..adds ActionListener to the OpenMenuItem
		 openProjectMenuItem.addActionListener(openProjectMenuItemActionListener);

		 Trace.exit("FileMenu.addOpenProjectMenuItemActionListener");
	 }

	 public void addSaveMenuItemActionListener(ActionListener saveMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addSaveMenuItemActionListener");
		 
		//..adds ActionListener to the SaveMenuItem
		saveMenuItem.addActionListener(saveMenuItemActionListener);

		 Trace.exit("FileMenu.addSaveMenuItemActionListener");
	 }
	 
	 public void addSaveAsMenuItemActionListener(ActionListener saveAsMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addSaveAsMenuItemActionListener");
		 
		 //..adds ActionListener to the SaveMenuItem
		 saveAsMenuItem.addActionListener(saveAsMenuItemActionListener);

		 Trace.exit("FileMenu.addSaveAsMenuItemActionListener");
	 }
	 
	 public void addRecentFilesMenuItemActionListener(ActionListener recentFilesMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addRecentFilesMenuItemActionListener");
		 
		 //..adds ActionListener to the RecentFilesMenu
		 recentFilesMenuItem.addActionListener(recentFilesMenuItemActionListener);

		 Trace.exit("FileMenu.addRecentFilesMenuItemActionListener");
	 }
	 
	 public void addExitMenuItemActionListener(ActionListener exitMenuItemActionListener)
	 {
		 Trace.enter("FileMenu.addExitMenuItemActionListener");
		 
		 //..adds ActionListener to the ExitMenuItem
		 exitMenuItem.addActionListener(exitMenuItemActionListener);

		 Trace.exit("FileMenu.addExitMenuItemActionListener");
	 }

	//.. Disable Default Listener	 
	 public void disableDefaultNewMenuItemListener()
	 {
		 Trace.enter("FileMenu.disableDefaultNewMenuItemListener");
		 
		 //..remove the NewMenuItemListener if it exists
		 newMenuItem.removeActionListener(newMenuItem);

		 Trace.exit("FileMenu.disableDefaultNewMenuItemListener");
	 }
	 
	 public void disableDefaultOpenMenuItemListener()
	 {
		 Trace.enter("FileMenu.disableDefaultOpenMenuItemListener");
		 
		 //..remove the OpenMenuItemListener if it exists
		 openMenuItem.removeActionListener(openMenuItem);

		 Trace.exit("FileMenu.disableDefaultOpenMenuItemListener");
	 }
	 public void disableDefaultOpenProjectMenuItemListener()
	 {
		 Trace.enter("FileMenu.disableDefaultOpenProjectMenuItemListener");
		 
		 //..remove the OpenMenuItemListener if it exists
		 openProjectMenuItem.removeActionListener(openProjectMenuItem);

		 Trace.exit("FileMenu.disableDefaultOpenProjectMenuItemListener");
	 }

	 public void disableDefaultSaveMenuItemListener()
	  {
		 Trace.enter("FileMenu.disableDefaultSaveMenuItemListener");
		 
		 //..remove the SaveMenuItemListener if it exists
		 saveMenuItem.removeActionListener(saveMenuItem);

		 Trace.exit("FileMenu.disableDefaultSaveMenuItemListener");
	  }
	 
	 public void disableDefaultSaveAsMenuItemListener()
	 {
		 Trace.enter("FileMenu.disableDefaultSaveAsMenuItemListener");
		 
		 //..remove the SaveAsMenuItemListener if it exists
		 saveAsMenuItem.removeActionListener(saveAsMenuItem);

		 Trace.exit("FileMenu.disableDefaultSaveAsMenuItemListener");
	 }
	 
	 public void disableDefaultRecentFilesMenuItemListener()
	 {
		 Trace.enter("FileMenu.disableDefaultRecentFilesMenuItemListener");
		 
		 //..remove the RecentFilesMenuItemListener if it exists
		 this.remove(recentFilesMenuItem);
		 this.remove(separatorThree);

		 Trace.exit("FileMenu.disableDefaultRecentFilesMenuItemListener");
	 }
	 
	 public void disableDefaultExitMenuItemListener()
	 {
		 Trace.enter("FileMenu.disableDefaultExitMenuItemListener");
		 
		 //remove the RecentFilesMenuItemListener if it exists
		 exitMenuItem.removeActionListener(exitMenuItem);

		 Trace.exit("FileMenu.disableDefaultExitMenuItemListener");
	 }

	 //.. GetHashtables
	//..  Get Open HashTable
	  static synchronized Hashtable getInternalFramesOpenFileNamesHashTable()
	 {
		  Trace.enter("FileMenu.getInternalFramesOpenFileNamesHashTable");
		  Trace.exit("FileMenu.getInternalFramesOpenFileNamesHashTable");
          
		  return internalFrameOpenFileNames;
	 }

	 //..Get OpenFilesVector
	 public static synchronized Vector getRecentFileEntriesVector()
	 {
		 Trace.enter("FileMenu.getRecentFileEntriesVector");
		 Trace.exit("FileMenu.getRecentFileEntriesVector");

		 return recentFileEntries;
	 }
	 //..Get New HashTabel
	  static synchronized Hashtable getInternalFramesNewFileNamesHashTable()
	 {
		  Trace.enter("FileMenu.getInternalFramesNewFileNamesHashTable");
		  Trace.exit("FileMenu.getInternalFramesNewFileNamesHashTable");
          
		  return internalFrameNewFileNames;
	 }

	//.. Remove InternalFrames from Open and New Hastables
	//.. Removes the InternalFrames and its filepath from Open Hashtable
	 static  void removeOpenFileEntry(MLInternalFrame internalFrame)
	 {
		 Trace.enter("FileMenu.removeOpenFileEntry");
		 
		 internalFrameOpenFileNames.remove(internalFrame);

		 Trace.exit("FileMenu.removeOpenFileEntry");
	 }
	
	 //.. updates the Conf File whenever required
	 public static synchronized void updateConfFile()
	 {
		 Trace.enter("FileMenu.updateConfFile");

		 //..update the appconst file Start ...................................

		 long ut = 0;

		 ut = TailMDI.ut + ((System.currentTimeMillis() - Tail.startTime)/1000);
		 
		 java.util.Properties appConst = new java.util.Properties();

		 try
		 {
			 //..Get the appconst from appconst.ml file
			 
			 //..Read the values into properties
			 appConst.load(new java.io.FileInputStream(TailMDI.APP_CONSTANTS_FILE));
			 appConst.setProperty("ut",""+ut);

			 //..Store the Properties into a file.
			 try
			 {
				 appConst.store(new java.io.FileOutputStream(TailMDI.APP_CONSTANTS_FILE),null);
			 }
			 catch(Exception writeException)
			 {
				 //..Do Nothing
				 Trace.exception(writeException);
			 }
		 }
		 catch(Exception ex)
		 {
			 //..Do Nothing
			 //Trace.exception(ex);

			 try
			 {
				 //..Create the file
				 java.io.File createFile = new java.io.File(TailMDI.APP_CONSTANTS_FILE);
				 createFile.createNewFile();
			 }
			 catch(Exception exp)
			 {
				 Trace.exception(exp);
			 }

			 //..Set the default values
			 int noOfProj = 4;
			 int maxLines = 100;
			 int noOfRecentFiles = 8;
			 int sendBugInfo = 1;
			 ut = 0;

			 //..add the properties to the file
			 appConst.setProperty("maxlines",""+100);
			 appConst.setProperty("noofproj",""+4);
			 appConst.setProperty("maxrecentfiles",""+8);
			 appConst.setProperty("sendbuginfo",""+1);
			 appConst.setProperty("ut",""+0);

			 //..Store the Properties into a file.
			 try
			 {
				 appConst.store(new java.io.FileOutputStream(TailMDI.APP_CONSTANTS_FILE),null);
			 }
			 catch(Exception writeException)
			 {
				 //..Do Nothing
				 Trace.exception(writeException);
			 }
		 }

		 //..update the appconst file end   ...................................
		 
		 //..Vector to store FileEntries
		 Vector fileVector=new Vector();

		 //..Vector which holds the FileEntries of the OpendFiles
		 Vector recentFileVector=FileMenu.getRecentFileEntriesVector();

		 	 try
			 {
				 //..Opens the conf file
				 BufferedReader bufferReader=new BufferedReader(new FileReader(TailMDI.CONF_FILE));

				 //..Reads the contents from the file and create a Vector
				 String line=bufferReader.readLine();
				 while(line!=null)
				 {
					 //..adds the File contents to the vector
					 fileVector.add(line);

					 //..reads the next Line
					 line=bufferReader.readLine();
				 }

         		 //.. close the File
				 bufferReader.close();

				 //..Initialize the required variables to null
				 MLInternalFrame tempFrame = null;
				 String fileEntryInOpenHashTable = null;
				 String fileEntryInVector=null;

				 for(int k=0;k<recentFileVector.size();k++)
				 {
					 //..File Name from recentFileVector
					 fileEntryInVector=(String)recentFileVector.get(k);
					 try
					 {
						 //..Debug
						 //System.outprintln("..ML.. fileEntryInVector -> "+fileEntryInVector);
						
						 //..See if that file name is in the 
						 //..Recent files vector
						
						 if(fileVector.contains( fileEntryInVector ))
						 {
							 //..Debug
							 //System.outprintln("..ML.. Contains in the Vector");

							 //..Yes the entry exists 
							 //..Make it the most recent file
		 
							 //..Since the entries in Vector are unique (ie no duplications)
							 //..Get the entry from the vector
							 //..Delete the entry from that position
							 //..Add it at the top
							 
							 //..gets the current index of the file if it is there
							 int index =fileVector.lastIndexOf(fileEntryInVector );

							 //..remove it from that index
							 fileVector.remove(index);
						
							 //..add it to the FirstPosition
							 fileVector.add(fileEntryInVector);

						 }
						 else
						 {
							 //..No the entry does not exist in the 
							 //..Recent files vector

							 //..Just add it at the top
							 fileVector.add(fileEntryInVector);
							
						 }
					 }
					 catch(java.util.NoSuchElementException nsee)
					 {							
						 //..notify to the user by  message box
						Trace.exception(nsee);
						
					 }					 					 
				 }
				 try
				 {
					 //..open the conf file in write mode
					 //..now update the conf file
					 //..***Important
					 /*********************************************************************
					  * 
					  *  See to it that the conf file doesn not contain 
					  *  more than 20 entries
					  * 
					  ******************************************************************/

					 //.. open the file in write mode using PrintWriter object
					 PrintWriter fileWriter=new PrintWriter(new BufferedWriter(new FileWriter(TailMDI.CONF_FILE)));
					 
					 int i=0;

					 //..gets the remaining file entries from the conf file
					 if(fileVector.size()>maxEntriesInConfFile)
					 {
						 i = fileVector.size()-maxEntriesInConfFile;
					 }
					 //..writes the fileentries into the conf file
					 for( ; i < fileVector.size() ; i++)
					 {
						 //gets the fileentry from the vector and writes into a file
						 fileWriter.println(fileVector.get(i));

						 //..flushes the printwriter object
						 fileWriter.flush();
					 }
				 }
				 catch(FileNotFoundException fnfe)
				 {
					 //fnfe.printStackTrace();
					 JOptionPane.showMessageDialog(Tail.tailMDI,fnfe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
					 Trace.exception(fnfe);
				 }
			 }
			 catch(FileNotFoundException fnfe)
			 {
				 /***************************
				  * 1. Show a message saying that the recent fiels informationa is lost
				  * 2. Tell the user that the application will try to recover
				  * 3. Create a new conf.ml file with no contents in it
				  * 4. Perform the same procedure whenever there is conf.ml file is not found
				  * 
				  */
				 //.. sends the Information to the user saying that the recentFile Information is lost
				 //JOptionPane.showMessageDialog(null,"Recent Files Information is Lost.Program will try to Recover","MakeLogic:FileMenu",JOptionPane.INFORMATION_MESSAGE );

				 Trace.exception(fnfe);
				 try
				 {
					//..opens the conf file
					 PrintWriter fileWriter=new PrintWriter(new BufferedWriter(new FileWriter(TailMDI.CONF_FILE)));
				 }
					
				 catch(IOException ioe)
				 {
					 JOptionPane.showMessageDialog(Tail.tailMDI,ioe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
					 Trace.exception(ioe);
				 }

			 }
			 catch(IOException ioe)
			 {
				 //ioe.printStackTrace();
				 JOptionPane.showMessageDialog(Tail.tailMDI,ioe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
				 Trace.exception(ioe);
			 }

		 Trace.exit("FileMenu.updateConfFile");
	 }

	 static synchronized void updateRecentFilesMenu(RecentFilesMenuItem recentFilesMenu)
	 {
		 Trace.enter("FileMenu.updateRecentFilesMenu");
		 
		 //..Remove all menu items inside recentFilesMenu
		 recentFilesMenu.removeAll();

		 //..Read the file and keep adding the MenuItems one by one

		 //open the recentfiles vector
		 Vector fileVector=new Vector();

		 try
		 {
			 //..Opens the conf file
			 BufferedReader bufferReader=new BufferedReader(new FileReader(TailMDI.CONF_FILE));
				 
			 //..Reads the contents from the file and create a Vector
			 String line=bufferReader.readLine();
			 while(line!=null)
			 {
				 //..adds the File contents to the vector
				 fileVector.add(line);

				 //..read the next Line
				 line=bufferReader.readLine();
			 }
			 //.. close the File
			 bufferReader.close();
		 }
		 catch(FileNotFoundException fnfe)
		 {
			 //Trace.exception(fnfe);
			 //fnfe.printStackTrace();
			 //JOptionPane.showMessageDialog(null,fnfe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);

			 //To DO

			 /***************************
			  * 1. Show a message saying that the recent fiels informationa is lost
			  * 2. Tell the user that the application will try to recover
			  * 3. Create a new conf.ml file with no contents in it
			  * 4. Perform the same procedure whenever there conf.ml file is not found
			  * 
			  */
			 //.. sends the Information to the user saying that the recentFile Information is lost
			 //JOptionPane.showMessageDialog(null,"Recent File Information is Lost.Program will try to Recover","MakeLogic:FileMenu",JOptionPane.INFORMATION_MESSAGE );
			 try
			 {
				 //..opens the conf file
				 PrintWriter fileWriter=new PrintWriter(new BufferedWriter(new FileWriter(TailMDI.CONF_FILE)));
			 }
			
			 catch(IOException ioe)
			 {
				 Trace.exception(ioe);
				 JOptionPane.showMessageDialog(Tail.tailMDI,ioe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
			 }
		 }
		 catch(IOException ioe)
		 {
			 //ioe.printStackTrace();
			 Trace.exception(ioe);
			 JOptionPane.showMessageDialog(Tail.tailMDI,ioe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
		 }

		 //..Now add the menu item components to the recent files menu
		 JMenuItem tempItem = null;

		 try
		 {
			 for(int i=fileVector.size()-1 ; i>=fileVector.size()-maxEntriesInRecentFilesMenu ; i--)
			 {
				 tempItem = new JMenuItem((String)fileVector.get(i));

				 //..Add handler to the tempItem
				 tempItem.addActionListener(recentFilesMenu);

				 //..Now add the menu item to the recent files menu
				 recentFilesMenu.add(tempItem);
			 }
		 }
		 catch(ArrayIndexOutOfBoundsException aiobe)
		 {
			 //..Do nothing
			 //Trace.exception(aiobe);
		 }
		 catch(Exception e)
		 {
			 Trace.exception(e);
		 }
	
		 Trace.exit("FileMenu.updateRecentFilesMenu");
	 }

	 //..returns Maximum Entries that can be kept in Conf File 
	 public int getMaxEntriesInConfFile()
	 {
		 Trace.enter("FileMenu.getMaxEntriesInConfFile");
		 Trace.exit("FileMenu.getMaxEntriesInConfFile");

		 return maxEntriesInConfFile;
	 }

	 //..returns Maximum Entries that can be kept in RecentFilesMenu
	 public int getMaxEntriesInRecentFilesMenu()
	 {
		 Trace.enter("FileMenu.getMaxEntriesInRecentFilesMenu");
		 Trace.exit("FileMenu.getMaxEntriesInRecentFilesMenu");
		 
		 return maxEntriesInRecentFilesMenu;
	 }

	 //..set functions for maxEntriesInConfFile and maxEntriesInRecentFilesMenu
	 public void setMaxEntriesInConfFile(int maxEntriesInConfFile)
	 {
		 Trace.enter("FileMenu.setMaxEntriesInConfFile "+maxEntriesInConfFile);
		 
		 //.. Max Entries in Conf File should not be negative
		 if(maxEntriesInConfFile<0) 
		 {
			 //..if maxEntriesInConfFile is negative set maxEntriesInConfFile to 20
			 this.maxEntriesInConfFile=20;
		 }
		 this.maxEntriesInConfFile=maxEntriesInConfFile;
		
		 Trace.exit("FileMenu.setMaxEntriesInConfFile "+maxEntriesInConfFile);
	 }

	 public void setMaxEntriesInRecentFilesMenu(int maxEntriesInRecentFilesMenu)
	 {
		 Trace.enter("FileMenu.setMaxEntriesInRecentFilesMenu "+maxEntriesInRecentFilesMenu);
		 
		 //.. Max Entries in RecentFiles Menu should not be negative
		 if(maxEntriesInRecentFilesMenu<0)
		 {
			/* if maxEntriesInRecentFilesMenu is negative
			 * set maxEntriesInConfFile to 8*/
			this.maxEntriesInRecentFilesMenu=4;
		 }

		 //.. gets the MaxEntries in Conf File
		 int maxEntriesInConfFile=getMaxEntriesInConfFile();

		 //..checks MaxEntries in RecentFiles Menu is lessthan Max Entries in Conf File
		 if(maxEntriesInRecentFilesMenu<maxEntriesInConfFile)
		 {
			 this.maxEntriesInRecentFilesMenu=maxEntriesInRecentFilesMenu;
		 }
		 else
		 {
			 /*.if MaxEntries in Recent Files Menu is greater than 
			  * MaxEntries in Recent Files, set MaxEntries in Recent Files to
			  * MaxEntries in ConfFile.
			  */
			 this.maxEntriesInRecentFilesMenu=maxEntriesInConfFile;
		 }

		 Trace.exit("FileMenu.setMaxEntriesInRecentFilesMenu "+maxEntriesInRecentFilesMenu);
	 }

	 //..returns the WindowListener
	 public WindowListener getDefaultClosingListener()
	 {
		 Trace.enter("FileMenu.getDefaultClosingListener");
		 
		 //..creates WindowsListener class
	   WindowAdapter windowAdapter=new WindowAdapter()
	   {
		   public void windowClosing(WindowEvent evt)
		   {
			   Trace.enter("WindowAdapter.windowClosing");
			   
			   //..calls the exitMenuItems actionPerformed method
			   //..It cares of saving the Files before closing,etc..,
			   exitMenuItem.actionPerformed(null);

			   Trace.exit("WindowAdapter.windowClosing");
		   }

	   };

		 Trace.exit("FileMenu.getDefaultClosingListener");

		 //..returns windowsListener
		return windowAdapter;
	 }

	 //..returns newMenuItem
	 public JMenuItem getNewMenuItem()
	 {
		 Trace.enter("FileMenu.getNewMenuItem");
		 Trace.exit("FileMenu.getNewMenuItem");

		 return newMenuItem;
	 }

	 //..returns OpenMenuItem
	 public JMenuItem getOpenMenuItem()
	 {
		 Trace.enter("FileMenu.getOpenMenuItem");
		 Trace.exit("FileMenu.getOpenMenuItem");

		 return openMenuItem;
	 }
	 //..returns OpenProjectMenuItem
	 public JMenuItem getOpenProjectMenuItem()
	 {
		 Trace.enter("FileMenu.getOpenProjectMenuItem");
		 Trace.exit("FileMenu.getOpenProjectMenuItem");

		 return openProjectMenuItem;
	 }

	 //..Get SaveMenuItem
	 public static JMenuItem getSaveMenuItem()
	 {
		 Trace.enter("FileMenu.getSaveMenuItem");
		 Trace.exit("FileMenu.getSaveMenuItem");

		 return saveMenuItem;
	 }

	 //..returns SaveAsMenuItem
	 public JMenuItem getSaveAsMenuItem()
	 {
		 Trace.enter("FileMenu.getSaveAsMenuItem");
		 Trace.exit("FileMenu.getSaveAsMenuItem");

		 return saveAsMenuItem;
	 }

	 //..returns RecentFilesMenuItem
	 public static JMenu getRecentFilesMenuItem()
	 {
		 Trace.enter("FileMenu.getRecentFilesMenuItem");
		 Trace.exit("FileMenu.getRecentFilesMenuItem");

		 return recentFilesMenuItem;
	 }

	 //..returns ExitMenuItem
	 public JMenuItem getExitMenuItem()
	 {
		 Trace.enter("FileMenu.getExitMenuItem");
		 Trace.exit("FileMenu.getExitMenuItem");

		 return exitMenuItem;

	 }
 }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Founder of MakeLogic - A Mobile Solutions and XML Web Services Company. B-Tech engineer from India's reputed IITs.

Designed and Authored products like MicroGraphs, MakeLogic VersionManager and MakeLogic Device Broker. Pls see http://www.makelogic.com

MakeLogic is a strategic business partner of Microsoft, Sun, IBM, HP and Citrix.

Contact : madanuuk@makelogic.com OR +91 - 40 - 55322433

Comments and Discussions