JEditorPane插入很多字符和撤銷 恢復操作, 下面是代碼, NewFrame類中 調用 UndoWrapper類
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import work.UndoWrapper;
public class NewJFrame extends javax.swing.JFrame implements ActionListener ...{
private JPanel jp1;
private JTextPane jep;
private JScrollPane jsp;
private JButton font;
private JButton redo;
private JButton cancel;
private JButton replace;
private static ArrayList<Document> beifenDoc = new ArrayList<Document>();
private static int cancelIndex = 0;
private static int docIndex = 0;
private static Document yyy;
private static UndoWrapper undo;
/**//**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) ...{
NewJFrame inst = new NewJFrame();
inst.setVisible(true);
}
public NewJFrame() ...{
super();
initGUI();
}
private void initGUI() ...{
try ...{
BorderLayout thisLayout = new BorderLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
...{
jp1 = new JPanel();
getContentPane().add(jp1, BorderLayout.NORTH);
...{
font = new JButton();
font.addActionListener(this);
jp1.add(font);
font.setText("font");
}
...{
redo = new JButton();
jp1.add(redo);
redo.addActionListener(this);
redo.setText("redo");
}
...{
cancel = new JButton();
jp1.add(cancel);
cancel.addActionListener(this);
cancel.setText("cancel");
}
...{
replace = new JButton();
jp1.add(replace);
replace.addActionListener(this);
replace.setText("replace");
}
}
...{
jsp = new JScrollPane();
getContentPane().add(jsp, BorderLayout.CENTER);
...{
jep = new JTextPane();
jep.setText(openFile("g:/spring_mass.adm.txt"));
jep.setEditable(false);
yyy = jep.getDocument();
undo = new UndoWrapper(jep);
//(Sundo.undoManager).undo();
beifenDoc.add(jep.getDocument());
jsp.setViewportView(jep);
// jep.setStyledDocument(new DefaultStyledDocument());
}
}
pack();
setSize(400, 300);
} catch (Exception e) ...{
e.printStackTrace();
}
}
/**//**
*
* @param editor
* 因爲 在 initGUI()中 已經將 本程序中的JEditorPane與UndoWrapper關聯,即new UndoWrapper(jep);
* 所以,採用現成的實現 JEditorPane 撤銷 恢復功能 的類 UndoWrapper,調用其中的undoManager屬性,
* 進行響應的操作。
*
*/
public static void cancel(JEditorPane editor) ...{
try...{
undo.undoManager.undo();
}catch(CannotUndoException e)...{
System.out.println("無可撤銷");
}
}
public static void redo(JEditorPane editor) ...{
try...{
undo.undoManager.redo();
}catch(CannotRedoException e)...{
System.out.println("無可恢復");
}
}
private String openFile(String filePath) ...{
File f = new File(filePath);
String ee = new String();
System.out.println("Opening file: " + filePath);
try ...{
InputStream is = new FileInputStream(f);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line; // 用來保存每行讀取的內容
line = reader.readLine(); // 讀取第一行
while (line != null) ...{ // 如果 line 爲空說明讀完了
buffer.append(line); // 將讀到的內容添加到 buffer 中
buffer.append(" "); // 添加換行符
line = reader.readLine(); // 讀取下一行
}
// System.out.print(buffer.toString());
ee = buffer.toString();
}
catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
return ee;
}
public static void replace(JEditorPane editor, String objective) ...{
try ...{
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, Color.BLUE);// 理解爲,先將顏色改變賦予屬性,
Document docs = editor.getDocument();
String fileString = editor.getText();
int size = fileString.length();
String selectedString = editor.getSelectedText();
if (selectedString == null)...{
return;
}
int s0 = fileString.indexOf(selectedString)
+ selectedString.length();
int s1 = fileString.indexOf(",", s0);
int s2 = fileString.indexOf(" ", s0);
int s3 = fileString.indexOf(" ", s0);
int s4 = fileString.indexOf(" ", s0);
if (s1 < 0)
s1 = size;
if (s2 < 0)
s2 = size;
if (s3 < 0)
s3 = size;
if (s4 < 0)
s4 = size;
int endIndex = Math.min(Math.min(Math.min(s1, s2), s3), s4);
if (endIndex > s0) ...{
docs.remove(s0, endIndex - s0);
docs.insertString(s0, objective, attr);
beifenDoc.add(docs);
} else ...{
int enter = fileString.indexOf(" ", s0);
if (enter == s0)
return;
int s00 = s0 + 1;
s1 = fileString.indexOf(",", s00);
s2 = fileString.indexOf(" ", s00);
s3 = fileString.indexOf(" ", s00);
s4 = fileString.indexOf(" ", s00);
if (s1 < 0)
s1 = size;
if (s2 < 0)
s2 = size;
if (s3 < 0)
s3 = size;
if (s4 < 0)
s4 = size;
endIndex = Math.min(Math.min(Math.min(s1, s2), s3), s4);
docs.remove(s00, endIndex - s00);
docs.insertString(s00, objective, attr);
beifenDoc.add(docs);
}
} catch (BadLocationException e) ...{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) ...{
Object obj = e.getSource();
if (obj == cancel) ...{
JEditorPane editor = jep;
cancel(editor);
}
if (obj == redo) ...{
JEditorPane editor = jep;
redo(editor);
}
if (obj == replace) ...{
JEditorPane editor = jep;
replace(editor, "QQ");
}
}
}
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
/**//**
* UndoWrapper is responsible for adding undo and redo support to text
* components.
*
* @author Antonio Vieiro ([email protected]), $Author: $
* @version $Revision: $
*/
public class UndoWrapper implements UndoableEditListener ...{
public UndoManager undoManager;
private UndoAction undoAction;
private RedoAction redoAction;
private JEditorPane textComponent;
/**//**
* Creates a new instance of UndoWrapper
*/
public UndoWrapper(JEditorPane aComponent) ...{
textComponent = aComponent;
undoManager = new UndoManager();
undoAction = new UndoAction();
redoAction = new RedoAction();
textComponent.getDocument().addUndoableEditListener(this);
textComponent.getInputMap()
.put((KeyStroke) undoAction.getValue(Action.ACCELERATOR_KEY),
"undo");
textComponent.getInputMap()
.put((KeyStroke) redoAction.getValue(Action.ACCELERATOR_KEY),
"redo");
textComponent.getActionMap().put("undo", undoAction);
textComponent.getActionMap().put("redo", redoAction);
}
public void undoableEditHappened(UndoableEditEvent e) ...{
undoManager.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
/**//**
* UndoAction is the Action responsible for handling the undo operation.
*/
class UndoAction extends AbstractAction ...{
public UndoAction() ...{
super("Cannot undo"); // TODO: I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Z"));
}
public void actionPerformed(ActionEvent e) ...{
try ...{
undoManager.undo();
} catch (CannotUndoException cue) ...{
// TODO: Use logging?
cue.printStackTrace(System.err);
}
updateUndoState();
redoAction.updateRedoState();
}
void updateUndoState() ...{
if (undoManager.canUndo()) ...{
setEnabled(true);
putValue(Action.NAME, "Undo"); // TODO I18N
} else ...{
setEnabled(false);
putValue(Action.NAME, "Cannot undo"); // TODO I18N
}
}
}
/**//**
* RedoAction is the Action responsible for handling the redo operation.
*/
class RedoAction extends AbstractAction ...{
public RedoAction() ...{
super("Cannot redo"); // TODO I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Y"));
}
public void actionPerformed(ActionEvent e) ...{
try ...{
undoManager.redo();
} catch (CannotRedoException cre) ...{
// TODO: Use logging?
cre.printStackTrace(System.err);
}
updateRedoState();
undoAction.updateUndoState();
}
void updateRedoState() ...{
if (undoManager.canRedo()) ...{
setEnabled(true);
putValue(Action.NAME, "Redo"); // TODO I18N
} else ...{
setEnabled(false);
putValue(Action.NAME, "Cannot redo"); // TODO I18N
}
}
}
UndoAction getUndoAction() ...{
return undoAction;
}
RedoAction getRedoAction() ...{
return redoAction;
}
}