Previous Page
Next Page

5.2. Text Viewers

The TextViewer class wraps the StyledText widget (see Figure 5-11 for the TextViewer hierarchy). Individual runs of text may have different styles associated with them, including foreground color, background color, and bold. Text viewers provide a document model to the client and manage the conversion of the document to the styled text information used by the text widget.

Figure 5-11. TextViewer hierarchy.

Useful APIs include:

addTextListener(ITextListener) Adds a text listener to this viewer.

appendVerifyKeyListener(VerifyKeyListener) Appends a verify key listener to the viewer's list of verify key listeners.

canDoOperation(int) Returns whether the operation specified by the given operation code can be performed.

changeTextPresentation(TextPresentation, boolean) Applies the color information encoded in the given text presentation.

doOperation(int) Performs the operation specified by the operation code on the target.

enableOperation(int, boolean) Enables/disables the given text operation.

getSelectedRange() Returns the range of the current selection in coordinates of this viewer's document.

getSelection() Returns the current selection for this provider.

getTextWidget() Returns the viewer's text widget.

isEditable() Returns whether the shown text can be manipulated.

refresh() Refreshes this viewer completely with information freshly obtained from the viewer's model.

setDocument(IDocument) Sets the given document as the text viewer's model and updates the presentation accordingly.

setEditable(boolean) Sets the editable mode.

setInput(Object) Sets or clears the input for this viewer. The TextViewer implementation of this method calls setDocument(IDocument) with the input object if the input object is an instance of IDocument or with null if the input object is not.

setRedraw(boolean) Enables/disables the redrawing of this text viewer.

setSelectedRange(int, int) Sets the selection to the specified range.

setSelection(ISelection, boolean) Sets a new selection for this viewer and optionally makes it visible.

setTextColor(Color) Applies the given color to this viewer's selection.

setTextColor(Color, int, int, boolean) Applies the given color to the specified section of this viewer.

setTextHover(ITextHover, String) Sets this viewer's text hover for the given content type.

The following example creates a text viewer containing styled text (see Figure 5-12).

import org.eclipse.jface.text.*;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TextViewerExample {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setText("Text Viewer Example");
      shell.setBounds(100, 100, 225, 125);
      shell.setLayout(new FillLayout());

      final TextViewer textViewer =
         new TextViewer(shell, SWT.MULTI | SWT.V_SCROLL);

      String string = "This is plain text\n"
         + "This is bold text\n"
         + "This is red text";
      Document document = new Document(string);

      TextPresentation style = new TextPresentation();
         new StyleRange(19, 17, null, null, SWT.BOLD));
      Color red = new Color(null, 255, 0, 0);
         new StyleRange(37, 16, red, null));
      textViewer.changeTextPresentation(style, true);;
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) display.sleep();

Figure 5-12. TextViewer example.

After creating the text viewer, a Document object is created that holds a string of text and is then assigned to the viewer. Next, a TextPresentation object is created to hold the style ranges. Two style ranges are added: one that sets a range of text to bold and a second that sets a range of text to the color red. The first argument to the StyleRange constructor is the index of the first character in the string to which the style should apply. The second argument is the number of characters that should be affected by the style. Finally, the style object is assigned to the viewer.

Previous Page
Next Page