KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > contentassist > CurrentValueContext


1 /*******************************************************************************
2  * Copyright (c) 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.contentassist;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.debug.core.model.IExpression;
15 import org.eclipse.debug.core.model.IValue;
16 import org.eclipse.debug.core.model.IVariable;
17 import org.eclipse.debug.ui.IDebugView;
18 import org.eclipse.jdt.core.IType;
19 import org.eclipse.jdt.debug.core.IJavaArray;
20 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
21 import org.eclipse.jdt.debug.core.IJavaObject;
22 import org.eclipse.jdt.debug.core.IJavaPrimitiveValue;
23 import org.eclipse.jdt.debug.core.IJavaStackFrame;
24 import org.eclipse.jdt.debug.core.IJavaType;
25 import org.eclipse.jdt.debug.core.IJavaValue;
26 import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
27 import org.eclipse.jdt.internal.debug.eval.ast.engine.ASTEvaluationEngine;
28 import org.eclipse.jdt.internal.debug.eval.ast.engine.ArrayRuntimeContext;
29 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchWindow;
34
35 public class CurrentValueContext extends CurrentFrameContext {
36
37     /* (non-Javadoc)
38      * @see org.eclipse.jdt.internal.debug.ui.contentassist.IJavaDebugContentAssistContext#getType()
39      */

40     public IType getType() throws CoreException {
41         IJavaValue value = resolveValue();
42         if (value == null || value instanceof IJavaPrimitiveValue) {
43             // no object selected, use the frame
44
return super.getType();
45         }
46         IType type = null;
47         if (value instanceof IJavaArray) {
48             // completion in context of Object
49
IJavaType[] types = ((IJavaDebugTarget)value.getDebugTarget()).getJavaTypes("java.lang.Object"); //$NON-NLS-1$
50
if (types.length > 0) {
51                 type = JavaDebugUtils.resolveType(types[0]);
52             }
53         } else {
54             type = JavaDebugUtils.resolveType(value);
55         }
56         if (type == null) {
57             unableToResolveType();
58         }
59         return type;
60      }
61     
62     /**
63      * Returns the value for which completions are to be computed for, or <code>null</code> if none.
64      *
65      * @return the value for which completions are to be computed for, or <code>null</code> if none
66      * @throws CoreException
67      */

68     protected IJavaValue resolveValue() throws CoreException {
69         IJavaStackFrame stackFrame= getStackFrame();
70         if (stackFrame == null) {
71             unableToResolveType();
72         }
73         
74         IWorkbenchWindow window= JDIDebugUIPlugin.getActiveWorkbenchWindow();
75         if (window == null) {
76             unableToResolveType();
77         }
78         IWorkbenchPage page= window.getActivePage();
79         if (page == null) {
80             unableToResolveType();
81         }
82         IDebugView view= (IDebugView)page.getActivePart();
83         if (view == null) {
84             unableToResolveType();
85         }
86         ISelection selection= view.getViewer().getSelection();
87         if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
88             IStructuredSelection viewerSelection= (IStructuredSelection)selection;
89             if (viewerSelection.size() > 1) {
90                 unableToResolveType();
91             }
92             Object JavaDoc element= viewerSelection.getFirstElement();
93             
94             IValue value= null;
95             if (element instanceof IVariable) {
96                 IVariable variable = (IVariable)element;
97                 if (!variable.getName().equals("this")) { //$NON-NLS-1$
98
value= variable.getValue();
99                 }
100             } else if (element instanceof IExpression) {
101                 value= ((IExpression)element).getValue();
102             }
103             if (value instanceof IJavaValue) {
104                 return (IJavaValue) value;
105             }
106         }
107         return null;
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.jdt.internal.debug.ui.contentassist.IJavaDebugContentAssistContext#getLocalVariables()
112      */

113     public String JavaDoc[][] getLocalVariables() throws CoreException {
114         IJavaValue value = resolveValue();
115         if (value instanceof IJavaArray) {
116             // do a song and dance to fake 'this' as an array receiver
117
return new String JavaDoc[][]{new String JavaDoc[] {ArrayRuntimeContext.ARRAY_THIS_VARIABLE}, new String JavaDoc[] {value.getJavaType().getName()}};
118         } else if (value instanceof IJavaObject) {
119             return new String JavaDoc[][]{};
120         }
121         return super.getLocalVariables();
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.jdt.internal.debug.ui.contentassist.IJavaDebugContentAssistContext#getSnippet(java.lang.String)
126      */

127     public String JavaDoc getSnippet(String JavaDoc snippet) throws CoreException {
128         IJavaValue value = resolveValue();
129         if (value instanceof IJavaArray) {
130             return ASTEvaluationEngine.replaceThisReferences(snippet);
131         }
132         return super.getSnippet(snippet);
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.jdt.internal.debug.ui.contentassist.CurrentFrameContext#isStatic()
137      */

138     public boolean isStatic() throws CoreException {
139         IJavaValue value = resolveValue();
140         if (value instanceof IJavaObject) {
141             return false;
142         }
143         return super.isStatic();
144     }
145
146     
147 }
148
Popular Tags