|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 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.handlers; |
| 13 |
|
| 14 |
import org.eclipse.core.commands.AbstractHandler; |
| 15 |
import org.eclipse.core.commands.ExecutionEvent; |
| 16 |
import org.eclipse.core.commands.ExecutionException; |
| 17 |
import org.eclipse.core.expressions.IEvaluationContext; |
| 18 |
import org.eclipse.ui.IEditorPart; |
| 19 |
import org.eclipse.ui.ISources; |
| 20 |
import org.eclipse.ui.IViewPart; |
| 21 |
import org.eclipse.ui.IWorkbenchPart; |
| 22 |
import org.eclipse.ui.IWorkbenchWindow; |
| 23 |
|
| 24 |
/** |
| 25 |
* Provide a Handler for the Close Part command. This can then be bound to |
| 26 |
* whatever keybinding the user prefers. |
| 27 |
* |
| 28 |
* @since 3.3 |
| 29 |
*/ |
| 30 |
public class ClosePartHandler extends AbstractHandler { |
| 31 |
|
| 32 |
/* |
| 33 |
* (non-Javadoc) |
| 34 |
* |
| 35 |
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 36 |
*/ |
| 37 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
| 38 |
IWorkbenchPart part = null; |
| 39 |
IWorkbenchWindow window = null; |
| 40 |
if (event.getApplicationContext() instanceof IEvaluationContext) { |
| 41 |
IEvaluationContext appContext = (IEvaluationContext) event |
| 42 |
.getApplicationContext(); |
| 43 |
window = (IWorkbenchWindow) appContext |
| 44 |
.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME); |
| 45 |
if (window == null) { |
| 46 |
throw new ExecutionException("No active workbench window"); //$NON-NLS-1$ |
| 47 |
} |
| 48 |
part = (IWorkbenchPart) appContext |
| 49 |
.getVariable(ISources.ACTIVE_PART_NAME); |
| 50 |
if (part == null) { |
| 51 |
throw new ExecutionException("No active part"); //$NON-NLS-1$ |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (part instanceof IEditorPart) { |
| 56 |
window.getActivePage().closeEditor((IEditorPart) part, true); |
| 57 |
} else if (part instanceof IViewPart) { |
| 58 |
window.getActivePage().hideView((IViewPart) part); |
| 59 |
} |
| 60 |
|
| 61 |
return null; |
| 62 |
} |
| 63 |
} |