KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.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 /**
37  * Page to create a new Java snippet file.
38  */

39 public class NewSnippetFileWizardPage extends WizardNewFileCreationPage {
40     
41     private static final String JavaDoc fgDefaultExtension= ".jpage"; //$NON-NLS-1$
42

43     public NewSnippetFileWizardPage(IStructuredSelection selection) {
44         super("createScrapBookPage", selection); //$NON-NLS-1$
45
setTitle(SnippetMessages.getString("NewSnippetFileWizardPage.title")); //$NON-NLS-1$
46
}
47
48     public boolean finish() {
49         // add extension if non is provided
50
String JavaDoc 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 JavaDoc() {
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()); //$NON-NLS-1$
78
}
79         }
80         return false;
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validatePage()
85      */

86     protected boolean validatePage() {
87         // check whether file with extension doesn't exist
88
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")); //$NON-NLS-1$
99
return false;
100                 }
101             } catch (CoreException e) {
102                 JDIDebugUIPlugin.log(e.getStatus());
103             }
104         }
105     
106         String JavaDoc 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")); //$NON-NLS-1$
113
return false;
114             }
115         }
116         return true;
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
121      */

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