Java Code Examples for org.eclipse.swt.custom.StyledText#getStyleRanges()

The following examples show how to use org.eclipse.swt.custom.StyledText#getStyleRanges() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TMinGenericEditorTest.java    From tm4e with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testTMHighlightInGenericEditorEdit() throws IOException, PartInitException {
	f = File.createTempFile("test" + System.currentTimeMillis(), ".ts");
	FileOutputStream fileOutputStream = new FileOutputStream(f);
	fileOutputStream.write("let a = '';".getBytes());
	fileOutputStream.close();
	f.deleteOnExit();
	editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(),
			f.toURI(), editorDescriptor.getId(), true);
	StyledText text = (StyledText)editor.getAdapter(Control.class);
	Assert.assertTrue(new DisplayHelper() {
		@Override
		protected boolean condition() {
			return text.getStyleRanges().length > 1;
		}
	}.waitForCondition(text.getDisplay(), 3000));
	int initialNumberOfRanges = text.getStyleRanges().length;
	text.setText("let a = '';\nlet b = 10;\nlet c = true;");
	Assert.assertTrue("More styles should have been added", new DisplayHelper() {
		@Override protected boolean condition() {
			return text.getStyleRanges().length > initialNumberOfRanges + 3;
		}
	}.waitForCondition(text.getDisplay(), 300000));
}
 
Example 2
Source File: HsMultiCellEditor.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 刷新拼写检查中错误单词的样式
 * @param ranges
 */
public void refreshErrorWordsStyle(List<StyleRange> ranges){
	StyledText styledText = cellEditor.viewer.getTextWidget();
	List<StyleRange> oldRangeList = new ArrayList<StyleRange>();
	for(StyleRange oldRange : styledText.getStyleRanges()){
		if (oldRange.underlineStyle != SWT.UNDERLINE_ERROR) {
			oldRangeList.add(oldRange);
		}
	}
	styledText.setStyleRange(null);

	styledText.setStyleRanges(oldRangeList.toArray(new StyleRange[oldRangeList.size()]));
	if (ranges != null) {
		for (StyleRange range : ranges) {
			styledText.setStyleRange(range);
		}
	}
}
 
Example 3
Source File: HsMultiCellEditor.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 刷新拼写检查中错误单词的样式
 * @param ranges
 */
public void refreshErrorWordsStyle(List<StyleRange> ranges){
	StyledText styledText = cellEditor.viewer.getTextWidget();
	List<StyleRange> oldRangeList = new ArrayList<StyleRange>();
	for(StyleRange oldRange : styledText.getStyleRanges()){
		if (oldRange.underlineStyle != SWT.UNDERLINE_ERROR) {
			oldRangeList.add(oldRange);
		}
	}
	styledText.setStyleRange(null);

	styledText.setStyleRanges(oldRangeList.toArray(new StyleRange[oldRangeList.size()]));
	if (ranges != null) {
		for (StyleRange range : ranges) {
			styledText.setStyleRange(range);
		}
	}
}
 
Example 4
Source File: LineBackgroundPainter.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private void drawCurrentLine(LineBackgroundEvent event, final IRegion lineRegion)
{
	final StyledText textWidget = fViewer.getTextWidget();
	final int offset = event.lineOffset;
	final RGBa lineHighlight = getCurrentTheme().getLineHighlight();
	event.lineBackground = getColorManager().getColor(lineHighlight.toRGB());

	// In this case, we should be overriding the bg of the style ranges for the line too!
	if (textWidget.isDisposed())
	{
		return;
	}
	// FIXME Only change bg colors of visible ranges!
	int replaceLength = 160;
	if (lineRegion != null)
	{
		replaceLength = Math.min(replaceLength, lineRegion.getLength());
	}

	// be safe about offsets
	int charCount = textWidget.getCharCount();
	if (offset + replaceLength > charCount)
	{
		replaceLength = charCount - offset;
		if (replaceLength < 0)
		{
			// Just playing safe here
			replaceLength = 0;
		}
	}
	final StyleRange[] ranges = textWidget.getStyleRanges(offset, replaceLength, true);
	if (ranges == null || ranges.length == 0)
	{
		return;
	}
	Color background = textWidget.getBackground();
	final int[] positions = new int[ranges.length << 1];
	int x = 0;
	boolean apply = false;
	for (StyleRange range : ranges)
	{
		if (range.background != null)
		{
			if (!range.background.equals(background))
			{
				positions[x] = range.start;
				positions[x + 1] = range.length;
				x += 2;
				continue;
			}
			apply = true;
		}
		range.background = null;
		positions[x] = range.start;
		positions[x + 1] = range.length;
		x += 2;
	}

	if (apply)
	{
		textWidget.setStyleRanges(offset, replaceLength, positions, ranges);
	}
}
 
Example 5
Source File: PyUnitViewTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testLineTracker() throws Exception {
    // PyUnitViewTest fails because it depends on org.eclipse.debug.ui.console.IConsoleLineTracker
    // being able to be loaded. But IConsoleLineTracker is in a plug-in with an activator that in
    // turn relies on the workbench being loaded, leading to a test error. This isn't a problem
    // when run within Eclipse as a (plain) JUint test because the Activator is skipped.
    // Since the classes under test rely on IConsoleLineTracker, the test must be run as a
    // GUI enabled Plug-in test (i.e workbench started), however if you do that the test fails
    // because of interactions with other services in the workbench.
    if (PydevPlugin.getDefault() != null) {
        if (SharedCorePlugin.skipKnownFailures()) {
            return;
        }
    }
    PyUnitView pyUnitView = new PyUnitView();
    PyUnitTestRun testRun = new PyUnitTestRun(null);
    String error = "File \"Y:\\test_python\\src\\mod1\\mod2\\test_it2.py\", line 45, in testAnotherCase";
    PyUnitTestResult result = new PyUnitTestResult(testRun, "fail", "c:\\temp.py", "TestCase.foo", "", "\n\n"
            + error + "\nfoo\n", "0");

    Display display = Display.getCurrent();
    if (display == null) {
        display = Display.getDefault();
    }
    Shell composite = new Shell(display);
    composite.setLayout(new FillLayout());

    StyledText text = new StyledText(composite, 0);
    pyUnitView.setTextComponent(text);
    pyUnitView.setOnlyCreateLinksForExistingFiles(false);
    pyUnitView.onSelectResult(result);

    //uncomment below to see results.
    //        composite.pack();
    //        composite.open();
    //
    //        while (!composite.isDisposed()) {
    //            if (!display.readAndDispatch()){
    //                display.sleep();
    //            }
    //        }

    StyleRange[] styleRanges = text.getStyleRanges();
    assertEquals(1, styleRanges.length);
    assertEquals(69, styleRanges[0].start);
    assertEquals(error.length(), styleRanges[0].length);
}