1 11 package org.eclipse.jdt.debug.ui; 12 13 14 import com.ibm.icu.text.MessageFormat; 15 import java.util.HashMap ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.ILaunchConfiguration; 20 import org.eclipse.debug.core.model.IPersistableSourceLocator; 21 import org.eclipse.debug.core.model.IStackFrame; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.JavaCore; 24 import org.eclipse.jdt.core.JavaModelException; 25 import org.eclipse.jdt.debug.core.IJavaReferenceType; 26 import org.eclipse.jdt.debug.core.IJavaStackFrame; 27 import org.eclipse.jdt.debug.core.IJavaThread; 28 import org.eclipse.jdt.internal.debug.ui.DebugUIMessages; 29 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 30 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages; 31 import org.eclipse.jdt.internal.debug.ui.launcher.SourceElementLabelProvider; 32 import org.eclipse.jdt.internal.debug.ui.launcher.SourceElementQualifierProvider; 33 import org.eclipse.jdt.launching.JavaRuntime; 34 import org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation; 35 import org.eclipse.jdt.launching.sourcelookup.JavaSourceLocator; 36 import org.eclipse.jface.window.Window; 37 import org.eclipse.ui.dialogs.TwoPaneElementSelector; 38 39 70 71 public class JavaUISourceLocator implements IPersistableSourceLocator { 72 73 77 public static final String ID_PROMPTING_JAVA_SOURCE_LOCATOR = IJavaDebugUIConstants.PLUGIN_ID + ".javaSourceLocator"; 79 86 public static final String ATTR_FIND_ALL_SOURCE_ELEMENTS = IJavaDebugUIConstants.PLUGIN_ID + ".ATTR_FIND_ALL_SOURCE_ELEMENTS"; 88 91 private IJavaProject fJavaProject; 92 93 96 private JavaSourceLocator fSourceLocator; 97 98 103 private boolean fAllowedToAsk; 104 105 109 private boolean fIsFindAllSourceElements = false; 110 111 115 private HashMap fTypesToSource = null; 116 117 120 public JavaUISourceLocator() { 121 fSourceLocator = new JavaSourceLocator(); 122 fAllowedToAsk = true; 123 } 124 125 134 public JavaUISourceLocator( 135 IJavaProject[] projects, 136 boolean includeRequired) 137 throws JavaModelException { 138 fSourceLocator = new JavaSourceLocator(projects, includeRequired); 139 fAllowedToAsk = true; 140 } 141 142 152 public JavaUISourceLocator(IJavaProject project) throws CoreException { 153 fJavaProject = project; 154 IJavaSourceLocation[] sls = 155 JavaSourceLocator.getDefaultSourceLocations(project); 156 fSourceLocator = new JavaSourceLocator(project); 157 if (sls != null) { 158 fSourceLocator.setSourceLocations(sls); 159 } 160 fAllowedToAsk = true; 161 } 162 163 166 public Object getSourceElement(IStackFrame stackFrame) { 167 Object res = findSourceElement(stackFrame); 168 if (res == null && fAllowedToAsk) { 169 IJavaStackFrame frame = 170 (IJavaStackFrame) stackFrame.getAdapter(IJavaStackFrame.class); 171 if (frame != null) { 172 try { 173 if (!frame.isObsolete()) { 174 showDebugSourcePage(frame); 175 res = fSourceLocator.getSourceElement(stackFrame); 176 } 177 } catch (DebugException e) { 178 } 179 } 180 } 181 return res; 182 } 183 184 private Object findSourceElement(IStackFrame stackFrame) { 185 if (isFindAllSourceElements()) { 186 Object [] sourceElements = fSourceLocator.getSourceElements(stackFrame); 187 if (sourceElements == null || sourceElements.length == 0) { 188 return null; 189 } 190 if (sourceElements.length == 1) { 191 return sourceElements[0]; 192 } 193 try { 194 IJavaStackFrame frame = (IJavaStackFrame)stackFrame; 195 IJavaReferenceType type = frame.getReferenceType(); 196 Object cachedSource = getSourceElement(type); 197 if (cachedSource != null) { 198 return cachedSource; 199 } 200 TwoPaneElementSelector dialog = new TwoPaneElementSelector(JDIDebugUIPlugin.getActiveWorkbenchShell(), new SourceElementLabelProvider(),new SourceElementQualifierProvider()); 202 dialog.setTitle(DebugUIMessages.JavaUISourceLocator_Select_Source_1); 203 dialog.setMessage(MessageFormat.format(DebugUIMessages.JavaUISourceLocator__Select_the_source_that_corresponds_to__0__2, new String []{type.getName()})); 204 dialog.setElements(sourceElements); 205 dialog.setMultipleSelection(false); 206 dialog.setUpperListLabel(DebugUIMessages.JavaUISourceLocator__Matching_files__3); 207 dialog.setLowerListLabel(DebugUIMessages.JavaUISourceLocator__Location__4); 208 dialog.open(); 209 Object [] result = dialog.getResult(); 210 if (result == null) { 211 return null; 212 } 213 Object sourceElement = result[0]; 214 cacheSourceElement(sourceElement, type); 215 return sourceElement; 216 } catch (CoreException e) { 217 JDIDebugUIPlugin.log(e); 218 return sourceElements[0]; 219 } 220 } 221 return fSourceLocator.getSourceElement(stackFrame); 222 } 223 224 private Object getSourceElement(IJavaReferenceType type) { 225 if (fTypesToSource == null) { 226 return null; 227 } 228 return fTypesToSource.get(type); 229 } 230 231 private void cacheSourceElement(Object sourceElement, IJavaReferenceType type) { 232 if (fTypesToSource == null) { 233 fTypesToSource = new HashMap (); 234 } 235 fTypesToSource.put(type, sourceElement); 236 } 237 238 247 private void showDebugSourcePage(final IJavaStackFrame frame) { 248 Runnable prompter = new Runnable () { 249 public void run() { 250 try { 251 String message = MessageFormat.format(LauncherMessages.JavaUISourceLocator_selectprojects_message, new String [] {frame.getDeclaringTypeName()}); 252 253 ILaunchConfiguration configuration = 254 frame.getLaunch().getLaunchConfiguration(); 255 JavaSourceLookupDialog dialog = 256 new JavaSourceLookupDialog( 257 JDIDebugUIPlugin.getActiveWorkbenchShell(), 258 message, 259 configuration); 260 int result = dialog.open(); 261 if (result == Window.OK) { 262 fAllowedToAsk = !dialog.isNotAskAgain(); 263 JavaUISourceLocator.this.initializeDefaults( 264 configuration); 265 } 266 } catch (CoreException e) { 267 if (e.getStatus().getCode() 269 != IJavaThread.ERR_THREAD_NOT_SUSPENDED) { 270 JDIDebugUIPlugin.log(e); 271 } 272 } 273 } 274 }; 275 JDIDebugUIPlugin.getStandardDisplay().syncExec(prompter); 276 } 277 278 281 public String getMemento() throws CoreException { 282 String memento = fSourceLocator.getMemento(); 283 String handle = fJavaProject.getHandleIdentifier(); 284 String findAll = Boolean.valueOf(isFindAllSourceElements()).toString(); 285 286 StringBuffer buffer = new StringBuffer (); 287 buffer.append("<project>"); buffer.append(handle); 289 buffer.append("</project>"); buffer.append("<findAll>"); buffer.append(findAll); 292 buffer.append("</findAll>"); buffer.append(memento); 294 return buffer.toString(); 295 } 296 297 300 public void initializeDefaults(ILaunchConfiguration configuration) 301 throws CoreException { 302 fSourceLocator.initializeDefaults(configuration); 303 fJavaProject = JavaRuntime.getJavaProject(configuration); 304 fIsFindAllSourceElements = 305 configuration.getAttribute(ATTR_FIND_ALL_SOURCE_ELEMENTS, false); 306 } 307 308 311 public void initializeFromMemento(String memento) throws CoreException { 312 if (memento.startsWith("<project>")) { int index = memento.indexOf("</project>"); if (index > 0) { 315 String handle = memento.substring(9, index); 316 int start = index + 19; 317 index = memento.indexOf("</findAll>", start); if (index > 0) { 319 String findAll = memento.substring(start, index); 320 Boolean all = Boolean.valueOf(findAll); 321 String rest = memento.substring(index + 10); 322 fJavaProject = (IJavaProject) JavaCore.create(handle); 323 fIsFindAllSourceElements = all.booleanValue(); 324 fSourceLocator.initializeFromMemento(rest); 325 } 326 } 327 } else { 328 int index = memento.indexOf('\n'); 330 String handle = memento.substring(0, index); 331 String rest = memento.substring(index + 1); 332 fJavaProject = (IJavaProject) JavaCore.create(handle); 333 fIsFindAllSourceElements = false; 334 fSourceLocator.initializeFromMemento(rest); 335 } 336 } 337 338 341 public IJavaSourceLocation[] getSourceLocations() { 342 return fSourceLocator.getSourceLocations(); 343 } 344 345 348 public void setSourceLocations(IJavaSourceLocation[] locations) { 349 fSourceLocator.setSourceLocations(locations); 350 } 351 352 363 public boolean isFindAllSourceElements() { 364 return fIsFindAllSourceElements; 365 } 366 367 375 public void setFindAllSourceElement(boolean findAll) { 376 fIsFindAllSourceElements = findAll; 377 } 378 379 } 380 | Popular Tags |