01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.browsing;
11:
12: import org.eclipse.core.resources.IWorkspace;
13: import org.eclipse.core.runtime.IAdaptable;
14:
15: import org.eclipse.jface.viewers.ISelection;
16: import org.eclipse.jface.viewers.IStructuredSelection;
17:
18: import org.eclipse.ui.IWorkbenchWindow;
19: import org.eclipse.ui.actions.OpenInNewWindowAction;
20:
21: import org.eclipse.jdt.core.IJavaElement;
22: import org.eclipse.jdt.core.JavaCore;
23:
24: /*
25: * XXX: This is a workaround for: http://dev.eclipse.org/bugs/show_bug.cgi?id=13070
26: * This class can be removed once the bug is fixed.
27: *
28: * @since 2.0
29: */
30: public class PatchedOpenInNewWindowAction extends OpenInNewWindowAction {
31:
32: private IWorkbenchWindow fWorkbenchWindow;
33:
34: public PatchedOpenInNewWindowAction(IWorkbenchWindow window,
35: IAdaptable input) {
36: super (window, input);
37: fWorkbenchWindow = window;
38: }
39:
40: public void run() {
41: JavaBrowsingPerspectiveFactory
42: .setInputFromAction(getSelectedJavaElement());
43: try {
44: super .run();
45: } finally {
46: JavaBrowsingPerspectiveFactory.setInputFromAction(null);
47: }
48: }
49:
50: private IJavaElement getSelectedJavaElement() {
51: if (fWorkbenchWindow.getActivePage() != null) {
52: ISelection selection = fWorkbenchWindow.getActivePage()
53: .getSelection();
54: if (selection instanceof IStructuredSelection
55: && !selection.isEmpty()) {
56: Object selectedElement = ((IStructuredSelection) selection)
57: .getFirstElement();
58: if (selectedElement instanceof IJavaElement)
59: return (IJavaElement) selectedElement;
60: if (!(selectedElement instanceof IJavaElement)
61: && selectedElement instanceof IAdaptable)
62: return (IJavaElement) ((IAdaptable) selectedElement)
63: .getAdapter(IJavaElement.class);
64: else if (selectedElement instanceof IWorkspace)
65: return JavaCore
66: .create(((IWorkspace) selectedElement)
67: .getRoot());
68: }
69: }
70: return null;
71: }
72: }
|