MyJCreator - Program for java compilation and run, version 1 built 11

2 views
Skip to first unread message

Justin

unread,
Aug 19, 2006, 8:29:55 AM8/19/06
to Open Projects For All
/*This is basically a minor build , any way it solves one of the major
hurdle , namely opening or saving to folders having spaces in between
them */

//Next major high prioty bug is to make command prompt display
//i.e the system.out.println() must display its output in a command
//prompt window

//date aug 19 ,2006


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

class MyJCreator extends JFrame
{

Process p =null; // used for executing javac

JMenuBar topBar ;
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());

//adding the text editor

tAreaEditor = new JTextArea (20,40);
tAreaEditor .setLineWrap(true) ;

//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);


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);

}

//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+"\"");
System.out.println("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

//******************************************************************************
//******************************************************************************

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:
Justin"+
"\n\nLicense:GNU GPL"+
"\n\ne-mail: justin...@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);
}
}

//************************

Reply all
Reply to author
Forward
0 new messages