KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.ILaunch;
18 import org.eclipse.debug.core.model.IExpression;
19 import org.eclipse.debug.core.model.IValue;
20 import org.eclipse.debug.core.model.IVariable;
21 import org.eclipse.debug.ui.DebugUITools;
22 import org.eclipse.debug.ui.IDebugView;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.debug.core.IJavaArray;
27 import org.eclipse.jdt.debug.core.IJavaStackFrame;
28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
29 import org.eclipse.jface.text.ITextSelection;
30 import org.eclipse.jface.text.ITextViewer;
31 import org.eclipse.jface.text.contentassist.ICompletionProposal;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36  
37 public class DetailsCompletionProcessor extends DisplayCompletionProcessor {
38
39     /**
40      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
41      */

42     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
43         try {
44             setErrorMessage(DisplayMessages.DetailsCompletionProcessor_0); //$NON-NLS-1$
45
IAdaptable context = DebugUITools.getDebugContext();
46             if (context == null) {
47                 return new ICompletionProposal[0];
48             }
49             IJavaStackFrame stackFrame= (IJavaStackFrame)context.getAdapter(IJavaStackFrame.class);
50             if (stackFrame == null) {
51                 return new ICompletionProposal[0];
52             }
53             
54             setErrorMessage(DisplayMessages.DetailsCompletionProcessor_1); //$NON-NLS-1$
55
IWorkbenchWindow window= JDIDebugUIPlugin.getActiveWorkbenchWindow();
56             if (window == null) {
57                 return new ICompletionProposal[0];
58             }
59             IWorkbenchPage page= window.getActivePage();
60             if (page == null) {
61                 return new ICompletionProposal[0];
62             }
63             IDebugView view= (IDebugView)page.getActivePart();
64             if (view == null) {
65                 return new ICompletionProposal[0];
66             }
67             ISelection selection= view.getViewer().getSelection();
68             if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
69                 return super.computeCompletionProposals(stackFrame, viewer, documentOffset);
70             }
71             
72             IStructuredSelection viewerSelection= (IStructuredSelection)selection;
73             if (viewerSelection.size() > 1) {
74                 return new ICompletionProposal[0];
75             }
76             Object JavaDoc element= viewerSelection.getFirstElement();
77             try {
78                 setErrorMessage(null);
79                 ITextSelection textSelection= (ITextSelection)viewer.getSelectionProvider().getSelection();
80                 IType receivingType= getReceivingType(stackFrame.getLaunch(), element);
81                 if (receivingType == null) {
82                     setErrorMessage(DisplayMessages.DetailsCompletionProcessor_2); //$NON-NLS-1$
83
return new ICompletionProposal[0];
84                 }
85                 IJavaProject project = receivingType.getJavaProject();
86         
87                 configureResultCollector(project, textSelection);
88                 int insertionPosition= computeInsertionPosition(receivingType, stackFrame);
89                 receivingType.codeComplete(viewer.getDocument().get().toCharArray(), insertionPosition, documentOffset,
90                      new char[0][], new char[0][],
91                      new int[0], false, getCollector());
92                      
93                  //Order here and not in result collector to make sure that the order
94
//applies to all proposals and not just those of the compilation unit.
95
return order(getCollector().getJavaCompletionProposals());
96             } catch (JavaModelException x) {
97                 handle(viewer, x);
98             } catch (DebugException de) {
99                 handle(viewer, de);
100             }
101             return new ICompletionProposal[0];
102         } finally {
103             releaseCollector();
104         }
105     }
106     
107     private IType getReceivingType(ILaunch launch, Object JavaDoc element) throws DebugException {
108         String JavaDoc originalTypeName= getReceivingTypeName(element);
109         if (originalTypeName == null) {
110             return null;
111         }
112         
113         String JavaDoc sourceName= originalTypeName;
114         // strip off generic info
115
int genIndex = sourceName.indexOf('<');
116         if (genIndex >= 0) {
117             sourceName = sourceName.substring(0, genIndex);
118         }
119         int dollarIndex= sourceName.indexOf('$');
120         if (dollarIndex >= 0) {
121             sourceName= sourceName.substring(0, dollarIndex);
122         }
123         int index = sourceName.lastIndexOf('.');
124         if (index >= 0) {
125             sourceName = sourceName.replace('.', IPath.SEPARATOR);
126         }
127         sourceName+=".java"; //$NON-NLS-1$
128

129         return resolveType(launch, originalTypeName, sourceName);
130     }
131
132     private String JavaDoc getReceivingTypeName(Object JavaDoc element) {
133         
134         IValue value= null;
135         try {
136             if (element instanceof IVariable) {
137                 value= ((IVariable)element).getValue();
138                 if (value instanceof IJavaArray) {
139                     return null;
140                 }
141             } else if (element instanceof IExpression) {
142                 value= ((IExpression)element).getValue();
143             }
144             if (value != null) {
145                 return value.getReferenceTypeName();
146             }
147         } catch (DebugException de) {
148             JDIDebugUIPlugin.log(de);
149         }
150                 
151         return null;
152     }
153 }
154
Popular Tags