我尝试应用 memento 模式在我的 tex 编辑器应用程序中包含撤消/重做功能。假设它是简化版:)。到目前为止,我还没有弄清楚如何保存然后恢复从键盘输入的确切文本。我的意思是,我必须在我的代码中将 state 属性与 textarea 相关联,以及一些其他修改。我试图将整个代码卡在三个类中,Main、Editor、Memento,如下所示有任何提示吗?谢谢
public class Main {
public static void main(String[] args) {
Editor viewEditor = new Editor();
viewEditor.setVisible(true);
//Storing changes in ArrayList
List<Memento> mementoList = new ArrayList<Memento>();
viewEditor.setState(" first and only state :)");
mementoList.add(viewEditor.saveStatetoMemento());
viewEditor.getStatefromMemento(mementoList.get(0));
}
}
public class Editor extends JFrame {
String state;
public void setState(String state) {
this.state = state;
}
public String getState(String state) {
return state;
}
public Memento saveStatetoMemento() {
System.out.println("Saving state to Memento in Editor.java ");
return new Memento(state);
}
public void getStatefromMemento(Memento memento) {
state = memento.getState();
System.out.println("State restored from memento" + state);
}
//Using UndoManager for handling undo/redo/ operations
private UndoManager um = new UndoManager();
public Editor() {
initUI();
}
public final void initUI() {
//Panel
JPanel panel = new JPanel();
//Text Field
final JTextArea textArea = new JTextArea("");
textArea.getDocument().addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
um.addEdit(e.getEdit());
state = textArea.getText();
}
});
textArea.setPreferredSize(new Dimension(550, 600));
textArea.setLineWrap(true);
textArea.setFont(new Font("Arial", Font.PLAIN, 20));
textArea.setEditable(true);
// Addind text field to panel
panel.add(textArea);
// Adding panel to JFrame
add(panel);
pack();
// Menubar
JMenuBar menubar = new JMenuBar();
// Menu Brudnopis
JMenu brudnopis = new JMenu("Brudnopis");
brudnopis.setMnemonic(KeyEvent.VK_B);
// Menu Items: Zakoncz
JMenuItem eMenuItemZakoncz = new JMenuItem("Zakoncz");
eMenuItemZakoncz.setMnemonic(KeyEvent.VK_K);
eMenuItemZakoncz.setToolTipText("Zakoncz program");
// Adding action for the item: "Zakoncz"
eMenuItemZakoncz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
//Menu Edit Item
JMenu edit = new JMenu("Edycja");
edit.setMnemonic(KeyEvent.VK_H);
//Menu items: undo and redo
JMenuItem undo = new JMenuItem("Undo");
undo.setMnemonic(KeyEvent.VK_Z);
undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
//undo.setAction(a);
undo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (um.canUndo()) {
um.undo();
}
}
});
JMenuItem redo = new JMenuItem("Redo");
redo.setMnemonic(KeyEvent.VK_Y);
redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
redo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (um.canRedo()) {
um.redo();
}
}
});
//Adding 'brudnopis' to menubar
menubar.add(brudnopis);
menubar.add(edit);
setJMenuBar(menubar);
//Dodanie opcji do paska menu
brudnopis.add(eMenuItemZakoncz);
edit.add(undo);
edit.add(redo);
setTitle("Brudnopis");
setSize(600, 700);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Memento {
private final String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}