package book.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//Download by http://www.codefans.net
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* 一个简单的文本编辑器,能够编辑html、java、cpp、txt文件,
* 并支持java的编译与运行
*/
public class SimpleEditor extends JFrame {
/********菜单栏、菜单和菜单项*********/
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu optionMenu = new JMenu("Option");
JMenu advanceMenu = new JMenu("Advance");
JMenu helpMenu = new JMenu("Help");
JMenu editMenu = new JMenu("Edit");
JMenuItem newMenuItem = new JMenuItem("new");
JMenuItem openMenuItem = new JMenuItem("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem exitMenuItem = new JMenuItem("exit");
JMenuItem findMenuItem = new JMenuItem("Find");
JMenuItem findNextMenuItem = new JMenuItem("Find Next");
JMenuItem replaceMenuItem = new JMenuItem("Replace");
JMenuItem compileMenuItem = new JMenuItem("Compile");
JMenuItem buildMenuItem = new JMenuItem("Build");
JMenuItem stopFlashMenuItem = new JMenuItem("Stop Flash");
JMenuItem startFlashMenuItem = new JMenuItem("Start Flash");
JMenuItem helpMenuItem = new JMenuItem("Help");
JMenuItem copyMenuItem = new JMenuItem("Copy");
JMenuItem cutMenuItem = new JMenuItem("Cut");
JMenuItem pasteMenuItem = new JMenuItem("Paste");
/********文件内容的显示****/
//用多个文本域存放多个文件内容,文本域放在JScrollPane里
//而JScrollPane放在JTabbedPane中,这就是一个多页面的布局
//多个文本域,每个文本域显示一个文件的内容
JTextArea[] fileTextAreas = new JTextArea[10];
//编译或者运行时的控制台信息
JTextArea consoleTextArea = new JTextArea();
JScrollPane[] fileScrollPanes = new JScrollPane[10];
JScrollPane consoleScrollPane;
//多个文本域放在不同tab里
JTabbedPane fileTabbedPane = new JTabbedPane();
/**********工具栏以及工具栏上的按钮**********/
JToolBar toolBar = new JToolBar();
JButton openButton = new JButton(new ImageIcon(loadImage("image/open.gif")));
JButton newButton = new JButton(new ImageIcon(loadImage("image/new.gif")));
JButton saveButton = new JButton(new ImageIcon(loadImage("image/save.gif")));
JButton helpButton = new JButton(new ImageIcon(loadImage("image/help.gif")));
JButton exitButton = new JButton(new ImageIcon(loadImage("image/close.gif")));
JButton compileButton = new JButton(new ImageIcon(loadImage("image/compile.gif")));
JButton buildButton = new JButton(new ImageIcon(loadImage("image/build.gif")));
JButton copyButton = new JButton(new ImageIcon(loadImage("image/copy.gif")));
JButton cutButton = new JButton(new ImageIcon(loadImage("image/cut.gif")));
JButton pasteButton = new JButton(new ImageIcon(loadImage("image/paste.gif")));
//该文本域显示当前光标在当前文本域中的行号
JTextArea showLineNoTextArea = new JTextArea();
//对话框窗体,程序中所有对话框都显示在该窗体中
JFrame dialogFrame = new JFrame();
/*****文件名以树型结构在左边显示*******/
JTree tree;
DefaultMutableTreeNode root;
DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[10];
/*******组件之间的分隔栏******/
JSplitPane leftRightSplitPane;
JSplitPane lineNoConsoleSplitPane;
JSplitPane treeFlashSplitPane;
JSplitPane tabbedLineNoSplitPane;
/**********文件选择、存储相关********/
//文件过滤器
Filter fileFilter = new Filter();
//文件选择器
FileChooser fileChooser = new FileChooser();
// 文件读写控制,0表示文件选择器读文件,1文件选择器标示写文件
int fileChooser_control = 0;
FileWriter fileWriter;
// tabbedPane中tab页的当前数量
int tb = 1;
int find_control = 0;
//文本域的控制器,指向当前操作的文本域
int textAreas_control = 0;
//当前文本域中的文本
String currentTextInTextArea;
//标志文件是否为新建的,如果是新建的文件,为true
boolean[] newFileFlags = new boolean[10];
//存放打开文件所在的目录
String[] directory = new String[10];
/********查找替换相关****/
//正在查找的字符串及其长度
String findWord;
int fingWordLength;
//保存正在查找的字符串在文本域中的文本的位置
int findIndex;
//被替换的文本的长度
int replaceLength = 0;
/********用于显示Flash的控制器****/
JLabel flashLabel = new JLabel(new ImageIcon(loadImage("image/Juggler0.gif")));
Timer timer = new Timer(100, new Act_timer());
int timerControl = 0;
/********帮助相关****/
Font font = new Font("Courier", Font.TRUETYPE_FONT, 14);
JTextArea helpTextArea = new JTextArea();
JFrame helpFrame = new JFrame("Help");
//构造函数
public SimpleEditor() {
super("文本编辑器");
//为窗体添加键盘事件处理器
//下面这一行非常重要,表示窗体能够接受焦点。
//如果没有这一句,按键盘会无效。
this.setFocusable(true);
this.addKeyListener(new MyKeyListener());
//为窗体添加窗口事件处理器
this.addWindowListener(new WindowListener());
//初始化
init();
setLocation(200, 200);
setVisible(true);
pack();
//初始时启动动画
timer.start();
}
private void init(){
/*******初始化文本域、目录、树节点*********/
for (int i = 0; i < 10; i++) {
nodes[i] = new DefaultMutableTreeNode("File" + (i + 1));
newFileFlags[i] = true;
fileTextAreas[i] = new JTextArea();
// 设置文本域中文本的字体
fileTextAreas[i].setFont(font);
// 为文本域的插入光标设置颜色
fileTextAreas[i].setCaretColor(Color.yellow);
// 设置文本域的背景和前景颜色
fileTextAreas[i].setBackground(new Color(70, 80, 91));
fileTextAreas[i].setForeground(Color.pink);
// 为文本域插入光标设置事件处理器
fileTextAreas[i].addCaretListener(new CaretLis_lineNo());
// 为文本域键盘设置事件处理器
fileTextAreas[i].addKeyListener(new MyKeyListener());
directory[i] = new String("/");
fileScrollPanes[i] = new JScrollPane(fileTextAreas[i],
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
root = new DefaultMutableTreeNode("正在编辑的文件");
root.add(nodes[0]);
tree = new JTree(root);
tree.setEditable(false);
tree.setForeground(new Color(200, 150, 10));
tree.setBackground(new Color(70, 80, 91));
//初始化控制台文本域
consoleTextArea.setFont(font);
consoleScrollPane = new JScrollPane(consoleTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
consoleTextArea.setForeground(new Color(200, 150, 10));
consoleTextArea.setBackground(new Color(70, 80, 91));
consoleTextArea.addMouseListener(new MouseListener_console());
//初始化显示当前光标行数的文本域
showLineNoTextArea.setEnabled(false);
showLineNoTextArea.setFont(font);
showLineNoTextArea.setBackground(new Color(70, 80,