use of org.eclipse.core.expressions.IEvaluationContext in project core by jcryptool.
the class ViewProviderPaletteViewer method makeAndAssignActions.
/**
* Creates the actions and assigns them to the viewers double click listener.
*/
private void makeAndAssignActions() {
doubleClickHandler = new AbstractHandler() {
public Object execute(ExecutionEvent event) {
final ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Object obj = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
if (obj instanceof PaletteEditPart) {
PaletteEditPart part = (PaletteEditPart) obj;
IConfigurationElement[] elements = Platform.getExtensionRegistry().getExtensionPoint(extensionPointId).getConfigurationElements();
for (IConfigurationElement element : elements) {
if (element.getAttribute("name").equals((// $NON-NLS-1$
(PaletteEntry) part.getModel()).getLabel())) {
// $NON-NLS-1$
Command command = commandService.getCommand(element.getAttribute("viewId"));
try {
return command.executeWithChecks(event);
} catch (Exception ex) {
LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
return (null);
}
}
}
}
return (null);
}
};
viewer.getControl().addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(final MouseEvent e) {
if (e.button == 1) {
// only left button double clicks
final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
ExecutionEvent event = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
try {
// run assigned action
doubleClickHandler.execute(event);
} catch (ExecutionException ex) {
LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
}
}
}
@Override
public void mouseDown(final MouseEvent e) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Object obj = selection.getFirstElement();
if (obj instanceof PaletteEditPart) {
AlgorithmView.showContextHelp(extensionPointId, ((PaletteEntry) ((PaletteEditPart) obj).getModel()).getLabel());
viewer.getControl().setFocus();
viewer.setSelection(selection);
}
}
});
}
use of org.eclipse.core.expressions.IEvaluationContext in project core by jcryptool.
the class ViewProviderTreeViewer method makeAndAssignActions.
/**
* creates the actions and assigns them to the viewers double click listener
*/
private void makeAndAssignActions() {
doubleClickHandler = new AbstractHandler() {
public Object execute(ExecutionEvent event) {
final ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
TreeObject treeObject = (TreeObject) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
IConfigurationElement[] elements = Platform.getExtensionRegistry().getExtensionPoint(extensionPointId).getConfigurationElements();
for (IConfigurationElement element : elements) {
if (element.getAttribute("name").equals(treeObject.getName())) {
// $NON-NLS-1$
// $NON-NLS-1$
Command command = commandService.getCommand(element.getAttribute("viewId"));
try {
return command.executeWithChecks(event);
} catch (Exception ex) {
LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
}
}
}
return (null);
}
};
addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(final DoubleClickEvent event) {
Object obj = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
if (obj instanceof TreeParent) {
if (viewer.getTree().getSelection()[0].getExpanded()) {
viewer.collapseToLevel(obj, 1);
} else {
viewer.expandToLevel(obj, 1);
}
} else if (obj instanceof TreeObject) {
try {
final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
ExecutionEvent executionEvent = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
// run assigned action
doubleClickHandler.execute(executionEvent);
} catch (ExecutionException ex) {
LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
}
}
}
});
addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(final SelectionChangedEvent event) {
Object treeObject = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
if (treeObject instanceof TreeParent) {
// $NON-NLS-1$
PlatformUI.getWorkbench().getHelpSystem().displayHelp(ViewsPlugin.PLUGIN_ID + ".algorithmsView");
getControl().setFocus();
} else if (treeObject instanceof TreeObject) {
AlgorithmView.showContextHelp(extensionPointId, ((TreeObject) treeObject).getName());
getControl().setFocus();
}
}
});
}
use of org.eclipse.core.expressions.IEvaluationContext in project translationstudio8 by heartsome.
the class NewHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
String parameter = event.getParameter("wizardParameter");
Command command = null;
ICommandService commandService = (ICommandService) HandlerUtil.getActiveSite(event).getService(ICommandService.class);
if (parameter == null || parameter.equalsIgnoreCase("project")) {
command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newProject");
} else if (parameter.equalsIgnoreCase("folder")) {
command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newFolder");
} else if (parameter.equalsIgnoreCase("tm")) {
command = commandService.getCommand("net.heartsome.cat.database.ui.tm.command.newTM");
} else if (parameter.equalsIgnoreCase("tb")) {
command = commandService.getCommand("net.heartsome.cat.database.ui.tb.command.newTB");
}
if (command == null) {
return null;
}
IEvaluationService evalService = (IEvaluationService) HandlerUtil.getActiveSite(event).getService(IEvaluationService.class);
IEvaluationContext currentState = evalService.getCurrentState();
ExecutionEvent executionEvent = new ExecutionEvent(command, Collections.EMPTY_MAP, this, currentState);
try {
command.executeWithChecks(executionEvent);
} catch (NotDefinedException e) {
LOGGER.error("", e);
} catch (NotEnabledException e) {
LOGGER.error("", e);
} catch (NotHandledException e) {
LOGGER.error("", e);
}
return null;
}
use of org.eclipse.core.expressions.IEvaluationContext in project tdi-studio-se by Talend.
the class TalendLaunchShortcutAction method updateEnablement.
/**
* Since these actions are re-created each time the run/debug as menu is filled, the enablement of this action is
* static.
*/
private void updateEnablement() {
// IWorkbenchWindow wb = DebugUIPlugin.getActiveWorkbenchWindow();
IWorkbenchWindow wb = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
boolean enabled = false;
if (wb != null) {
IWorkbenchPage page = wb.getActivePage();
if (page != null) {
ISelection selection = page.getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
try {
// check enablement logic, if any
Expression expression = fShortcut.getShortcutEnablementExpression();
if (expression == null) {
enabled = !structuredSelection.isEmpty();
} else {
List list = structuredSelection.toList();
IEvaluationContext context = new EvaluationContext(null, list);
//$NON-NLS-1$
context.addVariable("selection", list);
enabled = fShortcut.evalEnablementExpression(context, expression);
}
} catch (CoreException e) {
}
} else {
IEditorPart editor = page.getActiveEditor();
if (editor != null) {
enabled = true;
}
}
}
}
setEnabled(enabled);
}
use of org.eclipse.core.expressions.IEvaluationContext in project dbeaver by dbeaver.
the class ActionUtils method runCommand.
public static void runCommand(String commandId, ISelection selection, IServiceLocator serviceLocator) {
if (commandId != null) {
try {
ICommandService commandService = serviceLocator.getService(ICommandService.class);
IHandlerService handlerService = serviceLocator.getService(IHandlerService.class);
if (commandService != null) {
Command command = commandService.getCommand(commandId);
boolean needContextPatch = false;
if (selection != null) {
needContextPatch = true;
if (serviceLocator instanceof IWorkbenchSite) {
final ISelection curSelection = ((IWorkbenchSite) serviceLocator).getSelectionProvider().getSelection();
if (curSelection instanceof IStructuredSelection && selection instanceof IStructuredSelection) {
if (((IStructuredSelection) curSelection).size() == ((IStructuredSelection) selection).size() && ((IStructuredSelection) curSelection).getFirstElement() == ((IStructuredSelection) selection).getFirstElement()) {
// The same selection
needContextPatch = false;
}
}
}
}
if (selection != null && needContextPatch) {
// Create new eval context
IEvaluationContext context = new EvaluationContext(handlerService.createContextSnapshot(false), selection);
if (serviceLocator instanceof IWorkbenchPartSite) {
context.addVariable(ISources.ACTIVE_PART_NAME, ((IWorkbenchPartSite) serviceLocator).getPart());
}
context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
ParameterizedCommand pc = new ParameterizedCommand(command, null);
handlerService.executeCommandInContext(pc, null, context);
} else if (command != null) {
if (command.isEnabled()) {
handlerService.executeCommand(commandId, null);
} else {
log.warn("Command '" + commandId + "' is disabled");
}
} else {
log.warn("Command '" + commandId + "' not found");
}
}
} catch (Exception e) {
log.error("Can't execute command '" + commandId + "'", e);
}
}
}
Aggregations