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.actions;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.ui.IEditorRegistry;
018: import org.eclipse.ui.IWorkbenchPage;
019: import org.eclipse.ui.PartInitException;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.ide.DialogUtil;
022: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
023: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
024: import org.eclipse.ui.part.FileEditorInput;
025:
026: /**
027: * Standard action for opening a system editor on the currently selected file
028: * resource.
029: * <p>
030: * This class may be instantiated; it is not intended to be subclassed.
031: * </p>
032: */
033: public class OpenSystemEditorAction extends SelectionListenerAction {
034:
035: /**
036: * The id of this action.
037: */
038: public static final String ID = PlatformUI.PLUGIN_ID
039: + ".OpenSystemEditorAction";//$NON-NLS-1$
040:
041: /**
042: * The workbench page to open the editor in.
043: */
044: private IWorkbenchPage workbenchPage;
045:
046: /**
047: * Creates a new action that will open system editors on the then-selected file
048: * resources.
049: *
050: * @param page the workbench page in which to open the editor
051: */
052: public OpenSystemEditorAction(IWorkbenchPage page) {
053: super (IDEWorkbenchMessages.OpenSystemEditorAction_text);
054: setToolTipText(IDEWorkbenchMessages.OpenSystemEditorAction_toolTip);
055: setId(ID);
056: page.getWorkbenchWindow().getWorkbench().getHelpSystem()
057: .setHelp(this ,
058: IIDEHelpContextIds.OPEN_SYSTEM_EDITOR_ACTION);
059: if (page == null) {
060: throw new IllegalArgumentException();
061: }
062: this .workbenchPage = page;
063: }
064:
065: /**
066: * Return the workbench page to open the editor in.
067: *
068: * @return the workbench page to open the editor in
069: */
070: /* package */final IWorkbenchPage getWorkbenchPage() {
071: return workbenchPage;
072: }
073:
074: /**
075: * Opens a system editor on the given file resource.
076: *
077: * @param file the file resource
078: */
079: /* package */void openFile(IFile file) {
080: try {
081: getWorkbenchPage().openEditor(new FileEditorInput(file),
082: IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
083: } catch (PartInitException e) {
084: DialogUtil
085: .openError(
086: getWorkbenchPage().getWorkbenchWindow()
087: .getShell(),
088: IDEWorkbenchMessages.OpenSystemEditorAction_dialogTitle,
089: e.getMessage(), e);
090: }
091: }
092:
093: /* (non-Javadoc)
094: * Method declared on IAction.
095: */
096: public void run() {
097: Iterator itr = getSelectedResources().iterator();
098: while (itr.hasNext()) {
099: IResource resource = (IResource) itr.next();
100: if (resource instanceof IFile) {
101: openFile((IFile) resource);
102: }
103: }
104: }
105:
106: /**
107: * The <code>OpenSystemEditorAction</code> implementation of this
108: * <code>SelectionListenerAction</code> method enables the action only
109: * if the selection contains just file resources.
110: */
111: protected boolean updateSelection(IStructuredSelection selection) {
112: return super.updateSelection(selection)
113: && selectionIsOfType(IResource.FILE);
114: }
115: }
|