package Editor;
import java.awt.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.util.EventObject;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.undo.UndoManager;
import javax.swing.JOptionPane;
public class Framework extends JFrame implements ActionListener,DocumentListener{
String wenjian;
//private UndoManager um;
boolean f=false;
int flag=0;
String filename="";
String lujing;
String mingcheng;
JTextArea ta=new JTextArea(3,4);
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("文件");
JMenu edit=new JMenu("编辑");
JMenu geshi=new JMenu("格式");
JMenu chakan=new JMenu("查看");
JMenu help=new JMenu("帮助");
JMenuItem setup=new JMenuItem("新建");
JMenuItem open=new JMenuItem("打开");
JMenuItem save=new JMenuItem("保存"); //此三项添加到文件
JMenuItem lingcun=new JMenuItem("另存为");
JMenuItem exit=new JMenuItem("退出");
//JMenuItem back=new JMenuItem("撤销");
//back.setAccelerator(KeyStroke.getKeyStroke(KeyEvent,VK_Z,ActionEvent,CTRL_MASK));
JMenuItem copy=new JMenuItem("复制(Ctrl+c)");
JMenuItem cut=new JMenuItem("剪切(Ctrl+x)");
JMenuItem paste=new JMenuItem("粘贴(Ctrl+v)"); //此三项添加到编辑
JMenuItem ziti=new JMenuItem("字体"); //添加到格式
JMenuItem bangzhu=new JMenuItem("查看帮助"); //添加到帮助
JMenuItem zhuangtai=new JMenuItem("状态栏"); //添加到查看
JMenuItem tihuan=new JMenuItem("替换");
JMenuItem chazhao=new JMenuItem("查找");
private FileDialog filedialog;
public Framework()
{
add("Center",new JScrollPane(ta));
file.add(setup);
file.add(open);
file.add(save);
file.add(lingcun);
file.add(exit);
//edit.add(back);
edit.add(copy);
edit.add(cut);
edit.add(paste);
geshi.add(ziti);
chakan.add(zhuangtai);
chakan.add(tihuan);
chakan.add(chazhao);
help.add(bangzhu);
bar.add(file);
bar.add(edit);
bar.add(chakan);
bar.add(geshi);
bar.add(help);
this.setJMenuBar(bar);
setTitle("文本编辑器");
setSize(400,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(400,100);//设置窗口位置
setup.addActionListener(this);
exit.addActionListener(this);
open.addActionListener(this);
(ta.getDocument()).addDocumentListener(this);
ziti.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
FontDialog z=new FontDialog(ta);
z.setVisible(true);
}
});
tihuan.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
change g=new change(ta);
g.setVisible(true);
}
});
chazhao.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Finding f=new Finding(ta);
f.setVisible(true);
}
});
lingcun.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==lingcun)
saveas();
}
});
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==save)
if(f==false)
save();
else
add_save();
}
});
copy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.copy();
}
});
paste.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.paste();
}
});
cut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.cut();
}
});
/* back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(um.canUndo())
um.undo();
//ta.undo();
}
});*/
}
void open()
{
FileDialog filedialog=new FileDialog(this,"打开",FileDialog.LOAD);
filedialog.setVisible(true);
if(filedialog.getDirectory()!=null && filedialog.getFile()!=null){
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(filedialog.getDirectory()+filedialog.getFile())));
String temp="";
wenjian=filedialog.getDirectory()+filedialog.getFile();
String now ;
try {
while((now=(String)in.readLine())!=null){
temp=temp+now+"\n";
}
in.close();
String s=filedialog.getFile();
setTitle(s);
} catch (IOException e1) {
e1.printStackTrace();
}
ta.setText(temp);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}}
}
void save(){
try{
FileDialog filedialog=new FileDialog(this,"保存",FileDialog.SAVE);
if(filename=="")
{
filedialog.setVisible(true);
filename=filedialog.getDirectory()+filedialog.getFile();
lujing=filedialog.getDirectory();
mingcheng=filedialog.getFile();
}
if(lujing!=null && mingcheng!=null){
OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream(filename));
out.write(ta.getText());
out.close();
f=true;
//String s=filedialog.getFile();
setTitle(mingcheng);
}
}catch(Exception e){
System.out.println("Write error:"+e);
JOptionPane.showMessageDialog(this,"写入错误","警告",JOptionPane.WARNING_MESSAGE);
}
}
void add_save()//如果保存过的话,直接进行保存
{
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(ta.getText());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
f=true;
}
void saveas()
{
try{
FileDialog filedialog=new FileDialog(this,"保存",FileDialog.SAVE);
filedialog.setVisible(true);
filename=filedialog.getDirectory()+filedialog.getFile();
if(filedialog.getDirectory()!=null && filedialog.getFile()!=null){
OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream(filedialog.getDirectory()+filedialog.getFile()));
out.write(ta.getText());
out.close();
String s=filedialog.getFile();
setTitle(s);
}
}catch(Exception e){
System.out.println("Write error:"+e);
JOptionPane.showMessageDialog(this,"写入错误","警告",JOptionPane.WARNING_MESSAGE);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==setup)
{
if(f==true)
ta.setText(null);
else {
int m=JOptionPane.showConfirmDialog(this,"是否保存?","提示",JOptionPane.YES_NO_OPTION);
if (m==0){
save();
ta.setText(null);
}
if(m==1)
{
ta.setText(null);
}
}
setTitle("记事本");
filename="";
}
if(e.getSource()==exit)
{
if(f==true)
System.exit(0);
else
{
int n=JOptionPane.showConfirmDialog(this,"Y/N","提示",JOptionPane.YES_NO_OPTION);
if(n==0) {
save();
System.exit(0);
}
if(n==1) {
System.exit(0);
}
}
}
if(e.getSource()==open)
{
if(f==true)
{
open();
}
else {
int a=JOptionPane.showConfirmDialog(this,"是否保存?","提示",JOptionPane.YES_NO_OPTION);
if (a==0){
save();
open();
}
if(a==1)
{
open();
}
}
}
filename="";
}
public void changedUpdate(DocumentEvent e) { //检测到文件得改动后。f为false,需要继续保存
f=false;
}
public void insertUpdate(DocumentEvent e) {
f=false;
}
public void removeUpdate(DocumentEvent e) {
f=false;
}
}