title: Memento pattern undo/redo texteditor
body: <p>I tried to apply memento pattern to include undo/redo functions in my tex editor app. Let's say it is simplified version :). So far, I haven't figured out how to save and then restore the exact text input from the keyboard. I mean, I have to link the state attribute with the textarea in my code, plus some other modifications. I tried to stuck the whole code in three classes, Main, Editor, Memento like below
  Any hints? Thank you</p>
  <pre><code>public class Main {
      public static void main(String[] args) {
          
          Editor viewEditor = new Editor();
          viewEditor.setVisible(true);
  
          //Storing changes in ArrayList
          List&lt;Memento&gt; mementoList = new ArrayList&lt;Memento&gt;();
          
          viewEditor.setState(&quot; first and only state :)&quot;);
          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(&quot;Saving state to Memento in Editor.java &quot;);
          return new Memento(state);
      }
  
      public void getStatefromMemento(Memento memento) {
          state = memento.getState();
          System.out.println(&quot;State restored from memento&quot; + 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(&quot;&quot;);
          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(&quot;Arial&quot;, 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(&quot;Brudnopis&quot;);
          brudnopis.setMnemonic(KeyEvent.VK_B);
          // Menu Items: Zakoncz
          JMenuItem eMenuItemZakoncz = new JMenuItem(&quot;Zakoncz&quot;);
          eMenuItemZakoncz.setMnemonic(KeyEvent.VK_K);
          eMenuItemZakoncz.setToolTipText(&quot;Zakoncz program&quot;);
          // Adding action for the item: &quot;Zakoncz&quot;
          eMenuItemZakoncz.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent event) {
                  System.exit(0);
              }
          });
  
          //Menu Edit Item
          JMenu edit = new JMenu(&quot;Edycja&quot;);
          edit.setMnemonic(KeyEvent.VK_H);
          //Menu items: undo and redo
          JMenuItem undo = new JMenuItem(&quot;Undo&quot;);
          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(&quot;Redo&quot;);
          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(&quot;Brudnopis&quot;);
          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;
  }
  
   }
  
  </code></pre>