public final class Snippet { /** * Default constructor. */ private Snippet() { } /** * The number of line to display. */ private static final int LINE_LIMIT = 10; /** * The shell. */ private static Shell shell; /** * The timer to use. */ private static Timer timer; /** * The number of iteration past the 10 first. */ private static int loop; /** * @param args the program arguments. */ public static void main(final String[] args) { Display display = new Display(); shell = new Shell(display); shell.setText("TextViewer ex"); shell.setLayout(new FillLayout()); final int width = 175; final int height = 200; shell.setSize(width, height); timer = new Timer(); loop = 0; createContents(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); timer.cancel(); } /** * Create the contents. */ private static void createContents() { Composite comp = new Composite(shell, SWT.None); comp.setLayout(new GridLayout(1, false)); final TextViewer textViewer = new TextViewer(comp, SWT.MULTI | SWT.V_SCROLL); final Document document = new Document(); document.set("Initial text\n"); textViewer.setDocument(document); textViewer.getControl().setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true)); TimerTask task = new TimerTask() { /** * {@inheritDoc} */ @Override public void run() { final int numberOfLine = document.getNumberOfLines(); int currentNumberOfLine = numberOfLine; if (LINE_LIMIT < numberOfLine) { if (shell != null && !shell.isDisposed()) { shell.getDisplay().asyncExec(new Runnable() { public void run() { try { final IRegion region = document.getLineInformation(0); document.replace(0, region.getLength() + 1, ""); } catch (BadLocationException e) { e.printStackTrace(); } } }); } currentNumberOfLine--; loop++; } if (shell != null && !shell.isDisposed()) { final int realNumberOfLine = currentNumberOfLine; shell.getDisplay().asyncExec(new Runnable() { /** * {@inheritDoc} */ public void run() { try { IRegion info = document .getLineInformation(realNumberOfLine - 1); String text = "Occurence {0} {1} {2}\n"; String separator = ""; String index = ""; if (loop > 0) { separator = "-"; index = "" + loop; } document.replace(info.getOffset() + info.getLength(), 0, MessageFormat .format(text, realNumberOfLine, separator, index)); if (textViewer.getControl() != null && !textViewer.getControl().isDisposed()) { textViewer.refresh(); } } catch (BadLocationException e) { e.printStackTrace(); } }; }); } } }; final long period = 500L; timer.scheduleAtFixedRate(task, 0L, period); } }