I want to do undo/redo with a JTextArea. I have found some stuff on this already but as I have a JPopupMenu that does some of the actions of the JMenuItems that also do them, and, as the JMenuItems in the JPopupMenu also apply polymorphically to other classes that aren't using undo/redo, the code can't be be put in my class that applies this JPopupMenu to those JTextComponents.
I went here. However, I'm not quite sure of how the mechanisms of this would work, especially in my case. (I think though, that, in the case of the JTextArea and the popup menu that goes with it, I can add an action listener to them via the route of getComponentPopupMenu() )
Add an undo/redo function to your Java apps with Swing - JavaWorld
How would I go about this? I still am a bit confused on how to do this and what I need to ensure that I can apply this to functions that I haven't written yet but will later, or how to do it with a replaceAll() where multiple replaces may be done at once.
Also, is it possible to have an UndoManager support multiple undos or redos? (i.e., can I make a Notepad that allows more than one undo/redo or can I, unless I make all the code myself, only be stuck with one undo/redo like the Windows Notepad does?)
I'm trying to make a PasteAction that can be undone. I don't know what to put as I can't guarantee, given multiple undos possible, that the user might not have, say, added text before the point where the paste stuff was added, hence making storing the spot where it was originally added dubious as it may not reference the actual spot anymore?
I know that for cut, I can add it back, but I'd need to know where it started and stopped the cut so that I can put it back, but, again, with multiple edits possible, it could be changed to no longer put it back in the proper place.
Also, I'm looking online and I was wondering also, can undo manager make sure that it undoes all the keys typed between a pause or the last undoable action that was added? I mean, if I typed "Undoable" in a JTextArea and then told it to undo, would it remove the whole word or sequence or words or just remove the last letter, say, making it say "Undoabl" and making the user have to undo each letter one by one (a great way to turn people off of your product!!!!)?
Preferably, I'd like to somehow make it to use the CompoundEdit class or whatever it was called to somehow make that that a single edit made of several smaller ones (the characters).
I can make a paste action that behaves like a JTextArea (JTextComponent to be more exact, but you get the point) paste action.
public class PasteAction extends AbstractAction { public void actionPerformed(ActionEvent e) { int start=ta.getSelectionStart(); String startText=ta.getText().substring(0, start); String endText=ta.getText().substring(start); String data = (String) Toolkit.getDefaultToolkit() .getSystemClipboard().getData(DataFlavor.stringFlavor); String res=startText+data+endText; ta.setText(res); } }
I do get that if I undid that, how, I don't know yet, then if I did some other action, that the redo queue might be emptied as I did something that could be undone. (Or it's something like that where it would make it so that it couldn't redo.) Assuming that I could redo the paste action after undoing it, how would I?
Am I going to need DocumentListeners to keep track to see if an action that could be undo/redone happens? (I have a feeling I probably do but thought I'd ask.)
For starters, how would I support an undo/redo of a paste action and how could I use UndoManager (or something else in java that doesn't require a third party jar, which I have trouble getting into JGrasp as I keep needing to look up how to import them) or is there nothing to support being able to store more than one undo/redo at a time?