Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2007 IBM Corporation and others. |
2 |
* Copyright (c) 2007, 2009 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 14-38
Link Here
|
14 |
import org.eclipse.core.commands.AbstractHandler; |
14 |
import org.eclipse.core.commands.AbstractHandler; |
15 |
import org.eclipse.core.commands.ExecutionEvent; |
15 |
import org.eclipse.core.commands.ExecutionEvent; |
16 |
import org.eclipse.core.commands.ExecutionException; |
16 |
import org.eclipse.core.commands.ExecutionException; |
|
|
17 |
import org.eclipse.swt.widgets.Event; |
18 |
import org.eclipse.swt.widgets.Menu; |
19 |
import org.eclipse.swt.widgets.MenuItem; |
20 |
import org.eclipse.swt.widgets.Widget; |
17 |
import org.eclipse.ui.IEditorPart; |
21 |
import org.eclipse.ui.IEditorPart; |
18 |
import org.eclipse.ui.IPersistableEditor; |
22 |
import org.eclipse.ui.IPersistableEditor; |
19 |
import org.eclipse.ui.IWorkbenchPage; |
23 |
import org.eclipse.ui.IWorkbenchPage; |
|
|
24 |
import org.eclipse.ui.IWorkbenchPart; |
20 |
import org.eclipse.ui.IWorkbenchWindow; |
25 |
import org.eclipse.ui.IWorkbenchWindow; |
21 |
import org.eclipse.ui.PartInitException; |
26 |
import org.eclipse.ui.PartInitException; |
22 |
import org.eclipse.ui.XMLMemento; |
27 |
import org.eclipse.ui.XMLMemento; |
23 |
import org.eclipse.ui.handlers.HandlerUtil; |
28 |
import org.eclipse.ui.handlers.HandlerUtil; |
24 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
29 |
import org.eclipse.ui.internal.IWorkbenchConstants; |
|
|
30 |
import org.eclipse.ui.internal.PartPane; |
25 |
import org.eclipse.ui.internal.WorkbenchMessages; |
31 |
import org.eclipse.ui.internal.WorkbenchMessages; |
26 |
import org.eclipse.ui.internal.WorkbenchPage; |
32 |
import org.eclipse.ui.internal.WorkbenchPage; |
27 |
import org.eclipse.ui.internal.dialogs.DialogUtil; |
33 |
import org.eclipse.ui.internal.dialogs.DialogUtil; |
|
|
34 |
import org.eclipse.ui.internal.presentations.PresentablePart; |
28 |
|
35 |
|
29 |
/** |
36 |
/** |
30 |
* Open a new editor on the active editor's input. |
37 |
* Open a new editor on the input of the active editor or the editor that has |
|
|
38 |
* been selected by the mouse. |
39 |
* <p> |
40 |
* Since 3.5, this handler has been modified to monitor the contents of the |
41 |
* popup menu that has instantiated this handler (if applicable) so that the |
42 |
* handler will create a new editor for the editor that has been selected |
43 |
* instead of the workbench page's active editor. See bug 60833 for more |
44 |
* details. |
45 |
* </p> |
31 |
* |
46 |
* |
32 |
* @since 3.4 |
47 |
* @since 3.4 |
33 |
* |
48 |
* |
34 |
*/ |
49 |
*/ |
35 |
public class NewEditorHandler extends AbstractHandler { |
50 |
public class NewEditorHandler extends AbstractHandler { |
|
|
51 |
|
52 |
/** |
53 |
* Retrieves the editor that should the workbench should create a new editor |
54 |
* for. |
55 |
* @param event the current state of the application |
56 |
* @return the editor that a new editor should be created for, or <code>null</code> if no valid editor could be found |
57 |
* @since 3.5 |
58 |
*/ |
59 |
private IEditorPart getEditor(ExecutionEvent event) { |
60 |
IEditorPart editor = HandlerUtil.getActiveEditor(event); |
61 |
Object trigger = event.getTrigger(); |
62 |
if (trigger instanceof Event) { |
63 |
Event swtEvent = (Event) trigger; |
64 |
Widget widget = swtEvent.widget; |
65 |
// make sure it's actually a menu item, if a user invokes this |
66 |
// handler from a keybinding, there are no popup menus |
67 |
if (widget instanceof MenuItem) { |
68 |
MenuItem menuItem = (MenuItem) widget; |
69 |
Menu menu = menuItem.getParent(); |
70 |
Object data = menu.getData(); |
71 |
// retrieve the part that owns the context menu |
72 |
if (data instanceof PresentablePart) { |
73 |
PresentablePart part = (PresentablePart) data; |
74 |
PartPane pane = part.getPane(); |
75 |
IWorkbenchPart workbenchPart = pane.getPartReference().getPart(true); |
76 |
editor = workbenchPart instanceof IEditorPart ? (IEditorPart) workbenchPart : null; |
77 |
} |
78 |
} |
79 |
} |
80 |
return editor; |
81 |
} |
36 |
|
82 |
|
37 |
/* |
83 |
/* |
38 |
* (non-Javadoc) |
84 |
* (non-Javadoc) |
Lines 46-52
Link Here
|
46 |
if (page == null) { |
92 |
if (page == null) { |
47 |
return null; |
93 |
return null; |
48 |
} |
94 |
} |
49 |
IEditorPart editor = page.getActiveEditor(); |
95 |
IEditorPart editor = getEditor(event); |
50 |
if (editor == null) { |
96 |
if (editor == null) { |
51 |
return null; |
97 |
return null; |
52 |
} |
98 |
} |