1 11 package org.eclipse.ui.internal.console; 12 13 import org.eclipse.jface.text.BadLocationException; 14 import org.eclipse.jface.text.DocumentEvent; 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.IDocumentListener; 17 import org.eclipse.swt.custom.StyledText; 18 import org.eclipse.swt.events.VerifyEvent; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.ui.console.ConsolePlugin; 21 import org.eclipse.ui.console.IConsoleDocumentPartitioner; 22 import org.eclipse.ui.console.TextConsole; 23 import org.eclipse.ui.console.TextConsoleViewer; 24 25 30 public class IOConsoleViewer extends TextConsoleViewer { 31 34 private boolean fAutoScroll = true; 35 36 private IDocumentListener fDocumentListener; 37 38 public IOConsoleViewer(Composite parent, TextConsole console) { 39 super(parent, console); 40 } 41 42 public boolean isAutoScroll() { 43 return fAutoScroll; 44 } 45 46 public void setAutoScroll(boolean scroll) { 47 fAutoScroll = scroll; 48 } 49 50 55 protected void handleVerifyEvent(VerifyEvent e) { 56 IDocument doc = getDocument(); 57 String [] legalLineDelimiters = doc.getLegalLineDelimiters(); 58 String eventString = e.text; 59 try { 60 IConsoleDocumentPartitioner partitioner = (IConsoleDocumentPartitioner) doc.getDocumentPartitioner(); 61 if (!partitioner.isReadOnly(e.start)) { 62 boolean isCarriageReturn = false; 63 for (int i = 0; i < legalLineDelimiters.length; i++) { 64 if (e.text.equals(legalLineDelimiters[i])) { 65 isCarriageReturn = true; 66 break; 67 } 68 } 69 70 if (!isCarriageReturn) { 71 super.handleVerifyEvent(e); 72 return; 73 } 74 } 75 76 int length = doc.getLength(); 77 if (e.start == length) { 78 super.handleVerifyEvent(e); 79 } else { 80 try { 81 doc.replace(length, 0, eventString); 82 } catch (BadLocationException e1) { 83 } 84 e.doit = false; 85 } 86 } finally { 87 StyledText text = (StyledText) e.widget; 88 text.setCaretOffset(text.getCharCount()); 89 } 90 } 91 92 95 public void setReadOnly() { 96 ConsolePlugin.getStandardDisplay().asyncExec(new Runnable () { 97 public void run() { 98 StyledText text = getTextWidget(); 99 if (text != null && !text.isDisposed()) { 100 text.setEditable(false); 101 } 102 } 103 }); 104 } 105 106 109 public boolean isReadOnly() { 110 return !getTextWidget().getEditable(); 111 } 112 113 116 public void setDocument(IDocument document) { 117 IDocument oldDocument= getDocument(); 118 119 super.setDocument(document); 120 121 if (oldDocument != null) { 122 oldDocument.removeDocumentListener(getDocumentListener()); 123 } 124 if (document != null) { 125 document.addDocumentListener(getDocumentListener()); 126 } 127 } 128 129 private IDocumentListener getDocumentListener() { 130 if (fDocumentListener == null) { 131 fDocumentListener= new IDocumentListener() { 132 public void documentAboutToBeChanged(DocumentEvent event) { 133 } 134 135 public void documentChanged(DocumentEvent event) { 136 if (fAutoScroll) { 137 revealEndOfDocument(); 138 } 139 } 140 }; 141 } 142 return fDocumentListener; 143 } 144 } 145 | Popular Tags |