KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > newresource > BasicNewFileResourceWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.wizards.newresource;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.ui.IWorkbench;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchWindow;
19 import org.eclipse.ui.PartInitException;
20 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
21 import org.eclipse.ui.ide.IDE;
22 import org.eclipse.ui.internal.ide.DialogUtil;
23 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
24 import org.eclipse.ui.internal.wizards.newresource.ResourceMessages;
25
26 /**
27  * Standard workbench wizard that create a new file resource in the workspace.
28  * <p>
29  * This class may be instantiated and used without further configuration;
30  * this class is not intended to be subclassed.
31  * </p>
32  * <p>
33  * Example:
34  * <pre>
35  * IWorkbenchWizard wizard = new BasicNewFileResourceWizard();
36  * wizard.init(workbench, selection);
37  * WizardDialog dialog = new WizardDialog(shell, wizard);
38  * dialog.open();
39  * </pre>
40  * During the call to <code>open</code>, the wizard dialog is presented to the
41  * user. When the user hits Finish, a file resource at the user-specified
42  * workspace path is created, the dialog closes, and the call to
43  * <code>open</code> returns.
44  * </p>
45  */

46 public class BasicNewFileResourceWizard extends BasicNewResourceWizard {
47     private WizardNewFileCreationPage mainPage;
48
49     /**
50      * Creates a wizard for creating a new file resource in the workspace.
51      */

52     public BasicNewFileResourceWizard() {
53         super();
54     }
55
56     /* (non-Javadoc)
57      * Method declared on IWizard.
58      */

59     public void addPages() {
60         super.addPages();
61         mainPage = new WizardNewFileCreationPage("newFilePage1", getSelection());//$NON-NLS-1$
62
mainPage.setTitle(ResourceMessages.FileResource_pageTitle);
63         mainPage.setDescription(ResourceMessages.FileResource_description);
64         addPage(mainPage);
65     }
66
67     /* (non-Javadoc)
68      * Method declared on IWorkbenchWizard.
69      */

70     public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
71         super.init(workbench, currentSelection);
72         setWindowTitle(ResourceMessages.FileResource_shellTitle);
73         setNeedsProgressMonitor(true);
74     }
75
76     /* (non-Javadoc)
77      * Method declared on BasicNewResourceWizard.
78      */

79     protected void initializeDefaultPageImageDescriptor() {
80        ImageDescriptor desc = IDEWorkbenchPlugin.getIDEImageDescriptor("wizban/newfile_wiz.png");//$NON-NLS-1$
81
setDefaultPageImageDescriptor(desc);
82     }
83
84     /* (non-Javadoc)
85      * Method declared on IWizard.
86      */

87     public boolean performFinish() {
88         IFile file = mainPage.createNewFile();
89         if (file == null) {
90             return false;
91         }
92
93         selectAndReveal(file);
94
95         // Open editor on new file.
96
IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
97         try {
98             if (dw != null) {
99                 IWorkbenchPage page = dw.getActivePage();
100                 if (page != null) {
101                     IDE.openEditor(page, file, true);
102                 }
103             }
104         } catch (PartInitException e) {
105             DialogUtil.openError(dw.getShell(), ResourceMessages.FileResource_errorMessage,
106                     e.getMessage(), e);
107         }
108
109         return true;
110     }
111 }
112
Popular Tags