📄 analyseframe.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class AnalyseFrame extends JFrame implements DocumentListener {
private AccidenceAnalyser aa ;
//保留字表文件
String reserveFileName;
//类型种别码表文件
String classFileName;
/*parameters initialization*/
//the following variables are used in the GUI
private JPanel jContentPane = null;
private JToolBar toolBar = null;
private JMenuBar menuBar = null;
private JMenu fileMenu = null;
private JMenu analyseMenu = null;
private JMenu aboutMenu = null;
private Action openAction = null;
private Action saveAction = null;
private Action newAction = null;
private Action aboutAction = null;
private Action analyseAction = null;
private Action exitAction = null;
private JScrollPane inScrollPane = null;
private JTextArea inTextArea = null;
private JScrollPane outScrollPane = null;
private JTextArea outTextArea = null;
private File sourFile = null;//source file you want to compile
private JSplitPane splitPane = null;
private Document indocument;
private boolean edited = false;//judge whether the file is edited or not
/**
* This method initializes toolBar
*
* @return javax.swing.JToolBar
*/
private JToolBar getToolBar() {
if (toolBar == null) {
toolBar = new JToolBar();
toolBar.add(getOpenAction());
toolBar.add(getSaveAction());
toolBar.add(getNewAction());
toolBar.add(getAnalyseAction());
toolBar.add(getAboutAction());
toolBar.add(getExitAction());
}
return toolBar;
}
/**
* This method initializes menuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getmenuBar() {
if (menuBar == null) {
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
fileMenu.add(getNewAction());
fileMenu.add(getOpenAction());
fileMenu.add(getSaveAction());
fileMenu.addSeparator();
fileMenu.add(getExitAction());
menuBar.add(fileMenu);
analyseMenu = new JMenu("Analyse");
analyseMenu.setMnemonic('n');
analyseMenu.add(getAnalyseAction());
menuBar.add(analyseMenu);
aboutMenu = new JMenu("About");
aboutMenu.setMnemonic('A');
aboutMenu.add(getAboutAction());
menuBar.add(aboutMenu);
}
return menuBar;
}
/**
* This method initializes openAction
*
* @return javax.swing.Action
*/
private Action getOpenAction() {
if (openAction == null) {
openAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
openFile();
}
};
openAction.putValue(Action.NAME, "Open");
openAction.putValue(Action.SMALL_ICON, new ImageIcon(
AnalyseFrame.class.getResource("/img/open.png")));
openAction.putValue(Action.SHORT_DESCRIPTION, "Open a file");
openAction.putValue(Action.MNEMONIC_KEY, new Integer('O'));
}
return openAction;
}
/**
* This method initializes saveAction
*
* @return javax.swing.Action
*/
private Action getSaveAction() {
if (saveAction == null) {
saveAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
};
saveAction.putValue(Action.NAME, "Save");
saveAction.putValue(Action.SMALL_ICON, new ImageIcon(
AnalyseFrame.class.getResource("/img/save.png")));
saveAction.putValue(Action.SHORT_DESCRIPTION, "Save current file");
saveAction.putValue(Action.MNEMONIC_KEY, new Integer('S'));
}
return saveAction;
}
/**
* This method initializes newAction
*
* @return javax.swing.Action
*/
private Action getNewAction() {
if (newAction == null) {
newAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
newFile();
}
};
newAction.putValue(Action.NAME, "New");
newAction.putValue(Action.SMALL_ICON, new ImageIcon(
AnalyseFrame.class.getResource("/img/new.png")));
newAction.putValue(Action.SHORT_DESCRIPTION, "Create a new file");
newAction.putValue(Action.MNEMONIC_KEY, new Integer('N'));
}
return newAction;
}
/**
* This method initializes analyseAction
*
* @return javax.swing.Action
*/
private Action getAnalyseAction() {
if (analyseAction == null) {
analyseAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
analyseFile();
}
};
analyseAction.putValue(Action.NAME, "Analyse");
analyseAction.putValue(Action.SMALL_ICON, new ImageIcon(
AnalyseFrame.class.getResource("/img/analyse.png")));
analyseAction.putValue(Action.SHORT_DESCRIPTION,
"Analyse current file");
analyseAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
}
return analyseAction;
}
/**
* This method initializes aboutAction
*
* @return javax.swing.Action
*/
private Action getAboutAction() {
if (aboutAction == null) {
aboutAction = new AbstractAction() {
Icon imageme = new ImageIcon(AnalyseFrame.class
.getResource("/img/spence.jpg"));
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"项目:小编译器\n作者:毕洪宇\n学号: 052473\n专业:计算机科学与技术\n"
+ "联系:boylook314@126.com", "关于作者",
JOptionPane.INFORMATION_MESSAGE, imageme);
}
};
aboutAction.putValue(Action.NAME, "About");
aboutAction.putValue(Action.SMALL_ICON, new ImageIcon(
AnalyseFrame.class.getResource("/img/about.png")));
aboutAction
.putValue(Action.SHORT_DESCRIPTION, "About this program");
aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
}
return aboutAction;
}
/**
* This method initializes exitAction
*
* @return javax.swing.Action
*/
private Action getExitAction() {
if (exitAction == null) {
exitAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
exitAction.putValue(Action.NAME, "Exit");
exitAction.putValue(Action.SMALL_ICON, new ImageIcon(
AnalyseFrame.class.getResource("/img/exit.png")));
exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit this program");
exitAction.putValue(Action.MNEMONIC_KEY, new Integer('E'));
}
return exitAction;
}
/**
* This method initializes inscrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getInScrollPane() {
if (inScrollPane == null) {
inScrollPane = new JScrollPane();
inScrollPane.setViewportView(getInTextArea());
}
return inScrollPane;
}
/**
* This method initializes inTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getInTextArea() {
if (inTextArea == null) {
inTextArea = new JTextArea();
indocument = inTextArea.getDocument();
indocument.addDocumentListener(this);
inTextArea.setEditable(false);
inTextArea.setForeground(Color.magenta);
inTextArea.setFont(new Font(null, Font.BOLD, 14));
}
return inTextArea;
}
/**
* This method initializes outScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getOutScrollPane() {
if (outScrollPane == null) {
outScrollPane = new JScrollPane();
outScrollPane.setViewportView(getOutTextArea());
}
return outScrollPane;
}
/**
* This method initializes outTextField
*
* @return javax.swing.JTextField
*/
private JTextArea getOutTextArea() {
if (outTextArea == null) {
outTextArea = new JTextArea();
outTextArea.setEditable(false);
outTextArea.setForeground(Color.BLUE);
outTextArea.setFont(new Font(null, Font.BOLD, 12));
}
return outTextArea;
}
/**
* This method initializes splitPane
*
* @return javax.swing.JSplitPane
*/
private JSplitPane getSplitPane() {
if (splitPane == null) {
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
getInScrollPane(), getOutScrollPane());
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(350);
}
return splitPane;
}
/**
* mathod main to run this program
* @param args
*/
public static void main(String[] args) {
new AnalyseFrame();
}
/**
* This is the default constructor
*/
public AnalyseFrame() {
super();
initialize();
// 重定向到通过文本组件构建的组件输出流中。
System.setOut(new GUIPrintStream(System.out, outTextArea));
}
/**
* This method initializes this analyseFrame
*
* @return void
*/
private void initialize() {
// this.setSize(600, 400);
this.setContentPane(getJContentPane());
this.setTitle("小编译器");
this.setJMenuBar(getmenuBar());
this.setBounds(200, 150, 600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getToolBar(), java.awt.BorderLayout.NORTH);
jContentPane.add(getSplitPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes open file
*
* @return void
*/
private void openFile() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
sourFile = fc.getSelectedFile();
try {
inTextArea.read(new FileReader(sourFile), null);
this.setTitle("编译器 - " + sourFile.getName());
inTextArea.setEditable(true);
edited = false;
indocument = inTextArea.getDocument();
indocument.addDocumentListener(this);
outTextArea.setText("");
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "Can not open this file!",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* This method initializes saveFile
*
* @return void
*/
private void saveFile() {
if (!inTextArea.isEditable()) {
JOptionPane.showMessageDialog(this, "Please create a new file",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (sourFile == null) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
sourFile = fc.getSelectedFile();
} else
return;
}
try {
inTextArea.write(new FileWriter(sourFile));
this.setTitle("编译器 - " + sourFile.getName());
edited = false;
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "can not open this file!",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method initializes newFile
*
* @return void
*/
private void newFile() {
sourFile = null;
inTextArea.setText("");
this.setTitle("小编译器 - Noname");
inTextArea.setEditable(true);
outTextArea.setText("");
indocument = inTextArea.getDocument();
indocument.addDocumentListener(this);
edited = false;
}
/**
* This method initializes analyseFile
*
* @return void
*/
private void analyseFile(){
// 读取配置文件,得到系统属性
String cfgString[] = new String[2];
try {
cfgString = AnalyseFrame.loadAACfg("aaCfg.xml");
}
catch (Exception e) {
e.printStackTrace(System.err);
}
// 设置待读文件名
//保留字表文件
reserveFileName = cfgString[0];
//类型种别码表文件
classFileName = cfgString[1];
aa = new AccidenceAnalyser();
aa.setFilesPath(reserveFileName, classFileName);
//建立所需要的文件对象
aa.setSourFile(sourFile);
//初始化词法分析器
aa.initAA();
//初始化关键字表
aa.keyWordTable.initKeyWordTable();
//初始化类型种别码表
aa.classIdentity.initClassIdentityTable();
//开始进行词法分析
aa.startAA();
//分析完毕
}
/*
* DocumentListener to handle the changes of inTextArea when the current
* content of inTextArea, it will show '*' on the title to inform the user
* of the changing
*
* @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
*/
public void changedUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
if (!edited) {
edited = true;
setTitle(getTitle() + " *");
}
}
public void removeUpdate(DocumentEvent e) {
if (!edited) {
edited = true;
setTitle(getTitle() + " *");
}
}
private static String[] loadAACfg(String name) throws Exception {
String cfgString[] = new String[2];
/*解析xml配置文件*/
try {
/*创建文档工厂*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/*创建文档解析器*/
DocumentBuilder builder = factory.newDocumentBuilder();
/*解析配置文件*/
org.w3c.dom.Document doc = builder.parse(name);
/*规范化文档*/
doc.normalize();
/*查找接点表*/
NodeList nlists = doc.getElementsByTagName("FilePath");
for (int i = 0; i < nlists.getLength(); i++) {
Element item = (Element) nlists.item(i);
//取得需要的配置属性
cfgString[0] = item.getElementsByTagName("ReserveFileName").item(0).
getFirstChild().getNodeValue().trim();
cfgString[1] = item.getElementsByTagName("ClassFileName").item(0).
getFirstChild().getNodeValue().trim();
}
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("[ERROR]加载配置文件 " + name + " 错误!");
}
//返回属性数组
return cfgString;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -