KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > snippeteditor > JavaSnippetEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Sebastian Davids <sdavids@gmx.de> - bug 38919
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.debug.ui.snippeteditor;
13
14  
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.PrintStream JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IWorkspace;
24 import org.eclipse.core.resources.IncrementalProjectBuilder;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.QualifiedName;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.debug.core.DebugEvent;
33 import org.eclipse.debug.core.DebugException;
34 import org.eclipse.debug.core.DebugPlugin;
35 import org.eclipse.debug.core.IDebugEventFilter;
36 import org.eclipse.debug.core.ILaunchConfiguration;
37 import org.eclipse.debug.core.model.IBreakpoint;
38 import org.eclipse.debug.core.model.IDebugElement;
39 import org.eclipse.debug.core.model.IDebugTarget;
40 import org.eclipse.debug.core.model.IStackFrame;
41 import org.eclipse.debug.core.model.IThread;
42 import org.eclipse.debug.core.model.IValue;
43 import org.eclipse.debug.ui.DebugUITools;
44 import org.eclipse.debug.ui.IDebugModelPresentation;
45 import org.eclipse.debug.ui.IDebugUIConstants;
46 import org.eclipse.debug.ui.IValueDetailListener;
47 import org.eclipse.debug.ui.InspectPopupDialog;
48 import org.eclipse.jdt.core.CompletionRequestor;
49 import org.eclipse.jdt.core.IJavaElement;
50 import org.eclipse.jdt.core.IJavaProject;
51 import org.eclipse.jdt.core.JavaCore;
52 import org.eclipse.jdt.core.JavaModelException;
53 import org.eclipse.jdt.core.eval.IEvaluationContext;
54 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
55 import org.eclipse.jdt.debug.core.IJavaStackFrame;
56 import org.eclipse.jdt.debug.core.IJavaThread;
57 import org.eclipse.jdt.debug.core.IJavaType;
58 import org.eclipse.jdt.debug.core.IJavaValue;
59 import org.eclipse.jdt.debug.core.JDIDebugModel;
60 import org.eclipse.jdt.debug.eval.EvaluationManager;
61 import org.eclipse.jdt.debug.eval.IClassFileEvaluationEngine;
62 import org.eclipse.jdt.debug.eval.IEvaluationListener;
63 import org.eclipse.jdt.debug.eval.IEvaluationResult;
64 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
65 import org.eclipse.jdt.internal.debug.ui.JDIContentAssistPreference;
66 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
67 import org.eclipse.jdt.internal.debug.ui.JDISourceViewer;
68 import org.eclipse.jdt.internal.debug.ui.JavaDebugImages;
69 import org.eclipse.jdt.internal.debug.ui.JavaDebugOptionsManager;
70 import org.eclipse.jdt.internal.debug.ui.actions.DisplayAction;
71 import org.eclipse.jdt.internal.debug.ui.actions.EvaluateAction;
72 import org.eclipse.jdt.internal.debug.ui.actions.PopupInspectAction;
73 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression;
74 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
75 import org.eclipse.jdt.launching.IVMInstall;
76 import org.eclipse.jdt.launching.JavaRuntime;
77 import org.eclipse.jdt.ui.IContextMenuConstants;
78 import org.eclipse.jdt.ui.JavaUI;
79 import org.eclipse.jdt.ui.PreferenceConstants;
80 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
81 import org.eclipse.jface.action.Action;
82 import org.eclipse.jface.action.IMenuManager;
83 import org.eclipse.jface.dialogs.ErrorDialog;
84 import org.eclipse.jface.dialogs.MessageDialog;
85 import org.eclipse.jface.operation.IRunnableWithProgress;
86 import org.eclipse.jface.preference.IPreferenceStore;
87 import org.eclipse.jface.text.BadLocationException;
88 import org.eclipse.jface.text.IDocument;
89 import org.eclipse.jface.text.ITextSelection;
90 import org.eclipse.jface.text.contentassist.ContentAssistant;
91 import org.eclipse.jface.text.contentassist.IContentAssistant;
92 import org.eclipse.jface.text.source.ISourceViewer;
93 import org.eclipse.jface.text.source.IVerticalRuler;
94 import org.eclipse.jface.text.source.SourceViewerConfiguration;
95 import org.eclipse.jface.util.PropertyChangeEvent;
96 import org.eclipse.swt.custom.BusyIndicator;
97 import org.eclipse.swt.graphics.Image;
98 import org.eclipse.swt.widgets.Composite;
99 import org.eclipse.swt.widgets.Control;
100 import org.eclipse.swt.widgets.Display;
101 import org.eclipse.swt.widgets.Shell;
102 import org.eclipse.ui.IEditorInput;
103 import org.eclipse.ui.IEditorSite;
104 import org.eclipse.ui.IPageLayout;
105 import org.eclipse.ui.IPartListener2;
106 import org.eclipse.ui.IViewPart;
107 import org.eclipse.ui.IWorkbenchPage;
108 import org.eclipse.ui.IWorkbenchPartReference;
109 import org.eclipse.ui.PartInitException;
110 import org.eclipse.ui.PlatformUI;
111 import org.eclipse.ui.actions.WorkspaceModifyOperation;
112 import org.eclipse.ui.dialogs.SaveAsDialog;
113 import org.eclipse.ui.editors.text.EditorsUI;
114 import org.eclipse.ui.part.EditorActionBarContributor;
115 import org.eclipse.ui.part.FileEditorInput;
116 import org.eclipse.ui.part.IShowInTargetList;
117 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
118 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
119 import org.eclipse.ui.texteditor.IDocumentProvider;
120 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
121 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
122 import org.eclipse.ui.texteditor.TextOperationAction;
123
124 import com.sun.jdi.InvocationException;
125 import com.sun.jdi.ObjectReference;
126
127 /**
128  * An editor for Java snippets.
129  */

130 public class JavaSnippetEditor extends AbstractDecoratedTextEditor implements IDebugEventFilter, IEvaluationListener, IValueDetailListener {
131     public static final String JavaDoc IMPORTS_CONTEXT = "SnippetEditor.imports"; //$NON-NLS-1$
132

133     public final static int RESULT_DISPLAY= 1;
134     public final static int RESULT_RUN= 2;
135     public final static int RESULT_INSPECT= 3;
136     
137     private int fResultMode; // one of the RESULT_* constants
138

139     private IJavaProject fJavaProject;
140     private IEvaluationContext fEvaluationContext;
141     private IDebugTarget fVM;
142     private String JavaDoc[] fLaunchedClassPath;
143     private String JavaDoc fLaunchedWorkingDir;
144     private String JavaDoc fLaunchedVMArgs;
145     private IVMInstall fLaunchedVM;
146     private List JavaDoc fSnippetStateListeners;
147     
148     private boolean fEvaluating;
149     private IJavaThread fThread;
150     private boolean fStepFiltersSetting;
151     
152     private int fSnippetStart;
153     private int fSnippetEnd;
154     
155     private String JavaDoc[] fImports= null;
156     
157     private Image fOldTitleImage= null;
158     private IClassFileEvaluationEngine fEngine= null;
159     
160     /**
161      * The debug model presentation used for computing toString
162      */

163     private IDebugModelPresentation fPresentation= DebugUITools.newDebugModelPresentation(JDIDebugModel.getPluginIdentifier());
164     /**
165      * The result of a toString evaluation returned asynchronously by the
166      * debug model.
167      */

168     private String JavaDoc fResult;
169
170     /**
171      * A thread that waits to have a
172      * thread to perform an evaluation in.
173      */

174     private static class WaitThread extends Thread JavaDoc {
175         /**
176          * The display used for event dispatching.
177          */

178         private Display fDisplay;
179         
180         /**
181          * Indicates whether to continue event queue dispatching.
182          */

183         private volatile boolean fContinueEventDispatching = true;
184         
185         private Object JavaDoc fLock;
186         /**
187          * Creates a "wait" thread
188          *
189          * @param display the display to be used to read and dispatch events
190          * @param lock the monitor to wait on
191          */

192         private WaitThread(Display display, Object JavaDoc lock) {
193             super("Snippet Wait Thread"); //$NON-NLS-1$
194
setDaemon(true);
195             fDisplay = display;
196             fLock= lock;
197         }
198         public void run() {
199             try {
200                 synchronized (fLock) {
201                     //should be notified out of #setThread(IJavaThread)
202
fLock.wait(10000);
203                 }
204             } catch (InterruptedException JavaDoc e) {
205             } finally {
206                 // Make sure that all events in the asynchronous event queue
207
// are dispatched.
208
fDisplay.syncExec(new Runnable JavaDoc() {
209                     public void run() {
210                         // do nothing
211
}
212                 });
213                 
214                 // Stop event dispatching
215
fContinueEventDispatching= false;
216                 
217                 // Force the event loop to return from sleep () so that
218
// it stops event dispatching.
219
fDisplay.asyncExec(null);
220             }
221         }
222         /**
223          * Processes events.
224          */

225         protected void block() {
226             if (fDisplay == Display.getCurrent()) {
227                 while (fContinueEventDispatching) {
228                     if (!fDisplay.readAndDispatch())
229                         fDisplay.sleep();
230                 }
231             }
232         }
233     }
234     
235     /**
236      * Listens for part activation to set scrapbook active system property
237      * for action enablement.
238      */

239     private IPartListener2 fActivationListener = new IPartListener2() {
240
241         public void partActivated(IWorkbenchPartReference partRef) {
242             if ("org.eclipse.jdt.debug.ui.SnippetEditor".equals(partRef.getId())) { //$NON-NLS-1$
243
System.setProperty(JDIDebugUIPlugin.getUniqueIdentifier() + ".scrapbookActive", "true"); //$NON-NLS-1$ //$NON-NLS-2$
244
} else {
245                 System.setProperty(JDIDebugUIPlugin.getUniqueIdentifier() + ".scrapbookActive", "false"); //$NON-NLS-1$ //$NON-NLS-2$
246
}
247         }
248
249         public void partBroughtToTop(IWorkbenchPartReference partRef) {
250         }
251
252         public void partClosed(IWorkbenchPartReference partRef) {
253         }
254
255         public void partDeactivated(IWorkbenchPartReference partRef) {
256         }
257
258         public void partHidden(IWorkbenchPartReference partRef) {
259         }
260
261         public void partInputChanged(IWorkbenchPartReference partRef) {
262         }
263
264         public void partOpened(IWorkbenchPartReference partRef) {
265         }
266
267         public void partVisible(IWorkbenchPartReference partRef) {
268         }
269         
270     };
271     
272     public JavaSnippetEditor() {
273         super();
274         setDocumentProvider(JDIDebugUIPlugin.getDefault().getSnippetDocumentProvider());
275         IPreferenceStore store = new ChainedPreferenceStore(new IPreferenceStore[] {
276                 PreferenceConstants.getPreferenceStore(),
277                 EditorsUI.getPreferenceStore()});
278         setSourceViewerConfiguration(new JavaSnippetViewerConfiguration(JDIDebugUIPlugin.getDefault().getJavaTextTools(), store, this));
279         fSnippetStateListeners= new ArrayList JavaDoc(4);
280         setPreferenceStore(store);
281         setEditorContextMenuId("#JavaSnippetEditorContext"); //$NON-NLS-1$
282
setRulerContextMenuId("#JavaSnippetRulerContext"); //$NON-NLS-1$
283
}
284     
285     /* (non-Javadoc)
286      * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSetInput(org.eclipse.ui.IEditorInput)
287      */

288     protected void doSetInput(IEditorInput input) throws CoreException {
289         super.doSetInput(input);
290         IFile file= getFile();
291         if (file != null) {
292             String JavaDoc property= file.getPersistentProperty(new QualifiedName(JDIDebugUIPlugin.getUniqueIdentifier(), IMPORTS_CONTEXT));
293             if (property != null) {
294                 fImports = JavaDebugOptionsManager.parseList(property);
295             }
296         }
297     }
298         
299     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
300         super.init(site, input);
301         site.getWorkbenchWindow().getPartService().addPartListener(fActivationListener);
302     }
303
304     /* (non-Javadoc)
305      * @see org.eclipse.ui.IWorkbenchPart#dispose()
306      */

307     public void dispose() {
308         shutDownVM();
309         fPresentation.dispose();
310         fSnippetStateListeners= null;
311         ((JDISourceViewer) getSourceViewer()).dispose();
312         getSite().getWorkbenchWindow().getPartService().removePartListener(fActivationListener);
313         super.dispose();
314     }
315     
316     /**
317      * Actions for the editor popup menu
318      * @see org.eclipse.ui.texteditor.AbstractTextEditor#createActions()
319      */

320     protected void createActions() {
321         super.createActions();
322         if (getFile() != null) {
323             Action action = new TextOperationAction(SnippetMessages.getBundle(), "SnippetEditor.ContentAssistProposal.", this, ISourceViewer.CONTENTASSIST_PROPOSALS); //$NON-NLS-1$
324
action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
325             setAction("ContentAssistProposal", action);//$NON-NLS-1$
326
setAction("ShowInPackageView", new ShowInPackageViewAction(this)); //$NON-NLS-1$
327
setAction("Stop", new StopAction(this)); //$NON-NLS-1$
328
setAction("SelectImports", new SelectImportsAction(this)); //$NON-NLS-1$
329
}
330     }
331     
332     /* (non-Javadoc)
333      * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
334      */

335     protected void editorContextMenuAboutToShow(IMenuManager menu) {
336         super.editorContextMenuAboutToShow(menu);
337         addGroup(menu, ITextEditorActionConstants.GROUP_EDIT, IContextMenuConstants.GROUP_GENERATE);
338         addGroup(menu, ITextEditorActionConstants.GROUP_FIND, IContextMenuConstants.GROUP_SEARCH);
339         addGroup(menu, IContextMenuConstants.GROUP_SEARCH, IContextMenuConstants.GROUP_SHOW);
340         if (getFile() != null) {
341             addAction(menu, IContextMenuConstants.GROUP_SHOW, "ShowInPackageView"); //$NON-NLS-1$
342
addAction(menu, IContextMenuConstants.GROUP_ADDITIONS, "Run"); //$NON-NLS-1$
343
addAction(menu, IContextMenuConstants.GROUP_ADDITIONS, "Stop"); //$NON-NLS-1$
344
addAction(menu, IContextMenuConstants.GROUP_ADDITIONS, "SelectImports"); //$NON-NLS-1$
345
}
346     }
347
348     protected boolean isVMLaunched() {
349         return fVM != null;
350     }
351     
352     public boolean isEvaluating() {
353         return fEvaluating;
354     }
355     
356     public void evalSelection(int resultMode) {
357         if (!isInJavaProject()) {
358             reportNotInJavaProjectError();
359             return;
360         }
361         if (isEvaluating()) {
362             return;
363         }
364         
365         checkCurrentProject();
366         
367         evaluationStarts();
368
369         fResultMode= resultMode;
370         buildAndLaunch();
371         
372         if (fVM == null) {
373             evaluationEnds();
374             return;
375         }
376         fireEvalStateChanged();
377
378         ITextSelection selection= (ITextSelection) getSelectionProvider().getSelection();
379         String JavaDoc snippet= selection.getText();
380         fSnippetStart= selection.getOffset();
381         fSnippetEnd= fSnippetStart + selection.getLength();
382         
383         evaluate(snippet);
384     }
385     
386     /**
387      * Checks if the page has been copied/moved to a different project or the project has been renamed.
388      * Updates the launch configuration template if a copy/move/rename has occurred.
389      */

390     protected void checkCurrentProject() {
391         IFile file= getFile();
392         if (file == null) {
393             return;
394         }
395         try {
396             ILaunchConfiguration config = ScrapbookLauncher.getLaunchConfigurationTemplate(file);
397             if (config != null) {
398                 String JavaDoc projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
399                 IJavaProject pro = JavaCore.create(file.getProject());
400                 if (!pro.getElementName().equals(projectName)) {
401                     //the page has been moved to a "different" project
402
ScrapbookLauncher.setLaunchConfigMemento(file, null);
403                 }
404             }
405         } catch (CoreException ce) {
406             JDIDebugUIPlugin.log(ce);
407             ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.evaluating"), null, ce.getStatus()); //$NON-NLS-1$
408
evaluationEnds();
409             return;
410             
411         }
412     }
413     
414     protected void buildAndLaunch() {
415         IJavaProject javaProject= getJavaProject();
416         if (javaProject == null) {
417             return;
418         }
419         boolean build = !javaProject.getProject().getWorkspace().isAutoBuilding()
420             || !javaProject.hasBuildState();
421         
422         if (build) {
423             if (!performIncrementalBuild()) {
424                 return;
425             }
426         }
427         
428         boolean changed= classPathHasChanged();
429         if (!changed) {
430             changed = workingDirHasChanged();
431         }
432         if (!changed) {
433             changed = vmHasChanged();
434         }
435         if (!changed) {
436             changed = vmArgsChanged();
437         }
438         boolean launch= fVM == null || changed;
439
440         if (changed) {
441             shutDownVM();
442         }
443     
444         if (fVM == null) {
445             checkMultipleEditors();
446         }
447         if (launch && fVM == null) {
448             launchVM();
449             fVM= ScrapbookLauncher.getDefault().getDebugTarget(getFile());
450         }
451     }
452     
453     protected boolean performIncrementalBuild() {
454         IRunnableWithProgress r= new IRunnableWithProgress() {
455             public void run(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
456                 try {
457                     getJavaProject().getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, pm);
458                 } catch (CoreException e) {
459                     throw new InvocationTargetException JavaDoc(e);
460                 }
461             }
462         };
463         try {
464             PlatformUI.getWorkbench().getProgressService().run(true, false, r);
465         } catch (InterruptedException JavaDoc e) {
466             JDIDebugUIPlugin.log(e);
467             evaluationEnds();
468             return false;
469         } catch (InvocationTargetException JavaDoc e) {
470             JDIDebugUIPlugin.log(e);
471             evaluationEnds();
472             return false;
473         }
474         return true;
475     }
476     
477     protected void checkMultipleEditors() {
478         fVM= ScrapbookLauncher.getDefault().getDebugTarget(getFile());
479         //multiple editors are opened on the same page
480
if (fVM != null) {
481             DebugPlugin.getDefault().addDebugEventFilter(this);
482             try {
483                 IThread[] threads= fVM.getThreads();
484                 for (int i = 0; i < threads.length; i++) {
485                     IThread iThread = threads[i];
486                     if (iThread.isSuspended()) {
487                         iThread.resume();
488                     }
489                 }
490             } catch (DebugException de) {
491                 JDIDebugUIPlugin.log(de);
492             }
493         }
494     }
495     
496     protected void setImports(String JavaDoc[] imports) {
497         fImports= imports;
498         IFile file= getFile();
499         if (file == null) {
500             return;
501         }
502         String JavaDoc serialized= null;
503         if (imports != null) {
504             serialized= JavaDebugOptionsManager.serializeList(imports);
505         }
506         // persist
507
try {
508             file.setPersistentProperty(new QualifiedName(JDIDebugUIPlugin.getUniqueIdentifier(), IMPORTS_CONTEXT), serialized);
509         } catch (CoreException e) {
510             JDIDebugUIPlugin.log(e);
511             ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.imports"), null, e.getStatus()); //$NON-NLS-1$
512
}
513     }
514     
515     protected String JavaDoc[] getImports() {
516         return fImports;
517     }
518             
519     protected IEvaluationContext getEvaluationContext() {
520         if (fEvaluationContext == null) {
521             IJavaProject project= getJavaProject();
522             if (project != null) {
523                 fEvaluationContext= project.newEvaluationContext();
524             }
525         }
526         if (fEvaluationContext != null) {
527             if (getImports() != null) {
528                 fEvaluationContext.setImports(getImports());
529             } else {
530                 fEvaluationContext.setImports(new String JavaDoc[]{});
531             }
532         }
533         return fEvaluationContext;
534     }
535     
536     protected IJavaProject getJavaProject() {
537         if (fJavaProject == null) {
538             try {
539                 fJavaProject = findJavaProject();
540             } catch (JavaModelException e) {
541                 JDIDebugUIPlugin.log(e);
542                 showError(e.getStatus());
543             }
544         }
545         return fJavaProject;
546     }
547     
548     protected void shutDownVM() {
549         DebugPlugin.getDefault().removeDebugEventFilter(this);
550
551         // The real shut down
552
IDebugTarget target= fVM;
553         if (fVM != null) {
554             try {
555                 IBreakpoint bp = ScrapbookLauncher.getDefault().getMagicBreakpoint(fVM);
556                 if (bp != null) {
557                     fVM.breakpointRemoved(bp, null);
558                 }
559                 if (getThread() != null) {
560                     getThread().resume();
561                 }
562                 
563                 fVM.terminate();
564             } catch (DebugException e) {
565                 JDIDebugUIPlugin.log(e);
566                 ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.shutdown"), null, e.getStatus()); //$NON-NLS-1$
567
return;
568             }
569             vmTerminated();
570             ScrapbookLauncher.getDefault().cleanup(target);
571         }
572     }
573     
574     /**
575      * The VM has terminated, update state
576      */

577     protected void vmTerminated() {
578         fVM= null;
579         fThread= null;
580         fEvaluationContext= null;
581         fLaunchedClassPath= null;
582         if (fEngine != null) {
583             fEngine.dispose();
584         }
585         fEngine= null;
586         fireEvalStateChanged();
587     }
588     
589     public void addSnippetStateChangedListener(ISnippetStateChangedListener listener) {
590         if (fSnippetStateListeners != null && !fSnippetStateListeners.contains(listener)) {
591             fSnippetStateListeners.add(listener);
592         }
593     }
594     
595     public void removeSnippetStateChangedListener(ISnippetStateChangedListener listener) {
596         if (fSnippetStateListeners != null) {
597             fSnippetStateListeners.remove(listener);
598         }
599     }
600
601     protected void fireEvalStateChanged() {
602         Runnable JavaDoc r= new Runnable JavaDoc() {
603             public void run() {
604                 Shell shell= getShell();
605                 if (fSnippetStateListeners != null && shell != null && !shell.isDisposed()) {
606                     List JavaDoc v= new ArrayList JavaDoc(fSnippetStateListeners);
607                     for (int i= 0; i < v.size(); i++) {
608                         ISnippetStateChangedListener l= (ISnippetStateChangedListener) v.get(i);
609                         l.snippetStateChanged(JavaSnippetEditor.this);
610                     }
611                 }
612             }
613         };
614         Shell shell= getShell();
615         if (shell != null) {
616             getShell().getDisplay().asyncExec(r);
617         }
618     }
619     
620     protected void evaluate(String JavaDoc snippet) {
621         if (getThread() == null) {
622             WaitThread eThread= new WaitThread(Display.getCurrent(), this);
623             eThread.start();
624             eThread.block();
625         }
626         if (getThread() == null) {
627             IStatus status = new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR,
628                 SnippetMessages.getString("SnippetEditor.error.nocontext"), null); //$NON-NLS-1$
629
ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.evaluating"), null, status); //$NON-NLS-1$
630
evaluationEnds();
631             return;
632         }
633         boolean hitBreakpoints= JDIDebugModel.getPreferences().getBoolean(JDIDebugModel.PREF_SUSPEND_FOR_BREAKPOINTS_DURING_EVALUATION);
634         try {
635             getEvaluationEngine().evaluate(snippet,getThread(), this, hitBreakpoints);
636         } catch (DebugException e) {
637             JDIDebugUIPlugin.log(e);
638             ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.evaluating"), null, e.getStatus()); //$NON-NLS-1$
639
evaluationEnds();
640         }
641     }
642     
643     /* (non-Javadoc)
644      * @see org.eclipse.jdt.debug.eval.IEvaluationListener#evaluationComplete(org.eclipse.jdt.debug.eval.IEvaluationResult)
645      */

646     public void evaluationComplete(IEvaluationResult result) {
647             boolean severeErrors = false;
648             if (result.hasErrors()) {
649                 String JavaDoc[] errors = result.getErrorMessages();
650                 severeErrors = errors.length > 0;
651                 if (result.getException() != null) {
652                     showException(result.getException());
653                 }
654                 showAllErrors(errors);
655             }
656             IJavaValue value= result.getValue();
657             if (value != null && !severeErrors) {
658                 switch (fResultMode) {
659                 case RESULT_DISPLAY:
660                     displayResult(value);
661                     break;
662                 case RESULT_INSPECT:
663                     String JavaDoc snippet= result.getSnippet().trim();
664                     int snippetLength= snippet.length();
665                     if (snippetLength > 30) {
666                         snippet = snippet.substring(0, 15) + SnippetMessages.getString("SnippetEditor.ellipsis") + snippet.substring(snippetLength - 15, snippetLength); //$NON-NLS-1$
667
}
668                     snippet= snippet.replace('\n', ' ');
669                     snippet= snippet.replace('\r', ' ');
670                     snippet= snippet.replace('\t', ' ');
671                     JavaInspectExpression exp = new JavaInspectExpression(snippet, value);
672                     showExpression(exp);
673                     break;
674                 case RESULT_RUN:
675                     // no action
676
break;
677                 }
678             }
679             evaluationEnds();
680     }
681     
682     /**
683      * Make the expression view visible or open one
684      * if required.
685      */

686     protected void showExpressionView() {
687         Runnable JavaDoc r = new Runnable JavaDoc() {
688             public void run() {
689                 IWorkbenchPage page = JDIDebugUIPlugin.getActivePage();
690                 if (page != null) {
691                     IViewPart part = page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
692                     if (part == null) {
693                         try {
694                             page.showView(IDebugUIConstants.ID_EXPRESSION_VIEW);
695                         } catch (PartInitException e) {
696                             JDIDebugUIPlugin.log(e);
697                             showError(e.getStatus());
698                         }
699                     } else {
700                         page.bringToTop(part);
701                     }
702                 }
703             }
704         };
705         
706         async(r);
707     }
708         
709     protected void codeComplete(CompletionRequestor requestor) throws JavaModelException {
710         ITextSelection selection= (ITextSelection)getSelectionProvider().getSelection();
711         int start= selection.getOffset();
712         String JavaDoc snippet= getSourceViewer().getDocument().get();
713         IEvaluationContext e= getEvaluationContext();
714         if (e != null) {
715             e.codeComplete(snippet, start, requestor);
716         }
717     }
718          
719     protected IJavaElement[] codeResolve() throws JavaModelException {
720         ISourceViewer viewer= getSourceViewer();
721         if (viewer == null) {
722             return null;
723         }
724         ITextSelection selection= (ITextSelection) getSelectionProvider().getSelection();
725         int start= selection.getOffset();
726         int len= selection.getLength();
727         
728         String JavaDoc snippet= viewer.getDocument().get();
729         IEvaluationContext e= getEvaluationContext();
730         if (e != null) {
731             return e.codeSelect(snippet, start, len);
732         }
733         return null;
734     }
735     
736     protected void showError(IStatus status) {
737         evaluationEnds();
738         if (!status.isOK()) {
739             ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.evaluating2"), null, status); //$NON-NLS-1$
740
}
741     }
742     
743     protected void showError(String JavaDoc message) {
744         Status status= new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, message, null);
745         showError(status);
746     }
747     
748     protected void displayResult(IJavaValue result) {
749         StringBuffer JavaDoc resultString= new StringBuffer JavaDoc();
750         try {
751             IJavaType type = result.getJavaType();
752             if (type != null) {
753                 String JavaDoc sig= type.getSignature();
754                 if ("V".equals(sig)) { //$NON-NLS-1$
755
resultString.append(SnippetMessages.getString("SnippetEditor.noreturnvalue")); //$NON-NLS-1$
756
} else {
757                     if (sig != null) {
758                         resultString.append(SnippetMessages.getFormattedString("SnippetEditor.typename", result.getReferenceTypeName())); //$NON-NLS-1$
759
} else {
760                         resultString.append(" "); //$NON-NLS-1$
761
}
762                     resultString.append(DisplayAction.trimDisplayResult(evaluateToString(result)));
763                 }
764             } else {
765                 resultString.append(DisplayAction.trimDisplayResult(result.getValueString()));
766             }
767         } catch(DebugException e) {
768             JDIDebugUIPlugin.log(e);
769             ErrorDialog.openError(getShell(), SnippetMessages.getString("SnippetEditor.error.toString"), null, e.getStatus()); //$NON-NLS-1$
770
}
771         
772         final String JavaDoc message = resultString.toString();
773         Runnable JavaDoc r = new Runnable JavaDoc() {
774             public void run() {
775                 try {
776                     getSourceViewer().getDocument().replace(fSnippetEnd, 0, message);
777                     selectAndReveal(fSnippetEnd, message.length());
778                 } catch (BadLocationException e) {
779                 }
780             }
781         };
782         async(r);
783     }
784     
785     /**
786      * Returns the result of evaluating 'toString' on the given
787      * value.
788      *
789      * @param value object or primitive data type the 'toString'
790      * is required for
791      * @return the result of evaluating toString
792      * @exception DebugException if an exception occurs during the
793      * evaluation.
794      */

795     protected synchronized String JavaDoc evaluateToString(IJavaValue value) {
796         fResult= null;
797         fPresentation.computeDetail(value, this);
798         if (fResult == null) {
799             try {
800                 wait(10000);
801             } catch (InterruptedException JavaDoc e) {
802                 return SnippetMessages.getString("SnippetEditor.error.interrupted"); //$NON-NLS-1$
803
}
804         }
805         return fResult;
806     }
807     
808     /* (non-Javadoc)
809      * @see org.eclipse.debug.ui.IValueDetailListener#detailComputed(org.eclipse.debug.core.model.IValue, java.lang.String)
810      */

811     public synchronized void detailComputed(IValue value, final String JavaDoc result) {
812         fResult= result;
813         this.notifyAll();
814     }
815     
816     protected void showAllErrors(final String JavaDoc[] errors) {
817         IDocument document = getSourceViewer().getDocument();
818         String JavaDoc delimiter = document.getLegalLineDelimiters()[0];
819         
820         final StringBuffer JavaDoc errorString = new StringBuffer JavaDoc();
821         for (int i = 0; i < errors.length; i++) {
822             errorString.append(errors[i] + delimiter);
823         }
824         
825         Runnable JavaDoc r = new Runnable JavaDoc() {
826             public void run() {
827                 try {
828                     getSourceViewer().getDocument().replace(fSnippetStart, 0, errorString.toString());
829                     selectAndReveal(fSnippetStart, errorString.length());
830                 } catch (BadLocationException e) {
831                 }
832             }
833         };
834         async(r);
835     }
836     
837     private void showExpression(final JavaInspectExpression expression) {
838         Runnable JavaDoc r = new Runnable JavaDoc() {
839             public void run() {
840                 new InspectPopupDialog(getShell(), EvaluateAction.getPopupAnchor(getSourceViewer().getTextWidget()), PopupInspectAction.ACTION_DEFININITION_ID, expression).open();
841             }
842         };
843         async(r);
844     }
845     
846     
847     protected void showException(Throwable JavaDoc exception) {
848         if (exception instanceof DebugException) {
849             DebugException de = (DebugException)exception;
850             Throwable JavaDoc t= de.getStatus().getException();
851             if (t != null) {
852                 // show underlying exception
853
showUnderlyingException(t);
854                 return;
855             }
856         }
857         ByteArrayOutputStream JavaDoc bos= new ByteArrayOutputStream JavaDoc();
858         PrintStream JavaDoc ps= new PrintStream JavaDoc(bos, true);
859         exception.printStackTrace(ps);
860         
861         final String JavaDoc message = bos.toString();
862         Runnable JavaDoc r = new Runnable JavaDoc() {
863             public void run() {
864                 try {
865                     getSourceViewer().getDocument().replace(fSnippetEnd, 0, message);
866                     selectAndReveal(fSnippetEnd, message.length());
867                 } catch (BadLocationException e) {
868                 }
869             }
870         };
871         async(r);
872     }
873     
874     protected void showUnderlyingException(Throwable JavaDoc t) {
875         if (t instanceof InvocationException) {
876             InvocationException ie= (InvocationException)t;
877             ObjectReference ref= ie.exception();
878             String JavaDoc eName= ref.referenceType().name();
879             final String JavaDoc message= SnippetMessages.getFormattedString("SnippetEditor.exception", eName); //$NON-NLS-1$
880
Runnable JavaDoc r = new Runnable JavaDoc() {
881                 public void run() {
882                     try {
883                         getSourceViewer().getDocument().replace(fSnippetEnd, 0, message);
884                         selectAndReveal(fSnippetEnd, message.length());
885                     } catch (BadLocationException e) {
886                     }
887                 }
888             };
889             async(r);
890         } else {
891             showException(t);
892         }
893     }
894     
895     protected IJavaProject findJavaProject() throws JavaModelException {
896         IFile file = getFile();
897         if (file != null) {
898             IProject p= file.getProject();
899             try {
900                 if (p.getNature(JavaCore.NATURE_ID) != null) {
901                     return JavaCore.create(p);
902                 }
903             } catch (CoreException ce) {
904                 throw new JavaModelException(ce);
905             }
906         }
907         return null;
908     }
909         
910     protected boolean classPathHasChanged() {
911         String JavaDoc[] classpath= getClassPath(getJavaProject());
912         if (fLaunchedClassPath != null && !classPathsEqual(fLaunchedClassPath, classpath)) {
913             MessageDialog.openWarning(getShell(), SnippetMessages.getString("SnippetEditor.warning"), SnippetMessages.getString("SnippetEditor.warning.cpchange")); //$NON-NLS-2$ //$NON-NLS-1$
914
return true;
915         }
916         return false;
917     }
918     
919     protected boolean workingDirHasChanged() {
920         String JavaDoc wd = getWorkingDirectoryAttribute();
921         boolean changed = false;
922         if (wd == null || fLaunchedWorkingDir == null) {
923             if (wd != fLaunchedWorkingDir) {
924                 changed = true;
925             }
926         } else {
927             if (!wd.equals(fLaunchedWorkingDir)) {
928                 changed = true;
929             }
930         }
931         if (changed && fVM != null) {
932             MessageDialog.openWarning(getShell(), SnippetMessages.getString("SnippetEditor.Warning_1"), SnippetMessages.getString("SnippetEditor.The_working_directory_has_changed._Restarting_the_evaluation_context._2")); //$NON-NLS-1$ //$NON-NLS-2$
933
}
934         return changed;
935     }
936     
937     protected boolean vmArgsChanged() {
938         String JavaDoc args = getVMArgsAttribute();
939         boolean changed = false;
940         if (args == null || fLaunchedVMArgs == null) {
941             if (args != fLaunchedVMArgs) {
942                 changed = true;
943             }
944         } else {
945             if (!args.equals(fLaunchedVMArgs)) {
946                 changed = true;
947             }
948         }
949         if (changed && fVM != null) {
950             MessageDialog.openWarning(getShell(), SnippetMessages.getString("SnippetEditor.Warning_1"), SnippetMessages.getString("SnippetEditor.1")); //$NON-NLS-1$ //$NON-NLS-2$
951
}
952         return changed;
953     }
954     
955     protected boolean vmHasChanged() {
956         IVMInstall vm = getVMInstall();
957         boolean changed = false;
958         if (vm == null || fLaunchedVM == null) {
959             if (vm != fLaunchedVM) {
960                 changed = true;
961             }
962         } else {
963             if (!vm.equals(fLaunchedVM)) {
964                 changed = true;
965             }
966         }
967         if (changed && fVM != null) {
968             MessageDialog.openWarning(getShell(), SnippetMessages.getString("SnippetEditor.Warning_1"), SnippetMessages.getString("SnippetEditor.The_JRE_has_changed._Restarting_the_evaluation_context._2")); //$NON-NLS-1$ //$NON-NLS-2$
969
}
970         return changed;
971     }
972             
973     protected boolean classPathsEqual(String JavaDoc[] path1, String JavaDoc[] path2) {
974         if (path1.length != path2.length) {
975             return false;
976         }
977         for (int i= 0; i < path1.length; i++) {
978             if (!path1[i].equals(path2[i])) {
979                 return false;
980             }
981         }
982         return true;
983     }
984         
985     protected synchronized void evaluationStarts() {
986         if (fThread != null) {
987             try {
988                 IThread thread = fThread;
989                 fThread = null;
990                 thread.resume();
991             } catch (DebugException e) {
992                 JDIDebugUIPlugin.log(e);
993                 showException(e);
994                 return;
995             }
996         }
997         fEvaluating = true;
998         setTitleImage();
999         fireEvalStateChanged();
1000        showStatus(SnippetMessages.getString("SnippetEditor.evaluating")); //$NON-NLS-1$
1001
getSourceViewer().setEditable(false);
1002    }
1003    
1004    /**
1005     * Sets the tab image to indicate whether in the process of
1006     * evaluating or not.
1007     */

1008    protected void setTitleImage() {
1009        Image image=null;
1010        if (fEvaluating) {
1011            fOldTitleImage= getTitleImage();
1012            image= JavaDebugImages.get(JavaDebugImages.IMG_OBJS_SNIPPET_EVALUATING);
1013        } else {
1014            image= fOldTitleImage;
1015            fOldTitleImage= null;
1016        }
1017        if (image != null) {
1018            setTitleImage(image);
1019        }
1020    }
1021        
1022    protected void evaluationEnds() {
1023        Runnable JavaDoc r = new Runnable JavaDoc() {
1024            public void run() {
1025                fEvaluating= false;
1026                setTitleImage();
1027                fireEvalStateChanged();
1028                showStatus(""); //$NON-NLS-1$
1029
getSourceViewer().setEditable(true);
1030            }
1031        };
1032        async(r);
1033    }
1034    
1035    protected void showStatus(String JavaDoc message) {
1036        IEditorSite site=(IEditorSite)getSite();
1037        EditorActionBarContributor contributor= (EditorActionBarContributor)site.getActionBarContributor();
1038        contributor.getActionBars().getStatusLineManager().setMessage(message);
1039    }
1040    
1041    protected String JavaDoc[] getClassPath(IJavaProject project) {
1042        try {
1043            return JavaRuntime.computeDefaultRuntimeClassPath(project);
1044        } catch (CoreException e) {
1045            JDIDebugUIPlugin.log(e);
1046            return new String JavaDoc[0];
1047        }
1048    }
1049    
1050    protected Shell getShell() {
1051        return getSite().getShell();
1052    }
1053    
1054    /**
1055     * @see IDebugEventFilter#filterDebugEvents(DebugEvent[])
1056     */

1057    public DebugEvent[] filterDebugEvents(DebugEvent[] events) {
1058        for (int i = 0; i < events.length; i++) {
1059            DebugEvent e = events[i];
1060            Object JavaDoc source = e.getSource();
1061            if (source instanceof IDebugElement) {
1062                IDebugElement de = (IDebugElement)source;
1063                if (de instanceof IDebugTarget) {
1064                    if (de.getDebugTarget().equals(fVM)) {
1065                        if (e.getKind() == DebugEvent.TERMINATE) {
1066                            setThread(null);
1067                            Runnable JavaDoc r = new Runnable JavaDoc() {
1068                                public void run() {
1069                                    vmTerminated();
1070                                }
1071                            };
1072                            getShell().getDisplay().asyncExec(r);
1073                        }
1074                    }
1075                } else if (de instanceof IJavaThread) {
1076                    if (e.getKind() == DebugEvent.SUSPEND) {
1077                        IJavaThread jt = (IJavaThread)de;
1078                        try {
1079                            if (jt.equals(getThread()) && e.getDetail() == DebugEvent.EVALUATION) {
1080                                return null;
1081                            }
1082                            IJavaStackFrame f= (IJavaStackFrame)jt.getTopStackFrame();
1083                            if (f != null) {
1084                                IJavaDebugTarget target = (IJavaDebugTarget) f.getDebugTarget();
1085                                IBreakpoint[] bps = jt.getBreakpoints();
1086                                //last line of the eval method in ScrapbookMain1?
1087
int lineNumber = f.getLineNumber();
1088                                if (e.getDetail() == DebugEvent.STEP_END && (lineNumber == 20 || lineNumber == 21)
1089                                    && f.getDeclaringTypeName().equals("org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain1") //$NON-NLS-1$
1090
&& jt.getDebugTarget() == fVM) {
1091                                    // restore step filters
1092
target.setStepFiltersEnabled(fStepFiltersSetting);
1093                                    setThread(jt);
1094                                    return null;
1095                                } else if (e.getDetail() == DebugEvent.BREAKPOINT && bps.length > 0 && bps[0].equals(ScrapbookLauncher.getDefault().getMagicBreakpoint(jt.getDebugTarget()))) {
1096                                    // locate the 'eval' method and step over
1097
IStackFrame[] frames = jt.getStackFrames();
1098                                    for (int j = 0; j < frames.length; j++) {
1099                                        IJavaStackFrame frame = (IJavaStackFrame)frames[j];
1100                                        if (frame.getReceivingTypeName().equals("org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain1") && frame.getName().equals("eval")) { //$NON-NLS-1$ //$NON-NLS-2$
1101
// ignore step filters for this step
1102
fStepFiltersSetting = target.isStepFiltersEnabled();
1103                                            target.setStepFiltersEnabled(false);
1104                                            frame.stepOver();
1105                                            return null;
1106                                        }
1107                                    }
1108                                }
1109                            }
1110                        } catch (DebugException ex) {
1111                            JDIDebugUIPlugin.log(ex);
1112                        }
1113                    }
1114                }
1115            }
1116        }
1117        return events;
1118    }
1119    
1120    /* (non-Javadoc)
1121     * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(org.eclipse.jface.util.PropertyChangeEvent)
1122     */

1123    protected boolean affectsTextPresentation(PropertyChangeEvent event) {
1124        JavaSourceViewerConfiguration sourceViewerConfiguration = (JavaSourceViewerConfiguration) getSourceViewerConfiguration();
1125        return sourceViewerConfiguration.affectsTextPresentation(event);
1126    }
1127    
1128    /* (non-Javadoc)
1129     * @see org.eclipse.ui.texteditor.AbstractTextEditor#handlePreferenceStoreChanged(org.eclipse.jface.util.PropertyChangeEvent)
1130     */

1131    protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1132        JDISourceViewer isv= (JDISourceViewer) getSourceViewer();
1133        if (isv != null) {
1134            IContentAssistant assistant= isv.getContentAssistant();
1135            if (assistant instanceof ContentAssistant) {
1136                JDIContentAssistPreference.changeConfiguration((ContentAssistant) assistant, event);
1137            }
1138            SourceViewerConfiguration configuration = getSourceViewerConfiguration();
1139            if (configuration instanceof JavaSourceViewerConfiguration) {
1140                JavaSourceViewerConfiguration jsv = (JavaSourceViewerConfiguration) configuration;
1141                if (jsv.affectsTextPresentation(event)) {
1142                    jsv.handlePropertyChangeEvent(event);
1143                    isv.invalidateTextPresentation();
1144                }
1145            }
1146            super.handlePreferenceStoreChanged(event);
1147        }
1148    }
1149    
1150    protected IJavaThread getThread() {
1151        return fThread;
1152    }
1153    
1154    /**
1155     * Sets the thread to perform any evaluations in.
1156     * Notifies the WaitThread waiting on getting an evaluation thread
1157     * to perform an evaluation.
1158     */

1159    protected synchronized void setThread(IJavaThread thread) {
1160        fThread= thread;
1161        notifyAll();
1162    }
1163    
1164    protected void launchVM() {
1165        DebugPlugin.getDefault().addDebugEventFilter(this);
1166        fLaunchedClassPath = getClassPath(getJavaProject());
1167        fLaunchedWorkingDir = getWorkingDirectoryAttribute();
1168        fLaunchedVMArgs = getVMArgsAttribute();
1169        fLaunchedVM = getVMInstall();
1170        Runnable JavaDoc r = new Runnable JavaDoc() {
1171            public void run() {
1172                ScrapbookLauncher.getDefault().launch(getFile());
1173            }
1174        };
1175        BusyIndicator.showWhile(getShell().getDisplay(), r);
1176    }
1177    
1178    /**
1179     * Return the <code>IFile</code> associated with the current
1180     * editor input. Will return <code>null</code> if the current
1181     * editor input is for an external file
1182     */

1183    public IFile getFile() {
1184        IEditorInput input= getEditorInput();
1185        return (IFile) input.getAdapter(IFile.class);
1186    }
1187    
1188    /* (non-Javadoc)
1189     * @see org.eclipse.ui.texteditor.AbstractTextEditor#updateSelectionDependentActions()
1190     */

1191    protected void updateSelectionDependentActions() {
1192        super.updateSelectionDependentActions();
1193        fireEvalStateChanged();
1194    }
1195    
1196    /**
1197     * Terminates existing VM on a rename of the editor
1198     */

1199    protected void setTitle(String JavaDoc title) {
1200        cleanupOnRenameOrMove();
1201        super.setTitle(title);
1202    }
1203    
1204    /**
1205     * If the launch configuration has been copied, moved or
1206     * renamed, shut down any running VM and clear the relevant cached information.
1207     */

1208    protected void cleanupOnRenameOrMove() {
1209        if(isVMLaunched()) {
1210            shutDownVM();
1211        } else {
1212            fThread= null;
1213            fEvaluationContext= null;
1214            fLaunchedClassPath= null;
1215            
1216            if (fEngine != null) {
1217                fEngine.dispose();
1218                fEngine= null;
1219            }
1220        }
1221        fJavaProject= null;
1222    }
1223    
1224    /**
1225     * Returns whether this editor has been opened on a resource that
1226     * is in a Java project.
1227     */

1228    protected boolean isInJavaProject() {
1229        try {
1230            return findJavaProject() != null;
1231        } catch (JavaModelException jme) {
1232            JDIDebugUIPlugin.log(jme);
1233        }
1234        return false;
1235    }
1236    
1237    /**
1238     * Displays an error dialog indicating that evaluation
1239     * cannot occur outside of a Java Project.
1240     */

1241    protected void reportNotInJavaProjectError() {
1242        String JavaDoc projectName= null;
1243        IFile file= getFile();
1244        if (file != null) {
1245            IProject p= file.getProject();
1246            projectName= p.getName();
1247        }
1248        String JavaDoc message= ""; //$NON-NLS-1$
1249
if (projectName != null) {
1250            message = projectName + SnippetMessages.getString("JavaSnippetEditor._is_not_a_Java_Project._n_1"); //$NON-NLS-1$
1251
}
1252        showError(message + SnippetMessages.getString("JavaSnippetEditor.Unable_to_perform_evaluation_outside_of_a_Java_Project_2")); //$NON-NLS-1$
1253
}
1254    
1255    /**
1256     * Asks the user for the workspace path
1257     * of a file resource and saves the document there.
1258     * @see org.eclipse.ui.texteditor.AbstractTextEditor#performSaveAs(org.eclipse.core.runtime.IProgressMonitor)
1259     */

1260    protected void performSaveAs(IProgressMonitor progressMonitor) {
1261        Shell shell= getSite().getShell();
1262        SaveAsDialog dialog= new SaveAsDialog(shell);
1263        dialog.open();
1264        IPath path= dialog.getResult();
1265        
1266        if (path == null) {
1267            if (progressMonitor != null)
1268                progressMonitor.setCanceled(true);
1269            return;
1270        }
1271            
1272        IWorkspace workspace= ResourcesPlugin.getWorkspace();
1273        IFile file= workspace.getRoot().getFile(path);
1274        final IEditorInput newInput= new FileEditorInput(file);
1275        
1276        WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
1277            public void execute(final IProgressMonitor monitor) throws CoreException {
1278                IDocumentProvider dp= getDocumentProvider();
1279                dp.saveDocument(monitor, newInput, dp.getDocument(getEditorInput()), true);
1280            }
1281        };
1282        
1283        boolean success= false;
1284        try {
1285            getDocumentProvider().aboutToChange(newInput);
1286            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
1287            success= true;
1288        } catch (InterruptedException JavaDoc x) {
1289        } catch (InvocationTargetException JavaDoc x) {
1290            JDIDebugUIPlugin.log(x);
1291            String JavaDoc title= SnippetMessages.getString("JavaSnippetEditor.Problems_During_Save_As..._3"); //$NON-NLS-1$
1292
String JavaDoc msg= SnippetMessages.getString("JavaSnippetEditor.Save_could_not_be_completed.__4") + x.getTargetException().getMessage(); //$NON-NLS-1$
1293
MessageDialog.openError(shell, title, msg);
1294        } finally {
1295            getDocumentProvider().changed(newInput);
1296            if (success) {
1297                setInput(newInput);
1298            }
1299        }
1300        
1301        if (progressMonitor != null) {
1302            progressMonitor.setCanceled(!success);
1303        }
1304    }
1305    
1306    /* (non-Javadoc)
1307     * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
1308     */

1309    public boolean isSaveAsAllowed() {
1310        return true;
1311    }
1312    
1313    protected IClassFileEvaluationEngine getEvaluationEngine() {
1314        if (fEngine == null) {
1315            IPath outputLocation = getJavaProject().getProject().getWorkingLocation(JDIDebugUIPlugin.getUniqueIdentifier());
1316            java.io.File JavaDoc f = new java.io.File JavaDoc(outputLocation.toOSString());
1317            fEngine = EvaluationManager.newClassFileEvaluationEngine(getJavaProject(), (IJavaDebugTarget)getThread().getDebugTarget(), f);
1318        }
1319        if (getImports() != null) {
1320            fEngine.setImports(getImports());
1321        } else {
1322            fEngine.setImports(new String JavaDoc[]{});
1323        }
1324        return fEngine;
1325    }
1326    
1327    /* (non-Javadoc)
1328     * @see org.eclipse.ui.texteditor.AbstractTextEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
1329     */

1330    protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
1331        return new JDISourceViewer(parent, ruler, styles);
1332    }
1333    
1334    /**
1335     * Returns the working directory attribute for this scrapbook
1336     */

1337    protected String JavaDoc getWorkingDirectoryAttribute() {
1338        IFile file= getFile();
1339        if (file != null) {
1340            try {
1341                return ScrapbookLauncher.getWorkingDirectoryAttribute(file);
1342            } catch (CoreException e) {
1343                JDIDebugUIPlugin.log(e);
1344            }
1345        }
1346        return null;
1347    }
1348    
1349    /**
1350     * Returns the working directory attribute for this scrapbook
1351     */

1352    protected String JavaDoc getVMArgsAttribute() {
1353        IFile file= getFile();
1354        if (file != null) {
1355            try {
1356                return ScrapbookLauncher.getVMArgsAttribute(file);
1357            } catch (CoreException e) {
1358                JDIDebugUIPlugin.log(e);
1359            }
1360        }
1361        return null;
1362    }
1363    
1364    /**
1365     * Returns the vm install for this scrapbook
1366     */

1367    protected IVMInstall getVMInstall() {
1368        IFile file= getFile();
1369        if (file != null) {
1370            try {
1371                return ScrapbookLauncher.getVMInstall(file);
1372            } catch (CoreException e) {
1373                JDIDebugUIPlugin.log(e);
1374            }
1375        }
1376        return null;
1377    }
1378    
1379    /**
1380     * Executes the given runnable in the Display thread
1381     */

1382    protected void async(Runnable JavaDoc r) {
1383        Control control= getVerticalRuler().getControl();
1384        if (!control.isDisposed()) {
1385            control.getDisplay().asyncExec(r);
1386        }
1387    }
1388    
1389    protected void showAndSelect(final String JavaDoc text, final int offset) {
1390        Runnable JavaDoc r = new Runnable JavaDoc() {
1391            public void run() {
1392                try {
1393                    getSourceViewer().getDocument().replace(offset, 0, text);
1394                } catch (BadLocationException e) {
1395                    JDIDebugUIPlugin.log(e);
1396                }
1397                selectAndReveal(offset, text.length());
1398            }
1399        };
1400        async(r);
1401    }
1402    /* (non-Javadoc)
1403     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
1404     */

1405    public Object JavaDoc getAdapter(Class JavaDoc required) {
1406        if (required == IShowInTargetList.class) {
1407            return new IShowInTargetList() {
1408                public String JavaDoc[] getShowInTargetIds() {
1409                    return new String JavaDoc[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV };
1410                }
1411
1412            };
1413        }
1414        return super.getAdapter(required);
1415    }
1416
1417}
1418
Popular Tags