Click here to Skip to main content
15,896,557 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) 2005  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 java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Hashtable;
import java.util.Vector;


import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import javax.swing.*;

import com.makeLogic.utils.MLInternalFrame;
import com.makeLogic.TailInternalFrame;

/**
 * SaveAs MenuItem is one of the MenuItems of FileMenu.<br>
 * It opens a FileChooser DialogBox.<br>
 * User can enter a File Name and click on OK to save it into a New File.
 */
public class SaveAsMenuItem extends JMenuItem implements ActionListener
{
	//..Declaration
	Container container;
	TailInternalFrame internalFrame;

	SaveAsMenuItem(Container container)
	{	
		//..calls the super class constructor
		super("SaveAs");

		//.. sets the default keystroke as CTRL+A for thr SaveAsMenuItem
		setAccelerator(KeyStroke.getKeyStroke('A',java.awt.Event.CTRL_MASK,false));

		//..adds the Listener class to the SaveAsMenuItem
		addActionListener(this);

		/*//..firePropertyChange
		JFileChooser fileChooser= new JFileChooser();
		firePropertyChange("FileChooserOpened",fileChooser,null);
		*/

		this.container=container;
	}
	public void setKeyStrokeForSaveAsMenuItem(KeyStroke saveAsMenuItemKeyStroke)
	{
		//..sets the user defined Keystroke for the SaveAsMenuItem
		setAccelerator(saveAsMenuItemKeyStroke);
	}
	public void actionPerformed(ActionEvent evt)
	{
		//.. Default Handler

		//..OpenFiles Hashtable
		//Hashtable tempOpenMenuHash = FileMenu.getInternalFramesOpenFileNamesHashTable();

		//..NewFiles Hashtable
		//Hashtable tempNewMenuHash =FileMenu.getInternalFramesNewFileNamesHashTable();
		
		//..gets the OpenFileEntries into recentFiles Vector
		Vector recentFileEntries=FileMenu.getRecentFileEntriesVector();

		//.. gets the InternalFrames
		//System.outprintln("\nSaveAsMenuItem container : "+container);
		//System.outprintln("\nTDI Length "+container.getComponents().length);

		Component componentInternalFrames[]=container.getComponents();
	
		JRootPane rootPane = (JRootPane)container.getComponents()[0];
		//System.outprintln("\nroot pane Length "+rootPane.getComponents().length);

		JPanel panel = (JPanel)rootPane.getComponents()[0];

		////System.outprintln("\nSaveAsMenuItem componentInternalFrames : "+componentInternalFrames[0]);
		//System.outprintln("\nSaveAsMenuItem "+panel.getComponents()[0]);

		//.. stores the InternalFrames in a InternalFrames array
		//MLInternalFrame internalFrames[]=new MLInternalFrame[componentInternalFrames.length];
		TailInternalFrame internalFrames[]=new TailInternalFrame[componentInternalFrames.length];


		for(int i=0;i<internalFrames.length;i++)
		{

			try
			{
				//..gets the MLInternalFrames from the component array
				internalFrames[i]=(TailInternalFrame)componentInternalFrames[i];
			}
			catch(ClassCastException cce)
			{
				//..Do nothing
				//..Debug
				////System.outprintln(cce);
				JOptionPane.showMessageDialog(null,cce.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
				//cce.printStackTrace();
			}
			//..searches for the Active InternalFrame
			if(internalFrames[i].isSelected()==true)
			{
				//..gets the Active InternalFrame
				this.internalFrame=internalFrames[i];

				//..gets the EditorPane from the InternalFrame
				Component[] jsp = internalFrame.getContentPane().getComponents();
				JScrollPane jspn = (JScrollPane)internalFrame.getContentPane().getComponent(0);
				JViewport jvp  = (JViewport)jspn.getViewport();
				JEditorPane jep = (JEditorPane)jvp.getView();

				//..Do this if the frame belongs to open hash table
				//if(tempOpenMenuHash.containsKey(internalFrame))
				if(true)
				{
					//.. if it is  in Already Opened Ask the User to overwrite

					//..get the FileName of the Opend FileName
					//String FileName=(String)tempOpenMenuHash.get(internalFrame);
					

					//..creates a File Object with the FileName and passed to the FileChooser
					//File file=new File(FileName);

					//..Open a FileChooser
					JFileChooser fileChooser =new JFileChooser();

					//..Fire the property change event
					firePropertyChange("FileChooserOpened",fileChooser,null);

					//.. set the file path in the FileChooser
					//fileChooser.setSelectedFile(file);

					//.. show the FileChooser
					int response=fileChooser.showSaveDialog(container);

					//..Do this if Save Button is Clicked
					if(response==JFileChooser.APPROVE_OPTION)
					{

						//..gets the selected file						
						File file=fileChooser.getSelectedFile();

						//.. checks for File Existance
						if(file.exists()==true)
						{
							//..Do this if file exists

							//..Open a confirm dialog to overwrite the selected file
							int result=JOptionPane.showConfirmDialog(internalFrame,"This File Already Exists, Do You want to Replace the Existing File ?","Confirm Dialog",JOptionPane.YES_NO_OPTION);

							if(result==JOptionPane.YES_OPTION)
							{
								//..Overwrite the File
								try
								{
									//.. Open the file in output mode
									DataOutputStream outputStream= new DataOutputStream(new FileOutputStream(file.getAbsolutePath()));

									//.. write the EditorPane contents into a file
									outputStream.writeBytes(jep.getText());

									//..change the flag to saved mode
									//internalFrame.setChanged(false);

									//.. removes '*' from the Frame Title 
									//if(internalFrame.getTitle().endsWith("*"))
									//{
									//	internalFrame.setTitle(internalFrame.getTitle().substring(0,internalFrame.getTitle().length()-1));
									//}
														
								}
								catch(FileNotFoundException fnfe)
								{
									//fnfe.printStackTrace();
									JOptionPane.showMessageDialog(null,fnfe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
									//fnfe.printStackTrace();
								}
								catch(IOException ioexcep)
								{
									//ioexcep.printStackTrace();
									JOptionPane.showMessageDialog(null,ioexcep.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
									//ioexcep.printStackTrace();
								}

							}													
							else if(result==JOptionPane.NO_OPTION)
							{
								return;
							}

						}
						else
						{

							//.. Do this if file does not exists
							try
							{
								//.. Open the file in output mode
								DataOutputStream outputStream= new DataOutputStream(new FileOutputStream(file.getAbsolutePath()));

								//.. write the EditorPane contents into a  file
								outputStream.writeBytes(jep.getText());

								//..change the flag to saved mode
								//internalFrame.setChanged(false);

								//.. removes '*' from the Frame Title 
								//if(internalFrame.getTitle().endsWith("*"))
								//{
								//	internalFrame.setTitle(internalFrame.getTitle().substring(0,internalFrame.getTitle().length()-1));
								//}
								
								//..Debug
								//.. sets the New Title if changed
								//internalFrame.setTitle(file.getAbsolutePath());
							}
							catch(FileNotFoundException fnfe)
							{
								//fnfe.printStackTrace();
								JOptionPane.showMessageDialog(null,fnfe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
								//fnfe.printStackTrace();
							}
							catch(IOException ioexcep)
							{
								//ioexcep.printStackTrace();
								JOptionPane.showMessageDialog(null,ioexcep.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);																
								//ioexcep.printStackTrace();
							}

							//..add the file path in OpenHashtable
                            //tempOpenMenuHash.put(internalFrame,file.getAbsolutePath());

							//..add the file name to the recentFile vector
							recentFileEntries.add(file.getAbsolutePath());

						}
							
					}//.. END_IF (SAVE OPTION)
	
				}
				else
				{
					
					}


				//..Return once the document is saved
				return;
				
			}
			}
		}

}

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