📄 javafilter.java
字号:
while (end < content.length() && content.charAt(end) != '\n') {
end++;
}
content.delete(start, end);
deleteinfo(content, start);
return;
}
if (content.substring(start, start + 2).equals("/*")) { //end index shoud be exclusive
while (end < content.length() - 2 &&
!content.substring(end, end + 2).equals("*/")) {
end++;
}
content.delete(start, end + 2);
deleteinfo(content, start);
return;
}
}
}
// New File Action
class NewAction extends AbstractAction {
NewAction() {
super("New");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
newFile();
}
}
public void newFile() {
if (changed) {
int ret = JOptionPane.showConfirmDialog(this,
"The file have been changed, do you want to save it?",
"Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
switch (ret) {
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
break;
case JOptionPane.CANCEL_OPTION:
return;
}
}
textPane.setText("");
file = null;
changed = false;
setTitle();
}
// About Action
class AboutAction extends AbstractAction {
AboutAction() {
super("About");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(curComponet,
"Author: Jesse \nVersion: 1.0 Beta \nDate: July 3th 2006",
"About",
JOptionPane.INFORMATION_MESSAGE);
}
}
// Open File Action
class OpenAction extends AbstractAction {
OpenAction() {
super("Open");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
if (!changed) {
openFile();
} else {
int ret = JOptionPane.showConfirmDialog(curComponet,
"Save File?");
if (ret == JOptionPane.YES_OPTION) {
saveFile();
} else if (ret == JOptionPane.NO_OPTION) {
openFile();
}
}
}
}
// open the file
public void openFile() {
StringBuffer content = new StringBuffer();
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
try {
FileReader in = new FileReader(file);
int c;
while ((c = in.read()) != -1) {
content.append((char) c);
}
in.close();
} catch (FileNotFoundException fe) {
JOptionPane.showMessageDialog(curComponet, fe, "File not Found",
JOptionPane.ERROR_MESSAGE);
return;
} catch (IOException ie) {
JOptionPane.showMessageDialog(curComponet, ie,
"File read Error",
JOptionPane.ERROR_MESSAGE);
return;
}
setTitle();
changed = false;
textPane.setText(new String(content));
cur.formatDocument();
textPane.setCaretPosition(0);
}
}
// Save File Action
class SaveAction extends AbstractAction {
SaveAction() {
super("Save");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
if (changed) {
saveFile();
}
}
}
// save the file
public void saveFile() {
//a new File
if (file == null) {
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
if (file.exists()) {
int ret = JOptionPane.showConfirmDialog(this,
"The file exits, do you want to overwrite it?",
"Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
switch (ret) {
case JOptionPane.YES_OPTION:
saveToFile(file);
break;
case JOptionPane.NO_OPTION:
saveFile();
return;
case JOptionPane.CANCEL_OPTION:
return;
}
}
} else {
try {
file.createNewFile();
} catch (IOException ie) {
JOptionPane.showMessageDialog(curComponet, ie,
"Can not create a new file.",
JOptionPane.ERROR_MESSAGE);
}
saveToFile(file);
}
} else {
saveToFile(file);
}
}
// save to the file
public void saveToFile(File file) {
try {
FileWriter out = new FileWriter(file);
out.write(textPane.getText());
out.close();
} catch (IOException ie) {
JOptionPane.showMessageDialog(curComponet, ie, "File write Error",
JOptionPane.ERROR_MESSAGE);
return;
}
changed = false;
setTitle();
}
// Save File As Action
class SaveAsAction extends AbstractAction {
SaveAsAction() {
super("Save As...");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
saveAsFile();
}
}
public void saveAsFile() {
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
if (file.exists()) {
int ret = JOptionPane.showConfirmDialog(curComponet,
"The file exits, do you want to overwrite it?",
"Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
switch (ret) {
case JOptionPane.YES_OPTION:
saveToFile(file);
break;
case JOptionPane.NO_OPTION:
saveAsFile();
break;
case JOptionPane.CANCEL_OPTION:
return;
}
} else {
try {
file.createNewFile();
} catch (IOException ie) {
JOptionPane.showMessageDialog(curComponet, ie,
"Can not create a new file.",
JOptionPane.ERROR_MESSAGE);
}
saveToFile(file);
}
}
}
// Exit File Action
class ExitAction extends AbstractAction {
ExitAction() {
super("Exit");
setEnabled(true);
}
public void actionPerformed(ActionEvent e) {
if (changed) {
int ret = JOptionPane.showConfirmDialog(null, "Save File?",
"Confirm", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.YES_OPTION) {
saveFile();
System.exit(0);
} else if (ret == JOptionPane.NO_OPTION) {
System.exit(0);
}
}
System.exit(0);
}
}
// Undo Action
class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.undo();
} catch (CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
updateUndoState();
redoAction.updateRedoState();
}
protected void updateUndoState() {
if (undo.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
class RedoAction extends AbstractAction {
public RedoAction() {
super("Redo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.redo();
} catch (CannotRedoException ex) {
System.out.println("Unable to redo: " + ex);
ex.printStackTrace();
}
updateRedoState();
undoAction.updateUndoState();
}
protected void updateRedoState() {
if (undo.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undo.getRedoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
//The standard main method.
public static void main(String[] args) {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
javaFilter frame = new javaFilter();
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -