| 1 |
/* |
| 2 |
* Copyright (c) Eclipse contributors and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
*/ |
| 8 |
package org.eclipse.emf.edit.ui.action; |
| 9 |
|
| 10 |
|
| 11 |
import java.lang.reflect.Constructor; |
| 12 |
import java.lang.reflect.Method; |
| 13 |
import java.util.ResourceBundle; |
| 14 |
|
| 15 |
import org.eclipse.emf.common.CommonPlugin; |
| 16 |
import org.eclipse.emf.edit.ui.EMFEditUIPlugin; |
| 17 |
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; |
| 18 |
import org.eclipse.emf.edit.ui.util.FindAndReplaceTarget; |
| 19 |
import org.eclipse.jface.action.Action; |
| 20 |
import org.eclipse.jface.text.IFindReplaceTarget; |
| 21 |
import org.eclipse.ui.IWorkbenchPart; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* An action for showing the find and replace dialog. |
| 26 |
* Use {@link #create()} to create an instance. |
| 27 |
* Because this implementation use platform internals, it is not extensible because the platform internals could arbitrarily change. |
| 28 |
* |
| 29 |
* @since 2.14 |
| 30 |
*/ |
| 31 |
public final class FindAction extends Action |
| 32 |
{ |
| 33 |
/** |
| 34 |
* The constructor for reusing the platform's text editor's org.eclipse.ui.texteditor.FindReplaceAction. |
| 35 |
*/ |
| 36 |
private static final Constructor<? extends Action> FIND_AND_REPLACE_CONSTRUCTOR; |
| 37 |
|
| 38 |
/** |
| 39 |
* The resource bundle from the platform's org.eclipse.ui.texteditor.EditorMessages. |
| 40 |
*/ |
| 41 |
private static final ResourceBundle FIND_AND_REPLACE_RESOURCE_BUNDLE; |
| 42 |
|
| 43 |
static |
| 44 |
{ |
| 45 |
Constructor<? extends Action> findAndReplaceConstructor = null; |
| 46 |
ResourceBundle resourceBundle = null; |
| 47 |
try |
| 48 |
{ |
| 49 |
Class<?> editorMessagesClass = CommonPlugin.loadClass("org.eclipse.ui.workbench.texteditor", "org.eclipse.ui.texteditor.EditorMessages"); |
| 50 |
Method getBundleForConstructedKeysMethod = editorMessagesClass.getMethod("getBundleForConstructedKeys"); |
| 51 |
getBundleForConstructedKeysMethod.setAccessible(true); |
| 52 |
resourceBundle = (ResourceBundle)getBundleForConstructedKeysMethod.invoke(null); |
| 53 |
Class<?> findAndReplaceActionClass = CommonPlugin.loadClass("org.eclipse.ui.workbench.texteditor", "org.eclipse.ui.texteditor.FindReplaceAction"); |
| 54 |
@SuppressWarnings("unchecked") |
| 55 |
Constructor<? extends Action> constructor = (Constructor<? extends Action>)findAndReplaceActionClass.getConstructor(ResourceBundle.class, String.class, IWorkbenchPart.class); |
| 56 |
findAndReplaceConstructor = constructor; |
| 57 |
} |
| 58 |
catch (Throwable throwable) |
| 59 |
{ |
| 60 |
throwable.printStackTrace(); |
| 61 |
} |
| 62 |
|
| 63 |
FIND_AND_REPLACE_CONSTRUCTOR = findAndReplaceConstructor; |
| 64 |
FIND_AND_REPLACE_RESOURCE_BUNDLE = resourceBundle; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns a new find action or {@code null} if the bundle {@code org.eclipse.ui.workbench.texteditor} isn't installed. |
| 69 |
*/ |
| 70 |
public static FindAction create() |
| 71 |
{ |
| 72 |
if (FIND_AND_REPLACE_CONSTRUCTOR != null) |
| 73 |
{ |
| 74 |
return new FindAction(); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
return null; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* The current associated workbench part. |
| 84 |
* @see #setActiveWorkbenchPart(IWorkbenchPart) |
| 85 |
*/ |
| 86 |
private IWorkbenchPart workbenchPart; |
| 87 |
|
| 88 |
/** |
| 89 |
* Creates an instance, but the {@link #create()} is the only public way to create an instance. |
| 90 |
*/ |
| 91 |
private FindAction() |
| 92 |
{ |
| 93 |
super(EMFEditUIPlugin.INSTANCE.getString("_UI_FindReplace_menu_item"), ExtendedImageRegistry.INSTANCE.getImageDescriptor(EMFEditUIPlugin.INSTANCE.getImage("full/ctool16/Search"))); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Gets the {@link IFindReplaceTarget} adapter from the {@link #setActiveWorkbenchPart(IWorkbenchPart) active workbench part} |
| 98 |
* and if that's a {@link FindAndReplaceTarget}, [@link {@link FindAndReplaceTarget#initialize(IWorkbenchPart) initializes it}. |
| 99 |
* Then it creates the platform's {@code org.eclipse.ui.texteditor.FindReplaceDialog}. |
| 100 |
* |
| 101 |
* @see FindAndReplaceTarget#getAdapter(Class, IWorkbenchPart, org.eclipse.ui.plugin.AbstractUIPlugin) |
| 102 |
*/ |
| 103 |
@Override |
| 104 |
public void run() |
| 105 |
{ |
| 106 |
try |
| 107 |
{ |
| 108 |
Action findAndReplaceAction = FIND_AND_REPLACE_CONSTRUCTOR.newInstance(FIND_AND_REPLACE_RESOURCE_BUNDLE, "Editor.FindReplace.", workbenchPart); |
| 109 |
findAndReplaceAction.run(); |
| 110 |
} |
| 111 |
catch (Exception exception) |
| 112 |
{ |
| 113 |
EMFEditUIPlugin.INSTANCE.log(exception); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Sets the current active workbench part. |
| 119 |
* @param workbenchPart the current active workbench part. |
| 120 |
* @see EditingDomainActionBarContributor#activate() |
| 121 |
* @see EditingDomainActionBarContributor#deactivate() |
| 122 |
*/ |
| 123 |
public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) |
| 124 |
{ |
| 125 |
this.workbenchPart = workbenchPart; |
| 126 |
if (workbenchPart != null) |
| 127 |
{ |
| 128 |
setEnabled(workbenchPart.getAdapter(IFindReplaceTarget.class) != null); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Updates the action. |
| 134 |
* |
| 135 |
* @see EditingDomainActionBarContributor#update() |
| 136 |
*/ |
| 137 |
public void update() |
| 138 |
{ |
| 139 |
} |
| 140 |
} |