//Import JAVA class package
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class EditFile
{
//Default constructor
EditFile(){}
//Save file function
void SaveFile(String name,String str)
{
try
{
//Declare file output object
FileOutputStream fos = new FileOutputStream(name);
//Output save file
fos.write(str.getBytes());
}
catch (IOException e)
{
//An error occurs and print error message
System.out.println(e.toString());
}
}
//Open file function
String OpenFile(String name)
{
//Define the string str to save the file content
String str=null;
//define counter
int ch=0;
try
{
//Declare the file input object
FileInputStream fis = new FileInputStream(name);
//Traverse the input object and store the memory in str
while ((ch=fis.read())!=-1)
{
str=str+(char)ch;
}
//Close the file object
fis.close();
}
catch (IOException e)
{
//An error occurs and print error message
System.out.println(e.toString());
}
//return str
return str;
}
}
class MyMainFrame extends JFrame implements ActionListener
{
//file name
String filename=null;
//Find string
String findtxt=null;
//replace string
String replacetxt=null;
//Find the starting position
int findtxton=-1;
//Find the end bit
int findtxtoff=-1;
//middle panel
Container c=getContentPane();
//menu
JMenuBar mainMb=new JMenuBar();
//menu button
JMenu fileMenu=new JMenu("File");
JMenu editMenu=new JMenu("Edit");
JMenu typeMenu=new JMenu("format");
JMenuItem newItem=new JMenuItem("New");
JMenuItem openItem=new JMenuItem("open");
JMenuItem saveItem=new JMenuItem("Save");
JMenuItem saveasItem=new JMenuItem("Save as");
JMenuItem exitItem=new JMenuItem("Exit");
JMenuItem findItem=new JMenuItem("Find");
JMenuItem findnextItem=new JMenuItem("Find next");
JMenuItem changeItem=new JMenuItem("replace");
JCheckBoxMenuItem changelineItem=new JCheckBoxMenuItem("Automatic line wrap",false);
//Text field
JTextArea txt=new JTextArea();
JTextArea txt2=new JTextArea();
//Scroll bar
JScrollPane jsp;
//File operation object
EditFile ef=new EditFile();
//Constructor
MyMainFrame()
{
//Draw interface
c.setLayout(new BorderLayout());
jsp= new JScrollPane(txt);
jsp.setHorizontalScrollBarPolicy(jsp.HORIZONTAL_SCROLLBAR_AS_NEEDED);
c.add(jsp,BorderLayout.CENTER);
mainMb.add(fileMenu);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveasItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
mainMb.add(editMenu);
editMenu.add(findItem);
editMenu.add(findnextItem);
editMenu.add(changeItem);
mainMb.add(typeMenu);
typeMenu.add(changelineItem);
setJMenuBar(mainMb);
//Add event listening for the menu
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveasItem.addActionListener(this);
exitItem.addActionListener(this);
findItem.addActionListener(this);
findnextItem.addActionListener(this);
changeItem.addActionListener(this);
changelineItem.addActionListener(this);
}
//event handler function
public void actionPerformed(ActionEvent e)
{
//Create a file operation object
EditFile ef=new EditFile();
//If the button is clicked, it is new
if(e.getActionCommand()=="New")
{
//Set the text field to empty
txt.setText(null);
//The file name is empty
filename=null;
}
//If the button is clicked to open
if(e.getActionCommand()=="open")
{
//Define a file form
FileDialog openDialog=new FileDialog(this,"Open file...",FileDialog.LOAD);
//display form
openDialog.show();
//If the returned file is not empty
if(openDialog.getFile()!=null)
{
//Get the file name
filename=openDialog.getFile();
//Get file content
txt.setText(ef.OpenFile(openDialog.getFile()));
}
}
//If the button is clicked to save
if(e.getActionCommand()=="Save")
{
//If there is no file name
if(filename==null)
{
//Define a file form
FileDialog saveDialog=new FileDialog(this,"Save as...",FileDialog.SAVE);
//display form
saveDialog.show();
//If the returned file is not empty
if(saveDialog.getFile()!=null)
{
//Get the file name
filename=saveDialog.getFile();
//save file
ef.SaveFile(filename,txt.getText());
}
}
else
{
//Save the file directly
ef.SaveFile(filename,txt.getText());
}
}
//If you click the button to save as
if(e.getActionCommand()=="Save as")
{
//Define a file form
FileDialog saveDialog=new FileDialog(this,"Save as...",FileDialog.SAVE);
//display form
saveDialog.show();
//If the returned file is not empty
if(saveDialog.getFile()!=null)
{
//Get the file name
filename=saveDialog.getFile();
//save file
ef.SaveFile(filename,txt.getText());
}
}
//If the button is clicked to exit
if(e.getActionCommand()=="Exit")
{
//quit
System.exit(0);
}
//If the button is clicked, it is search
if(e.getActionCommand()=="Find")
{
//Define a dialog box
JOptionPane inputdia=new JOptionPane();
//Define dialog box to get search text
findtxt=inputdia.showInputDialog("Please enter the text you want to find",null);
//Define the actual coordinates
findtxton=-1;
findtxtoff=-1;
//Call the search function
find();
}
//If you click the button to find the next one
if(e.getActionCommand()=="Find next")
{
//Call the search function
find();
}
//If the button is clicked, replace
if(e.getActionCommand()=="replace")
{
//Define a dialog box
JOptionPane inputdia=new JOptionPane();
//Get find and replace text
findtxt=inputdia.showInputDialog("Please enter the text to be replaced",null);
replacetxt=inputdia.showInputDialog("Please enter the replaced text",null);
//Backup content
txt2.setText(replacetxt);
txt2.selectAll();
txt2.copy();
//Call the search function
find();
//if not found
if(findtxton==-1&&findtxtoff==-1)
{
}
//If found
else
{
//replace
txt.paste();
}
findtxton=-1;
findtxtoff=-1;
}
//If the button is clicked, it will automatically wrap
if(e.getActionCommand()=="Automatic line wrap")
{
//Judge current status
if(changelineItem.isSelected()==true)
{
//Set the scroll bar state
jsp.setHorizontalScrollBarPolicy(jsp.HORIZONTAL_SCROLLBAR_NEVER);
//Set whether to wrap or not
txt.setLineWrap(true);
}
else
{
jsp.setHorizontalScrollBarPolicy(jsp.HORIZONTAL_SCROLLBAR_AS_NEEDED);
txt.setLineWrap(false);
}
}
}
//find function
public void find()
{
//Define temporary string
String tempstr;
//Find text if it is not empty
if(findtxt!=null)
{
try
{
//Search
tempstr=txt.getText(findtxton+1,(txt.getText().length()-findtxton-1));
findtxton=findtxton+1+tempstr.indexOf(findtxt);
}
catch(Exception e)
{
}
//If the start bit changes, it is found
if(findtxton!=-1)
{
//Set the end bit
findtxtoff=findtxton+findtxt.length();
}
else
{
//Otherwise it displays not found
findtxtoff=-1;
JOptionPane.showMessageDialog(null,"Not found");
}
//Select the found part
txt.select(findtxton,findtxtoff);
}
}
}
public class TextEdit
{
//Main function
public static void main(String args[])
{
//Declaration to set the main form
MyMainFrame f=new MyMainFrame();
f.setSize(640,480);
f.setTitle("Text Editor");
//Display the main form
f.setVisible(true);
}
}
/*
Problems with automatic line wrapping of JTextArea
-------------------------------------------------- ----------------------------------
If the horizontal scroll bar is set to JSctollpane.
, then the JTextArea with this JSctollpane object added will have the line wrapping function, right?
-------------------------------------------------- --------------------------
JTextArea.setLineWrap(boolean wrap)
------------- -------------------------------------------------- ------------------
JTextArea.setLineWrap(boolean wrap)//General line wrap.
JTextArea.setWrapStyleWorld(boolean world)//Break lines using word edges as boundaries
*/