use of org.eclipse.jface.text.TextPresentation in project eclipse.platform.text by eclipse.
the class BrowserInformationControl method computeSizeHint.
@Override
public Point computeSizeHint() {
Point sizeConstraints = getSizeConstraints();
Rectangle trim = computeTrim();
int height = trim.height;
// FIXME: The HTML2TextReader does not render <p> like a browser.
// Instead of inserting an empty line, it just adds a single line break.
// Furthermore, the indentation of <dl><dd> elements is too small (e.g with a long @see line)
TextPresentation presentation = new TextPresentation();
String text;
try (HTML2TextReader reader = new HTML2TextReader(new StringReader(fInput.getHtml()), presentation)) {
text = reader.getString();
} catch (IOException e) {
// $NON-NLS-1$
text = "";
}
fTextLayout.setText(text);
fTextLayout.setWidth(sizeConstraints == null ? SWT.DEFAULT : sizeConstraints.x - trim.width);
Iterator<StyleRange> iter = presentation.getAllStyleRangeIterator();
while (iter.hasNext()) {
StyleRange sr = iter.next();
if (sr.fontStyle == SWT.BOLD)
fTextLayout.setStyle(fBoldStyle, sr.start, sr.start + sr.length - 1);
}
// does not return minimum width, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=217446
Rectangle bounds = fTextLayout.getBounds();
int lineCount = fTextLayout.getLineCount();
int textWidth = 0;
for (int i = 0; i < lineCount; i++) {
Rectangle rect = fTextLayout.getLineBounds(i);
int lineWidth = rect.x + rect.width;
if (i == 0)
lineWidth += fInput.getLeadingImageWidth();
textWidth = Math.max(textWidth, lineWidth);
}
bounds.width = textWidth;
// $NON-NLS-1$
fTextLayout.setText("");
int minWidth = bounds.width;
height = height + bounds.height;
// Add some air to accommodate for different browser renderings
minWidth += 15;
height += 15;
// Apply max size constraints
if (sizeConstraints != null) {
if (sizeConstraints.x != SWT.DEFAULT)
minWidth = Math.min(sizeConstraints.x, minWidth + trim.width);
if (sizeConstraints.y != SWT.DEFAULT)
height = Math.min(sizeConstraints.y, height);
}
// Ensure minimal size
int width = Math.max(MIN_WIDTH, minWidth);
height = Math.max(MIN_HEIGHT, height);
return new Point(width, height);
}
use of org.eclipse.jface.text.TextPresentation in project eclipse.platform.text by eclipse.
the class HTML2TextReaderTest method verify.
/**
* @param input input
* @param expectedOutput expected output
* @param styleRangeCount count
* @throws IOException test failure
* @deprecated pass actual style ranges
*/
@Deprecated
private void verify(String input, String expectedOutput, int styleRangeCount) throws IOException {
Reader reader = new StringReader(input);
TextPresentation textPresentation = new TextPresentation();
String result;
try (HTML2TextReader htmlReader = new HTML2TextReader(reader, textPresentation)) {
result = htmlReader.getString();
}
if (DEBUG)
System.out.println("<" + result + "/>");
assertEquals(expectedOutput, result);
Iterator<StyleRange> styleRangeIterator = textPresentation.getAllStyleRangeIterator();
List<StyleRange> ranges = new ArrayList<>();
while (styleRangeIterator.hasNext()) {
ranges.add(styleRangeIterator.next());
}
assertEquals("Incorrect number of style ranges", styleRangeCount, ranges.size());
Collections.sort(ranges, (r1, r2) -> r1.start - r2.start);
for (int i = 0; i < ranges.size() - 1; i++) {
StyleRange range1 = ranges.get(i);
StyleRange range2 = ranges.get(i + 1);
if (range1.start + range1.length > range2.start) {
assertTrue("StyleRanges overlap", false);
}
}
}
use of org.eclipse.jface.text.TextPresentation in project eclipse.platform.text by eclipse.
the class TextPresentationTest method setUpTextPresentation.
private void setUpTextPresentation() {
fTextPresentation = new TextPresentation();
fTextPresentation.setDefaultStyleRange(createStyleRange(0, 140, NORMAL));
for (int i = 0; i < fAllRanges.length; i++) fTextPresentation.addStyleRange(fAllRanges[i]);
}
use of org.eclipse.jface.text.TextPresentation in project eclipse.platform.text by eclipse.
the class PresentationReconciler method createPresentation.
/**
* Constructs a "repair description" for the given damage and returns this
* description as a text presentation. For this, it queries the partitioning
* of the damage region and asks the appropriate presentation repairer for
* each partition to construct the "repair description" for this partition.
*
* @param damage the damage to be repaired
* @param document the document whose presentation must be repaired
* @return the presentation repair description as text presentation or
* <code>null</code> if the partitioning could not be computed
*/
protected TextPresentation createPresentation(IRegion damage, IDocument document) {
try {
if (fRepairers == null || fRepairers.isEmpty()) {
TextPresentation presentation = new TextPresentation(damage, 100);
presentation.setDefaultStyleRange(new StyleRange(damage.getOffset(), damage.getLength(), null, null));
return presentation;
}
TextPresentation presentation = new TextPresentation(damage, 1000);
ITypedRegion[] partitioning = TextUtilities.computePartitioning(document, getDocumentPartitioning(), damage.getOffset(), damage.getLength(), false);
for (ITypedRegion r : partitioning) {
IPresentationRepairer repairer = getRepairer(r.getType());
if (repairer != null)
repairer.createPresentation(presentation, r);
}
return presentation;
} catch (BadLocationException x) {
return null;
}
}
use of org.eclipse.jface.text.TextPresentation in project eclipse.platform.text by eclipse.
the class ContextInformationPopup method internalShowContextFrame.
/**
* Shows the given context frame.
*
* @param frame the frame to display
* @param initial <code>true</code> if this is the first frame to be displayed
* @since 2.0
*/
private void internalShowContextFrame(ContextFrame frame, boolean initial) {
fContentAssistSubjectControlAdapter.installValidator(frame);
if (frame.fPresenter != null) {
if (fTextPresentation == null)
fTextPresentation = new TextPresentation();
fContentAssistSubjectControlAdapter.installContextInformationPresenter(frame);
frame.fPresenter.updatePresentation(frame.fOffset, fTextPresentation);
}
createContextInfoPopup();
fContextInfoText.setText(frame.fInformation.getInformationDisplayString());
if (fTextPresentation != null)
TextPresentation.applyTextPresentation(fTextPresentation, fContextInfoText);
resize(frame.fVisibleOffset);
if (initial) {
if (fContentAssistant.addContentAssistListener(this, ContentAssistant.CONTEXT_INFO_POPUP)) {
if (fContentAssistSubjectControlAdapter.getControl() != null) {
fTextWidgetSelectionListener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
validateContextInformation();
}
};
fContentAssistSubjectControlAdapter.addSelectionListener(fTextWidgetSelectionListener);
}
fContentAssistant.addToLayout(this, fContextInfoPopup, ContentAssistant.LayoutManager.LAYOUT_CONTEXT_INFO_POPUP, frame.fVisibleOffset);
fContextInfoPopup.setVisible(true);
}
} else {
fContentAssistant.layout(ContentAssistant.LayoutManager.LAYOUT_CONTEXT_INFO_POPUP, frame.fVisibleOffset);
}
}
Aggregations