use of org.eclipse.ui.commands.ICommandService in project dbeaver by serge-rider.
the class ActionUtils method findCommandDescription.
@Nullable
public static String findCommandDescription(String commandId, IServiceLocator serviceLocator, boolean shortcutOnly) {
String commandName = null;
String shortcut = null;
ICommandService commandService = serviceLocator.getService(ICommandService.class);
if (commandService != null) {
Command command = commandService.getCommand(commandId);
if (command != null && command.isDefined()) {
try {
commandName = command.getName();
} catch (NotDefinedException e) {
log.debug(e);
}
}
}
IBindingService bindingService = serviceLocator.getService(IBindingService.class);
if (bindingService != null) {
TriggerSequence sequence = null;
for (Binding b : bindingService.getBindings()) {
ParameterizedCommand parameterizedCommand = b.getParameterizedCommand();
if (parameterizedCommand != null && commandId.equals(parameterizedCommand.getId())) {
sequence = b.getTriggerSequence();
}
}
if (sequence == null) {
sequence = bindingService.getBestActiveBindingFor(commandId);
}
if (sequence != null) {
shortcut = sequence.format();
}
}
if (shortcutOnly) {
return shortcut == null ? "?" : shortcut;
}
if (shortcut == null) {
return commandName;
}
if (commandName == null) {
return shortcut;
}
return commandName + " (" + shortcut + ")";
}
use of org.eclipse.ui.commands.ICommandService 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.ui.commands.ICommandService in project translationstudio8 by heartsome.
the class ApplicationWorkbenchWindowAdvisor method addViewPartVisibleListener.
private void addViewPartVisibleListener(final IWorkbenchWindow window) {
final ICommandService commandService = (ICommandService) window.getService(ICommandService.class);
window.getActivePage().addPartListener(new PartAdapter2() {
private void setStates(String partId) {
if ("net.heartsome.cat.common.ui.navigator.view".equals(partId)) {
commandService.refreshElements("net.heartsome.cat.common.ui.navigator.command.OpenNavigatorView", null);
} else if ("net.heartsome.cat.ts.ui.views.DocumentPropertiesViewPart".equals(partId)) {
commandService.refreshElements("net.heartsome.cat.ts.ui.handlers.OpenDocumentPropertiesViewCommand", null);
} else if ("net.heartsome.cat.ts.ui.translation.view.matchview".equals(partId)) {
commandService.refreshElements("net.heartsome.cat.ts.ui.translation.menu.command.openMatchView", null);
} else if ("net.heartsome.cat.ts.ui.term.view.termView".equals(partId)) {
commandService.refreshElements("net.heartsome.cat.ts.ui.term.command.openTermView", null);
} else if ("net.heartsome.cat.ts.ui.qa.views.QAResultViewPart".equals(partId)) {
commandService.refreshElements("net.heartsome.cat.ts.ui.qa.handlers.OpenQAResultViewCommand", null);
} else if ("net.heartsome.cat.ts.websearch.ui.view.BrowserViewPart".equals(partId)) {
commandService.refreshElements("net.heartsome.cat.ts.websearch.showOrHideView", null);
}
}
/**
* 视图打开时
* @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
*/
public void partOpened(final IWorkbenchPartReference partRef) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
setStates(partRef.getId());
}
});
}
/**
* 视图关闭时
* @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
*/
public void partClosed(IWorkbenchPartReference partRef) {
if (!partRef.getPage().getWorkbenchWindow().getWorkbench().isClosing()) {
setStates(partRef.getId());
}
// 关闭时,根据是否是合并打开的文件,如果是,标识其是否被存储--robert
if ("net.heartsome.cat.ts.ui.xliffeditor.nattable.editor".equals(partRef.getId())) {
if (partRef.getPage().getWorkbenchWindow().getWorkbench().isClosing()) {
IXliffEditor xlfEditor = (IXliffEditor) partRef.getPart(true);
xlfEditor.setStore(true);
}
}
}
});
}
use of org.eclipse.ui.commands.ICommandService in project translationstudio8 by heartsome.
the class OpenToolBarHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
final IWorkbenchWindow activeWorkbenchWindow = HandlerUtil.getActiveWorkbenchWindowChecked(event);
if (activeWorkbenchWindow instanceof WorkbenchWindow) {
WorkbenchWindow window = (WorkbenchWindow) activeWorkbenchWindow;
window.toggleToolbarVisibility();
}
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
commandService.refreshElements(event.getCommand().getId(), null);
return null;
}
use of org.eclipse.ui.commands.ICommandService 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;
}
Aggregations