1 11 package org.eclipse.ui.internal.console; 12 13 14 import org.eclipse.jface.text.BadLocationException; 15 import org.eclipse.jface.text.DocumentEvent; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.IDocumentListener; 18 import org.eclipse.jface.text.ITypedRegion; 19 import org.eclipse.jface.text.TextViewer; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.custom.LineStyleEvent; 22 import org.eclipse.swt.custom.LineStyleListener; 23 import org.eclipse.swt.custom.StyleRange; 24 import org.eclipse.swt.custom.StyledText; 25 import org.eclipse.swt.graphics.Color; 26 import org.eclipse.swt.widgets.Composite; 27 28 public class MessageConsoleViewer extends TextViewer implements LineStyleListener { 29 30 protected InternalDocumentListener fInternalDocumentListener= new InternalDocumentListener(); 31 32 35 class InternalDocumentListener implements IDocumentListener { 36 39 public void documentAboutToBeChanged(DocumentEvent e) { 40 } 41 42 45 public void documentChanged(DocumentEvent e) { 46 revealEndOfDocument(); 47 } 48 } 49 50 57 public MessageConsoleViewer(Composite parent) { 58 super(parent, getSWTStyles()); 59 getTextWidget().setDoubleClickEnabled(true); 60 getTextWidget().setFont(parent.getFont()); 61 getTextWidget().addLineStyleListener(this); 62 getTextWidget().setEditable(false); 63 } 64 65 68 private static int getSWTStyles() { 69 int styles= SWT.H_SCROLL | SWT.V_SCROLL; 70 return styles; 71 } 72 73 76 protected void revealEndOfDocument() { 77 IDocument doc = getDocument(); 78 int lines = doc.getNumberOfLines(); 79 try { 80 int lineStartOffset = doc.getLineOffset(lines - 1); 82 StyledText widget= getTextWidget(); 83 if (lineStartOffset > 0) { 84 widget.setCaretOffset(lineStartOffset); 85 widget.showSelection(); 86 } 87 int lineEndOffset = lineStartOffset + doc.getLineLength(lines - 1); 88 if (lineEndOffset > 0) { 89 widget.setCaretOffset(lineEndOffset); 90 } 91 } catch (BadLocationException e) { 92 } 93 } 94 95 98 public void setDocument(IDocument doc) { 99 IDocument oldDoc= getDocument(); 100 IDocument document= doc; 101 if (oldDoc == null && document == null) { 102 return; 103 } 104 if (oldDoc != null) { 105 oldDoc.removeDocumentListener(fInternalDocumentListener); 106 if (oldDoc.equals(document)) { 107 document.addDocumentListener(fInternalDocumentListener); 108 return; 109 } 110 } 111 112 super.setDocument(document); 113 if (document != null) { 114 revealEndOfDocument(); 115 document.addDocumentListener(fInternalDocumentListener); 116 } 117 } 118 119 122 protected boolean canPerformFind() { 123 return (getTextWidget() != null && getVisibleDocument() != null && getVisibleDocument().getLength() > 0); 124 } 125 126 129 public void dispose() { 130 } 131 132 135 public void lineGetStyle(LineStyleEvent event) { 136 IDocument document = getDocument(); 137 if (document != null) { 138 MessageConsolePartitioner partitioner = (MessageConsolePartitioner)document.getDocumentPartitioner(); 139 if (partitioner != null) { 140 ITypedRegion[] regions = partitioner.computePartitioning(event.lineOffset, event.lineOffset + event.lineText.length()); 141 StyleRange[] styles = new StyleRange[regions.length]; 142 for (int i = 0; i < regions.length; i++) { 143 MessageConsolePartition partition = (MessageConsolePartition)regions[i]; 144 Color color = partition.getStream().getColor(); 145 styles[i] = new StyleRange(partition.getOffset(), partition.getLength(), color, null); 146 } 147 event.styles = styles; 148 } 149 } 150 } 151 } | Popular Tags |