Edit : please, have a look at the end of the post...
To begin, please excuse me for my bad english...
I'm trying to make a text editor in Java, using a JTabbedPane to edit multiple files. I have found no good help on the Internet to use UndoManager in that case.
So I tried something but it doesn't seem to work.
In fact, I tried to save all UndoableEdit by listening when I change tab. When I change tab, I save the UndoableEdits for the previously opened tab, then I clean the edits of the UndoManager and finaly, I restore the UndoableEdits of the new active tab.
The problem I have is that when I call the function that updates the undo/redo button state, the canUndo()/canRedo() methods always return false, but there are edits in the list... And I don't know why this happens, I don't understand what I forgot to do...
Obviously, however present the UndoableEdits are present in the "edits" Vector, UndoManager doesn't care of this. When I tried to force the buttons to be enabled, an exception throws... (javax.swing.undo.CannotUndoException / javax.swing.undo.CannotRedoException)
Could you help me please ?
Don't hesitate to ask me for further information if you need it.
Thank you.
My extension of the UndoManager class :
public class UndoManagerPerso extends UndoManager
{
private static final long serialVersionUID = 8386145389216281754L;
public ArrayList<UndoableEdit> getEdits()
{
ArrayList<UndoableEdit> listEdits = new ArrayList<UndoableEdit>();
listEdits.addAll(this.edits);
return listEdits;
}
public void setEdits(ArrayList<UndoableEdit> listEdits)
{
for(UndoableEdit ue:listEdits)
{
this.addEdit(ue);
}
}
}
The call of these functions, in my main window's class :
tabbedPane_editeur.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e)
{
if (e.getSource() instanceof JTabbedPane)
{
JTabbedPane pane = (JTabbedPane) e.getSource();
int previousTab = f_principale.this.activeTab;
int activeTab = pane.getSelectedIndex();
System.out.println("Selected paneNo : " + pane.getSelectedIndex() + ", précédent : " + previousTab);
f_principale.this.listEditeurs.get(previousTab).setListEdits( f_principale.this.undoManager.getEdits());
f_principale.this.undoManager.setEdits(f_principale.this.listEditeurs.get(activeTab).getListEdits());
f_principale.this.activeTab = activeTab;
f_principale.this.updateButtons();
}
}
});
public void updateButtons()
{
this.btnUndo.setText(undoManager.getUndoPresentationName());
this.btnRedo.setText(undoManager.getRedoPresentationName());
this.btnUndo.setEnabled(undoManager.canUndo());//returns false
this.btnRedo.setEnabled(undoManager.canRedo());//returns false
}
Edit : (1 day later) I have tested the UndoManader again today, and I noticed something. In fact, insert manually the UndoableEdit works ! But the number of edits increases too quickly to be normal...
I reach the default limit (100) quickly and that's why I thought it wasn't working at all yesterday : when the limit is reached, canUndo() / canRedo() seem to return false and when the button are forced to be enabled, exceptions are thrown.
Maybe when I switch tab, something wrong happens... But what ? Do you have any idea ? I'm going to have a look at this...
You should create a separate UndoManager for each Tab.