001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.wizards.newresource;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.jface.resource.ImageDescriptor;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.ui.IWorkbench;
016: import org.eclipse.ui.IWorkbenchPage;
017: import org.eclipse.ui.IWorkbenchWindow;
018: import org.eclipse.ui.PartInitException;
019: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
020: import org.eclipse.ui.ide.IDE;
021: import org.eclipse.ui.internal.ide.DialogUtil;
022: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
023: import org.eclipse.ui.internal.wizards.newresource.ResourceMessages;
024:
025: /**
026: * Standard workbench wizard that create a new file resource in the workspace.
027: * <p>
028: * This class may be instantiated and used without further configuration;
029: * this class is not intended to be subclassed.
030: * </p>
031: * <p>
032: * Example:
033: * <pre>
034: * IWorkbenchWizard wizard = new BasicNewFileResourceWizard();
035: * wizard.init(workbench, selection);
036: * WizardDialog dialog = new WizardDialog(shell, wizard);
037: * dialog.open();
038: * </pre>
039: * During the call to <code>open</code>, the wizard dialog is presented to the
040: * user. When the user hits Finish, a file resource at the user-specified
041: * workspace path is created, the dialog closes, and the call to
042: * <code>open</code> returns.
043: * </p>
044: */
045: public class BasicNewFileResourceWizard extends BasicNewResourceWizard {
046: private WizardNewFileCreationPage mainPage;
047:
048: /**
049: * Creates a wizard for creating a new file resource in the workspace.
050: */
051: public BasicNewFileResourceWizard() {
052: super ();
053: }
054:
055: /* (non-Javadoc)
056: * Method declared on IWizard.
057: */
058: public void addPages() {
059: super .addPages();
060: mainPage = new WizardNewFileCreationPage(
061: "newFilePage1", getSelection());//$NON-NLS-1$
062: mainPage.setTitle(ResourceMessages.FileResource_pageTitle);
063: mainPage
064: .setDescription(ResourceMessages.FileResource_description);
065: addPage(mainPage);
066: }
067:
068: /* (non-Javadoc)
069: * Method declared on IWorkbenchWizard.
070: */
071: public void init(IWorkbench workbench,
072: IStructuredSelection currentSelection) {
073: super .init(workbench, currentSelection);
074: setWindowTitle(ResourceMessages.FileResource_shellTitle);
075: setNeedsProgressMonitor(true);
076: }
077:
078: /* (non-Javadoc)
079: * Method declared on BasicNewResourceWizard.
080: */
081: protected void initializeDefaultPageImageDescriptor() {
082: ImageDescriptor desc = IDEWorkbenchPlugin
083: .getIDEImageDescriptor("wizban/newfile_wiz.png");//$NON-NLS-1$
084: setDefaultPageImageDescriptor(desc);
085: }
086:
087: /* (non-Javadoc)
088: * Method declared on IWizard.
089: */
090: public boolean performFinish() {
091: IFile file = mainPage.createNewFile();
092: if (file == null) {
093: return false;
094: }
095:
096: selectAndReveal(file);
097:
098: // Open editor on new file.
099: IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
100: try {
101: if (dw != null) {
102: IWorkbenchPage page = dw.getActivePage();
103: if (page != null) {
104: IDE.openEditor(page, file, true);
105: }
106: }
107: } catch (PartInitException e) {
108: DialogUtil.openError(dw.getShell(),
109: ResourceMessages.FileResource_errorMessage, e
110: .getMessage(), e);
111: }
112:
113: return true;
114: }
115: }
|