KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.ui.IEditorRegistry;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.ide.DialogUtil;
23 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
24 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
25 import org.eclipse.ui.part.FileEditorInput;
26
27 /**
28  * Standard action for opening a system editor on the currently selected file
29  * resource.
30  * <p>
31  * This class may be instantiated; it is not intended to be subclassed.
32  * </p>
33  */

34 public class OpenSystemEditorAction extends SelectionListenerAction {
35
36     /**
37      * The id of this action.
38      */

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

42     /**
43      * The workbench page to open the editor in.
44      */

45     private IWorkbenchPage workbenchPage;
46
47     /**
48      * Creates a new action that will open system editors on the then-selected file
49      * resources.
50      *
51      * @param page the workbench page in which to open the editor
52      */

53     public OpenSystemEditorAction(IWorkbenchPage page) {
54         super(IDEWorkbenchMessages.OpenSystemEditorAction_text);
55         setToolTipText(IDEWorkbenchMessages.OpenSystemEditorAction_toolTip);
56         setId(ID);
57         page.getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(this,
58                 IIDEHelpContextIds.OPEN_SYSTEM_EDITOR_ACTION);
59         if (page == null) {
60             throw new IllegalArgumentException JavaDoc();
61         }
62         this.workbenchPage = page;
63     }
64
65     /**
66      * Return the workbench page to open the editor in.
67      *
68      * @return the workbench page to open the editor in
69      */

70     /* package */final IWorkbenchPage getWorkbenchPage() {
71         return workbenchPage;
72     }
73
74     /**
75      * Opens a system editor on the given file resource.
76      *
77      * @param file the file resource
78      */

79     /* package */void openFile(IFile file) {
80         try {
81             getWorkbenchPage().openEditor(new FileEditorInput(file),
82                     IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
83         } catch (PartInitException e) {
84             DialogUtil.openError(getWorkbenchPage().getWorkbenchWindow()
85                     .getShell(), IDEWorkbenchMessages.OpenSystemEditorAction_dialogTitle,
86                     e.getMessage(), e);
87         }
88     }
89
90     /* (non-Javadoc)
91      * Method declared on IAction.
92      */

93     public void run() {
94         Iterator JavaDoc itr = getSelectedResources().iterator();
95         while (itr.hasNext()) {
96             IResource resource = (IResource) itr.next();
97             if (resource instanceof IFile) {
98                 openFile((IFile) resource);
99             }
100         }
101     }
102
103     /**
104      * The <code>OpenSystemEditorAction</code> implementation of this
105      * <code>SelectionListenerAction</code> method enables the action only
106      * if the selection contains just file resources.
107      */

108     protected boolean updateSelection(IStructuredSelection selection) {
109         return super.updateSelection(selection)
110                 && selectionIsOfType(IResource.FILE);
111     }
112 }
113
Popular Tags