📄 tigerframe.java
字号:
package GUI;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import Parse.Lexer;
import Parse.Token;
import Parse.Yylex;
import Parse.Grm;
import Parse.sym;
public class tigerFrame extends JFrame implements messPrinter {
private static final long serialVersionUID = 1L;
private JTree tree;
public static void main(String argv[]) throws java.io.IOException {
try {
tigerFrame frame = new tigerFrame();
frame.setVisible(true);
if (argv.length > 0 && !argv[0].equals("")) {
File f = new File(argv[0]);
frame.setFile(f);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "an error in main:"
+ e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
}
}
final JSplitPane splitPane = new JSplitPane();
final JSplitPane splitPane_1 = new JSplitPane();
private JTextPane textArea;
private boolean changed = false;
private String text = "";
private String filename = "";
private String filepath = "";
private JFrame self;
private JList errorlist;
/**
* Create the frame
*/
public tigerFrame() {
super();
if (!Config.initialed)
Config.readConfig();
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "project initial error:"
+ e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
if (changed) {
if (JOptionPane.YES_OPTION == JOptionPane
.showConfirmDialog(null,
"The file is chaned, save it ?",
"choose one", JOptionPane.YES_NO_OPTION)) {
saveFile();
}
}
}
});
self = this;
setBounds(100, 100, 729, 602);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
self.setTitle("source file-...");
// read the configs from xml file
/** ********************************* */
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JMenu fileMenu = new JMenu();
fileMenu.setFont(new Font("Courier New", Font.BOLD, 12));
fileMenu.setText("file(F)");
fileMenu.setMnemonic('F');
menuBar.add(fileMenu);
final JMenuItem fileMenuItem = new JMenuItem();
fileMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (changed) {
if (JOptionPane.YES_OPTION == JOptionPane
.showConfirmDialog(null,
"The file is chaned, save it ?",
"choose one", JOptionPane.YES_NO_OPTION)) {
saveFile();
}
}
File file = new File(GUI.Constants.folder);
JFileChooser chooser = new JFileChooser(file);
chooser.setDialogTitle("choose source file");
chooser.setFileFilter(new tigFileFilter());
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(null)) {
File f = chooser.getSelectedFile();
if (f.isFile()) {
filename = "";
text = "";
changed = false;
filepath = f.getAbsolutePath();
filename = f.getName();
self.setTitle("source file-" + f.getAbsolutePath());
openFile();
}
}
}
});
fileMenuItem.setFont(new Font("Courier New", Font.BOLD, 12));
fileMenuItem.setText("open");
fileMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
KeyEvent.CTRL_MASK));
fileMenu.add(fileMenuItem);
final JMenuItem saveMenuItem = new JMenuItem();
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (changed)
saveFile();
}
});
saveMenuItem.setFont(new Font("Courier New", Font.BOLD, 12));
saveMenuItem.setText("save");
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
KeyEvent.CTRL_MASK));
fileMenu.add(saveMenuItem);
final JMenu toolsMenu = new JMenu();
toolsMenu.setFont(new Font("Courier New", Font.BOLD, 12));
toolsMenu.setText("tools(T)");
toolsMenu.setMnemonic('T');
menuBar.add(toolsMenu);
final JMenuItem runMenuItem = new JMenuItem();
runMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (changed) {
if (JOptionPane.YES_OPTION == JOptionPane
.showConfirmDialog(null,
"The file is chaned, save it ?",
"choose one", JOptionPane.YES_NO_OPTION)) {
saveFile();
} else
return;
}
// parseFile();
build();
}
});
runMenuItem.setFont(new Font("Courier New", Font.BOLD, 12));
runMenuItem.setText("build");
toolsMenu.add(runMenuItem);
runMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,
KeyEvent.CTRL_MASK));
splitPane.setDividerSize(2);
splitPane.setDividerLocation(150);
getContentPane().add(splitPane, BorderLayout.CENTER);
splitPane_1.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane_1.setDividerSize(2);
splitPane_1.setDividerLocation(450);
splitPane.setRightComponent(splitPane_1);
final JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout());
splitPane_1.setLeftComponent(panel_1);
final JScrollPane scrollPane = new JScrollPane();
scrollPane
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel_1.add(scrollPane, BorderLayout.CENTER);
textArea = new JTextPane();
textArea.setFont(new Font("Courier New", Font.PLAIN, 14));
textArea.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent arg0) {
int pos = textArea.getCaretPosition();
if (!arg0.isAltDown() && !arg0.isControlDown())
changed = true;
colorText();
textArea.setCaretPosition(pos);
}
});
scrollPane.setViewportView(textArea);
final JPanel panel_2 = new JPanel();
panel_2.setLayout(new BorderLayout());
splitPane_1.setRightComponent(panel_2);
final JScrollPane scrollPane_1 = new JScrollPane();
panel_2.add(scrollPane_1, BorderLayout.CENTER);
errorlist = new JList();
errorlist.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
int index = errorlist.getSelectedIndex();
errorMouseCilcked(index);
}
});
errorlist.setCellRenderer(new ErrorListCellRenderer());
errorlist.setFont(new Font("Courier New", Font.PLAIN, 13));
scrollPane_1.setViewportView(errorlist);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
splitPane.setLeftComponent(panel);
final JScrollPane scrollPane_2 = new JScrollPane();
panel.add(scrollPane_2, BorderLayout.CENTER);
tree = new JTree();
tree.setFont(new Font("Courier New", Font.PLAIN, 13));
scrollPane_2.setViewportView(tree);
tree.setModel(new DefaultTreeModel(null));
}
public void setFile(File f) {
filename = f.getName();
filepath = f.getAbsolutePath();
self.setTitle("Source file-" + f.getAbsolutePath());
openFile();
}
protected void openFile() {
File f = new File(filepath);
FileReader fr;
try {
fr = new FileReader(f);
BufferedReader d = new BufferedReader(fr);
while (true) {
String s = d.readLine();
if (s == null)
break;
text += s + "\n";
}
textArea.setText(text);
colorText();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Open file error :"
+ e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
// e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Read file error :"
+ e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
// e.printStackTrace();
}
}
protected String transfer(String reg, String replace, String text) {
String tmp = text;
String re = "";
int index = -1;
while ((index = tmp.indexOf(reg)) >= 0) {
re += tmp.substring(0, index);
re += replace;
tmp = tmp.substring(index + reg.length(), tmp.length());
}
re += tmp;
return re;
}
protected void saveFile() {
if (filename == "" || filepath == "") {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("save file");
chooser.setFileFilter(new tigFileFilter());
int re = chooser.showSaveDialog(null);
if (re == JFileChooser.ERROR_OPTION)
JOptionPane.showMessageDialog(null, "Save file error :",
"error", JOptionPane.ERROR_MESSAGE);
else if (re == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
filepath = file.getAbsolutePath();
filename = file.getName();
if (filename.equals("")) {
filepath = "tig";
filename = "tig";
}
if (!filename.endsWith(".tig") || !filename.endsWith(".java")) {
filepath += ".tig";
filename += ".tig";
}
}
}
text = textArea.getText();
text = text.replaceAll("\r\n", "\n");
File f = new File(filepath);
try {
FileOutputStream fo = new FileOutputStream(f);
fo.write(text.getBytes());
fo.close();
changed = false;
colorText();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Find file error:"
+ e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
// e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Access file error :"
+ e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
// e.printStackTrace();
}
}
static SimpleAttributeSet COMM = new SimpleAttributeSet();
static SimpleAttributeSet KEY = new SimpleAttributeSet();
static SimpleAttributeSet BLACK = new SimpleAttributeSet();
static SimpleAttributeSet STRING = new SimpleAttributeSet();
// Best to reuse attribute sets as much as possible.
static {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -