KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > display > DataDisplay


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.debug.ui.display;
12
13
14 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.jface.text.ITextViewer;
19
20 /**
21  * An implementation of a data display for a text viewer
22  */

23 public class DataDisplay implements IDataDisplay {
24     
25     /**
26      * The text viewer this data display works on
27      */

28     private ITextViewer fTextViewer;
29     
30     /**
31      * Constructs a data display for the given text viewer.
32      *
33      * @param viewer text viewer
34      */

35     public DataDisplay(ITextViewer viewer) {
36         setTextViewer(viewer);
37     }
38
39     /**
40      * @see IDataDisplay#clear()
41      */

42     public void clear() {
43         IDocument document= getTextViewer().getDocument();
44         if (document != null) {
45             document.set(""); //$NON-NLS-1$
46
}
47     }
48     
49     /**
50      * @see IDataDisplay#displayExpression(String)
51      */

52     public void displayExpression(String JavaDoc expression) {
53         IDocument document= fTextViewer.getDocument();
54         int offset= document.getLength();
55         try {
56             // add a cariage return if needed.
57
if (offset != document.getLineInformationOfOffset(offset).getOffset()) {
58                 expression= System.getProperty("line.separator") + expression.trim(); //$NON-NLS-1$
59
}
60             document.replace(offset, 0, expression);
61             fTextViewer.setSelectedRange(offset + expression.length(), 0);
62             fTextViewer.revealRange(offset, expression.length());
63         } catch (BadLocationException ble) {
64             JDIDebugUIPlugin.log(ble);
65         }
66     }
67     
68     /**
69      * @see IDataDisplay#displayExpressionValue(String)
70      */

71     public void displayExpressionValue(String JavaDoc value) {
72         value= System.getProperty("line.separator") + '\t' + value; //$NON-NLS-1$
73
ITextSelection selection= (ITextSelection)fTextViewer.getSelectionProvider().getSelection();
74     
75         int offset= selection.getOffset() + selection.getLength();
76         int length= value.length();
77         try {
78             fTextViewer.getDocument().replace(offset, 0, value);
79         } catch (BadLocationException ble) {
80             JDIDebugUIPlugin.log(ble);
81         }
82         fTextViewer.setSelectedRange(offset + length, 0);
83         fTextViewer.revealRange(offset, length);
84     }
85
86     /**
87      * Sets the text viewer for this data display
88      *
89      * @param viewer text viewer
90      */

91     private void setTextViewer(ITextViewer viewer) {
92         fTextViewer = viewer;
93     }
94     
95     /**
96      * Returns the text viewer for this data display
97      *
98      * @return text viewer
99      */

100     protected ITextViewer getTextViewer() {
101         return fTextViewer;
102     }
103 }
104
Popular Tags