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.internal.ide.handlers;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.commands.ExecutionEvent;
017: import org.eclipse.core.commands.ExecutionException;
018: import org.eclipse.core.commands.IHandler;
019: import org.eclipse.core.commands.IHandlerListener;
020: import org.eclipse.core.resources.IContainer;
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.resources.ResourcesPlugin;
024: import org.eclipse.core.runtime.ListenerList;
025: import org.eclipse.jface.action.Action;
026: import org.eclipse.jface.action.IAction;
027: import org.eclipse.jface.dialogs.IDialogConstants;
028: import org.eclipse.jface.viewers.ISelection;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.ui.IWorkbenchPage;
031: import org.eclipse.ui.IWorkbenchWindow;
032: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
033: import org.eclipse.ui.PartInitException;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.ide.IDE;
036: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
037: import org.eclipse.ui.internal.ide.dialogs.OpenResourceDialog;
038:
039: /**
040: * Implements the open resource action. Opens a dialog prompting for a file and
041: * opens the selected file in an editor.
042: *
043: * @since 2.1
044: */
045: public final class OpenResourceHandler extends Action implements
046: IHandler, IWorkbenchWindowActionDelegate {
047:
048: /**
049: * The identifier of the parameter storing the file path.
050: */
051: private static final String PARAM_ID_FILE_PATH = "filePath"; //$NON-NLS-1$
052:
053: /**
054: * A collection of objects listening to changes to this manager. This
055: * collection is <code>null</code> if there are no listeners.
056: */
057: private transient ListenerList listenerList = null;
058:
059: /**
060: * Creates a new instance of the class.
061: */
062: public OpenResourceHandler() {
063: super ();
064: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
065: IIDEHelpContextIds.OPEN_WORKSPACE_FILE_ACTION);
066: }
067:
068: public final void addHandlerListener(final IHandlerListener listener) {
069: if (listenerList == null) {
070: listenerList = new ListenerList(ListenerList.IDENTITY);
071: }
072:
073: listenerList.add(listener);
074: }
075:
076: public final void dispose() {
077: listenerList = null;
078: }
079:
080: public final Object execute(final ExecutionEvent event)
081: throws ExecutionException {
082: final List files = new ArrayList();
083:
084: if (event.getParameter(PARAM_ID_FILE_PATH) == null) {
085: // Prompt the user for the resource to open.
086: Object[] result = queryFileResource();
087:
088: if (result != null) {
089: for (int i = 0; i < result.length; i++) {
090: if (result[i] instanceof IFile) {
091: files.add(result[i]);
092: }
093: }
094: }
095:
096: } else {
097: // Use the given parameter.
098: final IResource resource = (IResource) event
099: .getObjectParameterForExecution(PARAM_ID_FILE_PATH);
100: if (!(resource instanceof IFile)) {
101: throw new ExecutionException(
102: "filePath parameter must identify a file"); //$NON-NLS-1$
103: }
104: files.add(resource);
105: }
106:
107: if (files.size() > 0) {
108:
109: final IWorkbenchWindow window = PlatformUI.getWorkbench()
110: .getActiveWorkbenchWindow();
111: if (window == null) {
112: throw new ExecutionException(
113: "no active workbench window"); //$NON-NLS-1$
114: }
115:
116: final IWorkbenchPage page = window.getActivePage();
117: if (page == null) {
118: throw new ExecutionException("no active workbench page"); //$NON-NLS-1$
119: }
120:
121: try {
122: for (Iterator it = files.iterator(); it.hasNext();) {
123: IDE.openEditor(page, (IFile) it.next(), true);
124: }
125: } catch (final PartInitException e) {
126: throw new ExecutionException(
127: "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[] 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()
151: .getRoot();
152:
153: final OpenResourceDialog dialog = new OpenResourceDialog(
154: parent, input, IResource.FILE);
155: final int resultCode = dialog.open();
156: if (resultCode != IDialogConstants.OK_ID) {
157: return null;
158: }
159:
160: final Object[] result = dialog.getResult();
161:
162: return result;
163: }
164:
165: public final void removeHandlerListener(
166: final IHandlerListener listener) {
167: if (listenerList != null) {
168: listenerList.remove(listener);
169:
170: if (listenerList.isEmpty()) {
171: listenerList = null;
172: }
173: }
174: }
175:
176: public final void run(final IAction action) {
177: try {
178: execute(new ExecutionEvent());
179: } catch (final ExecutionException e) {
180: // TODO Do something meaningful and poignant.
181: }
182: }
183:
184: public final void selectionChanged(final IAction action,
185: final ISelection selection) {
186: // Do nothing.
187: }
188: }
|