Skip to content

Commit

Permalink
[eclipse] Attach F4 shortcut to the SARL open type hierarchy command.
Browse files Browse the repository at this point in the history
close #770

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Feb 1, 2018
1 parent c2fe4e9 commit c98314f
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 26 deletions.
Expand Up @@ -93,4 +93,5 @@ action.submitIssue=Submit issue to SARL tracker
view.packageExplorer = SARL Explorer
menu.mainMenu = SARL
menu.open.type.hierarchy=Open Type Hierarchy

key.scheme.name=SARL Keys
context.explorer.name=SARL Explorer
22 changes: 18 additions & 4 deletions main/coreplugins/io.sarl.eclipse/plugin.xml
Expand Up @@ -675,12 +675,13 @@
</projectSREProviderFactory>
</extension>

<!-- Add "Open Type Hierarchy" shortcut. -->
<!-- Add "Open Type Hierarchy" shortcut. https://www.eclipse.org/forums/index.php/t/157754/ -->
<extension point="org.eclipse.ui.commands">
<command
id="io.sarl.eclipse.popup.openTypeHierarchy"
categoryId="org.eclipse.ui.category.navigate"
name="%menu.open.type.hierarchy">
name="%menu.open.type.hierarchy"
helpContextId="org.eclipse.jdt.ui.open_type_action">
</command>
</extension>
<extension point="org.eclipse.ui.handlers">
Expand All @@ -693,10 +694,11 @@
<menuContribution locationURI="popup:io.sarl.eclipse.explorer.PackageExplorer?after=group.open">
<command commandId="io.sarl.eclipse.popup.openTypeHierarchy"
label="%menu.open.type.hierarchy"
mnemonic="n"
style="push">
<visibleWhen>
<with variable="selection">
<iterate ifEmpty="false">
<iterate ifEmpty="false" operator="or">
<adapt type="org.eclipse.core.resources.IResource">
<test property="org.eclipse.core.resources.name" value="*.sarl" />
</adapt>
Expand All @@ -706,5 +708,17 @@
</command>
</menuContribution>
</extension>

<extension point="org.eclipse.ui.bindings">
<scheme
id="io.sarl.eclipse.defaultAcceleratorConfiguration"
name="%key.scheme.name"
parentId="org.eclipse.ui.defaultAcceleratorConfiguration">
</scheme>
<key
sequence="F4"
schemeId="io.sarl.eclipse.defaultAcceleratorConfiguration"
contextId="io.sarl.eclipse.explorer.PackageExplorer"
commandId="io.sarl.eclipse.popup.openTypeHierarchy">
</key>
</extension>
</plugin>
3 changes: 3 additions & 0 deletions main/coreplugins/io.sarl.eclipse/plugin_customization.ini
Expand Up @@ -18,6 +18,9 @@ org.eclipse.ui/defaultPerspectiveId=io.sarl.eclipse.perspective.devel
# Enable line number ruler in all textual editors
org.eclipse.ui.editors/lineNumberRuler=true

# Force the key scheme to be the SARL keys
org.eclipse.ui/KEY_CONFIGURATION_ID=io.sarl.eclipse.defaultAcceleratorConfiguration

######
# Resources

Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.xtext.xbase.lib.util.ReflectExtensions;

/**
Expand Down Expand Up @@ -74,6 +75,8 @@ public class SARLPackageExplorerPart extends PackageExplorerPart {
*/
public static final String ID_PACKAGES = "io.sarl.eclipse.explorer.PackageExplorer"; //$NON-NLS-1$

private static final String CONTEXT_ID = ID_PACKAGES;

private static final String TAG_LAYOUT = "layout"; //$NON-NLS-1$

private static final String TAG_GROUP_LIBRARIES = "group_libraries"; //$NON-NLS-1$
Expand Down Expand Up @@ -165,7 +168,10 @@ public static SARLPackageExplorerPart openInActivePerspective() {

@Override
public void createPartControl(Composite parent) {
// Overriden for setting the label provider, which is set in a private function in the super type.
// Open the context
final IContextService contextService = getSite().getService(IContextService.class);
contextService.activateContext(CONTEXT_ID);
// Overridden for setting the label provider, which is set in a private function in the super type.
super.createPartControl(parent);
internalResetLabelProvider();
restoreFilterAndSorter();
Expand All @@ -181,7 +187,7 @@ protected void restoreFilterAndSorter() {

@Override
public void rootModeChanged(int newMode) {
// Overriden for setting the label provider, which is set in a private function in the super type.
// Overridden for setting the label provider, which is set in a private function in the super type.
super.rootModeChanged(newMode);
internalResetLabelProvider();
}
Expand Down
Expand Up @@ -217,7 +217,7 @@ private IResource findResource(Object[] elements) {
* @param scope the elements to consider for an element type that can be launched.
*/
private void searchAndLaunch(String mode, Object... scope) {
final ElementDescription element = searchAndSelect(scope);
final ElementDescription element = searchAndSelect(true, scope);
if (element != null) {
try {
launch(element.projectName, element.elementName, mode);
Expand Down
Expand Up @@ -91,7 +91,7 @@ protected String getQualifiedNameFor(SarlAgent element) {
}

@Override
protected Image getElementImage() {
protected Image getElementImage(Object element) {
return this.images.forAgent(JvmVisibility.PRIVATE, 0).createImage();
}

Expand Down
Expand Up @@ -61,7 +61,7 @@ protected URI getResourceURIForValidEObject(Object object) {
}

@Override
protected Image getElementImage() {
protected Image getElementImage(Object element) {
return JavaPluginImages.DESC_OBJS_CLASS.createImage();
}

Expand Down
Expand Up @@ -29,8 +29,9 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.util.OpenTypeHierarchyUtil;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jdt.ui.actions.OpenTypeHierarchyAction;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.xtend.core.xtend.XtendTypeDeclaration;
Expand Down Expand Up @@ -62,9 +63,9 @@ public class OpenTypeHierarchyHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ISelection activeSelection = HandlerUtil.getActiveMenuSelection(event);
if (activeSelection instanceof IStructuredSelection) {
final ElementDescription selectedElement = this.typeSelector.searchAndSelect(((IStructuredSelection) activeSelection).toArray());
final IStructuredSelection activeSelection = HandlerUtil.getCurrentStructuredSelection(event);
if (activeSelection != null && activeSelection != StructuredSelection.EMPTY) {
final ElementDescription selectedElement = this.typeSelector.searchAndSelect(false, activeSelection.toArray());
if (selectedElement != null) {
IJavaElement realJavaElement = null;
if (selectedElement.element instanceof IJavaElement) {
Expand All @@ -78,6 +79,9 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
if (realJavaElement != null) {
OpenTypeHierarchyUtil.open(realJavaElement, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}
} else {
final OpenTypeHierarchyAction jdtTool = new OpenTypeHierarchyAction(HandlerUtil.getActiveSite(event));
jdtTool.run(activeSelection);
}
}
return null;
Expand Down
Expand Up @@ -86,7 +86,13 @@ protected String getElementsLabel() {
}

@Override
protected Image getElementImage() {
protected Image getElementImage(Object element) {
if (element != null) {
final Image img = this.labelProvider.getImage(element);
if (img != null) {
return img;
}
}
return JavaPluginImages.DESC_OBJS_UNKNOWN.createImage();
}

Expand Down
Expand Up @@ -100,8 +100,10 @@ public abstract class AbstractSarlScriptInteractiveSelector<ET extends EObject,
@Inject
protected Jdt2Ecore jdt;

/** Provider of labels and icons.
*/
@Inject
private ILabelProvider labelProvider;
protected ILabelProvider labelProvider;

/** Replies if the given resource could be considered for discovering an agent to be launched.
*
Expand Down Expand Up @@ -268,18 +270,21 @@ private List<ElementDescription> findElements(Object[] selection, IProgressServi
/** Search the elements based on the given scope, and select one.
* If more than one element was found, the user selects interactively one.
*
* @param showEmptySelectionError indicates if this function shows an error when the selection is empty.
* @param scope the elements to consider for an element type that can be launched.
* @return the selected element.
* @return the selected element; or {@code null} if there is no selection.
*/
public ElementDescription searchAndSelect(Object... scope) {
public ElementDescription searchAndSelect(boolean showEmptySelectionError, Object... scope) {
try {
final List<ElementDescription> elements = findElements(scope, PlatformUI.getWorkbench().getProgressService());
ElementDescription element = null;
if (elements.isEmpty()) {
SARLEclipsePlugin.getDefault().openError(getShell(),
Messages.AbstractSarlScriptInteractiveSelector_1,
MessageFormat.format(Messages.AbstractSarlScriptInteractiveSelector_2, getElementLabel()),
null);
if (elements == null || elements.isEmpty()) {
if (showEmptySelectionError) {
SARLEclipsePlugin.getDefault().openError(getShell(),
Messages.AbstractSarlScriptInteractiveSelector_1,
MessageFormat.format(Messages.AbstractSarlScriptInteractiveSelector_2, getElementLabel()),
null);
}
} else if (elements.size() > 1) {
element = chooseElement(elements);
} else {
Expand Down Expand Up @@ -336,9 +341,10 @@ private ElementDescription chooseElement(List<ElementDescription> elements) {

/** Replies the icon associated to the elements.
*
* @param element the element for which the icon should be replied, or {@code null} if it is unknown.
* @return the icon.
*/
protected abstract Image getElementImage();
protected abstract Image getElementImage(Object element);

/** Description of an element to launch.
*
Expand Down Expand Up @@ -391,7 +397,6 @@ public String toString() {
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("synthetic-access")
private class LabelProvider implements ILabelProvider {

/** Constructor.
Expand Down Expand Up @@ -423,7 +428,7 @@ public void removeListener(ILabelProviderListener listener) {
@Override
public Image getImage(Object element) {
if (element instanceof ElementDescription) {
return getElementImage();
return getElementImage(((ElementDescription) element).element);
}
return AbstractSarlScriptInteractiveSelector.this.labelProvider.getImage(element);
}
Expand Down

0 comments on commit c98314f

Please sign in to comment.