2

我有 aJTextArea和 aJComboBox允许我循环浏览各种打开的文件 -JTextArea当我选择不同的文件时更改的内容。我试图为每个文件维护一个不同的撤消缓冲区,并为每个文件定义了一个单独的缓冲区UndoManager

我创建了一个更简单的 SSCCE 来演示我的问题,它使用两个缓冲区,我称之为“一个”和“两个”——用一个简单的按钮在它们之间切换。一旦UndoableEdit发生,它会检查活动缓冲区并对addEdit()相应的UndoManager. 当按下“撤消”按钮时,它会检查canUndo()并执行undo()相应的UndoManager. 我有一个名为 的标志ignoreEdit,用于在缓冲区之间切换以忽略这些编辑被记录。

如果我从不在缓冲区之间切换,那么我没有问题,撤消按预期工作。只有当我在缓冲区之间切换并似乎“破坏”文档时,它才会失败。以下步骤可用于重现问题:

在缓冲区“一”中,键入:

THIS
IS ONE
EXAMPLE

切换到缓冲区“二”,键入:

THIS
IS ANOTHER
EXAMPLE

切换到缓冲区“One”并多次按下“Undo”按钮。经过几次撤消操作后,缓冲区看起来像这样(光标无法选择前两行)。但是,根据 - 所以,它的内容textArea.getText()是正确的System.out.println()- 所以,它看起来像一个渲染问题?

THIS

THISIS ONE

这不是第一次有人尝试为每个文件实现独立的撤消缓冲区吗?我显然在 Document 模型上做错了,并且天生就破坏了它,但我正在寻找一些关于如何最好地解决这个问题的建议?

SSCCE 的代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;

public class SSCCE extends JFrame implements ActionListener, UndoableEditListener {
  private final JLabel labTextArea;
  private final JTextArea textArea;
  private final JScrollPane scrollTextArea;
  private final Document docTextArea;
  private final JButton bOne, bTwo, bUndo;
  private final UndoManager uOne, uTwo;
  private String sOne, sTwo;
  private boolean ignoreEdit = false;

  public SSCCE(String[] args) {
    setTitle("SSCCE - Short, Self Contained, Correct Example");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(300, 200);
    setLocationRelativeTo(null);

    labTextArea = new JLabel("One");
    getContentPane().add(labTextArea, BorderLayout.PAGE_START);

    uOne = new UndoManager();
    uTwo = new UndoManager();
    sOne = new String();
    sTwo = new String();

    textArea = new JTextArea();
    docTextArea = textArea.getDocument();
    docTextArea.addUndoableEditListener(this);
    scrollTextArea = new JScrollPane(textArea);
    getContentPane().add(scrollTextArea, BorderLayout.CENTER);

    JPanel pButtons = new JPanel();
    bOne = new JButton("One");
    bOne.addActionListener(this);
    bOne.setFocusable(false);
    pButtons.add(bOne, BorderLayout.LINE_START);
    bTwo = new JButton("Two");
    bTwo.addActionListener(this);
    bTwo.setFocusable(false);
    pButtons.add(bTwo, BorderLayout.LINE_END);
    bUndo = new JButton("Undo");
    bUndo.addActionListener(this);
    bUndo.setFocusable(false);
    pButtons.add(bUndo, BorderLayout.LINE_END);
    getContentPane().add(pButtons, BorderLayout.PAGE_END);

    setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(bOne)) {
      if (!labTextArea.getText().equals("One")) {
        sTwo = textArea.getText();
        ignoreEdit = true;
        textArea.setText(sOne);
        ignoreEdit = false;
        labTextArea.setText("One");
      }
    }
    else if (e.getSource().equals(bTwo)) {
      if (!labTextArea.getText().equals("Two")) {
        sOne = textArea.getText();
        ignoreEdit = true;
        textArea.setText(sTwo);
        ignoreEdit = false;
        labTextArea.setText("Two");
      }
    }
    else if (e.getSource().equals(bUndo)) {
      if (labTextArea.getText().equals("One")) {
        try {
          if (uOne.canUndo()) {
            System.out.println("Performing Undo for One");
            uOne.undo();
            System.out.println("Buffer One is now:\n" + textArea.getText() + "\n");
          }
          else {
            System.out.println("Nothing to Undo for One");
          }
        }
        catch (CannotUndoException ex) {
          ex.printStackTrace();
        }
      }
      else if (labTextArea.getText().equals("Two")) {
        try {
          if (uTwo.canUndo()) {
            System.out.println("Performing Undo for Two");
            uTwo.undo();
            System.out.println("Buffer Two is now:\n" + textArea.getText() + "\n");
          }
          else {
            System.out.println("Nothing to Undo for Two");
          }
        }
        catch (CannotUndoException ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  @Override
  public void undoableEditHappened(UndoableEditEvent e) {
    if (!ignoreEdit) {
      if (labTextArea.getText().equals("One")) {
        System.out.println("Adding Edit for One");
        uOne.addEdit(e.getEdit());
      }
      else if (labTextArea.getText().equals("Two")) {
        System.out.println("Adding Edit for Two");
        uTwo.addEdit(e.getEdit());
      }
    }
  }

  public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new SSCCE(args);
      }
    });
  }
}
4

2 回答 2

2

以前我曾尝试创建Document该类的一个新实例(每个都引用同一个 Undo 侦听器),并打算使用JTextArea.setDocument()而不是JTextArea.setText(). 但是,Document它是一个接口并且不能被实例化,但是在阅读了 mKorbel 发布的参考之后,我尝试使用PlainDocument该类来代替,它有效。

我决定维护一个HashMap<String, Document>来包含我的Document类并在它们之间切换。当我Document切换Document.

更新了下面的 SSCCE,现在使用JTextArea.setDocument()而不是JTextArea.setText(). 这还具有不需要ignoreEdit布尔值的优点,因为setDocument()不会触发UndoableEditEvent,而setText()会触发。然后每个都Document引用本地类UndoableEditListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;

public class SSCCE extends JFrame implements ActionListener, UndoableEditListener {
  private final JLabel labTextArea;
  private final JTextArea textArea;
  private final JScrollPane scrollTextArea;
  private final Document docTextArea;
  private final JButton bOne, bTwo, bUndo;
  private final UndoManager uOne, uTwo;
  private Document dOne, dTwo;

  public SSCCE(String[] args) {
    setTitle("SSCCE - Short, Self Contained, Correct Example");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(300, 200);
    setLocationRelativeTo(null);

    labTextArea = new JLabel("One");
    getContentPane().add(labTextArea, BorderLayout.PAGE_START);

    uOne = new UndoManager();
    uTwo = new UndoManager();
    dOne = new PlainDocument();
    dTwo = new PlainDocument();
    dOne.addUndoableEditListener(this);
    dTwo.addUndoableEditListener(this);

    textArea = new JTextArea();
    docTextArea = textArea.getDocument();
    docTextArea.addUndoableEditListener(this);
    textArea.setDocument(dOne);
    scrollTextArea = new JScrollPane(textArea);
    getContentPane().add(scrollTextArea, BorderLayout.CENTER);

    JPanel pButtons = new JPanel();
    bOne = new JButton("One");
    bOne.addActionListener(this);
    bOne.setFocusable(false);
    pButtons.add(bOne, BorderLayout.LINE_START);
    bTwo = new JButton("Two");
    bTwo.addActionListener(this);
    bTwo.setFocusable(false);
    pButtons.add(bTwo, BorderLayout.LINE_END);
    bUndo = new JButton("Undo");
    bUndo.addActionListener(this);
    bUndo.setFocusable(false);
    pButtons.add(bUndo, BorderLayout.LINE_END);
    getContentPane().add(pButtons, BorderLayout.PAGE_END);

    setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(bOne)) {
      if (!labTextArea.getText().equals("One")) {
        textArea.setDocument(dOne);
        labTextArea.setText("One");
      }
    }
    else if (e.getSource().equals(bTwo)) {
      if (!labTextArea.getText().equals("Two")) {
        textArea.setDocument(dTwo);
        labTextArea.setText("Two");
      }
    }
    else if (e.getSource().equals(bUndo)) {
      if (labTextArea.getText().equals("One")) {
        try {
          if (uOne.canUndo()) {
            System.out.println("Performing Undo for One");
            uOne.undo();
            System.out.println("Buffer One is now:\n" + textArea.getText() + "\n");
          }
          else {
            System.out.println("Nothing to Undo for One");
          }
        }
        catch (CannotUndoException ex) {
          ex.printStackTrace();
        }
      }
      else if (labTextArea.getText().equals("Two")) {
        try {
          if (uTwo.canUndo()) {
            System.out.println("Performing Undo for Two");
            uTwo.undo();
            System.out.println("Buffer Two is now:\n" + textArea.getText() + "\n");
          }
          else {
            System.out.println("Nothing to Undo for Two");
          }
        }
        catch (CannotUndoException ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  @Override
  public void undoableEditHappened(UndoableEditEvent e) {
    if (labTextArea.getText().equals("One")) {
      System.out.println("Adding Edit for One");
      uOne.addEdit(e.getEdit());
    }
    else if (labTextArea.getText().equals("Two")) {
      System.out.println("Adding Edit for Two");
      uTwo.addEdit(e.getEdit());
    }
  }

  public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new SSCCE(args);
      }
    });
  }
}
于 2014-05-27T14:02:46.073 回答
1

这本身不是一个答案,而是展示了解决同一问题的不同方法。

这样做是将 包装UndoableEditListener在一个自我管理的代理中,该代理有它自己的UndoManagerDocument.

使用 Java 7 和 Java 8 对此进行了测试:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import javax.swing.undo.UndoManager;

public class UndoExample {

    public static void main(String[] args) {
        new UndoExample();
    }

    private int index = 0;
    private Map<String, Undoer> mapUndoers;
    private JTextArea ta;
    private Undoer current;

    public UndoExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                mapUndoers = new HashMap<>(2);
                mapUndoers.put("One", new Undoer());
                mapUndoers.put("Two", new Undoer());

                ta = new JTextArea(4, 20);
                ta.setWrapStyleWord(true);
                ta.setLineWrap(true);

                JButton btnOne = new JButton("One");
                JButton btnTwo = new JButton("Two");
                ActionListener al = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        install(e.getActionCommand());
                    }
                };
                btnOne.addActionListener(al);
                btnTwo.addActionListener(al);

                JButton undo = new JButton("Undo");
                undo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (current != null) {
                            current.undo();
                        }
                    }
                });

                JPanel panel = new JPanel(new GridBagLayout());
                panel.add(btnOne);
                panel.add(btnTwo);
                panel.add(undo);

                install("One");

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(ta));
                frame.add(panel, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected void install(String name) {
        Undoer undoer = mapUndoers.get(name);
        if (undoer != null) {
            current = undoer;
            undoer.install(ta);
        }
    }

    public class Undoer implements UndoableEditListener {

        private UndoManager undoManager;
        private Document doc;

        public Undoer() {
            undoManager = new UndoManager();
            doc = createDocument();
            doc.addUndoableEditListener(this);
        }

        public void undo() {
            undoManager.undo();
        }

        public void undoOrRedo() {
            undoManager.undoOrRedo();
        }

        protected Document createDocument() {
            return new PlainDocument();
        }

        public void install(JTextComponent comp) {
            comp.setDocument(doc);
        }

        @Override
        public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
        }

    }

}
于 2014-05-27T23:57:24.957 回答