KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > PatchedOpenInNewWindowAction


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 package org.eclipse.jdt.internal.ui.browsing;
12
13 import org.eclipse.core.resources.IWorkspace;
14 import org.eclipse.core.runtime.IAdaptable;
15
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.actions.OpenInNewWindowAction;
21
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.JavaCore;
24
25 /*
26  * XXX: This is a workaround for: http://dev.eclipse.org/bugs/show_bug.cgi?id=13070
27  * This class can be removed once the bug is fixed.
28  *
29  * @since 2.0
30  */

31 public class PatchedOpenInNewWindowAction extends OpenInNewWindowAction {
32
33     private IWorkbenchWindow fWorkbenchWindow;
34
35     public PatchedOpenInNewWindowAction(IWorkbenchWindow window, IAdaptable input) {
36         super(window, input);
37         fWorkbenchWindow= window;
38     }
39
40     public void run() {
41         JavaBrowsingPerspectiveFactory.setInputFromAction(getSelectedJavaElement());
42         try {
43             super.run();
44         } finally {
45             JavaBrowsingPerspectiveFactory.setInputFromAction(null);
46         }
47     }
48
49     private IJavaElement getSelectedJavaElement() {
50         if (fWorkbenchWindow.getActivePage() != null) {
51             ISelection selection= fWorkbenchWindow.getActivePage().getSelection();
52             if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
53                 Object JavaDoc selectedElement= ((IStructuredSelection)selection).getFirstElement();
54                 if (selectedElement instanceof IJavaElement)
55                     return (IJavaElement)selectedElement;
56                 if (!(selectedElement instanceof IJavaElement) && selectedElement instanceof IAdaptable)
57                     return (IJavaElement)((IAdaptable)selectedElement).getAdapter(IJavaElement.class);
58                 else if (selectedElement instanceof IWorkspace)
59                         return JavaCore.create(((IWorkspace)selectedElement).getRoot());
60             }
61         }
62         return null;
63     }
64 }
65
Popular Tags