1 11 package org.eclipse.debug.internal.ui.sourcelookup; 12 13 import org.eclipse.core.resources.IMarker; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.debug.core.DebugEvent; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IDebugEventSetListener; 21 import org.eclipse.debug.core.ILaunch; 22 import org.eclipse.debug.core.model.IDebugElement; 23 import org.eclipse.debug.core.model.ISourceLocator; 24 import org.eclipse.debug.core.model.IStackFrame; 25 import org.eclipse.debug.core.model.IThread; 26 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector; 27 import org.eclipse.debug.internal.ui.DebugUIPlugin; 28 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 29 import org.eclipse.debug.internal.ui.views.launch.LaunchView; 30 import org.eclipse.debug.ui.DebugUITools; 31 import org.eclipse.debug.ui.IDebugModelPresentation; 32 import org.eclipse.debug.ui.IDebugUIConstants; 33 import org.eclipse.debug.ui.sourcelookup.SourceLookupDialog; 34 import org.eclipse.jface.dialogs.Dialog; 35 import org.eclipse.jface.resource.JFaceColors; 36 import org.eclipse.jface.text.BadLocationException; 37 import org.eclipse.jface.text.IDocument; 38 import org.eclipse.jface.text.IRegion; 39 import org.eclipse.jface.viewers.ISelection; 40 import org.eclipse.jface.viewers.ISelectionChangedListener; 41 import org.eclipse.jface.window.Window; 42 import org.eclipse.swt.SWT; 43 import org.eclipse.swt.events.SelectionAdapter; 44 import org.eclipse.swt.events.SelectionEvent; 45 import org.eclipse.swt.layout.GridData; 46 import org.eclipse.swt.layout.GridLayout; 47 import org.eclipse.swt.widgets.Button; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Text; 50 import org.eclipse.ui.IEditorInput; 51 import org.eclipse.ui.IEditorPart; 52 import org.eclipse.ui.IEditorSite; 53 import org.eclipse.ui.IReusableEditor; 54 import org.eclipse.ui.IViewPart; 55 import org.eclipse.ui.IWorkbenchPage; 56 import org.eclipse.ui.IWorkbenchWindow; 57 import org.eclipse.ui.PartInitException; 58 import org.eclipse.ui.PlatformUI; 59 import org.eclipse.ui.part.EditorPart; 60 import org.eclipse.ui.texteditor.IDocumentProvider; 61 import org.eclipse.ui.texteditor.ITextEditor; 62 63 76 public class CommonSourceNotFoundEditor extends EditorPart implements IReusableEditor, IDebugEventSetListener { 77 78 81 private Text fText; 82 85 protected Object fObject; 86 87 90 public void doSave(IProgressMonitor monitor) { 91 } 92 93 96 public void doSaveAs() { 97 } 98 99 102 public void gotoMarker(IMarker marker) { 103 } 104 105 108 public void init(IEditorSite site, IEditorInput input) throws PartInitException { 109 setSite(site); 110 setInput(input); 111 DebugPlugin.getDefault().addDebugEventListener(this); 112 } 113 114 117 public boolean isDirty() { 118 return false; 119 } 120 121 124 public boolean isSaveAsAllowed() { 125 return false; 126 } 127 128 131 public void createPartControl(Composite parent) { 132 GridLayout topLayout = new GridLayout(); 133 GridData data = new GridData(); 134 topLayout.numColumns = 1; 135 topLayout.verticalSpacing = 10; 136 parent.setLayout(topLayout); 137 parent.setLayoutData(data); 138 parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); 139 140 fText = new Text(parent,SWT.READ_ONLY|SWT.WRAP); 141 data = new GridData(GridData.FILL_HORIZONTAL); 142 data.grabExcessHorizontalSpace = true; 143 fText.setLayoutData(data); 144 fText.setForeground(JFaceColors.getErrorText(fText.getDisplay())); 145 fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); 146 if (getEditorInput() != null) { 147 setInput(getEditorInput()); 148 } 149 150 Button button = new Button(parent, SWT.PUSH); 151 data = new GridData(); 152 data.grabExcessHorizontalSpace = false; 153 data.grabExcessVerticalSpace = false; 154 button.setLayoutData(data); 155 button.setText(SourceLookupUIMessages.addSourceLocation_addButton2); button.addSelectionListener(new SelectionAdapter() { 157 public void widgetSelected(SelectionEvent evt) { 158 buttonSelected(); 159 } 160 }); 161 162 Dialog.applyDialogFont(parent); 163 164 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugHelpContextIds.NO_SOURCE_EDITOR); 165 } 166 167 172 private void buttonSelected(){ 173 ISourceLocator locator = null; 174 ILaunch launch = null; 175 IAdaptable selection = DebugUITools.getDebugContext(); 176 177 if(selection == null) return; 178 179 if (selection.getAdapter(ILaunch.class) != null ) { 180 launch = (ILaunch) selection.getAdapter(ILaunch.class); 181 locator = launch.getSourceLocator(); 182 } 183 else if (selection.getAdapter(IDebugElement.class) != null ) { 184 launch = ((IDebugElement)selection.getAdapter(IDebugElement.class)).getLaunch(); 185 locator = launch.getSourceLocator(); 186 } 187 else return; if (locator == null || !(locator instanceof AbstractSourceLookupDirector)) 189 return; 190 191 final SourceLookupDialog dialog = 192 new SourceLookupDialog(DebugUIPlugin.getShell(),(AbstractSourceLookupDirector) locator); 193 194 int result = dialog.open(); 195 if(result == Window.OK) { 196 resetEditor(); 197 } 198 } 199 208 public void resetEditor() 209 { 210 fireChangeEventsOnStack(); 211 212 if(fObject instanceof IStackFrame) { 213 return; } 215 216 final int lineNumber = getLineNumber(); 218 Runnable open = new Runnable () { 219 public void run() { 220 IWorkbenchWindow dwindow= DebugUIPlugin.getActiveWorkbenchWindow(); 221 if (dwindow != null) { 222 IWorkbenchPage page= dwindow.getActivePage(); 223 if (page != null) { 224 IDebugModelPresentation modelPres = DebugUITools.newDebugModelPresentation(); 225 IEditorInput input = modelPres.getEditorInput(fObject); 226 if (input != null) { 227 String id = modelPres.getEditorId(input, fObject); 228 if (id != null) { 229 try { 230 IEditorPart editorPart = page.openEditor(input, id); 231 if (editorPart instanceof ITextEditor && lineNumber >= 0) { 232 ITextEditor textEditor = (ITextEditor) editorPart; 234 IRegion region= getLineInformation(textEditor, lineNumber); 235 if (region != null) { 236 textEditor.selectAndReveal(region.getOffset(), 0); 237 } 238 } 239 } catch (PartInitException e1) { 240 } 241 } 242 } 243 modelPres.dispose(); 244 } 245 } 246 } 247 }; 248 closeEditor(); 249 DebugUIPlugin.getStandardDisplay().asyncExec(open); 251 } 252 253 256 private IRegion getLineInformation(ITextEditor editor, int lineNumber) { 257 IDocumentProvider provider= editor.getDocumentProvider(); 258 IEditorInput input= editor.getEditorInput(); 259 try { 260 provider.connect(input); 261 } catch (CoreException e) { 262 return null; 263 } 264 try { 265 IDocument document= provider.getDocument(input); 266 if (document != null) 267 return document.getLineInformation(lineNumber); 268 } catch (BadLocationException e) { 269 } finally { 270 provider.disconnect(input); 271 } 272 return null; 273 } 274 275 279 protected int getLineNumber(){ 280 int line = -1; 281 if(fObject instanceof IMarker) { 282 try{ 283 line=((Integer )((IMarker)fObject).getAttribute(IMarker.LINE_NUMBER)).intValue(); 284 } catch(CoreException e){} 285 } 286 return line; 287 } 288 289 292 public void setFocus() { 293 if (fText != null) { 294 fText.setFocus(); 295 } 296 } 297 298 301 public void setInput(IEditorInput input) { 302 super.setInput(input); 303 if(input instanceof CommonSourceNotFoundEditorInput) { 304 fObject = ((CommonSourceNotFoundEditorInput)input).getObject(); 305 } 306 setPartName(input.getName()); 307 if (fText != null) { 308 fText.setText(input.getToolTipText()+"\n"); } 310 } 311 312 315 protected void fireChangeEventsOnStack(){ 316 if(fObject instanceof IStackFrame) { 317 fireChangeEvent(DebugEvent.CONTENT, (IStackFrame)fObject); 318 } else if(fObject instanceof IDebugElement) { try{ 320 IThread[] threads =((IDebugElement)fObject).getDebugTarget().getThreads(); 321 for(int i=0; i< threads.length; i++) 322 { 323 fireChangeEvent(DebugEvent.CONTENT, threads[i].getTopStackFrame()); 324 } 325 } catch(DebugException e){} 326 } 327 } 328 329 333 public void fireChangeEvent(int detail, IDebugElement source) { 334 fireEvent(new DebugEvent(source, DebugEvent.CHANGE, detail)); 335 } 336 337 338 341 private void fireEvent(DebugEvent event) { 342 if(DebugPlugin.getDefault() != null) 343 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event}); 344 } 345 346 347 350 public void handleDebugEvents(DebugEvent[] events) { 351 for (int i = 0; i < events.length; i++) { 352 DebugEvent event = events[i]; 353 Object source= event.getSource(); 354 switch (event.getKind()) { 355 case DebugEvent.TERMINATE : 356 if(checkIfEditorShouldClose(source)) 357 closeEditor(); 358 break; 359 360 case DebugEvent.CHANGE : 361 if(!source.equals(fObject)) 362 return; 363 IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow(); 365 if (window == null) { 366 return; 367 } 368 IWorkbenchPage p= window.getActivePage(); 369 if (p == null) { 370 return; 371 } 372 IViewPart fLaunchView= p.findView(IDebugUIConstants.ID_DEBUG_VIEW); 373 if (fLaunchView instanceof ISelectionChangedListener) { 374 ISelection fSelection = ((LaunchView)fLaunchView).getViewer().getSelection(); 375 ((LaunchView)fLaunchView).clearSourceSelection(((IStackFrame)source).getThread()); 377 ((LaunchView)fLaunchView).getViewer().setSelection(fSelection, true); 378 } 379 break; 380 } 381 } 382 } 383 384 385 391 protected boolean checkIfEditorShouldClose(Object source) { 392 if (fObject instanceof IDebugElement && source instanceof IDebugElement) { 394 IDebugElement element = (IDebugElement)fObject; 395 IDebugElement sourceElement = (IDebugElement)source; 396 return sourceElement.equals(element.getDebugTarget()); 397 } 398 return false; 399 } 400 401 404 protected void closeEditor() 405 { 406 final IEditorPart editor = this; 407 DebugUIPlugin.getStandardDisplay().syncExec( 408 new Runnable () { 409 public void run() { 410 IWorkbenchWindow activeWorkbenchWindow = DebugUIPlugin.getActiveWorkbenchWindow(); 411 if (activeWorkbenchWindow != null) { 412 IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage(); 413 if (activePage != null) { 414 activePage.closeEditor(editor,false); 415 } 416 } 417 } 418 }); 419 } 420 421 422 425 public void dispose() { 426 DebugPlugin.getDefault().removeDebugEventListener(this); 427 super.dispose(); 428 } 429 430 431 } 432 433 434 | Popular Tags |