Search in sources :

Example 86 with StyledString

use of org.eclipse.jface.viewers.StyledString in project eclipse.platform.text by eclipse.

the class FileLabelProvider method getLineElementLabel.

private StyledString getLineElementLabel(LineElement lineElement) {
    int lineNumber = lineElement.getLine();
    String lineNumberString = Messages.format(SearchMessages.FileLabelProvider_line_number, Integer.valueOf(lineNumber));
    StyledString str = new StyledString(lineNumberString, StyledString.QUALIFIER_STYLER);
    FileMatch[] matches = lineElement.getMatches(fPage.getInput());
    Arrays.sort(matches, fMatchComparator);
    String content = lineElement.getContents();
    int pos = evaluateLineStart(matches, content, lineElement.getOffset());
    int length = content.length();
    // number of characters to leave away if the line is too long
    int charsToCut = getCharsToCut(length, matches);
    for (int i = 0; i < matches.length; i++) {
        FileMatch match = matches[i];
        int start = Math.max(match.getOriginalOffset() - lineElement.getOffset(), 0);
        // append gap between last match and the new one
        if (pos < start) {
            if (charsToCut > 0) {
                charsToCut = appendShortenedGap(content, pos, start, charsToCut, i == 0, str);
            } else {
                str.append(content.substring(pos, start));
            }
        }
        // append match
        int end = Math.min(match.getOriginalOffset() + match.getOriginalLength() - lineElement.getOffset(), lineElement.getLength());
        str.append(content.substring(start, end), DecoratingFileSearchLabelProvider.HIGHLIGHT_STYLE);
        pos = end;
    }
    // append rest of the line
    if (charsToCut > 0) {
        appendShortenedGap(content, pos, length, charsToCut, false, str);
    } else {
        str.append(content.substring(pos));
    }
    return str;
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 87 with StyledString

use of org.eclipse.jface.viewers.StyledString in project eclipse.platform.text by eclipse.

the class FileLabelProvider method getStyledText.

@Override
public StyledString getStyledText(Object element) {
    if (element instanceof LineElement)
        return getLineElementLabel((LineElement) element);
    if (!(element instanceof IResource))
        return new StyledString();
    IResource resource = (IResource) element;
    if (!resource.exists())
        new StyledString(SearchMessages.FileLabelProvider_removed_resource_label);
    String name = BasicElementLabels.getResourceName(resource);
    if (fOrder == SHOW_LABEL) {
        return getColoredLabelWithCounts(resource, new StyledString(name));
    }
    String pathString = BasicElementLabels.getPathLabel(resource.getParent().getFullPath(), false);
    if (fOrder == SHOW_LABEL_PATH) {
        StyledString str = new StyledString(name);
        String decorated = Messages.format(fgSeparatorFormat, new String[] { str.getString(), pathString });
        StyledCellLabelProvider.styleDecoratedString(decorated, StyledString.QUALIFIER_STYLER, str);
        return getColoredLabelWithCounts(resource, str);
    }
    StyledString str = new StyledString(Messages.format(fgSeparatorFormat, new String[] { pathString, name }));
    return getColoredLabelWithCounts(resource, str);
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) IResource(org.eclipse.core.resources.IResource)

Example 88 with StyledString

use of org.eclipse.jface.viewers.StyledString in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup method handleSetData.

/*
	 * @since 3.1
	 */
private void handleSetData(Event event) {
    TableItem item = (TableItem) event.item;
    int index = fProposalTable.indexOf(item);
    if (0 <= index && index < fFilteredProposals.size()) {
        ICompletionProposal current = fFilteredProposals.get(index);
        String displayString;
        StyleRange[] styleRanges = null;
        Image image = null;
        try {
            if (fIsColoredLabelsSupportEnabled && current instanceof ICompletionProposalExtension7 && Helper.okToUse(fProposalShell)) {
                BoldStylerProvider boldStylerProvider = fContentAssistant.getBoldStylerProvider();
                if (boldStylerProvider == null) {
                    boldStylerProvider = new BoldStylerProvider(fProposalShell.getFont());
                    fContentAssistant.setBoldStylerProvider(boldStylerProvider);
                }
                StyledString styledString = ((ICompletionProposalExtension7) current).getStyledDisplayString(fContentAssistSubjectControlAdapter.getDocument(), fFilterOffset, boldStylerProvider);
                displayString = styledString.getString();
                styleRanges = styledString.getStyleRanges();
            } else if (fIsColoredLabelsSupportEnabled && current instanceof ICompletionProposalExtension6) {
                StyledString styledString = ((ICompletionProposalExtension6) current).getStyledDisplayString();
                displayString = styledString.getString();
                styleRanges = styledString.getStyleRanges();
            } else {
                displayString = current.getDisplayString();
            }
        } catch (RuntimeException e) {
            // On failures to retrieve the proposal's text, insert a dummy entry and log the error.
            // $NON-NLS-1$
            displayString = JFaceTextMessages.getString("CompletionProposalPopup.error_retrieving_proposal");
            // $NON-NLS-1$
            String PLUGIN_ID = "org.eclipse.jface.text";
            ILog log = Platform.getLog(Platform.getBundle(PLUGIN_ID));
            // $NON-NLS-1$
            log.log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, JFaceTextMessages.getString("CompletionProposalPopup.unexpected_error"), e));
        }
        try {
            image = current.getImage();
        } catch (RuntimeException e) {
        // If we are unable to retrieve the proposal's image, leave it blank.
        }
        item.setText(displayString);
        if (fIsColoredLabelsSupportEnabled)
            TableOwnerDrawSupport.storeStyleRanges(item, 0, styleRanges);
        item.setImage(image);
        item.setData(current);
    } else {
    // this should not happen, but does on win32
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) TableItem(org.eclipse.swt.widgets.TableItem) StyleRange(org.eclipse.swt.custom.StyleRange) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) ILog(org.eclipse.core.runtime.ILog)

Example 89 with StyledString

use of org.eclipse.jface.viewers.StyledString in project linuxtools by eclipse.

the class StyledConsoleLogsTest method shouldGenerateStyledString.

@Test
public void shouldGenerateStyledString() {
    // given
    // when
    final StyledString result = StyledTextBuilder.parse(lineText);
    // then
    assertThat(SWTUtils.syncExec(() -> result.getStyleRanges())).isEqualTo(SWTUtils.syncExec(() -> expectedStyledString.getStyleRanges()));
    assertThat(result.getString()).isEqualTo(expectedStyledString.getString());
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) Test(org.junit.Test)

Example 90 with StyledString

use of org.eclipse.jface.viewers.StyledString in project linuxtools by eclipse.

the class DockerComposeConsole method writeContentInConsole.

private void writeContentInConsole(final String text) {
    Display.getDefault().asyncExec(() -> {
        final StyledString styledString = StyledTextBuilder.parse(text);
        final StyleRange[] styleRanges = styledString.getStyleRanges();
        Stream.of(styleRanges).forEach(range -> {
            try (final IOConsoleOutputStream consoleStream = newOutputStream()) {
                consoleStream.setColor(range.foreground);
                consoleStream.write(styledString.getString().substring(range.start, range.start + range.length).getBytes());
            } catch (IOException e) {
                Activator.log(e);
            }
        });
    });
}
Also used : IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream) StyleRange(org.eclipse.swt.custom.StyleRange) StyledString(org.eclipse.jface.viewers.StyledString) IOException(java.io.IOException)

Aggregations

StyledString (org.eclipse.jface.viewers.StyledString)95 Image (org.eclipse.swt.graphics.Image)17 JavaCompletionProposal (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)8 LazyJavaCompletionProposal (org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal)8 ViewerCell (org.eclipse.jface.viewers.ViewerCell)5 GridData (org.eclipse.swt.layout.GridData)5 Table (org.eclipse.swt.widgets.Table)5 Entry (java.util.Map.Entry)4 StyledCellLabelProvider (org.eclipse.jface.viewers.StyledCellLabelProvider)4 Styler (org.eclipse.jface.viewers.StyledString.Styler)4 Capability (org.osgi.resource.Capability)4 EObject (org.eclipse.emf.ecore.EObject)3 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 Composite (org.eclipse.swt.widgets.Composite)3 Label (org.eclipse.swt.widgets.Label)3 Version (org.osgi.framework.Version)3 Resource (org.osgi.resource.Resource)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)2 IStatus (org.eclipse.core.runtime.IStatus)2