|
/******************************************************************************
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 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");
Trace.enter("SaveAsMenuItem.SaveAsMenuItem");
//.. 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);
this.container=container;
Trace.exit("SaveAsMenuItem.SaveAsMenuItem");
}
public void setKeyStrokeForSaveAsMenuItem(KeyStroke saveAsMenuItemKeyStroke)
{
Trace.enter("SaveAsMenuItem.setKeyStrokeForSaveAsMenuItem");
//..sets the user defined Keystroke for the SaveAsMenuItem
setAccelerator(saveAsMenuItemKeyStroke);
Trace.exit("SaveAsMenuItem.setKeyStrokeForSaveAsMenuItem");
}
public void actionPerformed(ActionEvent evt)
{
Trace.enter("SaveAsMenuItem.actionPerformed");
//.. Default Handler
//..gets the OpenFileEntries into recentFiles Vector
Vector recentFileEntries=FileMenu.getRecentFileEntriesVector();
Component componentInternalFrames[]=container.getComponents();
JRootPane rootPane = (JRootPane)container.getComponents()[0];
JPanel panel = (JPanel)rootPane.getComponents()[0];
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
Trace.exception(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(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);
//..Open a FileChooser
JFileChooser fileChooser =new JFileChooser();
//..Fire the property change event
firePropertyChange("FileChooserOpened",fileChooser,null);
//.. 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());
}
catch(FileNotFoundException fnfe)
{
Trace.exception(fnfe);
//fnfe.printStackTrace();
//..JOptionPane.showMessageDialog(null,fnfe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
//fnfe.printStackTrace();
}
catch(IOException ioexcep)
{
Trace.exception(ioexcep);
//ioexcep.printStackTrace();
//..JOptionPane.showMessageDialog(null,ioexcep.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
//ioexcep.printStackTrace();
}
}
else if(result==JOptionPane.NO_OPTION)
{
Trace.exit("SaveAsMenuItem.actionPerformed");
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());
}
catch(FileNotFoundException fnfe)
{
Trace.exception(fnfe);
//fnfe.printStackTrace();
//..JOptionPane.showMessageDialog(null,fnfe.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
//fnfe.printStackTrace();
}
catch(IOException ioexcep)
{
Trace.exception(ioexcep);
//ioexcep.printStackTrace();
//..JOptionPane.showMessageDialog(null,ioexcep.getMessage(),"MakeLogic:FileMenu",JOptionPane.ERROR_MESSAGE);
//ioexcep.printStackTrace();
}
//..add the file name to the recentFile vector
recentFileEntries.add(file.getAbsolutePath());
}
}//.. END_IF (SAVE OPTION)
}
else
{
}
Trace.exit("SaveAsMenuItem.actionPerformed");
//..Return once the document is saved
return;
}
}
Trace.exit("SaveAsMenuItem.actionPerformed");
}
}
|
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.
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