我用GlazedList eventList创建了一个JTable。
我搜索了API,但是我不知道如何在此表中添加撤消/重做的可能性。我在API中找到以下类别:

UndoRedoSupport undoRedoSupport = new UndoRedoSupport<"what here?">("argument?"); UndoSupport undoSupport = new UndoSupport<"what to write here?">("argument?");

有谁知道如何使用它?

private void createComponents() {
    EventList<Dien> eventList = new BasicEventList<Dien>();
    actionList  = GenericsUtil.makeList();
    table  = new WebTable();
    searchField = new WebTextField(60);
    String[] headers = new String[]{"Code", "Name", "Number"};
    String[] properties = new String[]{"Code", "Name", "Number"};
    TextFilterator<Dien> dienFilterator = new TextFilterator<Dien>() {
        public void getFilterStrings(List baseList, Dien dien) {
            baseList.add(dien.getCode());
            baseList.add(dien.getName());
            baseList.add(dien.getNumber());
        }
    };
    MatcherEditor<Dien> textMatcherEditor = new TextComponentMatcherEditor<Dien>(searchField, dienFilterator);
    eventList = toolModel.getDiens();
    FilterList<Dien> filterList = new FilterList<Dien>(eventList, textMatcherEditor);
    TableFormat tableFormat = GlazedLists.tableFormat(properties, headers, new boolean[]{true,true,true});
    model = new EventTableModel<Dien>(filterList, tableFormat);
    model.addTableModelListener(new TableModelListener() {
        public void tableChanged(TableModelEvent e) {
            if(e.getType()==TableModelEvent.UPDATE){
                if(!panel.isPendingChanges())
                    panel.setPendingChange(true);
            }
        }
    });
    selectionModel = new EventSelectionModel<Dien>(filterList);

    table.setSelectionModel(selectionModel);
    table.setModel(model);

}

最佳答案

内置于GlazedLists的Undo / Redo类没有公共构造函数。而是通过UndoRedoSupport.install()静态方法将支持安装到特定事件列表。

当然,如果您使用的是Swing,则可以利用Swing的UndoManager类,而GlazedLists提供了一个带有UndoSupport类的简单包装器。同样,这只是用其install()方法初始化。

我创建了一个简单的Swing示例应用程序作为示例,以说明如何使用这些类。在我的示例中,我使用了一个简单的EventList字符串和一个JList。但这将应用于所有GlazedList支持的组件-UndoRedoSupport应用于EventList本身,而不是Swing组件。

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventListModel;
import ca.odell.glazedlists.swing.UndoSupport;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.undo.UndoManager;


public class GlazedListsUndoSample {

    private JFrame mainFrame;
    private JButton addItemButton;
    private JButton undoButton;
    private JButton redoButton;
    private UndoManager undoManager;

    private EventList<String> eventList = new BasicEventList<String>();

    public GlazedListsUndoSample() {
        //populateAvailableBooks();
        createGui();
        mainFrame.setVisible(true);

    }

    private void updateButtons() {
        //addBookButton.setEnabled(!books.isEmpty());
        undoButton.setEnabled(undoManager.canUndo());
        redoButton.setEnabled(undoManager.canRedo());
    }

    private void createGui() {
        undoManager = new UndoManager();
        UndoSupport.install(undoManager, eventList);

        mainFrame = new JFrame("GlazedLists Undo Example");
        mainFrame.setSize(600, 400);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        EventListModel model = new EventListModel(eventList);
        JList list = new JList(model);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JScrollPane(list), BorderLayout.CENTER);

        JPanel addBookPanel = new JPanel(new BorderLayout());
        addBookPanel.add(new JLabel("Item"), BorderLayout.WEST);
        final JTextField titleTextField = new JTextField(50);
        addBookPanel.add(titleTextField, BorderLayout.CENTER);

        addItemButton = new JButton("Add Item");
        addItemButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                eventList.add(titleTextField.getText());

                updateButtons();
            }
        });
        addBookPanel.add(addItemButton, BorderLayout.EAST);


        JPanel buttonPanel = new JPanel();

        undoButton = new JButton("Undo");
        undoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (undoManager.canUndo()) {
                    undoManager.undo();
                }

                updateButtons();
            }
        });

        redoButton = new JButton("Redo");
        redoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (undoManager.canRedo()) {
                    undoManager.redo();
                }

                updateButtons();
            }
        });

        updateButtons();

        buttonPanel.add(undoButton);
        buttonPanel.add(redoButton);

        mainPanel.add(addBookPanel, BorderLayout.NORTH);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
              new GlazedListsUndoSample();
            }
        });
    }
}


值得注意的是documentation确实暗示了其功能限制:


  并非ListEvent中描述的每个更改都会导致无法撤消的编辑。
  具体来说,列表元素IN PLACE的突变不会产生
  不可撤消的修改。例如,观察到的ObservableElementList
  元素的更改,或使用相同的对List.set(int,E)的调用
  该索引处的对象会产生一个没有
  对应的UndoRedoSupport.Edit对象。这些ListEvent是
  被忽略,因为他们缺乏足够的信息来撤消或重做
  更改。
  
  通常,UndoRedoSupport仅适用于与
  BasicEventList或围绕BasicEventList的琐碎包装器
  不影响元素的顺序或类型,例如
  ObservableElementList。高级转换,例如SortedList或
  FilterList将无法与此UndoRedoSupport类一起正常工作
  因为它们的内容受外部信息的控制
  自己(比较器和匹配器)。

10-08 07:13