Search in sources :

Example 1 with BidiSegmentListener

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

the class Test_org_eclipse_swt_custom_BidiSegmentListener method testListener.

private void testListener(final String message, final int[] segments, boolean exceptionExpected) {
    boolean exceptionThrown = false;
    BidiSegmentListener listener = event -> {
        assertEquals(message + " incorrect BidiSegmentEvent", 0, event.lineOffset);
        assertEquals(message + " incorrect BidiSegmentEvent", line, event.lineText);
        event.segments = segments;
        listenerCalled = true;
    };
    listenerCalled = false;
    try {
        text.addBidiSegmentListener(listener);
        text.getLocationAtOffset(0);
    } catch (IllegalArgumentException e) {
        exceptionThrown = true;
    } finally {
        text.removeBidiSegmentListener(listener);
    }
    if (exceptionExpected) {
        assertTrue(message + " expected exception not thrown", exceptionThrown);
    } else {
        assertFalse(message + " unexpected exception thrown", exceptionThrown);
    }
    if (SwtTestUtil.isBidi()) {
        assertTrue(message + " listener not called", listenerCalled);
    } else {
        assertFalse(message + " listener called when it shouldn't be", listenerCalled);
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) StyledText(org.eclipse.swt.custom.StyledText) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) After(org.junit.After) Assert.assertTrue(org.junit.Assert.assertTrue) StyleRange(org.eclipse.swt.custom.StyleRange) Test(org.junit.Test) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener)

Example 2 with BidiSegmentListener

use of org.eclipse.swt.custom.BidiSegmentListener in project egit by eclipse.

the class SpellcheckableMessageArea method configureHardWrap.

private void configureHardWrap() {
    if (shouldHardWrap()) {
        if (hardWrapSegmentListener == null) {
            final StyledText textWidget = getTextWidget();
            hardWrapSegmentListener = new BidiSegmentListener() {

                @Override
                public void lineGetSegments(BidiSegmentEvent e) {
                    if (e.widget == textWidget) {
                        int footerOffset = CommonUtils.getFooterOffset(textWidget.getText());
                        if (footerOffset >= 0 && e.lineOffset >= footerOffset) {
                            return;
                        }
                    }
                    int[] segments = calculateWrapOffsets(e.lineText, MAX_LINE_WIDTH);
                    if (segments != null) {
                        char[] segmentsChars = new char[segments.length];
                        Arrays.fill(segmentsChars, '\n');
                        e.segments = segments;
                        e.segmentsChars = segmentsChars;
                    }
                }
            };
            textWidget.addBidiSegmentListener(hardWrapSegmentListener);
            // XXX: workaround for https://bugs.eclipse.org/384886
            textWidget.setText(textWidget.getText());
            if (brokenBidiPlatformTextWidth != -1) {
                Layout restrictedWidthLayout = new Layout() {

                    @Override
                    protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
                        Point size = SpellcheckableMessageArea.this.getSize();
                        Rectangle trim = SpellcheckableMessageArea.this.computeTrim(0, 0, 0, 0);
                        size.x -= trim.width;
                        size.y -= trim.height;
                        if (size.x > brokenBidiPlatformTextWidth)
                            size.x = brokenBidiPlatformTextWidth;
                        return size;
                    }

                    @Override
                    protected void layout(Composite composite, boolean flushCache) {
                        Point size = computeSize(composite, SWT.DEFAULT, SWT.DEFAULT, flushCache);
                        textWidget.setBounds(0, 0, size.x, size.y);
                    }
                };
                setLayout(restrictedWidthLayout);
            }
        }
    } else if (hardWrapSegmentListener != null) {
        StyledText textWidget = getTextWidget();
        textWidget.removeBidiSegmentListener(hardWrapSegmentListener);
        // XXX: workaround for https://bugs.eclipse.org/384886
        textWidget.setText(textWidget.getText());
        hardWrapSegmentListener = null;
        if (brokenBidiPlatformTextWidth != -1)
            setLayout(new FillLayout());
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) BidiSegmentEvent(org.eclipse.swt.custom.BidiSegmentEvent) FillLayout(org.eclipse.swt.layout.FillLayout) Layout(org.eclipse.swt.widgets.Layout) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) FillLayout(org.eclipse.swt.layout.FillLayout)

Example 3 with BidiSegmentListener

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

the class Test_org_eclipse_swt_custom_BidiSegmentListener method testStyleRangeSegmenting.

private void testStyleRangeSegmenting(final int[] segments, int[] boldRanges) {
    boolean exceptionThrown = false;
    BidiSegmentListener listener = event -> {
        assertEquals(" incorrect BidiSegmentEvent", 0, event.lineOffset);
        assertEquals(" incorrect BidiSegmentEvent", line, event.lineText);
        event.segments = segments;
        listenerCalled = true;
    };
    listenerCalled = false;
    try {
        text.addBidiSegmentListener(listener);
        text.setStyleRange(null);
        for (int i = 0; i < boldRanges.length; i += 2) {
            StyleRange styleRange = new StyleRange(boldRanges[i], boldRanges[i + 1], null, null, SWT.BOLD);
            text.setStyleRange(styleRange);
        }
        text.getLocationAtOffset(0);
    } catch (IllegalArgumentException e) {
        exceptionThrown = true;
    } finally {
        text.removeBidiSegmentListener(listener);
    }
    assertFalse(" unexpected exception thrown", exceptionThrown);
    if (SwtTestUtil.isBidi()) {
        assertTrue(" listener not called", listenerCalled);
    } else {
        assertFalse(" listener called when it shouldn't be", listenerCalled);
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) StyledText(org.eclipse.swt.custom.StyledText) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) After(org.junit.After) Assert.assertTrue(org.junit.Assert.assertTrue) StyleRange(org.eclipse.swt.custom.StyleRange) Test(org.junit.Test) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) StyleRange(org.eclipse.swt.custom.StyleRange) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener)

Example 4 with BidiSegmentListener

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

the class Test_org_eclipse_swt_custom_StyledText method test_addBidiSegmentListenerLorg_eclipse_swt_custom_BidiSegmentListener.

@Test
public void test_addBidiSegmentListenerLorg_eclipse_swt_custom_BidiSegmentListener() {
    String line = "Line1";
    boolean exceptionThrown = false;
    BidiSegmentListener listener = event -> listenerCalled = true;
    try {
        text.addBidiSegmentListener(null);
    } catch (IllegalArgumentException e) {
        exceptionThrown = true;
    }
    assertTrue("Expected exception not thrown", exceptionThrown);
    listenerCalled = false;
    text.setText(line);
    text.addBidiSegmentListener(listener);
    // cause StyledText to call the BidiSegmentListener.
    text.getLocationAtOffset(0);
    if (SwtTestUtil.isBidi()) {
        assertTrue("Listener not called", listenerCalled);
    } else {
        assertFalse("Listener called when it shouldn't be", listenerCalled);
    }
    listenerCalled = false;
    text.removeBidiSegmentListener(listener);
    // cause StyledText to call the BidiSegmentListener.
    text.getLocationAtOffset(0);
    assertFalse("Listener not removed", listenerCalled);
}
Also used : Printer(org.eclipse.swt.printing.Printer) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) StyledTextContent(org.eclipse.swt.custom.StyledTextContent) VerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener) StyledText(org.eclipse.swt.custom.StyledText) Rectangle(org.eclipse.swt.graphics.Rectangle) HashMap(java.util.HashMap) MovementEvent(org.eclipse.swt.custom.MovementEvent) TextChangeListener(org.eclipse.swt.custom.TextChangeListener) Point(org.eclipse.swt.graphics.Point) Clipboard(org.eclipse.swt.dnd.Clipboard) Event(org.eclipse.swt.widgets.Event) LineBackgroundListener(org.eclipse.swt.custom.LineBackgroundListener) VerifyListener(org.eclipse.swt.events.VerifyListener) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Map(java.util.Map) TextTransfer(org.eclipse.swt.dnd.TextTransfer) After(org.junit.After) RGB(org.eclipse.swt.graphics.RGB) Assert.fail(org.junit.Assert.fail) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener) Font(org.eclipse.swt.graphics.Font) LineStyleListener(org.eclipse.swt.custom.LineStyleListener) ExtendedModifyListener(org.eclipse.swt.custom.ExtendedModifyListener) Before(org.junit.Before) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Assert.assertNotNull(org.junit.Assert.assertNotNull) MovementListener(org.eclipse.swt.custom.MovementListener) Assert.assertTrue(org.junit.Assert.assertTrue) StyleRange(org.eclipse.swt.custom.StyleRange) Caret(org.eclipse.swt.widgets.Caret) Test(org.junit.Test) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) Display(org.eclipse.swt.widgets.Display) CaretListener(org.eclipse.swt.custom.CaretListener) Transfer(org.eclipse.swt.dnd.Transfer) Assert.assertNull(org.junit.Assert.assertNull) Color(org.eclipse.swt.graphics.Color) BidiUtil(org.eclipse.swt.internal.BidiUtil) Assert.assertFalse(org.junit.Assert.assertFalse) ModifyListener(org.eclipse.swt.events.ModifyListener) SWT(org.eclipse.swt.SWT) ST(org.eclipse.swt.custom.ST) FontData(org.eclipse.swt.graphics.FontData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Assert.assertEquals(org.junit.Assert.assertEquals) SelectionListener(org.eclipse.swt.events.SelectionListener) GridLayout(org.eclipse.swt.layout.GridLayout) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener) Test(org.junit.Test)

Example 5 with BidiSegmentListener

use of org.eclipse.swt.custom.BidiSegmentListener in project egit by eclipse.

the class SpellcheckableMessageArea method computeBrokenBidiPlatformTextWidth.

private void computeBrokenBidiPlatformTextWidth(int textWidth) {
    class BidiSegmentListenerTester implements BidiSegmentListener {

        boolean called;

        @Override
        public void lineGetSegments(BidiSegmentEvent event) {
            called = true;
        }
    }
    BidiSegmentListenerTester tester = new BidiSegmentListenerTester();
    StyledText textWidget = getTextWidget();
    textWidget.addBidiSegmentListener(tester);
    // $NON-NLS-1$
    textWidget.setText(" ");
    textWidget.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    textWidget.removeBidiSegmentListener(tester);
    brokenBidiPlatformTextWidth = tester.called ? -1 : textWidth;
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) BidiSegmentEvent(org.eclipse.swt.custom.BidiSegmentEvent) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener)

Aggregations

BidiSegmentListener (org.eclipse.swt.custom.BidiSegmentListener)5 StyledText (org.eclipse.swt.custom.StyledText)5 SWT (org.eclipse.swt.SWT)3 StyleRange (org.eclipse.swt.custom.StyleRange)3 After (org.junit.After)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Before (org.junit.Before)3 Test (org.junit.Test)3 BidiSegmentEvent (org.eclipse.swt.custom.BidiSegmentEvent)2 Point (org.eclipse.swt.graphics.Point)2 Rectangle (org.eclipse.swt.graphics.Rectangle)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CaretListener (org.eclipse.swt.custom.CaretListener)1 ExtendedModifyListener (org.eclipse.swt.custom.ExtendedModifyListener)1 LineBackgroundListener (org.eclipse.swt.custom.LineBackgroundListener)1 LineStyleListener (org.eclipse.swt.custom.LineStyleListener)1 MovementEvent (org.eclipse.swt.custom.MovementEvent)1