KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > OpenFileAction


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.actions;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.jface.util.OpenStrategy;
15 import org.eclipse.ui.IEditorDescriptor;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.ide.IDE;
20 import org.eclipse.ui.internal.ide.DialogUtil;
21 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
22 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
23 import org.eclipse.ui.part.FileEditorInput;
24
25 /**
26  * Standard action for opening an editor on the currently selected file
27  * resource(s).
28  * <p>
29  * Note that there is a different action for opening closed projects:
30  * <code>OpenResourceAction</code>.
31  * </p>
32  * <p>
33  * This class may be instantiated; it is not intended to be subclassed.
34  * </p>
35  */

36 public class OpenFileAction extends OpenSystemEditorAction {
37
38     /**
39      * The id of this action.
40      */

41     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".OpenFileAction";//$NON-NLS-1$
42

43     /**
44      * The editor to open.
45      */

46     private IEditorDescriptor editorDescriptor;
47
48     /**
49      * Creates a new action that will open editors on the then-selected file
50      * resources. Equivalent to <code>OpenFileAction(page,null)</code>.
51      *
52      * @param page the workbench page in which to open the editor
53      */

54     public OpenFileAction(IWorkbenchPage page) {
55         this(page, null);
56     }
57
58     /**
59      * Creates a new action that will open instances of the specified editor on
60      * the then-selected file resources.
61      *
62      * @param page the workbench page in which to open the editor
63      * @param descriptor the editor descriptor, or <code>null</code> if unspecified
64      */

65     public OpenFileAction(IWorkbenchPage page, IEditorDescriptor descriptor) {
66         super(page);
67         setText(descriptor == null ? IDEWorkbenchMessages.OpenFileAction_text : descriptor.getLabel());
68         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
69                 IIDEHelpContextIds.OPEN_FILE_ACTION);
70         setToolTipText(IDEWorkbenchMessages.OpenFileAction_toolTip);
71         setId(ID);
72         this.editorDescriptor = descriptor;
73     }
74
75     /**
76      * Ensures that the contents of the given file resource are local.
77      *
78      * @param file the file resource
79      * @return <code>true</code> if the file is local, and <code>false</code> if
80      * it could not be made local for some reason
81      */

82     boolean ensureFileLocal(final IFile file) {
83         //Currently fails due to Core PR. Don't do it for now
84
//1G5I6PV: ITPCORE:WINNT - IResource.setLocal() attempts to modify immutable tree
85
//file.setLocal(true, IResource.DEPTH_ZERO);
86
return true;
87     }
88
89     /**
90      * Opens an editor on the given file resource.
91      *
92      * @param file the file resource
93      */

94     void openFile(IFile file) {
95         try {
96             boolean activate = OpenStrategy.activateOnOpen();
97             if (editorDescriptor == null) {
98                 IDE.openEditor(getWorkbenchPage(), file, activate);
99             } else {
100                 if (ensureFileLocal(file)) {
101                     getWorkbenchPage().openEditor(new FileEditorInput(file),
102                             editorDescriptor.getId(), activate);
103                 }
104             }
105         } catch (PartInitException e) {
106             DialogUtil.openError(getWorkbenchPage().getWorkbenchWindow()
107                     .getShell(), IDEWorkbenchMessages.OpenFileAction_openFileShellTitle,
108                     e.getMessage(), e);
109         }
110     }
111
112 }
113
Popular Tags