⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sourcepane.java

📁 一个C语言子集的编译器
💻 JAVA
字号:
/*
 * @(#)SourcePane.java	1.0.0  05/13/2007
 *
 * Copyright 2007 Xuxt@buaa, No.34060520 All rights reserved.
 */
package com.king4solomon.homework.compiler.gui;

import java.io.*;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;

import java.awt.*;
import java.util.*;
import java.awt.event.*;

import com.king4solomon.homework.compiler.pvm.*;

@SuppressWarnings( { "serial", "unused" })
public class SourcePane extends JTabbedPane {
	public SourcePane() {
		super();
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getClickCount() == 2
						&& e.getButton() == MouseEvent.BUTTON1) {
					int tabIndex = indexAtLocation(e.getX(), e.getY());
					if (tabIndex != -1) {
						selected = ((MyScrollPane) getComponentAt(tabIndex)).pane;
						if (selected.saved) {
							remove(tabIndex);
							nameSet.remove(selected.file.getAbsolutePath());
							index -= 1;
							if (index != -1)
								selected = ((MyScrollPane) getComponentAt(0)).pane;
							else selected = null;
						} else {
							JFrame frame = new JFrame();
							String[] options = { "Yes", "No", "Cancel" };
							int s = JOptionPane
									.showOptionDialog(
											frame,
											"The current file has not been saved yet, save changes?",
											"Save Resource",
											JOptionPane.YES_NO_CANCEL_OPTION,
											JOptionPane.QUESTION_MESSAGE, null,
											options, null);
							if (s == 0) {
								selected.saved = true;
								try {
									PrintWriter out = new PrintWriter(
											selected.file);
									out.print(selected.getText());
									out.close();
								} catch (Exception e1) {
									e1.printStackTrace();
								}
								remove(tabIndex);
								nameSet.remove(selected.file.getAbsolutePath());
								index -= 1;
								if (index != -1)
									selected = ((MyScrollPane) getComponentAt(0)).pane;
								else selected = null;
							} else if (s == 1) {
								selected.saved = true;
								remove(tabIndex);
								nameSet.remove(selected.file.getAbsolutePath());
								index -= 1;
								if (index != -1)
									selected = ((MyScrollPane) getComponentAt(0)).pane;
								else selected = null;
							} else {
								frame.dispose();
							}

						}
					}
				} else if (e.getClickCount() == 1
						&& e.getButton() == MouseEvent.BUTTON1) {
					int tabIndex = indexAtLocation(e.getX(), e.getY());
					if (tabIndex != -1)
						selected = ((MyScrollPane) getComponentAt(tabIndex)).pane;
				}
			}
		});
	}

	public int open(File f) {
		if (!hasOpened(f.getAbsolutePath())) {
			nameSet.add(f.getAbsolutePath());
			MyTextPane blank = new MyTextPane();
			int lineNo = 0;
			blank.file = f;
			MyScrollPane scrolled = new MyScrollPane(blank,
					JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED, blank);
			addTab(f.getName(), scrolled);
			selected = blank;
			index += 1;
			setSelectedIndex(index);
			try {
				BufferedReader in = new BufferedReader(new FileReader(f));
				String bufLine = new String();

				int lCount = 0;
				while (true) {
					bufLine = in.readLine();
					if (bufLine == null) {
						break;
					} else {
						lineNo++;
						blank.insert(bufLine, new SimpleAttributeSet());
						lCount++;
					}
				}

				in.close();

			} catch (Exception e) {
				InfoPane.setBlank();
				InfoPane.write(e.getMessage());
			}
			return lineNo;
		} else
			return 0;

	}

	public static void savSlct() {
		if (index == -1)
			return;
		selected.saved = true;
		try {
			PrintWriter out = new PrintWriter(selected.file);
			out.print(selected.getText());
			out.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	public static File savSlctAs(){
		if (index == -1)
			return new File("");
		selected.saved = true;
		try {
			JFileChooser fChooser = new JFileChooser();
			fChooser.setDialogTitle("Save As");
			fChooser.setMultiSelectionEnabled(false);
			fChooser.setFileFilter(new FileFilter() {
				public boolean accept(File f) {
					return f.getName().toLowerCase().endsWith(".txt")
							|| f.isDirectory();
				};

				public String getDescription() {
					return "TXT File";
				}
			});
			int result = fChooser.showOpenDialog(new JFrame());
			if (result == 0) {
				PrintWriter out = new PrintWriter(fChooser.getSelectedFile());
				out.print(selected.getText());
				out.close();
			}
			return fChooser.getSelectedFile();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new File("");
		
	}

	public void addC(Component c) {
		if (!hasTable) {
			cTable = c;
			nameSet.add("StackVM");
			index+=1;
			super.addTab("StackVM", c);
			setSelectedIndex(index);
			hasTable = true;
		}
	}

	public static void setTable(StackTable st) {
		table = st;
	}

	public static void paint() {
		cTable.repaint();
	}

	private static boolean hasOpened(String fName) {
		for (int idx = 0; idx < nameSet.size(); idx++) {
			if (nameSet.get(idx) == fName)
				return true;
		}
		return false;
	}

	private static LinkedList<String> nameSet = new LinkedList<String>();

	private static boolean hasTable = false;

	public static StackTable table;

	public static MyTextPane selected;

	public static int index = -1;

	private static Component cTable;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -