Sie sind auf Seite 1von 23

import javax.swing.

*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.filechooser.*;

import java.io.*;

class Notepad extends WindowAdapter implements ActionListener

static int tabCount;

static String selectedText;

static String findNext ="";

static int indexOfString=0;

static int k =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 = "";

//----------------------For Find and Replace frame-------------------------

JFrame findReplaceFrame;

JPanel panel;

JPanel panel2;

JPanel panel3;

JLabel label1;

JLabel label2;

JButton findButton;

JButton replaceButton;

JButton replaceAllButton;

JButton cancelButton;

JTextField findText;

JTextField replaceText;

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

Notepad()

{
frame = new JFrame("Notepad");

textArea = new JTextArea();

textArea.setFont(font);

textArea2 = new JTextArea();

textArea2.setEditable(false);

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.setBounds(0,0,800,600);

frame.setVisible(true);

// tabCount = tabbedPane.getTabCount();

// System.out.println (tabCount);
//-------------------------Add Action Listners-------------------------------------------------

frame.addWindowListener(this);

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

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

findReplaceFrame = new JFrame("Find and Replace");

findReplaceFrame.addWindowListener(this);

findReplaceFrame.setLayout(new GridLayout(1,3));

panel = new JPanel();


panel2 = new JPanel();

panel3 = new JPanel();

panel.setLayout(new GridLayout(2,1));

panel2.setLayout(new GridLayout(2,1));

panel3.setLayout(new GridLayout(4,1));

label1 = new JLabel("Find");

label2 = new JLabel("Replace with");

findButton = new JButton("Find Next...");

findButton.addActionListener(this);

replaceButton = new JButton("Replace");

replaceButton.addActionListener(this);

replaceAllButton = new JButton("Replace All");

replaceAllButton.addActionListener(this);

cancelButton = new JButton("Cancel");

cancelButton.addActionListener(this);

findText = new JTextField();

findText.setColumns(50);

// findText.se

replaceText = new JTextField();

replaceText.setColumns(30);

panel.add(label1);

panel.add(label2);
panel2.add(findText);

panel2.add(replaceText);

panel3.add(findButton);

panel3.add(replaceButton);

panel3.add(replaceAllButton);

panel3.add(cancelButton);

findReplaceFrame.add(panel);

findReplaceFrame.add(panel2);

findReplaceFrame.add(panel3);

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

public void actionPerformed(ActionEvent ae)

try

//Methods inherited from class java.util.EventObject -- getSource()

System.out.println ("get source" +ae.getSource().getClass().toString());

String className = ae.getSource().getClass().toString();

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


// System.out.println (className);

JButton jb=null;

JMenuItem mi=null;

if(className.equals("class javax.swing.JMenuItem"))

// System.out.println ("-- --");

mi = (JMenuItem)ae.getSource();

else

jb = (JButton)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();
}

compileSave = false;

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)

System.out.println ("I am in 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)

findReplaceFrame.setVisible(true);

findReplaceFrame.setBounds(300,300,450,150);

else if(jb == findButton)

System.out.println ("i m in findButton ");

String str = findText.getText();

String str2 = jta.getText();

int j = str2.indexOf(str,k);

System.out.println (j);

if(j==-1)

k=0;

j= str2.indexOf(str,k);

jta.setSelectionStart(j);

jta.setSelectionEnd(j+str.length());

jta.setSelectedTextColor(Color.green);

k = j+str.length();

}
else if(jb == replaceButton)

System.out.println ("I m in replacebutton ");

if(jta.getSelectedText()!=null)

System.out.println (";;;;;");

String str = replaceText.getText();

jta.replaceSelection(str);

else if(jb == replaceAllButton)

System.out.println ("i m in replace All button");

String str = jta.getText();

String str2 = replaceText.getText();

String str3 = findText.getText();

int j;

System.out.println (str);

System.out.println (str.indexOf(str3));

while((j=str.indexOf(str3))!=-1)

System.out.println ("i m in while"+str.indexOf(str3));

jta.replaceRange(str2,j,j+str3.length());
str = jta.getText();

else if(jb == cancelButton)

findReplaceFrame.dispose();

else{

if(mi == mCompile)

System.out.println (currentDir);

textArea2.setText("");

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

Runtime runtime = Runtime.getRuntime();

Process process = runtime.exec("javac.exe


"+currentDir+"\\"+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;

String line2 = null;


if((line=output.readLine())!= null)

{ textArea2.append(line+"\n");

while((line=output.readLine())!=null)

textArea2.append(line+"\n");

BufferedReader outputError = new BufferedReader(new


InputStreamReader(process.getErrorStream()));

textArea2.append("\n");

if((line2=outputError.readLine())!= null)

{ textArea2.append(line2+"\n");

while((line2=outputError.readLine())!=null)

textArea2.append(line2+"\n");

} // END of actionPerformed

catch(Exception e)
{

e.printStackTrace();

public void windowActivated(WindowEvent we)

System.out.println("------------------------- Windows Activated------------------");

public void windowOpened(WindowEvent we)

System.out.println("------------------------- Windows Opened------------------");

public void windowClosing(WindowEvent we)

System.out.println("------------------------- Windows Closed------------------");

JFrame jFrame = (JFrame)we.getWindow();

if(jFrame == frame)

findReplaceFrame.dispose();

jFrame.dispose();

jFrame.dispose();

}
public static void main(String[] args)

new Notepad();

Das könnte Ihnen auch gefallen