You are on page 1of 10

import javax.swing.

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

class Notepad implements ActionListener


{
static int tabCount;
static String selectedText;
static String findNext ="";
static int indexOfString=0;
boolean compileSave = false;
JFrame frame;
JPanel panel;
JMenuBar menubar;

JMenu mFile,mEdit,mBuild;
JPopupMenu pop1;
JMenuItem mNew,mOpen,mSave,mSaveAs,mExit;
JMenuItem mCut,mCopy,mPaste,mFind,mFindNext,mFindReplace;
JMenuItem mCompile,mRun;
JTextArea textArea,textArea2;
JSplitPane splitPane;
JScrollPane scrollPane1,scrollPane2;
JTabbedPane tabbedPane;

ImageIcon icon;
Font font = new Font("ARIAL",Font.PLAIN,20);
static String currentDir = "";

Notepad()
{
frame = new JFrame("Notepad");

textArea = new JTextArea();


textArea.setFont(font);
textArea2 = new JTextArea();

menubar = new JMenuBar();


tabbedPane = new JTabbedPane(JTabbedPane.TOP); // Default is also top

tabbedPane.addTab("Untitled - File",new JScrollPane(textArea)); // Add text


Area into JscrollPane and then add this JscrollPane into TAB

scrollPane1 = new JScrollPane(textArea2); // Add textArea2 into


second ScrollPane
splitPane = new
JSplitPane(JSplitPane.VERTICAL_SPLIT,tabbedPane,scrollPane1);

splitPane.setDividerLocation(500); // Divide the


vertical pane
splitPane.setOneTouchExpandable(true);

frame.add(menubar);
frame.setJMenuBar(menubar); // SET
the MENU BAR at TOP

mFile = new JMenu("File");


mEdit = new JMenu("Edit"); //
Menus
mBuild = new JMenu("Build");

mNew = new JMenuItem("New");


System.out.println (mNew);
mOpen = new JMenuItem("Open");
mSave = new JMenuItem("Save"); //
Menu Items For FIle
mSaveAs = new JMenuItem("Save As");
mExit = new JMenuItem("Exit");

mCut = new JMenuItem("Cut");


mCopy = new JMenuItem("Copy");
mPaste = new JMenuItem("Paste"); // Menu Items
For Edit
mFind = new JMenuItem("Find");
mFindNext = new JMenuItem("Find Next");
mFindReplace = new JMenuItem("Find & Replace");

mCompile = new JMenuItem("Compile");


mRun = new JMenuItem("Run");
// Menu Items For BUILD
mFile.add(mNew);
mFile.add(mOpen);
mFile.add(mSave); // ADD menus
items(of FILE) to MENU
mFile.add(mSaveAs);
mFile.add(mExit);

mEdit.add(mCut);
mEdit.add(mCopy);
mEdit.add(mPaste); // ADD menus items(of
EDIT) to MENU
mEdit.add(mFind);
mEdit.add(mFindNext);
mEdit.add(mFindReplace);

mBuild.add(mCompile);
mBuild.add(mRun); // ADD menus items(of
BUILD) to MENU

menubar.add(mFile);
menubar.add(mEdit); // ADD Menus to "MENU BAR"
menubar.add(mBuild);

frame.add(splitPane); // ADD SPLIT PANE


to FRAME

frame.setVisible(true);

// tabCount = tabbedPane.getTabCount();
// System.out.println (tabCount);

//-------------------------Add Action
Listners-------------------------------------------------

mNew.addActionListener(this);
mOpen.addActionListener(this);
mSave.addActionListener(this);
mSaveAs.addActionListener(this);
mExit.addActionListener(this);

mCut.addActionListener(this);
mCopy.addActionListener(this);
mPaste.addActionListener(this);
mFind.addActionListener(this);
mFindNext.addActionListener(this);
mFindReplace.addActionListener(this);

mRun.addActionListener(this);
mCompile.addActionListener(this);

//--------------------------------------------------------------------------------------------

public void actionPerformed(ActionEvent ae)


{
try
{

//Methods inherited from class java.util.EventObject --


getSource()
System.out.println ("get source" +ae.getSource());
System.out.println (" get ID" +ae.getID());

JMenuItem mi = (JMenuItem)ae.getSource();
JFileChooser fileChooser = new
JFileChooser("c:\\Users\\ANKIT\\Desktop\\ankit");

tabCount = tabbedPane.getTabCount();

//---------------Here getting the view port and current


view----------------------
int i = tabbedPane.getSelectedIndex();
JScrollPane jsp =
(JScrollPane)tabbedPane.getComponentAt(i);
JViewport jvp = jsp.getViewport();
JTextArea jta = (JTextArea)jvp.getView();
//----------------------------------------------------------------------
---------

if(mi == mCompile) // check if code is save or not


compileSave = true;

if(mi == mNew)
{
JTextArea jta2 = new JTextArea();
jta.setFont(font);

tabCount = tabbedPane.getTabCount();
System.out.println (tabCount);

tabbedPane.addTab("Untitled - File",new
JScrollPane(jta2));
tabbedPane.setSelectedIndex(tabCount);
}

else if(mi == mOpen)


{
tabCount = tabbedPane.getTabCount();

System.out.println (Notepad.this);
int s = fileChooser.showOpenDialog(null);
System.out.println (JFileChooser.APPROVE_OPTION);
System.out.println (s);
if (s == JFileChooser.APPROVE_OPTION)
{

File file = fileChooser.getSelectedFile();


Reader r = new BufferedReader(new FileReader(file));
char ch[] = new char[(int)file.length()];
System.out.println ("length of characters is "+ch.length);
r.read(ch,0,ch.length);
String str = new String(ch);

tabCount = tabbedPane.getTabCount();
System.out.println (tabCount);

JTextArea jta2 = new JTextArea();


currentDir = ""+
fileChooser.getCurrentDirectory();

tabbedPane.addTab(fileChooser.getName(file),new JScrollPane(jta2));
jta2.setText(str);
jta2.setFont(font);
//jta.getActions();
tabbedPane.setSelectedIndex(tabCount);
//Set FOCUS to a tab in a JTabbedPane
r.close();

}
}

else if(mi == mSave || mi == mSaveAs || compileSave == true)


{

String tabTitle =
tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
if((!(tabTitle.equals("Untitled - File")) && !(mi ==
mSaveAs)) || compileSave == true)
{
System.out.println ("I am here");

char str[] = jta.getText().toCharArray(); // convert


string to char array
FileOutputStream fos = new
FileOutputStream(currentDir+"\\"+tabTitle);
System.out.println (str);
byte b[] = new byte[str.length];
System.out.println (str.length);
for(int j=0;j<b.length;j++)
{

b[j] = (byte)str[j];
System.out.print((char)b[j]);

}
fos.write(b);
fos.flush();
fos.close();

}
else
{

JFileChooser saveFileChooser = new


JFileChooser();
int s =
saveFileChooser.showSaveDialog(frame);
System.out.println (s);
if(s == JFileChooser.APPROVE_OPTION)
{
System.out.println (jta.getText());

File file =
saveFileChooser.getSelectedFile();
Writer wr = new
FileWriter(saveFileChooser.getCurrentDirectory()+"\\"+file.getName());
currentDir =
""+saveFileChooser.getCurrentDirectory();
String str = jta.getText();
wr.write(str);
wr.flush(); // flush the stream

//JTabbedPane tbPane =
(JTabbedPane)tabbedPane.getTabComponentAt(i);

tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(),file.getName());
wr.close();

}
}
}

else if(mi == mExit)


{
int j = JOptionPane.showConfirmDialog(null,"Are you sure
want to exit","",JOptionPane.YES_NO_OPTION);
System.out.println (j);
if(j==0)
frame.dispose();
}

else if(mi == mCopy)


{

selectedText = jta.getSelectedText();
System.out.println (jta.getSelectedText());

else if(mi == mCut)


{
selectedText = jta.getSelectedText();

jta.replaceRange("",jta.getSelectionStart(),jta.getSelectionEnd());
}
else if(mi == mPaste)
{
jta.replaceSelection(selectedText);
}

else if(mi == mFind)


{
JOptionPane optionPane = new JOptionPane();
String str = optionPane.showInputDialog(frame,"Find
What","FIND",optionPane.OK_CANCEL_OPTION);
System.out.println (str);
if(str!=null)
{
findNext = str;
String str2 = jta.getText();
int j = str2.indexOf(str);
indexOfString = j+findNext.length();
System.out.println (j);
jta.setSelectionStart(j);
jta.setSelectionEnd(j+str.length());
jta.setSelectedTextColor(Color.green);
}
}

else if(mi == mFindNext)


{

if(findNext.length()!=0)
{

String str2 = jta.getText();

int j = str2.indexOf(findNext,indexOfString);
if(j==-1)
{
indexOfString = 0;
j = str2.indexOf(findNext,indexOfString);

System.out.println ("index of find next


string is ------ "+j);
jta.setSelectionStart(j);
jta.setSelectionEnd(j+findNext.length());
jta.setSelectedTextColor(Color.green);
indexOfString = j+findNext.length();

}
}

else if(mi == mFindReplace)


{
JOptionPane optionPane = new JOptionPane();
optionPane.showInputDialog("ss");
//dialog.show();
}

if(mi == mCompile)
{
textArea2.setText("");
System.out.println ("I am in compile");
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("javac.exe
"+tabbedPane.getTitleAt(i));
BufferedReader input = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
String line = null;

if((line=input.readLine())!= null)
{ textArea2.append(line+"\n");
while((line=input.readLine())!=null)
{
textArea2.append(line+"\n");
}
}
else
{
textArea2.setText("Process Successfully
Completed");
}

if(mi == mRun)
{

System.out.println ("I am in RUN");


int dot = tabbedPane.getTitleAt(i).indexOf(".");
String str = tabbedPane.getTitleAt(i).substring(0,dot);
System.out.println (str);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java "+str);
BufferedReader output = new BufferedReader(new
InputStreamReader(process.getInputStream()));
String line = null;

if((line=output.readLine())!= null)
{ textArea2.append(line+"\n");
while((line=output.readLine())!=null)
{
textArea2.append(line+"\n");
}
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Notepad();
}
}

You might also like