//future enhancements : use of textpane
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
class MyJCreator extends JFrame
{
Process p =null; // used for executing javac
JPopupMenu popup;
JMenuBar topBar ;
JMenuItem itemCu,itemCo,itemPa;
JMenu menuFile , menuEdit , menuHelp ;
JMenuItem itmNew , itmOpen , itmSave , itmClose , itmCopy , itmCut ,
itmPaste , itmAbout ;
JTextArea tAreaEditor ;
JFileChooser myFileChooser , mySaveFile ;
Container c ;
String nameOfTheFile ,runPathforJava;
String OpenOrSaveFilePath;
//buttons for toolbar
JButton buttonNew, buttonOpen, buttonSave, buttonCompile , buttonRun ;
//icons for toolbar
Icon newIcon, saveIcon, openIcon , compileIcon, runIcon;
MyJCreator ()
{
super("My JCreator ");
c = getContentPane();
c.setLayout(new BorderLayout());
popup = new JPopupMenu();
//adding the text editor
tAreaEditor = new JTextArea (20,40);
tAreaEditor .setLineWrap(true) ;
//Adding popup
//popup.add(itmCopy);
//creates the new file chooser
myFileChooser = new JFileChooser();
mySaveFile = new JFileChooser();
topBar = new JMenuBar ();
// adding contents for the menu --> file
menuFile = new JMenu ("File");
itmNew = new JMenuItem ("New");
itmOpen = new JMenuItem ("Open");
itmSave = new JMenuItem ("Save");
itmClose = new JMenuItem ("Close");
menuFile .add(itmNew );
menuFile .add(itmOpen );
menuFile .add( itmSave );
menuFile .add(itmClose );
topBar .add(menuFile );
//*********************************************
// adding contents for the menu --> Edit
menuEdit = new JMenu ("Edit");
itmCut = new JMenuItem ("Cut");
itmCopy = new JMenuItem ("Copy");
itmPaste = new JMenuItem ("Paste");
menuEdit .add(itmCut );
menuEdit .add(itmCopy );
menuEdit .add(itmPaste );
topBar .add(menuEdit );
//*********************************************
//***********************************************
//***********************************************
menuHelp = new JMenu ("Help");
itmAbout = new JMenuItem ("About");
menuHelp .add(itmAbout );
topBar .add(menuHelp );
//*********************************************
//ADDING ACTION LISTENERS FOR THE MENU
//--------------------------------------------------------------------
MyMenuListener mm1 =new MyMenuListener ();
itmNew .addActionListener(mm1);
itmSave .addActionListener(mm1);
itmOpen .addActionListener(mm1);
// edit listeners
itmCut.addActionListener(mm1);
itmPaste.addActionListener(mm1);
itmCopy.addActionListener(mm1);
// Help listeners
itmAbout.addActionListener(mm1);
MousePopupListener pml = new MousePopupListener();
tAreaEditor.addMouseListener(pml);
Mypoplistener menuListener = new Mypoplistener();
popup.add(itemCu = new JMenuItem("Cut"));
itemCu.addActionListener(menuListener);
popup.add(itemCo = new JMenuItem("Copy"));
itemCo.addActionListener(menuListener);
popup.add(itemPa = new JMenuItem("Paste"));
itemPa.addActionListener(menuListener);
popup.addSeparator();
popup.setLabel("Justification");
popup.setBorder(new BevelBorder(BevelBorder.RAISED));
addMouseListener(new MousePopupListener());
tAreaEditor.add(popup);
setJMenuBar(topBar);
//___________________________________________________
//ADDING THE TOOLBAR AND ITS BUTTONS
JToolBar togglePanel = new JToolBar();
//creating icon buttons for new, open and save
//creating icons for the buttons
newIcon = new ImageIcon("new2.jpg");
openIcon = new ImageIcon("open2.jpg");
saveIcon = new ImageIcon("save2.gif");
compileIcon = new ImageIcon("compile.png");
runIcon = new ImageIcon("run.png");
buttonNew = new JButton(newIcon);
buttonNew.setToolTipText("Create a new file");
buttonOpen = new JButton();
buttonOpen.setToolTipText("Open another file");
buttonOpen.setIcon(openIcon);
buttonSave = new JButton(saveIcon);
buttonSave.setToolTipText("Save the source code");
//creating the button for compilation and running
buttonCompile = new JButton(compileIcon);
buttonCompile.setToolTipText("Compile the source code");
buttonRun = new JButton(runIcon);
buttonRun.setToolTipText("Run the compiled code");
//also add the button for compilation and running
togglePanel .add(buttonNew );
togglePanel .add(buttonOpen );
togglePanel .add(buttonSave );
//for dimension constructor first is width
togglePanel .addSeparator(new Dimension(40,10));
togglePanel .add(buttonCompile);
togglePanel .add(buttonRun );
c.add(togglePanel ,BorderLayout.NORTH);
//_________________________________________________
//ADDING ACTION LISTENERS FOR THE 2 BUTTONS
//--------------------------------------------------------------------
MyButtonListener butLisn =new MyButtonListener ();
/*we use the menu listner object mm1 here, because that class
contains
*actions for new , save, and open , GOt it ?*/
buttonNew .addActionListener(mm1 );
buttonOpen .addActionListener(mm1 );
buttonSave .addActionListener(mm1 );
buttonCompile .addActionListener(butLisn );
buttonRun .addActionListener(butLisn );
JScrollPane myScrollPane = new JScrollPane(tAreaEditor );
c.add( myScrollPane);
}
//**************************************************
private class Mypoplistener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{if(event.getSource() ==itemCo)
{ tAreaEditor.copy();
}
if(event.getSource() ==itemCu)
{ tAreaEditor.cut();
}
if(event.getSource() ==itemPa)
{ tAreaEditor.paste();
}
}
}
//***************************************************
//*********************************************************
//*********************************************************
//LISTENERS FOR COMPILATION AND RUNNING
private class MyButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonCompile )
{
Process pCompile = null;
Runtime rCompile = Runtime.getRuntime();
Runtime rt = Runtime.getRuntime();
//r.traceInstructions(true);
try
{
Process proc = rt.exec("javac
"+OpenOrSaveFilePath);
InputStream stderr =
proc.getErrorStream();
InputStreamReader isr = new
InputStreamReader(stderr);
BufferedReader br = new
BufferedReader(isr);
String line = null;
String fullList = "";
while ( (line = br.readLine()) !=
null)
fullList = fullList +
line+"\n";
int exitVal = proc.waitFor();
System.out.println("Process
exitValue: " + exitVal);
if( fullList != null)
JOptionPane.showMessageDialog(null,
fullList, "Compile errors",JOptionPane.ERROR_MESSAGE ) ;
System.out.println(fullList);
//pCompile = rCompile.exec("javac
"+OpenOrSaveFilePath );
}
catch(Exception ex)
{
}
}//end of if condition
if(e.getSource() == buttonRun )
{
Process pRun = null;
Runtime rRun = Runtime.getRuntime();
try
{
System.out.println("trying to run the
program");
//OpenOrSaveFilePath contains the word
".java" --> so
//we have to remove that and it is
stored in runPath
String runName =
nameOfTheFile.substring(0,nameOfTheFile.length() - 5);
System.out.println(runName);
pRun = rRun .exec("java -classpath "+
runPathforJava +" "+runName );
}
catch(Exception ex)
{
}
}//end of if condition
} //end of actionPerformed
} // end of inner class
//******************************************************************************
class MousePopupListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
showPopup(e);
}
public void mouseClicked(MouseEvent e)
{
showPopup(e);
}
public void mouseReleased(MouseEvent e)
{
showPopup(e);
}
private void showPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
//******************************************************************************
private class MyMenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == itmNew || e.getSource() ==
buttonNew )
tAreaEditor.setText("");
if( e.getSource() == itmOpen || e.getSource() ==
buttonOpen )
{
FileInputStream readFile;
int returnval = mySaveFile .showOpenDialog(c) ;
File f =mySaveFile .getSelectedFile() ;
String openFilePath = f.getPath() ;
OpenOrSaveFilePath = openFilePath;
nameOfTheFile = f.getName();
runPathforJava = f.getParent() ;
System.out.println("The path of the file is: "+
runPathforJava );
try
{
readFile = new FileInputStream( openFilePath );
byte data[] = new byte[readFile.available()];
readFile.read(data);
String byteText = new String(data);
tAreaEditor .setText(byteText);
}
catch(Exception excp)
{
System.out.println("get the hell out of
here");
}
}
if( e.getSource() == itmSave || e.getSource()
==buttonSave)
{
FileOutputStream outputfile;
int returnval = mySaveFile .showSaveDialog(c) ;
File f =mySaveFile .getSelectedFile() ;
String savedFilePath = f.getPath() ;
OpenOrSaveFilePath = savedFilePath;
nameOfTheFile = f.getName();
runPathforJava = f.getParent() ;
System.out.println("The path of the file is:
"+ savedFilePath );
try {
outputfile = new FileOutputStream(
savedFilePath );
//
byte data[] =tAreaEditor
.getText().getBytes();
outputfile.write(data);
}
catch(Exception excp)
{
System.out.println("get the hell out of
here");
}
}
if( e.getSource() == itmCut )
tAreaEditor.cut();
if( e.getSource() == itmCopy )
tAreaEditor.copy();
if( e.getSource() == itmPaste )
tAreaEditor.paste();
if( e.getSource() == itmAbout )
JOptionPane.showMessageDialog(null, "name:
Nandagopal"+
"\n\nLicense:GNU GPL"+
"\n\ne-mail: nanda..@gmail.com");
}
}
}
class MyJCreatorTest
{
public static void main(String args[])
{
MyJCreator m1 = new MyJCreator ();
m1.setSize(640,450);
m1.setVisible(true);
m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}