use of org.eclipse.core.expressions.IEvaluationContext in project dbeaver by serge-rider.
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 && command.isEnabled()) {
handlerService.executeCommand(commandId, null);
}
}
} catch (Exception e) {
log.error("Can't execute command '" + commandId + "'", e);
}
}
}
use of org.eclipse.core.expressions.IEvaluationContext in project translationstudio8 by heartsome.
the class ConcordanceSearchHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
if (!isEnabled()) {
return null;
}
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor instanceof IXliffEditor) {
IXliffEditor xliffEditor = (IXliffEditor) editor;
String XLIFF_EDITOR_ID = "net.heartsome.cat.ts.ui.xliffeditor.nattable.editor";
IEditorPart editorRefer = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorRefer.getSite().getId().equals(XLIFF_EDITOR_ID)) {
// IProject project = ((FileEditorInput) editorRefer.getEditorInput()).getFile().getProject();
IFile file = ((FileEditorInput) editorRefer.getEditorInput()).getFile();
ProjectConfiger projectConfig = ProjectConfigerFactory.getProjectConfiger(file.getProject());
List<DatabaseModelBean> lstDatabase = projectConfig.getAllTmDbs();
if (lstDatabase.size() == 0) {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.ConcordanceSearchHandler.msgTitle"), Messages.getString("handler.ConcordanceSearchHandler.msg"));
return null;
}
String selectText = xliffEditor.getSelectPureText();
if ((selectText == null || selectText.equals("")) && xliffEditor.getSelectedRowIds().size() == 1) {
selectText = xliffEditor.getXLFHandler().getSrcPureText(xliffEditor.getSelectedRowIds().get(0));
} else if (selectText == null) {
selectText = "";
}
String srcLang = xliffEditor.getSrcColumnName();
String tgtLang = xliffEditor.getTgtColumnName();
ConcordanceSearchDialog dialog = new ConcordanceSearchDialog(editorRefer.getSite().getShell(), file, srcLang, tgtLang, selectText.trim());
Language srcLangL = LocaleService.getLanguageConfiger().getLanguageByCode(srcLang);
Language tgtLangL = LocaleService.getLanguageConfiger().getLanguageByCode(tgtLang);
dialog.open();
if (srcLangL.isBidi() || tgtLangL.isBidi()) {
dialog.getShell().setOrientation(SWT.RIGHT_TO_LEFT);
}
if (selectText != null && !selectText.trim().equals("")) {
dialog.initGroupIdAndSearch();
IWorkbenchPartSite site = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
ICommandService commandService = (ICommandService) site.getService(ICommandService.class);
Command command = commandService.getCommand(ActionFactory.COPY.getCommandId());
IEvaluationService evalService = (IEvaluationService) site.getService(IEvaluationService.class);
IEvaluationContext currentState = evalService.getCurrentState();
ExecutionEvent executionEvent = new ExecutionEvent(command, Collections.EMPTY_MAP, this, currentState);
try {
command.executeWithChecks(executionEvent);
} catch (Exception e1) {
}
}
}
}
return null;
}
use of org.eclipse.core.expressions.IEvaluationContext in project sling by apache.
the class SelectionUtils method getSelectionFromEvaluationContext.
public static ISelection getSelectionFromEvaluationContext(Object evaluationContext) {
if (!(evaluationContext instanceof IEvaluationContext)) {
return null;
}
IEvaluationContext ctx = (IEvaluationContext) evaluationContext;
Object selection = ctx.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
if (!(selection instanceof ISelection)) {
return null;
}
return (ISelection) selection;
}
use of org.eclipse.core.expressions.IEvaluationContext in project linuxtools by eclipse.
the class VagrantToolBarContributionItem method execute.
private void execute(String id, IStructuredSelection selection) {
ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service != null ? service.getCommand(id) : null;
if (command != null && command.isDefined()) {
try {
ParameterizedCommand pCmd = ParameterizedCommand.generateCommand(command, null);
IHandlerService handlerSvc = PlatformUI.getWorkbench().getService(IHandlerService.class);
IEvaluationContext ctx = handlerSvc.getCurrentState();
ctx = new EvaluationContext(ctx, selection);
ctx.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
handlerSvc.executeCommandInContext(pCmd, null, ctx);
} catch (Exception e) {
Activator.log(e);
}
}
}
use of org.eclipse.core.expressions.IEvaluationContext in project linuxtools by eclipse.
the class CommandUtils method execute.
/**
* Executes the command identified by the given {@code id} on a context
* based on the given selection
*
* @param id
* the id of the command to execute
* @param selection
* the selection to use as a context to run the command
*/
public static void execute(final String id, final IStructuredSelection selection) {
final ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
final Command command = service != null ? service.getCommand(id) : null;
if (command != null && command.isDefined()) {
try {
ParameterizedCommand pCmd = ParameterizedCommand.generateCommand(command, null);
IHandlerService handlerSvc = PlatformUI.getWorkbench().getService(IHandlerService.class);
IEvaluationContext ctx = handlerSvc.getCurrentState();
ctx = new EvaluationContext(ctx, selection);
ctx.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
handlerSvc.executeCommandInContext(pCmd, null, ctx);
} catch (Exception e) {
Activator.log(e);
}
}
}
Aggregations