KickJava   Java API By Example, From Geeks To Geeks.

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


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

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     /**
33      * Internal document listener.
34      */

35     class InternalDocumentListener implements IDocumentListener {
36         /* (non-Javadoc)
37          * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
38          */

39         public void documentAboutToBeChanged(DocumentEvent e) {
40         }
41         
42         /* (non-Javadoc)
43          * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
44          */

45         public void documentChanged(DocumentEvent e) {
46             revealEndOfDocument();
47         }
48     }
49     
50     /**
51      * Creates a new console viewer and adds verification checking
52      * to only allow text modification if the text is being modified
53      * in the editable portion of the underlying document.
54      *
55      * @see org.eclipse.swt.events.VerifyListener
56      */

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     /**
66      * Returns the SWT style flags used when instantiating this viewer
67      */

68     private static int getSWTStyles() {
69         int styles= SWT.H_SCROLL | SWT.V_SCROLL;
70         return styles;
71     }
72
73     /**
74      * Reveals (makes visible) the end of the current document
75      */

76     protected void revealEndOfDocument() {
77         IDocument doc = getDocument();
78         int lines = doc.getNumberOfLines();
79         try {
80             // lines are 0-based
81
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     /* (non-Javadoc)
96      * @see org.eclipse.jface.text.ITextViewer#setDocument(org.eclipse.jface.text.IDocument)
97      */

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     /* (non-Javadoc)
120      * @see org.eclipse.jface.text.TextViewer#canPerformFind()
121      */

122     protected boolean canPerformFind() {
123         return (getTextWidget() != null && getVisibleDocument() != null && getVisibleDocument().getLength() > 0);
124     }
125         
126     /**
127      * Dispose this viewer and resources
128      */

129     public void dispose() {
130     }
131     
132     /* (non-Javadoc)
133      * @see org.eclipse.swt.custom.LineStyleListener#lineGetStyle(org.eclipse.swt.custom.LineStyleEvent)
134      */

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