KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > CommonSourceNotFoundEditor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.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 /**
64  * Editor for when source is not found. Shows the source name and has
65  * a button to add new containers.
66  * Editor ID: IInternalDebugUIConstants.ID_COMMON_SOURCE_NOT_FOUND_EDITOR = org.eclipse.debug.ui.sourcelookup.CommonSourceNotFoundEditor
67  *
68  * May be subclassed if a debugger requires additional buttons on the editor. For example,
69  * a button may be added if the user has the additional option of using generated source
70  * for debugging.
71  *
72  * @see AbstractSourceLookupDirector
73  * @see CommonSourceNotFoundEditorInput
74  * @since 3.0
75  */

76 public class CommonSourceNotFoundEditor extends EditorPart implements IReusableEditor, IDebugEventSetListener {
77     
78     /**
79      * Text widgets used for this editor
80      */

81     private Text fText;
82     /**
83      * object for which the source is showing for (i.e., stackframe, breakpoint)
84      */

85     protected Object JavaDoc fObject;
86     
87     /**
88      * @see org.eclipse.ui.IEditorPart#doSave(IProgressMonitor)
89      */

90     public void doSave(IProgressMonitor monitor) {
91     }
92     
93     /**
94      * @see org.eclipse.ui.IEditorPart#doSaveAs()
95      */

96     public void doSaveAs() {
97     }
98     
99     /**
100      * @see org.eclipse.ui.IEditorPart#gotoMarker(IMarker)
101      */

102     public void gotoMarker(IMarker marker) {
103     }
104     
105     /**
106      * @see org.eclipse.ui.IEditorPart#init(IEditorSite, IEditorInput)
107      */

108     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
109         setSite(site);
110         setInput(input);
111         DebugPlugin.getDefault().addDebugEventListener(this);
112     }
113     
114     /**
115      * @see org.eclipse.ui.IEditorPart#isDirty()
116      */

117     public boolean isDirty() {
118         return false;
119     }
120     
121     /**
122      * @see org.eclipse.ui.IEditorPart#isSaveAsAllowed()
123      */

124     public boolean isSaveAsAllowed() {
125         return false;
126     }
127     
128     /**
129      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(Composite)
130      */

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); //$NON-NLS-1$
156
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     /**
168      * Handles the event when the "add source container" button is selected.
169      * Displays the <code>EditSourceLookupPathDialog</code> so the user
170      * can add additional source containers that should be searched.
171      */

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; //should not occur
188
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     /**
200      * Clears the (source not found) editor inputs associated with the stack and
201      * opening the editor again, if the item is not a stackframe. A marker will be added
202      * and the editor will be told to scroll to the marker.
203      *
204      * If it is a stackframe, the launch view will pick up the change event and open a new
205      * editor automatically.
206      *
207      */

208     public void resetEditor()
209     {
210         fireChangeEventsOnStack();
211         
212         if(fObject instanceof IStackFrame) {
213             return; //launch view will pick up from here
214
}
215
216         // close this editor and open a new editor
217
final int lineNumber = getLineNumber();
218         Runnable JavaDoc open = new Runnable JavaDoc() {
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 JavaDoc 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                                         // position to line number
233
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         // get new editor input
250
DebugUIPlugin.getStandardDisplay().asyncExec(open);
251     }
252     
253     /**
254      * Returns the line information for the given line in the given editor
255      */

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     /**
276      * Returns the line number associated with the breakpoint (marker).
277      * @return the line number to scroll to
278      */

279     protected int getLineNumber(){
280         int line = -1;
281         if(fObject instanceof IMarker) {
282             try{
283                 line=((Integer JavaDoc)((IMarker)fObject).getAttribute(IMarker.LINE_NUMBER)).intValue();
284             } catch(CoreException e){}
285         }
286         return line;
287     }
288     
289     /**
290      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
291      */

292     public void setFocus() {
293         if (fText != null) {
294             fText.setFocus();
295         }
296     }
297     
298     /**
299      * @see IReusableEditor#setInput(org.eclipse.ui.IEditorInput)
300      */

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"); //$NON-NLS-1$
309
}
310     }
311
312     /**
313      * Fires change event(s) to clear the source file history of the items in the stack.
314      */

315     protected void fireChangeEventsOnStack(){
316         if(fObject instanceof IStackFrame) {
317             fireChangeEvent(DebugEvent.CONTENT, (IStackFrame)fObject);
318         } else if(fObject instanceof IDebugElement) { //loop through all threads and clear the cached source files
319
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     /**
330      * Fire a debug change event with detail
331      * @param detail @see DebugEvent
332      */

333     public void fireChangeEvent(int detail, IDebugElement source) {
334         fireEvent(new DebugEvent(source, DebugEvent.CHANGE, detail));
335     }
336     
337     
338     /**
339      * Fire a debug event
340      */

341     private void fireEvent(DebugEvent event) {
342         if(DebugPlugin.getDefault() != null)
343             DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
344     }
345     
346     
347     /* (non-Javadoc)
348      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
349      */

350     public void handleDebugEvents(DebugEvent[] events) {
351         for (int i = 0; i < events.length; i++) {
352             DebugEvent event = events[i];
353             Object JavaDoc 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                 // Trigger a selectionChange event
364
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                     // To clear the stackframe stored in the launchView
376
((LaunchView)fLaunchView).clearSourceSelection(((IStackFrame)source).getThread());
377                     ((LaunchView)fLaunchView).getViewer().setSelection(fSelection, true);
378                 }
379                 break;
380             }
381         }
382     }
383     
384     
385     /**
386      * Checks if the source of the terminate event is associated with this editor
387      * object.
388      * @param source the source of the event
389      * @return true if the <code>source</code> is related to this editor, false otherwise
390      */

391     protected boolean checkIfEditorShouldClose(Object JavaDoc source) {
392         //Make sure terminate event is for me
393
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     /**
402      * Closes this editor.
403      */

404     protected void closeEditor()
405     {
406         final IEditorPart editor = this;
407         DebugUIPlugin.getStandardDisplay().syncExec(
408                 new Runnable JavaDoc() {
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     /* (non-Javadoc)
423      * @see org.eclipse.ui.IWorkbenchPart#dispose()
424      */

425     public void dispose() {
426         DebugPlugin.getDefault().removeDebugEventListener(this);
427         super.dispose();
428     }
429     
430     
431 }
432
433
434
Popular Tags