KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > IOConsoleViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
26  * Viewer used to display an IOConsole
27  *
28  * @since 3.1
29  */

30 public class IOConsoleViewer extends TextConsoleViewer {
31     /**
32      * will always scroll with output if value is true.
33      */

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     /*
51      * (non-Javadoc)
52      *
53      * @see org.eclipse.jface.text.TextViewer#handleVerifyEvent(org.eclipse.swt.events.VerifyEvent)
54      */

55     protected void handleVerifyEvent(VerifyEvent e) {
56         IDocument doc = getDocument();
57         String JavaDoc[] legalLineDelimiters = doc.getLegalLineDelimiters();
58         String JavaDoc 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     /**
93      * makes the associated text widget uneditable.
94      */

95     public void setReadOnly() {
96         ConsolePlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
97             public void run() {
98                 StyledText text = getTextWidget();
99                 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 }
145
Popular Tags