KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > UntitledTextFileWizard


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.ui.internal.editors.text;
12
13 import org.eclipse.core.filesystem.EFS;
14 import org.eclipse.core.filesystem.IFileStore;
15
16 import org.eclipse.core.runtime.IPath;
17
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.wizard.Wizard;
20
21 import org.eclipse.ui.editors.text.EditorsUI;
22
23 import org.eclipse.ui.IEditorDescriptor;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorRegistry;
26 import org.eclipse.ui.INewWizard;
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.PartInitException;
31
32 /**
33  * Wizard with no page that creates the untitled text file
34  * and opens the text editor.
35  *
36  * @since 3.1
37  */

38 public class UntitledTextFileWizard extends Wizard implements INewWizard {
39
40     private IWorkbenchWindow fWindow;
41
42     public UntitledTextFileWizard() {
43     }
44
45     /*
46      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
47      */

48     public void dispose() {
49         fWindow= null;
50     }
51
52     private IFileStore queryFileStore() {
53         IPath stateLocation= EditorsPlugin.getDefault().getStateLocation();
54         IPath path= stateLocation.append("/_" + new Object JavaDoc().hashCode()); //$NON-NLS-1$
55
return EFS.getLocalFileSystem().getStore(path);
56     }
57
58     private String JavaDoc getEditorId(IFileStore fileStore) {
59         IWorkbench workbench= fWindow.getWorkbench();
60         IEditorRegistry editorRegistry= workbench.getEditorRegistry();
61         IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(fileStore.getName());
62         if (descriptor != null)
63             return descriptor.getId();
64         return EditorsUI.DEFAULT_TEXT_EDITOR_ID;
65     }
66
67     private IEditorInput createEditorInput(IFileStore fileStore) {
68         return new NonExistingFileEditorInput(fileStore, TextEditorMessages.NewTextEditorAction_namePrefix);
69     }
70
71     /*
72      * @see org.eclipse.jface.wizard.Wizard#performFinish()
73      */

74     public boolean performFinish() {
75         IFileStore fileStore= queryFileStore();
76         IEditorInput input= createEditorInput(fileStore);
77         String JavaDoc editorId= getEditorId(fileStore);
78         IWorkbenchPage page= fWindow.getActivePage();
79         try {
80             page.openEditor(input, editorId);
81         } catch (PartInitException e) {
82             EditorsPlugin.log(e);
83             return false;
84         }
85         return true;
86     }
87
88     /*
89      * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
90      */

91     public void init(IWorkbench workbench, IStructuredSelection selection) {
92         fWindow= workbench.getActiveWorkbenchWindow();
93     }
94 }
95
Popular Tags