KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > handlers > OpenResourceHandler


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
12 package org.eclipse.ui.internal.ide.handlers;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.IHandler;
21 import org.eclipse.core.commands.IHandlerListener;
22 import org.eclipse.core.resources.IContainer;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.ListenerList;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.ide.IDE;
38 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
39 import org.eclipse.ui.internal.ide.dialogs.OpenResourceDialog;
40
41 /**
42  * Implements the open resource action. Opens a dialog prompting for a file and
43  * opens the selected file in an editor.
44  *
45  * @since 2.1
46  */

47 public final class OpenResourceHandler extends Action implements IHandler,
48         IWorkbenchWindowActionDelegate {
49
50     /**
51      * The identifier of the parameter storing the file path.
52      */

53     private static final String JavaDoc PARAM_ID_FILE_PATH = "filePath"; //$NON-NLS-1$
54

55     /**
56      * A collection of objects listening to changes to this manager. This
57      * collection is <code>null</code> if there are no listeners.
58      */

59     private transient ListenerList listenerList = null;
60
61     /**
62      * Creates a new instance of the class.
63      */

64     public OpenResourceHandler() {
65         super();
66         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
67                 IIDEHelpContextIds.OPEN_WORKSPACE_FILE_ACTION);
68     }
69
70     public final void addHandlerListener(final IHandlerListener listener) {
71         if (listenerList == null) {
72             listenerList = new ListenerList(ListenerList.IDENTITY);
73         }
74
75         listenerList.add(listener);
76     }
77
78     public final void dispose() {
79         listenerList = null;
80     }
81
82     public final Object JavaDoc execute(final ExecutionEvent event)
83             throws ExecutionException {
84         final List JavaDoc files = new ArrayList JavaDoc();
85
86         if (event.getParameter(PARAM_ID_FILE_PATH) == null) {
87             // Prompt the user for the resource to open.
88
Object JavaDoc[] result = queryFileResource();
89
90             if (result != null) {
91                 for (int i = 0; i < result.length; i++) {
92                     if (result[i] instanceof IFile) {
93                         files.add(result[i]);
94                     }
95                 }
96             }
97
98         } else {
99             // Use the given parameter.
100
final IResource resource = (IResource) event
101                     .getObjectParameterForExecution(PARAM_ID_FILE_PATH);
102             if (!(resource instanceof IFile)) {
103                 throw new ExecutionException(
104                         "filePath parameter must identify a file"); //$NON-NLS-1$
105
}
106             files.add(resource);
107         }
108
109         if (files.size() > 0) {
110
111             final IWorkbenchWindow window = PlatformUI.getWorkbench()
112                     .getActiveWorkbenchWindow();
113             if (window == null) {
114                 throw new ExecutionException("no active workbench window"); //$NON-NLS-1$
115
}
116
117             final IWorkbenchPage page = window.getActivePage();
118             if (page == null) {
119                 throw new ExecutionException("no active workbench page"); //$NON-NLS-1$
120
}
121
122             try {
123                 for (Iterator JavaDoc it = files.iterator(); it.hasNext();) {
124                     IDE.openEditor(page, (IFile) it.next(), true);
125                 }
126             } catch (final PartInitException e) {
127                 throw new ExecutionException("error opening file in editor", e); //$NON-NLS-1$
128
}
129         }
130
131         return null;
132     }
133
134     public final void init(final IWorkbenchWindow window) {
135         // Do nothing.
136
}
137
138     /**
139      * Query the user for the resources that should be opened
140      *
141      * @return the resource that should be opened.
142      */

143     private final Object JavaDoc[] queryFileResource() {
144         final IWorkbenchWindow window = PlatformUI.getWorkbench()
145                 .getActiveWorkbenchWindow();
146         if (window == null) {
147             return null;
148         }
149         final Shell parent = window.getShell();
150         final IContainer input = ResourcesPlugin.getWorkspace().getRoot();
151
152         final OpenResourceDialog dialog = new OpenResourceDialog(parent, input,
153                 IResource.FILE);
154         final int resultCode = dialog.open();
155         if (resultCode != IDialogConstants.OK_ID) {
156             return null;
157         }
158
159         final Object JavaDoc[] result = dialog.getResult();
160
161         return result;
162     }
163
164     public final void removeHandlerListener(final IHandlerListener listener) {
165         if (listenerList != null) {
166             listenerList.remove(listener);
167
168             if (listenerList.isEmpty()) {
169                 listenerList = null;
170             }
171         }
172     }
173
174     public final void run(final IAction action) {
175         try {
176             execute(new ExecutionEvent());
177         } catch (final ExecutionException e) {
178             // TODO Do something meaningful and poignant.
179
}
180     }
181
182     public final void selectionChanged(final IAction action,
183             final ISelection selection) {
184         // Do nothing.
185
}
186 }
187
Popular Tags