Search in sources :

Example 6 with StyledTextContent

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

the class StyledTextContentSpec method test_Line_To_Offset.

public void test_Line_To_Offset() {
    StyledTextContent content = getContentInstance();
    content.setText("This\nis a test\rrepeat\nend\r");
    assertTrue(":1a:", content.getOffsetAtLine(0) == 0);
    assertTrue(":1b:", content.getOffsetAtLine(1) == 5);
    assertTrue(":1c:", content.getOffsetAtLine(2) == 15);
    assertTrue(":1d:", content.getOffsetAtLine(3) == 22);
    assertTrue(":1e:", content.getOffsetAtLine(4) == 26);
    content.setText("This\r\nis a test");
    assertTrue(":2a:", content.getOffsetAtLine(0) == 0);
    assertTrue(":2b:", content.getOffsetAtLine(1) == 6);
    content.setText("\r\n");
    assertTrue(":3a:", content.getOffsetAtLine(0) == 0);
    assertTrue(":3b:", content.getOffsetAtLine(1) == 2);
    content.setText("\r\n\n\r\r\n");
    assertTrue(":4a:", content.getOffsetAtLine(0) == 0);
    assertTrue(":4b:", content.getOffsetAtLine(1) == 2);
    assertTrue(":4c:", content.getOffsetAtLine(2) == 3);
    assertTrue(":4d:", content.getOffsetAtLine(3) == 4);
    assertTrue(":4e:", content.getOffsetAtLine(4) == 6);
    content.setText("\r\ntest\r\n");
    assertTrue(":5a:", content.getOffsetAtLine(0) == 0);
    assertTrue(":5b:", content.getOffsetAtLine(1) == 2);
    assertTrue(":5c:", content.getOffsetAtLine(2) == 8);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 7 with StyledTextContent

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

the class StyledTextContentSpec method test_Replace.

public void test_Replace() {
    StyledTextContent content = getContentInstance();
    String newText;
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 4, "a");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":1a:", newText.equals("This\na test\r"));
    assertTrue(":1b:", content.getLineCount() == 3);
    assertTrue(":1c:", content.getLine(0).equals("This"));
    assertTrue(":1d:", content.getLine(1).equals("a test"));
    assertTrue(":1e:", content.getLine(2).isEmpty());
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 2, "was");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":2a:", newText.equals("This\nwas a test\r"));
    assertTrue(":2b:", content.getLineCount() == 3);
    assertTrue(":2c:", content.getLine(0).equals("This"));
    assertTrue(":2d:", content.getLine(1).equals("was a test"));
    assertTrue(":2e:", content.getLine(2).isEmpty());
    content.setText("This is a test\r");
    content.replaceTextRange(5, 2, "was");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":3a:", newText.equals("This was a test\r"));
    assertTrue(":3b:", content.getLineCount() == 2);
    assertTrue(":3c:", content.getLine(0).equals("This was a test"));
    assertTrue(":3d:", content.getLineAtOffset(15) == 0);
    content.setText("Line 1\nLine 2\nLine 3");
    content.replaceTextRange(0, 7, "La\nLb\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":4a:", newText.equals("La\nLb\nLine 2\nLine 3"));
    assertTrue(":4b:", content.getLine(0).equals("La"));
    assertTrue(":4c:", content.getLine(1).equals("Lb"));
    assertTrue(":4d:", content.getLine(2).equals("Line 2"));
    assertTrue(":4e:", content.getLine(3).equals("Line 3"));
    content.setText(getTestText());
    newText = content.getTextRange(0, content.getCharCount());
    int start = content.getOffsetAtLine(6);
    int end = content.getOffsetAtLine(11);
    content.replaceTextRange(start, end - start, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":5a:", content.getLineCount() == 12);
    assertTrue(":5a:", content.getLine(5).isEmpty());
    assertTrue(":5a:", content.getLine(6).isEmpty());
    start = content.getOffsetAtLine(7);
    content.replaceTextRange(start, content.getCharCount() - start, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":5a:", content.getLineCount() == 8);
    assertTrue(":5a:", content.getLine(5).isEmpty());
    assertTrue(":5a:", content.getLine(6).isEmpty());
    assertTrue(":5a:", content.getLine(7).isEmpty());
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 8 with StyledTextContent

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

the class Test_org_eclipse_swt_custom_TextChangeListener method test_textChangingLorg_eclipse_swt_custom_TextChangingEvent.

@Test
public void test_textChangingLorg_eclipse_swt_custom_TextChangingEvent() {
    StyledTextContent content = styledText.getContent();
    TextChangeListener listener = new TextChangeListener() {

        @Override
        public void textChanging(TextChangingEvent event) {
            switch(verify) {
                case 1:
                    {
                        assertTrue(":1a:", event.replaceLineCount == 0);
                        assertTrue(":1b:", event.newLineCount == 1);
                        break;
                    }
                case 2:
                    {
                        assertTrue(":2a:", event.replaceLineCount == 2);
                        assertTrue(":2b:", event.newLineCount == 0);
                        break;
                    }
                case 3:
                    {
                        assertTrue(":3a:", event.replaceLineCount == 0);
                        assertTrue(":3b:", event.newLineCount == 2);
                        break;
                    }
                case 4:
                    {
                        assertTrue(":4:", false);
                        break;
                    }
                case 5:
                    {
                        assertTrue(":5a:", event.replaceLineCount == 0);
                        assertTrue(":5b:", event.newLineCount == 1);
                        break;
                    }
                case 6:
                    {
                        assertTrue(":6a:", event.replaceLineCount == 1);
                        assertTrue(":6b:", event.newLineCount == 0);
                        break;
                    }
                case 7:
                    {
                        assertTrue(":7a:", event.replaceLineCount == 0);
                        assertTrue(":7b:", event.newLineCount == 0);
                        break;
                    }
                case 8:
                    {
                        assertTrue(":8a:", event.replaceLineCount == 1);
                        assertTrue(":8b:", event.newLineCount == 0);
                        break;
                    }
                case 9:
                    {
                        assertTrue(":9a:", event.replaceLineCount == 1);
                        assertTrue(":9b:", event.newLineCount == 0);
                        break;
                    }
                case 10:
                    {
                        assertTrue(":10:", false);
                        break;
                    }
                case 11:
                    {
                        assertTrue(":11:", false);
                        break;
                    }
                case 12:
                    {
                        assertTrue(":12a:", event.replaceLineCount == 0);
                        assertTrue(":12b:", event.newLineCount == 1);
                        break;
                    }
                case 13:
                    {
                        assertTrue(":13a:", event.replaceLineCount == 0);
                        assertTrue(":13b:", event.newLineCount == 1);
                        break;
                    }
                case 14:
                    {
                        assertTrue(":14:", false);
                        break;
                    }
                case 15:
                    {
                        assertTrue(":15a:", event.replaceLineCount == 1);
                        assertTrue(":15b:", event.newLineCount == 2);
                        break;
                    }
                case 16:
                    {
                        assertTrue(":16:", false);
                        break;
                    }
                case 17:
                    {
                        assertTrue(":17:", false);
                        break;
                    }
                case 18:
                    {
                        assertTrue(":18a:", event.replaceLineCount == 0);
                        assertTrue(":18b:", event.newLineCount == 2);
                        break;
                    }
                case 19:
                    {
                        assertTrue(":19a:", event.replaceLineCount == 0);
                        assertTrue(":19b:", event.newLineCount == 3);
                        break;
                    }
                case 20:
                    {
                        assertTrue(":20:", false);
                        break;
                    }
            }
        }

        @Override
        public void textChanged(TextChangedEvent event) {
        }

        @Override
        public void textSet(TextChangedEvent event) {
        }
    };
    content.addTextChangeListener(listener);
    boolean exceptionHandled = false;
    verify = 1;
    styledText.setText("testing");
    styledText.replaceTextRange(0, 0, "\n");
    verify = 2;
    styledText.setText("\n\n");
    styledText.replaceTextRange(0, 2, "a");
    verify = 3;
    styledText.setText("a");
    styledText.replaceTextRange(0, 1, "\n\n");
    verify = 4;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        styledText.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":4: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 5;
    styledText.setText("Line 1\r\nLine 2");
    styledText.replaceTextRange(0, 0, "\r");
    verify = 6;
    styledText.setText("This\nis a test\nline 3\nline 4");
    styledText.replaceTextRange(21, 7, "");
    verify = 7;
    styledText.setText("This\nis a test\r");
    styledText.replaceTextRange(5, 9, "");
    verify = 8;
    styledText.setText("\nL1\r\nL2\r\n");
    styledText.replaceTextRange(7, 2, "");
    verify = 9;
    styledText.setText("L1\r\n");
    styledText.replaceTextRange(2, 2, "test");
    verify = 10;
    styledText.setText("L1\r\n");
    try {
        styledText.replaceTextRange(3, 1, "");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":10: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 11;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        styledText.replaceTextRange(1, 2, "");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":11: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 12;
    styledText.setText("L1\r");
    styledText.replaceTextRange(3, 0, "\n");
    verify = 13;
    styledText.setText("L1\n");
    styledText.replaceTextRange(2, 0, "\r");
    verify = 14;
    styledText.setText("L1\r\n");
    try {
        styledText.replaceTextRange(3, 0, "test");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":14: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 15;
    styledText.setText("L1\r\n");
    styledText.replaceTextRange(2, 2, "test\n\n");
    verify = 16;
    styledText.setText("L1\r\n");
    try {
        styledText.replaceTextRange(3, 1, "test\r\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":16: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 17;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        styledText.replaceTextRange(1, 2, "test\n\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":17: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 18;
    styledText.setText("L1\r");
    styledText.replaceTextRange(3, 0, "\ntest\r\n");
    verify = 19;
    styledText.setText("L1\n");
    styledText.replaceTextRange(2, 0, "test\r\r\r");
    verify = 20;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        styledText.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":20: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    content.removeTextChangeListener(listener);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent) TextChangingEvent(org.eclipse.swt.custom.TextChangingEvent) TextChangeListener(org.eclipse.swt.custom.TextChangeListener) TextChangedEvent(org.eclipse.swt.custom.TextChangedEvent) Test(org.junit.Test)

Example 9 with StyledTextContent

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

the class Test_org_eclipse_swt_custom_TextChangeListener method test_textSetLorg_eclipse_swt_custom_TextChangedEvent.

@Test
public void test_textSetLorg_eclipse_swt_custom_TextChangedEvent() {
    StyledTextContent content = styledText.getContent();
    TextChangeListener listener = new TextChangeListener() {

        @Override
        public void textChanging(TextChangingEvent event) {
        }

        @Override
        public void textChanged(TextChangedEvent event) {
        }

        @Override
        public void textSet(TextChangedEvent event) {
            switch(verify) {
                case 1:
                    {
                        assertTrue(":1:", styledText.getText().equals("testing"));
                        break;
                    }
                case 2:
                    {
                        assertTrue(":2:", styledText.getText().equals("\n\n"));
                        break;
                    }
                case 3:
                    {
                        assertTrue(":3:", styledText.getText().equals("a"));
                        break;
                    }
                case 4:
                    {
                        assertTrue(":4:", styledText.getText().isEmpty());
                        break;
                    }
            }
        }
    };
    content.addTextChangeListener(listener);
    verify = 1;
    styledText.setText("testing");
    verify = 2;
    styledText.setText("\n\n");
    verify = 3;
    styledText.setText("a");
    verify = 4;
    try {
        styledText.setText(null);
    } catch (IllegalArgumentException ex) {
        assertTrue(":4:", true);
    }
    content.removeTextChangeListener(listener);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent) TextChangingEvent(org.eclipse.swt.custom.TextChangingEvent) TextChangeListener(org.eclipse.swt.custom.TextChangeListener) TextChangedEvent(org.eclipse.swt.custom.TextChangedEvent) Test(org.junit.Test)

Example 10 with StyledTextContent

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

the class Test_org_eclipse_swt_custom_StyledText method test_getLineDelimiter.

@Test
public void test_getLineDelimiter() {
    final String lineDelimiter = "\n";
    StyledTextContent content = text.getContent();
    assertEquals(content.getLineDelimiter(), text.getLineDelimiter());
    content = new StyledTextContent() {

        @Override
        public void addTextChangeListener(TextChangeListener listener) {
        }

        @Override
        public int getCharCount() {
            return 0;
        }

        @Override
        public String getLine(int lineIndex) {
            return "";
        }

        @Override
        public int getLineAtOffset(int offset) {
            return 0;
        }

        @Override
        public int getLineCount() {
            return 0;
        }

        @Override
        public String getLineDelimiter() {
            return lineDelimiter;
        }

        @Override
        public int getOffsetAtLine(int lineIndex) {
            return 0;
        }

        @Override
        public String getTextRange(int start, int length) {
            return "";
        }

        @Override
        public void removeTextChangeListener(TextChangeListener listener) {
        }

        @Override
        public void replaceTextRange(int start, int replaceLength, String text) {
        }

        @Override
        public void setText(String text) {
        }
    };
    text.setContent(content);
    assertEquals(lineDelimiter, text.getLineDelimiter());
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent) TextChangeListener(org.eclipse.swt.custom.TextChangeListener) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Aggregations

StyledTextContent (org.eclipse.swt.custom.StyledTextContent)26 TextChangeListener (org.eclipse.swt.custom.TextChangeListener)6 Test (org.junit.Test)6 TextChangedEvent (org.eclipse.swt.custom.TextChangedEvent)3 TextChangingEvent (org.eclipse.swt.custom.TextChangingEvent)3 Point (org.eclipse.swt.graphics.Point)3 StyleRange (org.eclipse.swt.custom.StyleRange)1 Color (org.eclipse.swt.graphics.Color)1