Search in sources :

Example 6 with ExtendedModifyListener

use of org.eclipse.swt.custom.ExtendedModifyListener in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_ExtendedModifyListener method test_modifyTextLorg_eclipse_swt_custom_ExtendedModifyEvent.

@Test
public void test_modifyTextLorg_eclipse_swt_custom_ExtendedModifyEvent() {
    ExtendedModifyListener listener = event -> {
        switch(verify) {
            case 1:
                {
                    assertTrue(":1a:", event.start == 0);
                    assertTrue(":1b:", event.length == 1);
                    assertTrue(":1c:", event.replacedText.isEmpty());
                    break;
                }
            case 2:
                {
                    assertTrue(":2a:", event.start == 0);
                    assertTrue(":2b:", event.length == 1);
                    assertTrue(":2c:", event.replacedText.equals("\n\n"));
                    break;
                }
            case 3:
                {
                    assertTrue(":3a:", event.start == 0);
                    assertTrue(":3b:", event.length == 2);
                    assertTrue(":3c:", event.replacedText.equals("a"));
                    break;
                }
            case 4:
                {
                    assertTrue(":4:", false);
                    break;
                }
            case 5:
                {
                    assertTrue(":5a:", event.start == 0);
                    assertTrue(":5b:", event.length == 1);
                    assertTrue(":5c:", event.replacedText.isEmpty());
                    break;
                }
            case 6:
                {
                    assertTrue(":6a:", event.start == 21);
                    assertTrue(":6b:", event.length == 0);
                    assertTrue(":6c:", event.replacedText.equals("\nline 4"));
                    break;
                }
            case 7:
                {
                    assertTrue(":7a:", event.start == 5);
                    assertTrue(":7b:", event.length == 0);
                    assertTrue(":7c:", event.replacedText.equals("is a test"));
                    break;
                }
            case 8:
                {
                    assertTrue(":8a:", event.start == 7);
                    assertTrue(":8b:", event.length == 0);
                    assertTrue(":8c:", event.replacedText.equals("\r\n"));
                    break;
                }
            case 9:
                {
                    assertTrue(":9a:", event.start == 2);
                    assertTrue(":9b:", event.length == 4);
                    assertTrue(":9c:", event.replacedText.equals("\r\n"));
                    break;
                }
            case 10:
                {
                    assertTrue(":10:", false);
                    break;
                }
            case 11:
                {
                    assertTrue(":11:", false);
                    break;
                }
            case 12:
                {
                    assertTrue(":12a:", event.start == 3);
                    assertTrue(":12b:", event.length == 1);
                    assertTrue(":12c:", event.replacedText.isEmpty());
                    break;
                }
            case 13:
                {
                    assertTrue(":13a:", event.start == 2);
                    assertTrue(":13b:", event.length == 1);
                    assertTrue(":13c:", event.replacedText.isEmpty());
                    break;
                }
            case 14:
                {
                    assertTrue(":14:", false);
                    break;
                }
            case 15:
                {
                    assertTrue(":15a:", event.start == 2);
                    assertTrue(":15b:", event.length == 6);
                    assertTrue(":15c:", event.replacedText.equals("\r\n"));
                    break;
                }
            case 16:
                {
                    assertTrue(":16:", false);
                    break;
                }
            case 17:
                {
                    assertTrue(":17:", false);
                    break;
                }
            case 18:
                {
                    assertTrue(":18a:", event.start == 3);
                    assertTrue(":18b:", event.length == 7);
                    assertTrue(":18c:", event.replacedText.isEmpty());
                    break;
                }
            case 19:
                {
                    assertTrue(":19a:", event.start == 2);
                    assertTrue(":19b:", event.length == 7);
                    assertTrue(":19c:", event.replacedText.isEmpty());
                    break;
                }
            case 20:
                {
                    assertTrue(":20:", false);
                    break;
                }
            case 21:
                {
                    assertTrue(":21a:", event.start == 0);
                    assertTrue(":21b:", event.length == 16);
                    assertTrue(":21c:", event.replacedText.equals("L1\r\nL2\r\nL3\r\nL4\r\n"));
                    break;
                }
        }
    };
    styledText.addExtendedModifyListener(listener);
    boolean exceptionHandled = false;
    verify = 0;
    styledText.setText("testing");
    verify = 1;
    styledText.replaceTextRange(0, 0, "\n");
    verify = 0;
    styledText.setText("\n\n");
    verify = 2;
    styledText.replaceTextRange(0, 2, "a");
    verify = 0;
    styledText.setText("a");
    verify = 3;
    styledText.replaceTextRange(0, 1, "\n\n");
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 4;
    try {
        styledText.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":4: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("Line 1\r\nLine 2");
    verify = 5;
    styledText.replaceTextRange(0, 0, "\r");
    verify = 0;
    styledText.setText("This\nis a test\nline 3\nline 4");
    verify = 6;
    styledText.replaceTextRange(21, 7, "");
    verify = 0;
    styledText.setText("This\nis a test\r");
    verify = 7;
    styledText.replaceTextRange(5, 9, "");
    verify = 0;
    styledText.setText("\nL1\r\nL2\r\n");
    verify = 8;
    styledText.replaceTextRange(7, 2, "");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 9;
    styledText.replaceTextRange(2, 2, "test");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 10;
    try {
        styledText.replaceTextRange(3, 1, "");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":10: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 11;
    try {
        styledText.replaceTextRange(1, 2, "");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":11: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r");
    verify = 12;
    styledText.replaceTextRange(3, 0, "\n");
    verify = 0;
    styledText.setText("L1\n");
    verify = 13;
    styledText.replaceTextRange(2, 0, "\r");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 14;
    try {
        styledText.replaceTextRange(3, 0, "test");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":14: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 15;
    styledText.replaceTextRange(2, 2, "test\n\n");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 16;
    try {
        styledText.replaceTextRange(3, 1, "test\r\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":16: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 17;
    try {
        styledText.replaceTextRange(1, 2, "test\n\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":17: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r");
    verify = 18;
    styledText.replaceTextRange(3, 0, "\ntest\r\n");
    verify = 0;
    styledText.setText("L1\n");
    verify = 19;
    styledText.replaceTextRange(2, 0, "test\r\r\r");
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 20;
    try {
        styledText.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":20: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 21;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    styledText.removeExtendedModifyListener(listener);
}
Also used : Shell(org.eclipse.swt.widgets.Shell) StyledText(org.eclipse.swt.custom.StyledText) SWT(org.eclipse.swt.SWT) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ExtendedModifyListener(org.eclipse.swt.custom.ExtendedModifyListener) Before(org.junit.Before) ExtendedModifyListener(org.eclipse.swt.custom.ExtendedModifyListener) Test(org.junit.Test)

Example 7 with ExtendedModifyListener

use of org.eclipse.swt.custom.ExtendedModifyListener in project pentaho-kettle by pentaho.

the class StyledTextComp method addUndoRedoSupport.

// Start Functions for Undo / Redo on wSrcipt
private void addUndoRedoSupport() {
    styledText.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent event) {
            if (styledText.getSelectionCount() == styledText.getCharCount()) {
                bFullSelection = true;
                try {
                    event.wait(2);
                } catch (Exception e) {
                // Ignore errors
                }
            }
        }

        public void widgetDefaultSelected(SelectionEvent event) {
        }
    });
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {

        public void modifyText(ExtendedModifyEvent event) {
            int iEventLength = event.length;
            int iEventStartPostition = event.start;
            // Unterscheidung um welche Art es sich handelt Delete or Insert
            String newText = styledText.getText();
            String repText = event.replacedText;
            String oldText = "";
            int iEventType = -1;
            // if((event.length!=newText.length()) || newText.length()==1){
            if ((event.length != newText.length()) || (bFullSelection)) {
                if (repText != null && repText.length() > 0) {
                    oldText = newText.substring(0, event.start) + repText + newText.substring(event.start + event.length);
                    iEventType = UndoRedoStack.DELETE;
                    iEventLength = repText.length();
                } else {
                    oldText = newText.substring(0, event.start) + newText.substring(event.start + event.length);
                    iEventType = UndoRedoStack.INSERT;
                }
                if ((oldText != null && oldText.length() > 0) || (iEventStartPostition == event.length)) {
                    UndoRedoStack urs = new UndoRedoStack(iEventStartPostition, newText, oldText, iEventLength, iEventType);
                    if (undoStack.size() == MAX_STACK_SIZE) {
                        undoStack.remove(undoStack.size() - 1);
                    }
                    undoStack.add(0, urs);
                }
            }
            bFullSelection = false;
        }
    });
}
Also used : ExtendedModifyEvent(org.eclipse.swt.custom.ExtendedModifyEvent) ExtendedModifyListener(org.eclipse.swt.custom.ExtendedModifyListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) SelectionListener(org.eclipse.swt.events.SelectionListener)

Aggregations

ExtendedModifyListener (org.eclipse.swt.custom.ExtendedModifyListener)7 ExtendedModifyEvent (org.eclipse.swt.custom.ExtendedModifyEvent)5 VerifyKeyListener (org.eclipse.swt.custom.VerifyKeyListener)5 ModifyListener (org.eclipse.swt.events.ModifyListener)5 TextChangeListener (org.eclipse.swt.custom.TextChangeListener)4 KeyEvent (org.eclipse.swt.events.KeyEvent)4 ModifyEvent (org.eclipse.swt.events.ModifyEvent)4 VerifyEvent (org.eclipse.swt.events.VerifyEvent)4 StyledText (org.eclipse.swt.custom.StyledText)3 TextChangedEvent (org.eclipse.swt.custom.TextChangedEvent)3 TextChangingEvent (org.eclipse.swt.custom.TextChangingEvent)3 ControlEvent (org.eclipse.swt.events.ControlEvent)3 ControlListener (org.eclipse.swt.events.ControlListener)3 FocusEvent (org.eclipse.swt.events.FocusEvent)3 FocusListener (org.eclipse.swt.events.FocusListener)3 KeyListener (org.eclipse.swt.events.KeyListener)3 MouseEvent (org.eclipse.swt.events.MouseEvent)3 MouseListener (org.eclipse.swt.events.MouseListener)3 SelectionEvent (org.eclipse.swt.events.SelectionEvent)3 SelectionListener (org.eclipse.swt.events.SelectionListener)3