001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.console;
011:
012: import org.eclipse.jface.text.BadLocationException;
013: import org.eclipse.jface.text.DocumentEvent;
014: import org.eclipse.jface.text.IDocument;
015: import org.eclipse.jface.text.IDocumentListener;
016: import org.eclipse.swt.custom.StyledText;
017: import org.eclipse.swt.events.VerifyEvent;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.ui.console.ConsolePlugin;
020: import org.eclipse.ui.console.IConsoleDocumentPartitioner;
021: import org.eclipse.ui.console.TextConsole;
022: import org.eclipse.ui.console.TextConsoleViewer;
023:
024: /**
025: * Viewer used to display an IOConsole
026: *
027: * @since 3.1
028: */
029: public class IOConsoleViewer extends TextConsoleViewer {
030: /**
031: * will always scroll with output if value is true.
032: */
033: private boolean fAutoScroll = true;
034:
035: private IDocumentListener fDocumentListener;
036:
037: public IOConsoleViewer(Composite parent, TextConsole console) {
038: super (parent, console);
039: }
040:
041: public boolean isAutoScroll() {
042: return fAutoScroll;
043: }
044:
045: public void setAutoScroll(boolean scroll) {
046: fAutoScroll = scroll;
047: }
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.eclipse.jface.text.TextViewer#handleVerifyEvent(org.eclipse.swt.events.VerifyEvent)
053: */
054: protected void handleVerifyEvent(VerifyEvent e) {
055: IDocument doc = getDocument();
056: String[] legalLineDelimiters = doc.getLegalLineDelimiters();
057: String eventString = e.text;
058: try {
059: IConsoleDocumentPartitioner partitioner = (IConsoleDocumentPartitioner) doc
060: .getDocumentPartitioner();
061: if (!partitioner.isReadOnly(e.start)) {
062: boolean isCarriageReturn = false;
063: for (int i = 0; i < legalLineDelimiters.length; i++) {
064: if (e.text.equals(legalLineDelimiters[i])) {
065: isCarriageReturn = true;
066: break;
067: }
068: }
069:
070: if (!isCarriageReturn) {
071: super .handleVerifyEvent(e);
072: return;
073: }
074: }
075:
076: int length = doc.getLength();
077: if (e.start == length) {
078: super .handleVerifyEvent(e);
079: } else {
080: try {
081: doc.replace(length, 0, eventString);
082: } catch (BadLocationException e1) {
083: }
084: e.doit = false;
085: }
086: } finally {
087: StyledText text = (StyledText) e.widget;
088: text.setCaretOffset(text.getCharCount());
089: }
090: }
091:
092: /**
093: * makes the associated text widget uneditable.
094: */
095: public void setReadOnly() {
096: ConsolePlugin.getStandardDisplay().asyncExec(new Runnable() {
097: public void run() {
098: StyledText text = getTextWidget();
099: if (text != null && !text.isDisposed()) {
100: text.setEditable(false);
101: }
102: }
103: });
104: }
105:
106: /**
107: * @return <code>false</code> if text is editable
108: */
109: public boolean isReadOnly() {
110: return !getTextWidget().getEditable();
111: }
112:
113: /* (non-Javadoc)
114: * @see org.eclipse.jface.text.ITextViewer#setDocument(org.eclipse.jface.text.IDocument)
115: */
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: }
|