001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.actions;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.jface.util.OpenStrategy;
014: import org.eclipse.ui.IEditorDescriptor;
015: import org.eclipse.ui.IWorkbenchPage;
016: import org.eclipse.ui.PartInitException;
017: import org.eclipse.ui.PlatformUI;
018: import org.eclipse.ui.ide.IDE;
019: import org.eclipse.ui.internal.ide.DialogUtil;
020: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
021: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
022: import org.eclipse.ui.part.FileEditorInput;
023:
024: /**
025: * Standard action for opening an editor on the currently selected file
026: * resource(s).
027: * <p>
028: * Note that there is a different action for opening closed projects:
029: * <code>OpenResourceAction</code>.
030: * </p>
031: * <p>
032: * This class may be instantiated; it is not intended to be subclassed.
033: * </p>
034: */
035: public class OpenFileAction extends OpenSystemEditorAction {
036:
037: /**
038: * The id of this action.
039: */
040: public static final String ID = PlatformUI.PLUGIN_ID
041: + ".OpenFileAction";//$NON-NLS-1$
042:
043: /**
044: * The editor to open.
045: */
046: private IEditorDescriptor editorDescriptor;
047:
048: /**
049: * Creates a new action that will open editors on the then-selected file
050: * resources. Equivalent to <code>OpenFileAction(page,null)</code>.
051: *
052: * @param page the workbench page in which to open the editor
053: */
054: public OpenFileAction(IWorkbenchPage page) {
055: this (page, null);
056: }
057:
058: /**
059: * Creates a new action that will open instances of the specified editor on
060: * the then-selected file resources.
061: *
062: * @param page the workbench page in which to open the editor
063: * @param descriptor the editor descriptor, or <code>null</code> if unspecified
064: */
065: public OpenFileAction(IWorkbenchPage page,
066: IEditorDescriptor descriptor) {
067: super (page);
068: setText(descriptor == null ? IDEWorkbenchMessages.OpenFileAction_text
069: : descriptor.getLabel());
070: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
071: IIDEHelpContextIds.OPEN_FILE_ACTION);
072: setToolTipText(IDEWorkbenchMessages.OpenFileAction_toolTip);
073: setId(ID);
074: this .editorDescriptor = descriptor;
075: }
076:
077: /**
078: * Ensures that the contents of the given file resource are local.
079: *
080: * @param file the file resource
081: * @return <code>true</code> if the file is local, and <code>false</code> if
082: * it could not be made local for some reason
083: */
084: boolean ensureFileLocal(final IFile file) {
085: //Currently fails due to Core PR. Don't do it for now
086: //1G5I6PV: ITPCORE:WINNT - IResource.setLocal() attempts to modify immutable tree
087: //file.setLocal(true, IResource.DEPTH_ZERO);
088: return true;
089: }
090:
091: /**
092: * Opens an editor on the given file resource.
093: *
094: * @param file the file resource
095: */
096: void openFile(IFile file) {
097: try {
098: boolean activate = OpenStrategy.activateOnOpen();
099: if (editorDescriptor == null) {
100: IDE.openEditor(getWorkbenchPage(), file, activate);
101: } else {
102: if (ensureFileLocal(file)) {
103: getWorkbenchPage().openEditor(
104: new FileEditorInput(file),
105: editorDescriptor.getId(), activate);
106: }
107: }
108: } catch (PartInitException e) {
109: DialogUtil
110: .openError(
111: getWorkbenchPage().getWorkbenchWindow()
112: .getShell(),
113: IDEWorkbenchMessages.OpenFileAction_openFileShellTitle,
114: e.getMessage(), e);
115: }
116: }
117:
118: }
|