List of usage examples for org.eclipse.jface.bindings Binding getParameterizedCommand
public final ParameterizedCommand getParameterizedCommand()
From source file:com.aptana.editor.findbar.impl.FindBarActions.java
License:Open Source License
/** * @return a map with the commands -> bindings available. *//* w w w. jav a2 s. com*/ public HashMap<String, List<TriggerSequence>> getCommandToBindings() { HashMap<String, List<TriggerSequence>> commandToBinding = new HashMap<String, List<TriggerSequence>>(); IWorkbenchPartSite site = textEditor.getSite(); IBindingService service = (IBindingService) site.getService(IBindingService.class); Binding[] bindings = service.getBindings(); for (int i = 0; i < bindings.length; i++) { Binding binding = bindings[i]; ParameterizedCommand command = binding.getParameterizedCommand(); if (command != null) { String id = command.getId(); // Filter only the actions we decided would be active. // // Note: we don't just make all actions active because they conflict with the find bar // expected accelerators, so, things as Alt+W don't work -- even a second Ctrl+F isn't properly // treated as specified in the options. // A different option could be filtering those out and let everything else enabled, // but this would need to be throughly tested to know if corner-cases work. if (fCommandToHandler.containsKey(id)) { List<TriggerSequence> list = commandToBinding.get(id); if (list == null) { list = new ArrayList<TriggerSequence>(); commandToBinding.put(id, list); } list.add(binding.getTriggerSequence()); } // Uncomment to know which actions will be disabled // else // { // try // { // System.out.println("Command disabled: " + id + ": " + command.getName()); // } // catch (NotDefinedException e) // { // // } // } } } return commandToBinding; }
From source file:com.aptana.scripting.keybindings.internal.KeybindingsManager.java
License:Open Source License
private void popup(final Shell shell, final IBindingService bindingService, final IContextService contextService, final ICommandElementsProvider commandElementsProvider, final List<CommandElement> commandElements, final Event event, final Binding binding, final Point initialLocation) { PopupDialog popupDialog = new PopupDialog(shell, PopupDialog.INFOPOPUP_SHELLSTYLE, true, false, false, false, false, null, null) {// ww w . j av a 2s. c om @Override protected Point getInitialLocation(Point initialSize) { Display display = shell.getDisplay(); Point cursorLocation = display.getCursorLocation(); if (initialLocation != null) { // Warp the cursor ? // if (!cursorLocation.equals(initialLocation)) // { // display.setCursorLocation(initialLocation); // } return initialLocation; } return cursorLocation; } protected Control createDialogArea(Composite parent) { registerShellType(); // Create a composite for the dialog area. final Composite composite = new Composite(parent, SWT.NONE); final GridLayout compositeLayout = new GridLayout(); compositeLayout.marginHeight = 1; compositeLayout.marginWidth = 1; composite.setLayout(compositeLayout); composite.setLayoutData(new GridData(GridData.FILL_BOTH)); // Layout the table. final Table commandElementTable = new Table(composite, SWT.FULL_SELECTION | SWT.SINGLE | SWT.NO_SCROLL); final GridData gridData = new GridData(GridData.FILL_BOTH); commandElementTable.setLayoutData(gridData); commandElementTable.setLinesVisible(true); // Initialize the columns and rows. final TableColumn columnCommandName = new TableColumn(commandElementTable, SWT.LEFT, 0); final TableColumn columnAccelerator = new TableColumn(commandElementTable, SWT.CENTER, 1); int mnemonic = 0; for (CommandElement commandElement : commandElements) { final String[] text = { commandElement.getDisplayName(), (mnemonic < MNEMONICS.length() ? String.valueOf(MNEMONICS.charAt(mnemonic++)) : "") }; //$NON-NLS-1$ final TableItem item = new TableItem(commandElementTable, SWT.NULL); item.setText(text); item.setData(CommandElement.class.getName(), commandElement); } if (binding != null) { ParameterizedCommand originalParameterizedCommand = binding.getParameterizedCommand(); // Add original command if (originalParameterizedCommand != null) { try { String name = originalParameterizedCommand.getName(); final TableItem item = new TableItem(commandElementTable, SWT.NULL); item.setText(new String[] { name, (mnemonic < MNEMONICS.length() ? String.valueOf(MNEMONICS.charAt(mnemonic++)) : "") }); //$NON-NLS-1$ item.setData(ParameterizedCommand.class.getName(), originalParameterizedCommand); } catch (NotDefinedException nde) { IdeLog.logError(ScriptingActivator.getDefault(), nde.getMessage(), nde); } } } Dialog.applyDialogFont(parent); columnAccelerator.pack(); columnCommandName.pack(); columnAccelerator.setWidth(columnAccelerator.getWidth() * 4); /* * If the user double-clicks on the table row, it should execute the selected command. */ commandElementTable.addListener(SWT.DefaultSelection, new Listener() { public final void handleEvent(final Event event) { // Try to execute the corresponding command. Object commandElement = null; Object parameterizedCommand = null; final TableItem[] selection = commandElementTable.getSelection(); if (selection.length > 0) { commandElement = selection[0].getData(CommandElement.class.getName()); parameterizedCommand = selection[0].getData(ParameterizedCommand.class.getName()); } close(); if (commandElement instanceof CommandElement) { executeCommandElement(commandElementsProvider, (CommandElement) commandElement); } else if (parameterizedCommand instanceof ParameterizedCommand) { try { executeCommand(binding, event); } catch (CommandException e) { IdeLog.logError(ScriptingActivator.getDefault(), e.getMessage(), e); } } } }); commandElementTable.addKeyListener(new KeyListener() { public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { if (!e.doit) { return; } int index = MNEMONICS.indexOf(e.character); if (index != -1) { if (index < commandElementTable.getItemCount()) { e.doit = false; TableItem tableItem = commandElementTable.getItem(index); Object commandElement = tableItem.getData(CommandElement.class.getName()); Object parameterizedCommand = tableItem .getData(ParameterizedCommand.class.getName()); close(); if (commandElement instanceof CommandElement) { executeCommandElement(commandElementsProvider, (CommandElement) commandElement); } else if (parameterizedCommand instanceof ParameterizedCommand) { try { executeCommand(binding, event); } catch (CommandException ex) { IdeLog.logError(ScriptingActivator.getDefault(), ex.getMessage(), ex); } } } } } }); return composite; } protected Color getBackground() { return getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); } @Override protected Control createContents(Composite parent) { return super.createContents(parent); } @Override public int open() { showingCommandsMenu = true; bindingService.setKeyFilterEnabled(false); return super.open(); } @Override public boolean close() { boolean closed = super.close(); if (closed) { showingCommandsMenu = false; bindingService.setKeyFilterEnabled(true); } return closed; } /** * Registers the shell as the same type as its parent with the context support. This ensures that it does * not modify the current state of the application. */ private final void registerShellType() { final Shell shell = getShell(); contextService.registerShell(shell, contextService.getShellType((Shell) shell.getParent())); } }; popupDialog.open(); }
From source file:com.aptana.scripting.keybindings.internal.KeybindingsManager.java
License:Open Source License
/** * Performs the actual execution of the command by looking up the current handler from the command manager. If there * is a handler and it is enabled, then it tries the actual execution. Execution failures are logged. * /*from w ww . ja v a 2 s . com*/ * @param binding * The binding that should be executed; should not be <code>null</code>. * @param trigger * The triggering event; may be <code>null</code>. * @return <code>true</code> if there was a handler; <code>false</code> otherwise. * @throws CommandException * if the handler does not complete execution for some reason. It is up to the caller of this method to * decide whether to log the message, display a dialog, or ignore this exception entirely. */ final void executeCommand(final Binding binding, final Event trigger) throws CommandException { final ParameterizedCommand parameterizedCommand = binding.getParameterizedCommand(); // Dispatch to the handler. final IHandlerService handlerService = (IHandlerService) workbench.getService(IHandlerService.class); final Command command = parameterizedCommand.getCommand(); command.setEnabled(handlerService.getCurrentState()); try { handlerService.executeCommand(parameterizedCommand, trigger); } catch (final NotDefinedException e) { // The command is not defined. Forwarded to the IExecutionListener. } catch (final NotEnabledException e) { // The command is not enabled. Forwarded to the IExecutionListener. } catch (final NotHandledException e) { // There is no handler. Forwarded to the IExecutionListener. } }
From source file:com.aptana.shared_core.bindings.KeyBindingHelper.java
License:Open Source License
/** * @param commandId the command we want to know about * @return the 'best' key sequence that will activate the given command *//* w w w . j av a 2 s . co m*/ public static KeySequence getCommandKeyBinding(String commandId) { Assert.isNotNull(commandId); final IBindingService bindingSvc = (IBindingService) PlatformUI.getWorkbench() .getAdapter(IBindingService.class); TriggerSequence keyBinding = bindingSvc.getBestActiveBindingFor(commandId); if (keyBinding instanceof KeySequence) { return (KeySequence) keyBinding; } List<Tuple<Binding, ParameterizedCommand>> matches = new ArrayList<Tuple<Binding, ParameterizedCommand>>(); //Ok, it may be that the binding we're looking for is not active, so, let's give a spin on all //the bindings Binding[] bindings = bindingSvc.getBindings(); for (Binding binding : bindings) { ParameterizedCommand command = binding.getParameterizedCommand(); if (command != null) { if (commandId.equals(command.getId())) { matches.add(new Tuple<Binding, ParameterizedCommand>(binding, command)); } } } for (Tuple<Binding, ParameterizedCommand> tuple : matches) { if (tuple.o1.getTriggerSequence() instanceof KeySequence) { KeySequence keySequence = (KeySequence) tuple.o1.getTriggerSequence(); return keySequence; } } return null; }
From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java
License:Open Source License
private final Control createButtonBar(final Composite parent) { final Composite buttonBar = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(2, false); layout.marginWidth = 0;/*from ww w . ja v a 2s.c om*/ buttonBar.setLayout(layout); buttonBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); final Button removeBindingButton = new Button(buttonBar, SWT.PUSH); removeBindingButton.setText(Messages.getString("KeyPreferencePage.remove_shortcuts")); //$NON-NLS-1$ removeBindingButton.addSelectionListener(new SelectionAdapter() { @Override public final void widgetSelected(final SelectionEvent event) { if (selectedCommand != null) { Binding b = commandMap.get(selectedCommand.getId()); if (b != null) { Binding userBinding = new KeyBinding((KeySequence) b.getTriggerSequence(), null, activeSchemeId, b.getContextId(), null, null, null, Binding.USER); userMap.put(userBinding.getTriggerSequence(), userBinding); commandMap.remove(selectedCommand.getId()); } refreshViewer(); doValidate(); } } }); final Button restore = new Button(buttonBar, SWT.PUSH); restore.setText(Messages.getString("KeyPreferencePage.restore_command")); //$NON-NLS-1$ restore.addSelectionListener(new SelectionAdapter() { @Override public final void widgetSelected(final SelectionEvent event) { if (selectedCommand != null) { String id = selectedCommand.getId(); Binding b = commandMap.get(id); userMap.remove(b.getTriggerSequence()); for (Binding sb : systemMap.values()) { if (sb.getParameterizedCommand() != null && sb.getParameterizedCommand().getId().equals(id)) { commandMap.put(sb.getParameterizedCommand().getId(), sb); break; } } refreshViewer(); doValidate(); } } }); return buttonBar; }
From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java
License:Open Source License
@SuppressWarnings("unused") private void createDefinitionArea(Composite parent) { final Composite composite = new Composite(parent, SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); composite.setLayout(new GridLayout(2, false)); new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.command2")); //$NON-NLS-1$ nameField = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER | SWT.READ_ONLY); nameField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.description")); //$NON-NLS-1$ descriptionField = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.READ_ONLY | SWT.WRAP); GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false); layoutData.heightHint = 50;//from ww w . j a va 2 s . co m descriptionField.setLayoutData(layoutData); new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.key_sequence")); //$NON-NLS-1$ Composite keyGroup = new Composite(composite, SWT.NONE); keyGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true)); GridLayout layout = new GridLayout(2, false); layout.marginHeight = 0; layout.marginWidth = 0; keyGroup.setLayout(layout); keyField = new Text(keyGroup, SWT.SINGLE | SWT.LEAD | SWT.BORDER); keyField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); keySequenceField = new KeySequenceText(keyField); keySequenceField.setKeyStrokeLimit(2); keySequenceField.addPropertyChangeListener(new IPropertyChangeListener() { public final void propertyChange(final PropertyChangeEvent event) { if (!event.getOldValue().equals(event.getNewValue())) { final KeySequence keySequence = keySequenceField.getKeySequence(); if (selectedCommand == null || !keySequence.isComplete()) return; boolean empty = keySequence.isEmpty(); Binding newBinding; Binding b = commandMap.get(selectedCommand.getId()); if (b != null) { if (!keySequence.equals(b.getTriggerSequence())) { newBinding = new KeyBinding(keySequence, empty ? null : b.getParameterizedCommand(), activeSchemeId, b.getContextId(), null, null, null, Binding.USER); userMap.remove(b.getTriggerSequence()); userMap.put(keySequence, newBinding); if (empty) commandMap.remove(selectedCommand.getId()); else commandMap.put(selectedCommand.getId(), newBinding); } } else if (!empty) { ParameterizedCommand pc = new ParameterizedCommand(selectedCommand, null); newBinding = new KeyBinding(keySequence, pc, activeSchemeId, "org.eclipse.ui.contexts.window", //$NON-NLS-1$ null, null, null, Binding.USER); userMap.put(keySequence, newBinding); commandMap.put(selectedCommand.getId(), newBinding); } refreshViewer(); doValidate(); keyField.setSelection(keyField.getTextLimit()); } } }); final Button helpButton = new Button(keyGroup, SWT.ARROW | SWT.LEFT); helpButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); helpButton.setToolTipText(Messages.getString("KeyPreferencePage.add_a_special_key")); //$NON-NLS-1$ // Arrow buttons aren't normally added to the tab list. Let's fix that. final Control[] tabStops = keyGroup.getTabList(); final ArrayList<Control> newTabStops = new ArrayList<Control>(); for (int i = 0; i < tabStops.length; i++) { Control tabStop = tabStops[i]; newTabStops.add(tabStop); if (keyField.equals(tabStop)) newTabStops.add(helpButton); } keyGroup.setTabList(newTabStops.toArray(new Control[newTabStops.size()])); // Construct the menu to attach to the above button. final Menu addKeyMenu = new Menu(helpButton); final Iterator<?> trappedKeyItr = KeySequenceText.TRAPPED_KEYS.iterator(); while (trappedKeyItr.hasNext()) { final KeyStroke trappedKey = (KeyStroke) trappedKeyItr.next(); final MenuItem menuItem = new MenuItem(addKeyMenu, SWT.PUSH); menuItem.setText(trappedKey.format()); menuItem.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { keySequenceField.insert(trappedKey); keyField.setFocus(); keyField.setSelection(keyField.getTextLimit()); } }); } helpButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent selectionEvent) { Point buttonLocation = helpButton.getLocation(); buttonLocation = composite.toDisplay(buttonLocation.x, buttonLocation.y); Point buttonSize = helpButton.getSize(); addKeyMenu.setLocation(buttonLocation.x, buttonLocation.y + buttonSize.y); addKeyMenu.setVisible(true); } }); new Label(composite, SWT.NONE); userLabel = new Label(composite, SWT.NONE); userLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); }
From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java
License:Open Source License
private void createConflictsArea(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); composite.setLayout(new GridLayout(1, false)); new Label(composite, SWT.NONE).setText(Messages.getString("KeyPreferencePage.conflicts")); //$NON-NLS-1$ conflictViewer = new TableViewer(composite, SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION); Table table = conflictViewer.getTable(); TableViewerColumn bindingNameColumn = new TableViewerColumn(conflictViewer, SWT.LEAD); bindingNameColumn.getColumn().setWidth(250); table.setLayoutData(new GridData(250, 80)); bindingNameColumn.setLabelProvider(new ZColumnLabelProvider() { @Override//from w ww . ja v a 2 s . c o m public String getText(Object element) { if (element instanceof Binding[]) { StringBuilder sb = new StringBuilder(); for (Binding binding : ((Binding[]) element)) { ParameterizedCommand parameterizedCommand = binding.getParameterizedCommand(); String s = Messages.getString("KeyPreferencePage.undefined"); //$NON-NLS-1$ if (parameterizedCommand != null) try { s = parameterizedCommand.getName(); } catch (NotDefinedException e) { // do nothing } if (sb.length() > 0) sb.append("; "); //$NON-NLS-1$ sb.append(s); } return sb.toString(); } return element.toString(); } }); conflictViewer.setContentProvider(ArrayContentProvider.getInstance()); conflictViewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); if (!selection.isEmpty()) { Binding[] conflict = (Binding[]) selection.getFirstElement(); bindingViewer.setSelection(new StructuredSelection(conflict[0])); } } }); }
From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java
License:Open Source License
private void updateViewer() { for (Binding binding : systemMap.values()) { ParameterizedCommand parameterizedCommand = binding.getParameterizedCommand(); if (parameterizedCommand != null) commandMap.put(parameterizedCommand.getId(), binding); }/*w w w . j a va 2 s . c o m*/ for (Binding binding : userMap.values()) { ParameterizedCommand parameterizedCommand = binding.getParameterizedCommand(); if (parameterizedCommand != null) commandMap.put(parameterizedCommand.getId(), binding); else { Binding systemBinding = systemMap.get(binding.getTriggerSequence()); if (systemBinding != null) { parameterizedCommand = systemBinding.getParameterizedCommand(); if (parameterizedCommand != null) commandMap.remove(parameterizedCommand.getId()); } } } refreshViewer(); }
From source file:com.bdaum.zoom.ui.internal.preferences.KeyPreferencePage.java
License:Open Source License
@Override protected String doValidate() { Map<TriggerSequence, List<Binding>> conflictMap = new HashMap<>(); for (Map.Entry<TriggerSequence, Binding> entry : systemMap.entrySet()) { TriggerSequence key = entry.getKey(); Binding binding = userMap.get(key); if (binding == null) binding = entry.getValue();// ww w. j a v a 2 s . c om if (binding.getParameterizedCommand() != null && binding.getSchemeId().equals(activeSchemeId)) { TriggerSequence triggerSequence = binding.getTriggerSequence(); List<Binding> list = conflictMap.get(triggerSequence); if (list == null) { list = new ArrayList<Binding>(); conflictMap.put(triggerSequence, list); } list.add(binding); } } for (Map.Entry<TriggerSequence, Binding> entry : userMap.entrySet()) { TriggerSequence key = entry.getKey(); Binding userBinding = entry.getValue(); Binding binding = systemMap.get(key); if (binding != null && binding.getParameterizedCommand() != null && userBinding.getParameterizedCommand() != null && !binding.getParameterizedCommand().getId() .equals(userBinding.getParameterizedCommand().getId()) && binding.getSchemeId().equals(activeSchemeId)) { TriggerSequence triggerSequence = binding.getTriggerSequence(); List<Binding> list = conflictMap.get(triggerSequence); if (list == null) { list = new ArrayList<Binding>(); conflictMap.put(triggerSequence, list); } if (!list.contains(binding)) list.add(binding); } } List<Binding[]> conflicts = new ArrayList<Binding[]>(); for (List<Binding> conflict : conflictMap.values()) if (conflict.size() > 1) conflicts.add(conflict.toArray(new Binding[conflict.size()])); conflictViewer.setInput(conflicts); return conflicts.isEmpty() ? null : Messages.getString("KeyPreferencePage.there_are_conflicts"); //$NON-NLS-1$ }
From source file:com.google.dart.tools.ui.internal.preferences.DartKeyBindingPersistence.java
License:Open Source License
public Binding findBinding(String commandName, String platform) throws NotDefinedException { Binding[] bindings = bindingService.getBindings(); if (bindings != null) { for (Binding binding : bindings) { if (binding.getSchemeId().equals(DART_BINDING_SCHEME)) { if ((platform != null && platform.equals(binding.getPlatform())) || binding.getPlatform() == null) { ParameterizedCommand pc = binding.getParameterizedCommand(); if (pc != null) { Command cmd = pc.getCommand(); if (cmd != null) { if (commandName.equals(pc.getName())) { return binding; }//from w w w . ja v a 2 s . c o m } } } } } } return null; }