package Jeditor;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.text.SimpleDateFormat;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.Document;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class Jeditor {
private JFrame f, f_font, f_close;
private JMenuBar mb;
private JPopupMenu rightmenu;
private JMenu mf, ms, help, sub_newcreate, tool;
private JMenuItem undoIt,redoIt,copyIt, pasteIt, selectAllIt, cutIt, clearIt,
searchandreplace,countIt;
private JMenuItem closeItem, subIt_c, subIt_java, subIt_perl, subIt_html,
subIt_standard, about, font, chara_color, back_color, open, save,
saveas;
private Container con, container;
private JToolBar toolBar;
private JPanel pcom_lab, pcom, picta, pclose, pclosebut;
private JTextArea ta;
private TextArea tabout;
private JTextField jf1, jf2;
private JFileChooser opendia, savedia;
private JDialog dsear_rep, fabout,f_count;
private File file;
private JCheckBox capscheck;
private int[] filenum = new int[] { 1, 1, 1, 1, 1 };
private JLabel labtest, labtime, lablincol, labclose, searchcontent,
replaceas, labcodetype,labcount;
private int num = 1, line = 1, col = 1, start, end, fostyle = Font.PLAIN,
fosize = 16, position, startposition, endposition, totalindex,
startindex,Numcount,CNcount,Characount,spacecount,Sumcount,othercount,huanhangcount;
private JComboBox<String> jcbfont, jcbsize, jcbstyle;
private JButton okbut, canbut, abbut, closeyesbut, closenobut, closecanbut,
searnextbut, replacenextbut, replaceallbut, sear_repcanbut,
searpreviousbut,okcount;
private String fontFont[] = { "SERIF", "SANS_SERIF", "MONOSPACED", "DIALOG",
"DIALOG_INPUT" };
private String fontStyle[] = { "PLAIN", "BOLD", "ITALIC", "BOLD+ITALIC" };
private String fontSize[] = { "10", "12", "14", "16", "18", "20", "22",
"24", "26", "48", "72" };
private String filetype = "txt", tempstr, fofont = "Serif", styletemp,
filePath = "D:\\Documents\\Desktop\\新建文档(" + num + ").txt";
private String strabout, jf1str, jf2str, substr, template, codetype;
private Font tafont, labfont,aboutfont;
private ImageIcon imgcon, closewarcon;
private Image imglogo;
private SimpleDateFormat sdf;
private Clipboard clipboard;
private StringSelection strsect;
private StringBuffer sufr;
private boolean isinit = true, isstart = true,issave=false;
private BufferedInputStream bin;
private UndoManager umg;
private Document doc;
public Jeditor() {
init();
}
public void init() {
f = new JFrame(filePath+" - Jeditor");//实例化记事本窗体,并设置标题
imglogo = Toolkit.getDefaultToolkit().getImage(
Jeditor.class.getResource("/pic/bridge.jpg"));//实例化Image对象
f.setIconImage(imglogo); //设置标题前logo
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//设置窗体关闭功能不做任何处理(后面关掉时要提醒是否 保存)
f.setBounds(300, 100, 500, 600);
// 设置菜单
mb = new JMenuBar();
mf = new JMenu("文件");
sub_newcreate = new JMenu("新建");
subIt_standard = new JMenuItem("标准文本");
subIt_c = new JMenuItem("c++");
subIt_java = new JMenuItem("java");
subIt_html = new JMenuItem("html");
subIt_perl = new JMenuItem("perl");
sub_newcreate.add(subIt_standard);
sub_newcreate.add(subIt_c);
sub_newcreate.add(subIt_java);
sub_newcreate.add(subIt_html);
sub_newcreate.add(subIt_perl);
open = new JMenuItem("打开");
save = new JMenuItem("保存");
saveas = new JMenuItem("另存为");
closeItem = new JMenuItem("退出");
mf.add(sub_newcreate);
mf.add(open);
mf.add(save);
mf.add(saveas);
mf.add(closeItem);
// 设置菜单项
ms = new JMenu("设置");
font = new JMenuItem("文字字体");
chara_color = new JMenuItem("文字颜色");
back_color = new JMenuItem("背景颜色");
ms.add(font);
ms.add(chara_color);
ms.add(back_color);
// 工具菜单项
tool = new JMenu("工具");
searchandreplace = new JMenuItem("查找&替换");
countIt=new JMenuItem("字数统计");
tool.add(searchandreplace);
tool.add(countIt);
help = new JMenu("帮助");
about = new JMenuItem("关于");
help.add(about);
mb.add(mf);
mb.add(ms);
mb.add(tool);
mb.add(help);
// 打开和保存触发的文件对话框
opendia = new JFileChooser();
opendia.setDialogTitle("打开文件");
savedia = new JFileChooser();
savedia.setDialogTitle("另存为");
opendia.setFileSelectionMode(JFileChooser.OPEN_DIALOG);//open类型
// savedia.setFileSelectionMode(JFileChooser.SAVE_DIALOG);//!!!!!这里不能显示,否则不能给文件默认初始化命名(对应相应的文件类型)
// 设置文字设置选择框
f_font = new JFrame("文字设置");
f_font.setIconImage(imglogo);
pcom_lab = new JPanel(new GridLayout(2, 1));//设置为两行一列
pcom = new JPanel();
jcbfont = new JComboBox<String>(fontFont);//实例化下拉列表框(字体)
jcbfont.setBorder(BorderFactory.createTitledBorder("字体"));//设置标题边框
jcbstyle = new JComboBox<String>(fontStyle);//实例化下拉列表框(粗细)
jcbstyle.setBorder(BorderFactory.createTitledBorder("粗细"));
jcbsize = new JComboBox<String>(fontSize);//实例化下拉列表框(大小)
jcbsize.setBorder(BorderFactory.createTitledBorder("大小"));
labtest = new JLabel("示例:ABCabcxyzXYZ", JLabel.CENTER);//示例标签
pcom.add(jcbfont);
pcom.add(jcbstyle);
pcom.add(jcbsize);
pcom_lab.add(pcom);
pcom_lab.add(labtest);
okbut = new JButton("确定");
canbut = new JButton("取消");
f_font.setLayout(new FlowLayout());//流式布局
f_font.add(pcom_lab);
f_font.add(okbut);
f_font.add(canbut);
f_font.setBounds(200, 300, 360, 300);
f_font.setResizable(false);//不可缩放
f_font.setVisible(false);//默认不可见
// 查找框(界面设置比较繁琐)
dsear_rep = new JDialog(f,"查找&替换");//示例化查找框(基于主窗体)
dsear_rep.setBounds(450, 450, 500, 212);
container = dsear_rep.getContentPane();//获取容器对象
container.setLayout(null);
searchcontent = new JLabel("查找内容:");
searchcontent.setBounds(26, 27, 71, 24);
container.add(searchcontent);
replaceas = new JLabel("替换为:");
replaceas.setBounds(26, 70, 71, 24);
container.add(replaceas);
jf1 = new JTextField();
jf1.setBounds(123, 24, 196, 30);
container.add(jf1);
jf1.setColumns(10);
jf2 = new JTextField();
jf2.setBounds(123, 70, 196, 30);
dsear_rep.getContentPane().add(jf2);
jf2.setColumns(10);
searpreviousbut = new JButton("查找上一个");
searpreviousbut.setBounds(353, 10, 103, 23);
container.add(searpreviousbut);
searnextbut = new JButton("查找下一个");
searnextbut.setBounds(353, 38, 103, 23);
container.add(searnextbut);
replacenextbut = new JButton("替换");
replacenextbut.setBounds(353, 70, 103, 23);
container.add(replacenextbut);
replaceallbut = new JButton("替换全部");
replaceallbut.setBounds(353, 98, 103, 23);
container.add(replaceallbut);
capscheck = new JCheckBox("区分大小写");
capscheck.setBounds(94, 127, 124, 30);
container.add(capscheck);
sear_repcanbut = new JButton("取消");