KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > OpenWorkspaceFileAction


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
12 package org.eclipse.ui.internal.ide.actions;
13
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.ide.IDE;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
32 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
33 import org.eclipse.ui.internal.ide.dialogs.OpenResourceDialog;
34
35 /**
36  * Implements the open resource action.
37  * Opens a dialog prompting for a file and opens the selected file in an editor.
38  *
39  * @since 2.1
40  */

41 public class OpenWorkspaceFileAction extends Action implements
42         IWorkbenchWindowActionDelegate {
43     IWorkbenchWindow workbenchWindow;
44
45     /**
46      * Creates a new instance of the class.
47      */

48     public OpenWorkspaceFileAction() {
49         super();
50         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
51                 IIDEHelpContextIds.OPEN_WORKSPACE_FILE_ACTION);
52     }
53
54     /**
55      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
56      */

57     public void dispose() {
58         // do nothing
59
}
60
61     /**
62      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
63      */

64     public void init(IWorkbenchWindow window) {
65         workbenchWindow = window;
66     }
67
68     /**
69      * Query the user for the resource that should be opened
70      *
71      * @return the resource that should be opened or null if the
72      * resource selection dialog was cancelled.
73      */

74     IFile queryFileResource() {
75         Shell parent = workbenchWindow.getShell();
76         IContainer input = ResourcesPlugin.getWorkspace().getRoot();
77
78         OpenResourceDialog dialog = new OpenResourceDialog(parent, input,
79                 IResource.FILE);
80         int resultCode = dialog.open();
81         if (resultCode != IDialogConstants.OK_ID)
82             return null;
83
84         Object JavaDoc[] result = dialog.getResult();
85         if (result == null || result.length == 0
86                 || result[0] instanceof IFile == false)
87             return null;
88
89         return (IFile) result[0];
90     }
91
92     /**
93      * Collect all resources in the workbench, open a dialog asking the user to
94      * select a file and open the file in an editor.
95      */

96     public void run(IAction action) {
97         IFile file = queryFileResource();
98
99         if (file == null)
100             return;
101         try {
102             IWorkbenchPage page = workbenchWindow.getActivePage();
103             if (page != null) {
104                 IDE.openEditor(page, file, true);
105             }
106         } catch (CoreException x) {
107             String JavaDoc title = IDEWorkbenchMessages.OpenWorkspaceFileAction_errorTitle;
108             String JavaDoc message = IDEWorkbenchMessages.OpenWorkspaceFileAction_errorMessage;
109             IDEWorkbenchPlugin.log(title, x.getStatus());
110             ErrorDialog.openError(workbenchWindow.getShell(), title, message, x
111                     .getStatus());
112         }
113     }
114
115     /**
116      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
117      */

118     public void selectionChanged(IAction action, ISelection selection) {
119         // do nothing
120
}
121
122 }
123
Popular Tags