C
Reaction score
0

Profile posts Postings About

  • Yo carrotlord, I gotta level with you dawg. you've been illin' on homies for a while now and people's starting to take notice. Chillax a bit and have some respect G, don't give hate a chance.
    It's the 4th already! Why have elections not finished yet?
    C
    Carrotlord
    They are done. I just didn't say anything.
    Tpcool
    Tpcool
    The suspense is killing me!!
    S
    Shane
    I'm still alive, I'm too busy to get killed by suspense.

    <div class='bbc_spoiler'>
    <span class='spoiler_title'>Spoiler</span> <input type='button' class='bbc_spoiler_show' value='Show' />
    <div class='bbc_spoiler_wrapper'><div class='bbc_spoiler_content' style="display:none;"><div class='mirai_spoiler_wrapper'>And that's my lame attempt at a joke. :/ </div></div></div>
    </div>
    Okay, uh... I have a working model of undo/redo in a text editor and the source code.

    I'll give you the details behind the code as long as you import the right classes and such.

    Code:
        private UndoManagerTeam undoManageTeam = new UndoManagerTeam();
        private UndoAction undoActionObj = new UndoAction();
        private RedoAction redoActionObj = new RedoAction();
        private JMenuItem undoItem;
        private JMenuItem redoItem;

    Those were some of the fields, i.e. class variables, of my text editor GUI. There is a menu item for undo, a menu item for redo (which are attached to Ctrl+Z and Ctrl+Y respectively), an undo object, a redo object, and a team of undoManager objects.

    The only reason I have a "team" of undo managers instead of just one is that each text edit tab requires its own undo/redo capabilities. I assume you want the same feature for your TSC editor or whatever you're building.

    Code:
    private class UndoManagerTeam {
        private ArrayList<UndoManager> undoManageTeam =
                                       new ArrayList<UndoManager>();
        
        private void addManager() {
            undoManageTeam.add(new UndoManager());
        }
        
        private void removeManager(int index) {
            undoManageTeam.remove(index);
        }
        
        private UndoManager getCurrentManager() {
            int currentIndex = tabManageObj.getCurrentTabIndex();
            return undoManageTeam.get(currentIndex);
        }
    }

    The above code is the code for the undo manager team. It's just an abstraction for an ArrayList. UndoManager is a built-in java class.

    Code:
    private class CustomUndoableEditListener implements UndoableEditListener {
        @Override
        public void undoableEditHappened(UndoableEditEvent event) {
            undoManageTeam.getCurrentManager().addEdit(event.getEdit());
        }
    }
    
    private class UndoAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent event) {
            try {
                UndoManager currentManager = undoManageTeam.getCurrentManager();
                if (currentManager.canUndo())
                    currentManager.undo();
            } catch (CannotUndoException exceptionInfo) {
                //Ignore the exception.
            }
        }
    }
    
    private class RedoAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent event) {
            try {
                UndoManager currentManager = undoManageTeam.getCurrentManager();
                if (currentManager.canRedo())
                    currentManager.redo();
            } catch (CannotRedoException exceptionInfo) {
                //Ignore the exception.
            }
        }
    }

    The above code for RedoAction, UndoAction, and CustomUndoableEditListener are the basis for your undo/redo stuff.

    Code:
        private ArrayList<CustomUndoableEditListener> editListeners =
                new ArrayList<CustomUndoableEditListener>();
    
        private void addTab() {
            JScrollPane newScrollPane = new JScrollPane();
            JTextPane newTextPane = new JTextPane();
            newScrollPane.setViewportView(newTextPane);
            textPaneList.add(newTextPane);
            tabList.add(newScrollPane);
            mainTabbedPane.addTab("New File",newScrollPane);
            filesBeingEdited.add("");
            undoManageTeam.addManager();
            editListeners.add(new [COLOR="Red"][B]CustomUndoableEditListener[/B][/COLOR]());
            addPropertiesToTextPane(newTextPane);
            switchToLastTab();
        }

    Finally, whenever a new tab is created by the user, an new CustomUndoableEditListener must be created for that tab.

    I'm sorry if that made little sense. I copy-pasta'd most of the undo/redo code from the Java tutorials and made modifications so that it'd work for multiple tabs. I got my information here: http://download.oracle.com/javase/tutorial/uiswing/components/generaltext.html#undo

    So yeah. Feel free to bombard me with additional questions. The undo/redo stuff is not just limited to text, so you could use it to undo CS map editing actions and emulate the other stuff you see in CaveEditor.
  • Loading…
  • Loading…
Top