| /******************************************************************************* |
| * Copyright (c) 2000, 2004 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.ui.internal.editors.text; |
| |
| import java.io.File; |
| |
| import org.eclipse.core.runtime.IPath; |
| |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.wizard.Wizard; |
| |
| import org.eclipse.ui.editors.text.EditorsUI; |
| |
| import org.eclipse.ui.IEditorDescriptor; |
| import org.eclipse.ui.IEditorInput; |
| import org.eclipse.ui.IEditorRegistry; |
| import org.eclipse.ui.INewWizard; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.PartInitException; |
| |
| /** |
| * Wizard with no page that creates the untitled text file |
| * and opens the text editor. |
| * |
| * @since 3.1 |
| */ |
| public class UntitledTextFileWizard extends Wizard implements INewWizard { |
| |
| private IWorkbenchWindow fWindow; |
| |
| public UntitledTextFileWizard() { |
| } |
| |
| /* |
| * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() |
| */ |
| public void dispose() { |
| fWindow= null; |
| } |
| |
| private File queryFile() { |
| IPath stateLocation= EditorsPlugin.getDefault().getStateLocation(); |
| IPath path= stateLocation.append("/_" + new Object().hashCode()); //$NON-NLS-1$ //$NON-NLS-2$ |
| return new File(path.toOSString()); |
| } |
| |
| private String getEditorId(File file) { |
| IWorkbench workbench= fWindow.getWorkbench(); |
| IEditorRegistry editorRegistry= workbench.getEditorRegistry(); |
| IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(file.getName()); |
| if (descriptor != null) |
| return descriptor.getId(); |
| return EditorsUI.DEFAULT_TEXT_EDITOR_ID; |
| } |
| |
| private IEditorInput createEditorInput(File file) { |
| return new NonExistingFileEditorInput(file, TextEditorMessages.NewTextEditorAction_namePrefix); |
| } |
| |
| /* |
| * @see org.eclipse.jface.wizard.Wizard#performFinish() |
| */ |
| public boolean performFinish() { |
| File file= queryFile(); |
| IEditorInput input= createEditorInput(file); |
| String editorId= getEditorId(file); |
| IWorkbenchPage page= fWindow.getActivePage(); |
| try { |
| page.openEditor(input, editorId); |
| } catch (PartInitException e) { |
| EditorsPlugin.log(e); |
| return false; |
| } |
| return true; |
| } |
| |
| /* |
| * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) |
| */ |
| public void init(IWorkbench workbench, IStructuredSelection selection) { |
| fWindow= workbench.getActiveWorkbenchWindow(); |
| } |
| } |