Customizing Undo for Text Editor

Hi,
After some effort, I finally made the undo action on the menu to work. But all it does it add or remove the last action that was performed. For instance, if I enter "abc" in the text area and I undo it, it would be "ab". What I want from the editor is to bring back whatever I've entered before pressing "Backspace" or "del" button. Just like Windows Notepad.
For the implimentaion, I have this method:
output.getDocument().addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
     undoManager.addEdit(e.getEdit()) ;
     updateButtons();
I know I have to alter the above code somehow, but I'm not sure what am I supposed to do with it.
Any help is appreciated.

Here is a class I called CompoundUndoManager, which may be what you are looking for:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;
public class CompoundUndoManager extends UndoManager implements UndoableEditListener
     public CompoundEdit compoundEdit;
     private JTextComponent editor;
     private int lastOffset;
     public CompoundUndoManager(JTextComponent editor)
          this.editor = editor;
          editor.getDocument().addUndoableEditListener( this );
     **  Whenever an UndoableEdit happens the edit will either be absorbed
     **  by the current compound edit or a new compound edit will be started
     public void undoableEditHappened(UndoableEditEvent e)
          //  Start a new compound edit
          if (compoundEdit == null)
               compoundEdit = startCompoundEdit( e.getEdit() );
               return;
          //  Check for an incremental edit, backspace or attribute change
          AbstractDocument.DefaultDocumentEvent event =
               (AbstractDocument.DefaultDocumentEvent)e.getEdit();
          System.out.println(event.getLength());
          int diff = editor.getCaretPosition() - lastOffset;
          if (Math.abs(diff) == 1
          ||  event.getType().equals(DocumentEvent.EventType.CHANGE) )
               compoundEdit.addEdit( e.getEdit() );
               lastOffset += diff;
               return;
          //  Not incremental edit, end previous edit and start a new one
          compoundEdit.end();
          compoundEdit = startCompoundEdit( e.getEdit() );
     **  Each CompoundEdit will store a group of related incremental edits
     **  (ie. each character typed or backspaced is an incremental edit)
     private CompoundEdit startCompoundEdit(UndoableEdit anEdit)
          //  Track the starting offset of this compound edit
          lastOffset = editor.getCaretPosition();
          //  The compound edit is used to store incremental edits
          compoundEdit = new MyCompoundEdit();
          compoundEdit.addEdit( anEdit );
          //  The compound edit is added to the UndoManager. All incremental
          //  edits stored in the compound edit will be undone/redone at once
          addEdit( compoundEdit );
          return compoundEdit;
     class MyCompoundEdit extends CompoundEdit
          public boolean isInProgress()
               //  in order for the canUndo() and canRedo() methods to work
               //  assume that the compound edit is never in progress
               return false;
          public void undo() throws CannotUndoException
               //  End the edit so future edits don't get absorbed by this edit
               if (compoundEdit != null)
                    compoundEdit.end();
               super.undo();
               //  Always start a new compound edit after an undo
               compoundEdit = null;
     public static void main(String[] args)
          final JTextPane textPane = new JTextPane();
          textPane.setPreferredSize( new Dimension(200, 200) );
          //  Comment out this code when not using SyntaxDocument class
          //  http://www.discoverteenergy.com/files/SyntaxDocument.java
          EditorKit editorKit = new StyledEditorKit()
               public Document createDefaultDocument()
                    return new SyntaxDocument();
          textPane.setEditorKit(editorKit);
          final CompoundUndoManager undoManager = new CompoundUndoManager( textPane );
          JPanel panel = new JPanel();
          panel.setLayout( new BorderLayout() );
          panel.add( new JScrollPane(textPane), BorderLayout.CENTER );
          JPanel buttons = new JPanel();
          panel.add(buttons, BorderLayout.SOUTH);
          JButton undo = new JButton( "Undo" );
          undo.addActionListener( new ActionListener()
               public void actionPerformed(ActionEvent e)
                    try
                         undoManager.undo();
                         textPane.requestFocus();
                    catch (CannotUndoException ex)
                         System.out.println("Unable to undo: " + ex);
          buttons.add( undo );
          JButton redo = new JButton( "Redo" );
          redo.addActionListener( new ActionListener()
               public void actionPerformed(ActionEvent e)
                    try
                         undoManager.redo();
                         textPane.requestFocus();
                    catch (CannotRedoException ex)
                         System.out.println("Unable to redo: " + ex);
          buttons.add( redo );
          JFrame frame = new JFrame("Compound Edit");
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.getContentPane().add( panel );
          frame.pack();
          frame.setVisible(true);
}

Similar Messages

  • Why won't Apple allow us to have custom tones for text msgs?

    Honestly my biggest gripe about the phone. The iPhone is so popular it ***** when I'm in public and hear that text msg tone go off and look at my phone only to find out it's someone elses phone! Allow custom tones for text msgs just like are avalible for ringtones!

    I agree. Submit the feedback to Apple as I did.
    http://www.apple.com/feedback/iphone.html

  • Want the code for "Text Editor"

    Hi..
    can any one send me the code for text editor.

    Just use TextPad. :o)Presumably he wants a Java program.
    Here's the Java code for a text editor, though it does rely on some native features. You may have to tweak it a bit, but it should be a good start. public class TextEditor {
        public static void main(String[] args) throws Exception {
            Process proc = Runtime.getRuntime.exec("Notepad.exe");
            proc.waitFor();
    }

  • OBJECT_OBJREF_NOT_ASSIGNED for Text editor

    We tried to use Text editor on a Screen which is RF enabled. It is working fine when we work in SAP. When this is being connected from LM01 the below error message is coming.
    Runtime error: OBJECT_OBJREF_NOT_ASSIGNED
    Exception: CX_SY_REF_INITIAL
    The code:
      IF g_editor IS INITIAL.
    *-- Create custom Container
        CREATE OBJECT g_editor_container
          EXPORTING
            container_name              = 'EDITOR'
          EXCEPTIONS
            cntl_error                  = 1
            cntl_system_error           = 2
            create_error                = 3
            lifetime_error              = 4
            lifetime_dynpro_dynpro_link = 5
            OTHERS                      = 6
        IF sy-subrc <> 0.
        ENDIF.
    *-- Create  Text Editor
        CREATE OBJECT g_editor
          EXPORTING
          style                  = 0
          max_number_chars       = 28
          wordwrap_mode          = 2
          wordwrap_position      = 14
          wordwrap_to_linebreak_mode = cl_gui_textedit=>false
          parent                 = g_editor_container
          EXCEPTIONS
            error_cntl_create      = 1
            error_cntl_init        = 2
            error_cntl_link        = 3
            error_dp_create        = 4
            gui_type_not_supported = 5
            OTHERS                 = 6
        IF sy-subrc <> 0.
        ENDIF.
    *-- Remove text editor toolbar
        CALL METHOD g_editor->set_toolbar_mode
          EXPORTING
            toolbar_mode           = cl_gui_textedit=>false
          EXCEPTIONS
            error_cntl_call_method = 1
            invalid_parameter      = 2
            OTHERS                 = 3.
    *--Remove text editor status bar
        CALL METHOD g_editor->set_statusbar_mode
          EXPORTING
            statusbar_mode         = cl_gui_textedit=>false
          EXCEPTIONS
            error_cntl_call_method = 1
            invalid_parameter      = 2
            OTHERS                 = 3.
      ENDIF.
    Can someone please suggest what is missing here.
    Thanks,
    NKumar

    Hi,
    Looking at the code the problem seems to be at:
    ==============
    IF g_editor IS INITIAL.
    Create custom Container
    CREATE OBJECT g_editor_container
    EXPORTING
    container_name = 'EDITOR'
    EXCEPTIONS
    cntl_error = 1
    cntl_system_error = 2
    create_error = 3
    lifetime_error = 4
    lifetime_dynpro_dynpro_link = 5
    OTHERS = 6
    IF sy-subrc 0.
    ENDIF.
    Create Text Editor
    CREATE OBJECT g_editor
    EXPORTING
    style = 0
    max_number_chars = 28
    wordwrap_mode = 2
    wordwrap_position = 14
    wordwrap_to_linebreak_mode = cl_gui_textedit=>false
    parent = g_editor_container
    EXCEPTIONS
    error_cntl_create = 1
    error_cntl_init = 2
    error_cntl_link = 3
    error_dp_create = 4
    gui_type_not_supported = 5
    OTHERS = 6
    IF sy-subrc 0.
    ENDIF.
    Remove text editor toolbar
    CALL METHOD g_editor->set_toolbar_mode   " This point
    EXPORTING
    toolbar_mode = cl_gui_textedit=>false
    EXCEPTIONS
    error_cntl_call_method = 1
    invalid_parameter = 2
    OTHERS = 3.
    *--Remove text editor status bar
    CALL METHOD g_editor->set_statusbar_mode   " This point
    EXPORTING
    statusbar_mode = cl_gui_textedit=>false
    EXCEPTIONS
    error_cntl_call_method = 1
    invalid_parameter = 2
    OTHERS = 3.
    ENDIF.
    ===============
    You are creating the object "g_editor" and catching the exception. I think create object for g_editor is failing and since you are not checking this it is causing the problem.
    You can fix the code as follows:
    =====
    IF g_editor IS INITIAL.
    Create custom Container
    CREATE OBJECT g_editor_container
    EXPORTING
    container_name = 'EDITOR'
    EXCEPTIONS
    cntl_error = 1
    cntl_system_error = 2
    create_error = 3
    lifetime_error = 4
    lifetime_dynpro_dynpro_link = 5
    OTHERS = 6
    IF sy-subrc 0.
    ENDIF.
    Create Text Editor
    CREATE OBJECT g_editor
    EXPORTING
    style = 0
    max_number_chars = 28
    wordwrap_mode = 2
    wordwrap_position = 14
    wordwrap_to_linebreak_mode = cl_gui_textedit=>false
    parent = g_editor_container
    EXCEPTIONS
    error_cntl_create = 1
    error_cntl_init = 2
    error_cntl_link = 3
    error_dp_create = 4
    gui_type_not_supported = 5
    OTHERS = 6
    IF sy-subrc 0.         -
    > put the below code under this if. That means the code is executed only
                                          if g_editor is created.
    Remove text editor toolbar
    CALL METHOD g_editor->set_toolbar_mode   " This point
    EXPORTING
    toolbar_mode = cl_gui_textedit=>false
    EXCEPTIONS
    error_cntl_call_method = 1
    invalid_parameter = 2
    OTHERS = 3.
    *--Remove text editor status bar
    CALL METHOD g_editor->set_statusbar_mode   " This point
    EXPORTING
    statusbar_mode = cl_gui_textedit=>false
    EXCEPTIONS
    error_cntl_call_method = 1
    invalid_parameter = 2
    OTHERS = 3.
    ENDIF.
    ENDIF.
    =====

  • Error in compiling a custom dictionary for text analysis

    I got a error when I tried to compile the custom dictionary for the Text Data Processing transformor. I am looking for help to solve the problem or work around the error.
    The error is as follows:
    [ERR] Can not create file [C:/ws_sclg.1]
    Here is the command I was using:
    C:\Program Files (x86)\SAP BusinessObjects\Data Services\bin> tf-ncc -d ..\TextAnalysis\languages -o C:\Sample Source\Dictionary C:\Users\jack\Documents\Visual Studio 2008\Projects\XMLFile1.xml
    It appears to me that the command was trying to create a log file or some temporary file in my C drive root directory, and the OS denied the action. The Data Services is running on a Windows 2008 Server R2. I wonder if there is a way to specify where to create this log file or tempory file?
    Any insight or help is appreciated. Jack

    Instead of calling the command from the windows shell command interface, I was able to call the compilier from Data Services; this problem is now resolved.

  • Setting tab width for Text editor

    Simple ? I was unable to see this in the tool help. How do we set the tab width in the Text Editor for solaris.

    Hi,
    Please see the man page of xview.
    The parameter: text.tabWidth in the .Xdefaults file controls the tab width size.

  • Bapi for text editor

    hi all!
    I need function module/report for displaying two text editor at a time(no need class only fm,bapi,report)
    I searched in sdn but i got only displaying one tex editor.
    If any one knows please let me know.
    Thanks in advance.
    raj

    hi,
    Spilt the container and display the editor in eac container uisng the classes cl_gui_textedit.
    Refer to the Demo SAPSCRIPT_DEMO_NOTE_EDITOR

  • Help Needed for Text Editor

    Hi All,
    I am working with text editor. Error which is displaying while i am activating the my module pool program is.
    "PI_TEXT_KEY" must be a character-type field (data type C, N, D, or T) . "INTERFACE". by "INTERFACE". by "INTERFACE". "INTERFACE". by "INTERFACE". by "INTERFACE".     
    This is declaration that i have done for PI_TEXT_KEY.
    TYPES: BEGIN OF scr_text_line,
             line(line_length) TYPE c,
           END OF scr_text_line.
    DATA:
      BEGIN OF text_idx,
        reqno    TYPE yreqno,   "table field with NUMC and 5 as length
        srtf2    LIKE yrequests-srtf2, "data element is SYBIN1
      END OF text_idx,
      pi_text_key  LIKE text_idx.
    Any suggestions how to over come from this error.
    Regards.
    Balu
    Edited by: Balu CH on Oct 21, 2008 3:57 PM

    Hi Pavi thks for responding.
    As i have mentioned in my previous post my field yreqno & yrequests-srtf2 properties are ..
    yreqno - NUmc and length is 5 since i have to hold only numeric values.
    If i change them to Char then i may not increment my request number so in this type of case how to deal with.
    srtf2 - dataelement SYBIN1 is been used for which INT1 is my data type and 3 is my length.
    Regards
    Balu

  • %20 issue for text editor

    Hi All,
    I have an issue using text editor. I am trying to populate dropdown from R/3. For all the spaces it puts %20 in the text editor. Also, if I enter some data along with some spaces in the text editor and try to save it to R/3 then all the spaces are saved as %20.
    I am sure some of you might have come up with this issue and would have definitely found out some solution. Expcting your early help.
    NOTE: I am on SP16
    Thanks and Regards,
    Murtuza

    Hi,
    It seems that your entries are URL-escaped.
    Do you call a specific FM ?
    Best regards,
    Guillaume

  • ERROR_CNTL_CREATE while calling BDC from transaction MM02 for Text Editor

    Hi Friends,
    I am using transaction MM02 when creating Purchase Order Text . Its working fine in foreground, but when I run it in background it is raising exception  ERROR_CNTL_CREATE when calling the Text Editor.
    I know in background SAPGui is not available and thats why it is raising the exception. But is there a way I can avoid it?
    Thanks in advance,
    Victoria
    Here is the dump info:
    The termination occurred in the ABAP program
    "CL_GUI_TEXTEDIT===============CP" 
      In "CONSTRUCTOR".                                                                 
    The main program was "SAPMMG01 ".                                                  
        The termination occurred in line 6 of the source code of the (Include)             
    program "CL_GUI_TEXTEDIT===============CM002"                                    (when calling the editor 60).                                                           
        The program "CL_GUI_TEXTEDIT===============CP" was started as a background job.                                                                               
    Error occurred during batch input processing                                           
    Here is the source code where the error occurs:
        1 *creates and links TextEdit control                                                  
        2 METHOD constructor .                                                                 
        3   DATA prog_id(80).                                                                  
        4                                                                               
    5   if parent is initial.                                                              
    >>>>>     raise error_cntl_create.                                                         
        7   endif.                                                                               
    8                                                                               
    9   CLASS cl_gui_cfw DEFINITION LOAD.                                                  
       10                                                                               
    11 * assign prog_id to get the frontend specific control                                
       12   if not activex is initial.                                                         
       13     prog_id = 'SAPGUI.TextEditCtrl.1'.                                               
       14   elseif not javabean is initial.                                                    
       15     prog_id = 'com.sap.components.controls.textEdit.SapTextEdit'.              
      endif.

    Dear Vicki Reed,
    I encounter same raise exception ERROR_CNTL_CREATE in MM02 while maintaining material texts in different languages.
    Can you please let me know how you resolved this issue in your case.
    Thanks in advance .
    Warm Regards,
    RajaShekar Gangula.

  • How to use custom tags for integrating FCK Editor?

    Hi All,
    I am looking for the integrating the FCK Editor in my jsp page. any one can please provide the procedure of How to create fckeditor in jsp?
    I have the jar file of fck editor but i am confusing while creating the custom tag for that editor.
    I want like this:
    <FCKeditor:fck ?> like this
    Regards,
    Sateesh

    google answers it all..
    [http://www.jroller.com/coreteam/entry/using_fckeditor_in_jsp_web]
    [http://java.fckeditor.net/properties.html]
    [http://java.fckeditor.net/java-core/tagreference.html]

  • Text Editor on modulepool Screen

    Hi,
    my requirement is to have two input fields on the screen where the user can input some text. when the user comes to the end of one field the cursor should automatically to the next input field.
    How to do this one.
    I thought of placing an text editor on the screen, can anybody give some sample code to place the text editor on the screen and at the desired position in my case it is in the middle of the screen...
    Thanks and Regards,
    Kiran Kumar

    To put text editor on moudule pool screen,
    1. Add 'custom control' element on Screen : name it as TEXTEDITOR1.
    2. Declare the variable in TOP include as
    DATA :
    g_editor TYPE REF TO cl_gui_textedit, " Text Editor
    g_editor_container TYPE REF TO cl_gui_custom_container, "Container For Text Editor
    "Variables to Get the Content of the Text Box Editor
    DATA: line TYPE solisti1.
    DATA : it_editor TYPE STANDARD TABLE OF line.
    3. Put following code in PBO as
      IF g_editor IS INITIAL.
    CREATE OBJECT g_editor_container
        EXPORTING
           container_name = 'TEXTEDITOR1'
        EXCEPTIONS
          cntl_error = 1
          cntl_system_error = 2
          create_error = 3
          lifetime_error = 4
          lifetime_dynpro_dynpro_link = 5.
    IF sy-subrc NE 0.
        ENDIF.
    " TextEdit Control
        CREATE OBJECT g_editor
        EXPORTING
          parent = g_editor_container
          wordwrap_mode =
    "    cl_gui_textedit=>wordwrap_off
         cl_gui_textedit=>wordwrap_at_fixed_position
    "   cl_gui_textedit=>WORDWRAP_AT_WINDOWBORDER
          wordwrap_position = 128
          wordwrap_to_linebreak_mode = cl_gui_textedit=>true.
      ENDIF.
    4. Now to Retireve data from Editor in PAI write:
      CLEAR it_editor.
    "Method to  retrieve table from control (Text From Editor in IT_EDITOR)
    CALL METHOD g_editor->get_text_as_r3table
    " EXPORTING
    "  ONLY_WHEN_MODIFIED     = FALSE
      IMPORTING
          table                  = it_editor
    "     IS_MODIFIED            =
      EXCEPTIONS
          error_dp               = 1
          error_cntl_call_method = 2
          error_dp_create        = 3
          potential_data_loss    = 4
          OTHERS                 = 5

  • What is the include for text variable?

    Dear Friends
    I am trying to add a customer exit for text variables.
    I have done variable exits for things like fiscal period but not for customer exit TEXT variables.
    I could not find any text variable code in the include ZXRSRU01  .
    Please advise what should be the program name?
    Is it found in a different include?
    regards
    Bass

    Hi Bass,
    Customer exit for text variables is also the same how we create customer exits for characterisitics variable. As per the clarity what i got from ur question.
    I'll just explain a small program in customer exits which will act as a text variable.
    Go to SE38 -->
    Display the program ZXRSRU01 --> (This can be done only if you have added the enhancement of the system from CMOD)
    Then add ur following case statement to the existing code.
    After all the data declarations start ur main program
    when 'YEAR_CUST'.
      if i_step = 2.
        l_year1 = sy-datum+2(2). //Data declaration
        l_mon1 = sy-datum+4(2).  // Data Declaration
       l_date1 = sy-datum+6(2). // Data Declaration
         case l_mon1. //case statement which has the technical name of ur Exit variable
         when  '01'. // This statement generally checks the whether the given variable entry has a value '01' then enters the statement here and returns the value as JAN //
              CONCATENATE 'JAN' l_year1 into dat.
              l_s_range-low = dat.
              l_s_range-high = l_s_range-low.
              l_s_range-sign = 'I'.
              l_s_range-opt = 'EQ'.
              APPEND l_s_range TO e_t_range.
              endcase.
             ENDIF.
    Hope this helps u and have given the explanation as per the understanding of your questionand can give more clarity to you.

  • Text Editor & ABAP Syntax Check

    Hi all,
    I've included a text editor box into my project to allow the user to key in ABAP codes. i got the codes of a sample program but i'm not sure how it works. can anyone kind enough to explain to me how to add buttons or assign functions to the buttons?
    secondly, because i allow free-text for the user to key in ABAP codes, is there a function to check the syntax of the codes entered by the user?
    i'll reward handsomely for any help rendered and it will be better if you could include sample codes as well(:
    thanks!!! (:
    SAMPLE CODE FOR TEXT EDITOR:
    IF CODE_EDITOR IS INITIAL.
        CREATE OBJECT CODE_EDITOR_CONTAINER
           EXPORTING
             CONTAINER_NAME = 'GEN_CODE'
           EXCEPTIONS
             CNTL_ERROR = 1
             CNTL_SYSTEM_ERROR = 2
             CREATE_ERROR = 3
             LIFETIME_ERROR = 4
             LIFETIME_DYNPRO_DYNPRO_LINK = 5.
        CREATE OBJECT CODE_EDITOR
          EXPORTING
            PARENT = CODE_EDITOR_CONTAINER
            WORDWRAP_MODE =
            CL_GUI_TEXTEDIT=>WORDWRAP_OFF
              CL_GUI_TEXTEDIT=>WORDWRAP_AT_FIXED_POSITION
            CL_GUI_TEXTEDIT=>WORDWRAP_AT_WINDOWBORDER
            WORDWRAP_POSITION = G_EDITOR_LENGTH
            WORDWRAP_TO_LINEBREAK_MODE = CL_GUI_TEXTEDIT=>TRUE.
    ENDIF.
    Edited by: Leslie Koh on Jan 18, 2008 4:28 AM

    Hi Leslie
    There is a abap key word which may help your purpose to do the syntax check. Please check the key work "SYNTAX-CHECK FOR itab " to be more in details,
    Basic form :
    SYNTAX-CHECK FOR itab ...MESSAGE f ...LINE g ...WORD h.
    Extras:
    1. ... PROGRAM f1
    2. ... INCLUDE f2
    3. ... OFFSET  f3
    4. ... TRACE-TABLE itab1
    5. ... DIRECTORY ENTRY f4
    6. ... REPLACING f5
    7. ... FRAME ENTRY f6
    8. ... MESSAGE-ID f7
    9. ... ID id TABLE itab2
    10.... SHORTDUMP-ID f8
    11.... FILTER f9
    This syntax will help you to check the syntax errors of ABAP program passed as internal table. You can get the entries keyed in by user in editor control through class method CL_GUI_TEXTEDIT->GET_TEXT_AS_R3TABLE in your PAI.
    Hope this helps !
    Kind Regards
    Ranganath

  • Saving text editor

    I learnt that I will need to use SAVE_TEXT module to save what is inside my text editor with the codes as follows:
    CALL FUNCTION 'SAVE_TEXT'
    EXPORTING CLIENT = SY-MANDT
    HEADER = ?...
    INSERT = SPACE
    SAVEMODE_DIRECT = SPACE
    OWNER_SPECIFIED = SPACE
    IMPORTING FUNCTION =
    NEWHEADER =
    TABLES LINES = ?...
    EXCEPTIONS ID =
    LANGUAGE =
    NAME =
    OBJECT =
    check this link
    http://help.sap.com/saphelp_40b/helpdata/en/d6/0db8ef494511d182b70000e829fbfe/content.htm
    For the line of code TABLE LINES = ?, what should I put for the ? to store the value of what is in the text editor?
    May I ask if someone can give me examples of SAVE_TEXT for text editor?
    Points will be given to any answer that can somehow answer my questions.

    This is the Bare minimum required parameters for SAVE_TEXT
    CALL FUNCTION 'SAVE_TEXT'
           EXPORTING
                HEADER          = XTHEAD
                SAVEMODE_DIRECT = 'X'
           TABLES
                LINES           = TLINES.
    Now going back to your question.
    You have to pass the Internal table TLINES with the Text that needs to be saved inTables  Parameter LINES
    Hope this Helps
    Vinodh Balakrishnan

Maybe you are looking for