1 11 package org.eclipse.jdt.internal.debug.ui.snippeteditor; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.resources.IWorkspaceRoot; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.jdt.core.JavaCore; 20 import org.eclipse.jdt.internal.debug.ui.ExceptionHandler; 21 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds; 22 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.ui.IWorkbenchPage; 29 import org.eclipse.ui.IWorkbenchPart; 30 import org.eclipse.ui.PartInitException; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.dialogs.WizardNewFileCreationPage; 33 import org.eclipse.ui.ide.IDE; 34 import org.eclipse.ui.part.ISetSelectionTarget; 35 36 39 public class NewSnippetFileWizardPage extends WizardNewFileCreationPage { 40 41 private static final String fgDefaultExtension= ".jpage"; 43 public NewSnippetFileWizardPage(IStructuredSelection selection) { 44 super("createScrapBookPage", selection); setTitle(SnippetMessages.getString("NewSnippetFileWizardPage.title")); } 47 48 public boolean finish() { 49 String fileName= getFileName(); 51 if (fileName != null && !fileName.endsWith(fgDefaultExtension)) { 52 setFileName(fileName + fgDefaultExtension); 53 } 54 55 boolean retValue= super.validatePage(); 56 57 final IFile file= createNewFile(); 58 if (retValue && file != null) { 59 Shell shell= getShell(); 60 IWorkbenchPage page= JDIDebugUIPlugin.getActivePage(); 61 if (shell == null || page == null) { 62 return true; 63 } 64 final IWorkbenchPart focusPart= page.getActivePart(); 65 if (focusPart instanceof ISetSelectionTarget) { 66 shell.getDisplay().asyncExec(new Runnable () { 67 public void run() { 68 ISelection selection= new StructuredSelection(file); 69 ((ISetSelectionTarget) focusPart).selectReveal(selection); 70 } 71 }); 72 } 73 try { 74 IDE.openEditor(page, file, true); 75 return true; 76 } catch (PartInitException e) { 77 ExceptionHandler.handle(e, shell, SnippetMessages.getString("NewSnippetFileWizardPage.open_error.message"), e.getMessage()); } 79 } 80 return false; 81 } 82 83 86 protected boolean validatePage() { 87 boolean valid= super.validatePage(); 89 if (!valid) 90 return false; 91 92 IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot(); 93 IPath containerPath= getContainerFullPath(); 94 if (containerPath != null && containerPath.segmentCount() > 0) { 95 IProject project= workspaceRoot.getProject(containerPath.segment(0)); 96 try { 97 if (!project.hasNature(JavaCore.NATURE_ID)) { 98 setErrorMessage(SnippetMessages.getString("NewSnippetFileWizardPage.error.OnlyInJavaProject")); return false; 100 } 101 } catch (CoreException e) { 102 JDIDebugUIPlugin.log(e.getStatus()); 103 } 104 } 105 106 String fileName= getFileName(); 107 if (fileName != null && !fileName.endsWith(fgDefaultExtension)) { 108 fileName= fileName + fgDefaultExtension; 109 IPath path= getContainerFullPath(); 110 111 if (path != null && workspaceRoot.exists(path.append(fileName))) { 112 setErrorMessage(SnippetMessages.getString("NewSnippetFileWizardPage.error.AlreadyExists")); return false; 114 } 115 } 116 return true; 117 } 118 119 122 public void createControl(Composite parent) { 123 super.createControl(parent); 124 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.NEW_SNIPPET_WIZARD_PAGE); 125 } 126 } 127 | Popular Tags |