List of usage examples for org.eclipse.jface.bindings.keys KeySequence isComplete
public final boolean isComplete()
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 www .java 2 s . c o 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:net.sourceforge.vrapper.plugin.bindingkeeper.preferences.KeySequenceInput.java
License:Open Source License
@Override protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); getText().addFocusListener(new FocusListener() { @Override/*from w w w. jav a 2 s . c o m*/ public void focusGained(FocusEvent e) { bindingService.setKeyFilterEnabled(false); } @Override public void focusLost(FocusEvent e) { bindingService.setKeyFilterEnabled(true); } }); getText().addDisposeListener(new DisposeListener() { @Override public void widgetDisposed(DisposeEvent e) { if (!bindingService.isKeyFilterEnabled()) { bindingService.setKeyFilterEnabled(true); } } }); final KeySequenceText keySequenceText = new KeySequenceText(getText()); keySequenceText.setKeyStrokeLimit(1); keySequenceText.addPropertyChangeListener(new IPropertyChangeListener() { @Override public final void propertyChange(final PropertyChangeEvent event) { if (!event.getOldValue().equals(event.getNewValue())) { final KeySequence keySequence = keySequenceText.getKeySequence(); if (!keySequence.isComplete()) { return; } getText().setSelection(getText().getTextLimit()); } } }); applyDialogFont(composite); return composite; }
From source file:org.eclipse.ui.internal.keys.BindingPersistence.java
License:Open Source License
/** * Reads all of the binding definitions from the preferences. * //from w w w.ja v a 2s . c o m * @param preferences * The memento for the commands preferences key. * @param bindingManager * The binding manager to which the bindings should be added; * must not be <code>null</code>. * @param commandService * The command service for the workbench; must not be * <code>null</code>. */ private static final void readBindingsFromPreferences(final IMemento preferences, final BindingManager bindingManager, final CommandManager commandService) { List warningsToLog = new ArrayList(1); if (preferences != null) { final IMemento[] preferenceMementos = preferences.getChildren(TAG_KEY_BINDING); int preferenceMementoCount = preferenceMementos.length; for (int i = preferenceMementoCount - 1; i >= 0; i--) { final IMemento memento = preferenceMementos[i]; // Read out the command id. String commandId = readOptional(memento, ATT_COMMAND_ID); if (commandId == null) { commandId = readOptional(memento, ATT_COMMAND); } String viewParameter = null; final Command command; if (commandId != null) { command = commandService.getCommand(commandId); } else { command = null; } // Read out the scheme id. String schemeId = readOptional(memento, ATT_KEY_CONFIGURATION_ID); if (schemeId == null) { schemeId = readRequired(memento, ATT_CONFIGURATION, warningsToLog, "Key bindings need a scheme or key configuration"); //$NON-NLS-1$ if (schemeId == null) { continue; } } // Read out the context id. String contextId = readOptional(memento, ATT_CONTEXT_ID); if (contextId == null) { contextId = readOptional(memento, ATT_SCOPE); } if (LEGACY_DEFAULT_SCOPE.equals(contextId)) { contextId = null; } if (contextId == null) { contextId = IContextIds.CONTEXT_ID_WINDOW; } // Read out the key sequence. String keySequenceText = readOptional(memento, ATT_KEY_SEQUENCE); KeySequence keySequence = null; if (keySequenceText == null) { keySequenceText = readRequired(memento, ATT_STRING, warningsToLog, "Key bindings need a key sequence or string"); //$NON-NLS-1$ if (keySequenceText == null) { continue; } // The key sequence is in the old-style format. keySequence = convert2_1Sequence(parse2_1Sequence(keySequenceText)); } else { // The key sequence is in the new-style format. try { keySequence = KeySequence.getInstance(keySequenceText); } catch (final ParseException e) { addWarning(warningsToLog, "Could not parse", null, //$NON-NLS-1$ commandId, "keySequence", keySequenceText); //$NON-NLS-1$ continue; } if (keySequence.isEmpty() || !keySequence.isComplete()) { addWarning(warningsToLog, "Key bindings cannot use an empty or incomplete key sequence", //$NON-NLS-1$ null, commandId, "keySequence", keySequence //$NON-NLS-1$ .toString()); continue; } } // Read out the locale and platform. final String locale = readOptional(memento, ATT_LOCALE); final String platform = readOptional(memento, ATT_PLATFORM); // Read out the parameters final ParameterizedCommand parameterizedCommand; if (command == null) { parameterizedCommand = null; } else if (viewParameter != null) { HashMap parms = new HashMap(); parms.put(ShowViewMenu.VIEW_ID_PARM, viewParameter); parameterizedCommand = ParameterizedCommand.generateCommand(command, parms); } else { parameterizedCommand = readParameters(memento, warningsToLog, command); } final Binding binding = new KeyBinding(keySequence, parameterizedCommand, schemeId, contextId, locale, platform, null, Binding.USER); bindingManager.addBinding(binding); } } // If there were any warnings, then log them now. logWarnings(warningsToLog, "Warnings while parsing the key bindings from the preference store"); //$NON-NLS-1$ }
From source file:org.eclipse.ui.internal.keys.BindingPersistence.java
License:Open Source License
private static KeySequence readKeySequence(IConfigurationElement configurationElement, List warningsToLog, String commandId, String keySequenceText) { KeySequence keySequence = null; if (keySequenceText.equals(configurationElement.getAttribute(ATT_STRING))) { // The key sequence is in the old-style format. try {/*from w w w. jav a2 s .com*/ keySequence = convert2_1Sequence(parse2_1Sequence(keySequenceText)); } catch (final IllegalArgumentException e) { addWarning(warningsToLog, "Could not parse key sequence", //$NON-NLS-1$ configurationElement, commandId, "keySequence", //$NON-NLS-1$ keySequenceText); return null; } } else { // The key sequence is in the new-style format. try { keySequence = KeySequence.getInstance(keySequenceText); } catch (final ParseException e) { addWarning(warningsToLog, "Could not parse key sequence", //$NON-NLS-1$ configurationElement, commandId, "keySequence", //$NON-NLS-1$ keySequenceText); return null; } if (keySequence.isEmpty() || !keySequence.isComplete()) { addWarning(warningsToLog, "Key bindings should not have an empty or incomplete key sequence", //$NON-NLS-1$ configurationElement, commandId, "keySequence", //$NON-NLS-1$ keySequence.toString()); return null; } } return keySequence; }
From source file:org.eclipse.ui.internal.keys.NewKeysPreferencePage.java
License:Open Source License
private final void createDataControls(final Composite parent) { GridLayout layout;//from w w w. ja va 2 s. co m GridData gridData; // Creates the data area. final Composite dataArea = new Composite(parent, SWT.NONE); layout = new GridLayout(2, true); layout.marginWidth = 0; dataArea.setLayout(layout); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.FILL; dataArea.setLayoutData(gridData); // LEFT DATA AREA // Creates the left data area. final Composite leftDataArea = new Composite(dataArea, SWT.NONE); layout = new GridLayout(3, false); leftDataArea.setLayout(layout); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.verticalAlignment = SWT.TOP; gridData.horizontalAlignment = SWT.FILL; leftDataArea.setLayoutData(gridData); // The command name label. final Label commandNameLabel = new Label(leftDataArea, SWT.NONE); commandNameLabel.setText(NewKeysPreferenceMessages.CommandNameLabel_Text); // The current command name. commandNameValueLabel = new Label(leftDataArea, SWT.NONE); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalSpan = 2; gridData.horizontalAlignment = SWT.FILL; commandNameValueLabel.setLayoutData(gridData); final Label commandDescriptionlabel = new Label(leftDataArea, SWT.LEAD); commandDescriptionlabel.setText(NewKeysPreferenceMessages.CommandDescriptionLabel_Text); gridData = new GridData(); gridData.verticalAlignment = SWT.BEGINNING; commandDescriptionlabel.setLayoutData(gridData); fDescriptionText = new Text(leftDataArea, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.READ_ONLY); // The binding label. final Label bindingLabel = new Label(leftDataArea, SWT.NONE); bindingLabel.setText(NewKeysPreferenceMessages.BindingLabel_Text); // The key sequence entry widget. fBindingText = new Text(leftDataArea, SWT.BORDER); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.FILL; gridData.widthHint = 200; fBindingText.setLayoutData(gridData); fBindingText.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { fBindingService.setKeyFilterEnabled(false); } public void focusLost(FocusEvent e) { fBindingService.setKeyFilterEnabled(true); } }); fBindingText.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { if (!fBindingService.isKeyFilterEnabled()) { fBindingService.setKeyFilterEnabled(true); } } }); fKeySequenceText = new KeySequenceText(fBindingText); fKeySequenceText.setKeyStrokeLimit(4); fKeySequenceText.addPropertyChangeListener(new IPropertyChangeListener() { public final void propertyChange(final PropertyChangeEvent event) { if (!event.getOldValue().equals(event.getNewValue())) { final KeySequence keySequence = fKeySequenceText.getKeySequence(); if (!keySequence.isComplete()) { return; } BindingElement activeBinding = (BindingElement) keyController.getBindingModel() .getSelectedElement(); if (activeBinding != null) { activeBinding.setTrigger(keySequence); } fBindingText.setSelection(fBindingText.getTextLimit()); } } }); // Button for adding trapped key strokes final Button addKeyButton = new Button(leftDataArea, SWT.LEFT | SWT.ARROW); addKeyButton.setToolTipText(NewKeysPreferenceMessages.AddKeyButton_ToolTipText); gridData = new GridData(); gridData.heightHint = fSchemeCombo.getCombo().getTextHeight(); addKeyButton.setLayoutData(gridData); // Arrow buttons aren't normally added to the tab list. Let's fix that. final Control[] tabStops = dataArea.getTabList(); final ArrayList newTabStops = new ArrayList(); for (int i = 0; i < tabStops.length; i++) { Control tabStop = tabStops[i]; newTabStops.add(tabStop); if (fBindingText.equals(tabStop)) { newTabStops.add(addKeyButton); } } final Control[] newTabStopArray = (Control[]) newTabStops.toArray(new Control[newTabStops.size()]); dataArea.setTabList(newTabStopArray); // Construct the menu to attach to the above button. final Menu addKeyMenu = new Menu(addKeyButton); 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() { public void widgetSelected(SelectionEvent e) { fKeySequenceText.insert(trappedKey); fBindingText.setFocus(); fBindingText.setSelection(fBindingText.getTextLimit()); } }); } addKeyButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent selectionEvent) { Point buttonLocation = addKeyButton.getLocation(); buttonLocation = dataArea.toDisplay(buttonLocation.x, buttonLocation.y); Point buttonSize = addKeyButton.getSize(); addKeyMenu.setLocation(buttonLocation.x, buttonLocation.y + buttonSize.y); addKeyMenu.setVisible(true); } }); // The when label. final Label whenLabel = new Label(leftDataArea, SWT.NONE); whenLabel.setText(NewKeysPreferenceMessages.WhenLabel_Text); // The when combo. fWhenCombo = new ComboViewer(leftDataArea); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.FILL; gridData.horizontalSpan = 2; ViewerComparator comparator = new ViewerComparator(); fWhenCombo.setComparator(comparator); fWhenCombo.getCombo().setVisibleItemCount(ITEMS_TO_SHOW); fWhenCombo.getCombo().setLayoutData(gridData); fWhenCombo.setContentProvider(new ModelContentProvider()); fWhenCombo.setLabelProvider(new ListLabelProvider()); fWhenCombo.addSelectionChangedListener(new ISelectionChangedListener() { public final void selectionChanged(final SelectionChangedEvent event) { ContextElement context = (ContextElement) ((IStructuredSelection) event.getSelection()) .getFirstElement(); if (context != null) { keyController.getContextModel().setSelectedElement(context); } } }); IPropertyChangeListener whenListener = new IPropertyChangeListener() { // Sets the combo selection when a new keybinding is selected? public void propertyChange(PropertyChangeEvent event) { if (event.getSource() == keyController.getContextModel() && CommonModel.PROP_SELECTED_ELEMENT.equals(event.getProperty())) { Object newVal = event.getNewValue(); StructuredSelection structuredSelection = newVal == null ? null : new StructuredSelection(newVal); fWhenCombo.setSelection(structuredSelection, true); } } }; keyController.addPropertyChangeListener(whenListener); // RIGHT DATA AREA // Creates the right data area. final Composite rightDataArea = new Composite(dataArea, SWT.NONE); layout = new GridLayout(1, false); rightDataArea.setLayout(layout); gridData = new GridData(SWT.FILL, SWT.FILL, true, true); rightDataArea.setLayoutData(gridData); new Label(rightDataArea, SWT.NONE); // filler // The description label. final Label descriptionLabel = new Label(rightDataArea, SWT.NONE); descriptionLabel.setText(NewKeysPreferenceMessages.ConflictsLabel_Text); gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.horizontalAlignment = SWT.FILL; descriptionLabel.setLayoutData(gridData); conflictViewer = new TableViewer(rightDataArea, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION); Table table = conflictViewer.getTable(); table.setHeaderVisible(true); TableColumn bindingNameColumn = new TableColumn(table, SWT.LEAD); bindingNameColumn.setText(NewKeysPreferenceMessages.CommandNameColumn_Text); bindingNameColumn.setWidth(150); TableColumn bindingContextNameColumn = new TableColumn(table, SWT.LEAD); bindingContextNameColumn.setText(NewKeysPreferenceMessages.WhenColumn_Text); bindingContextNameColumn.setWidth(150); gridData = new GridData(SWT.FILL, SWT.FILL, true, true); //gridData.horizontalIndent = 10; table.setLayoutData(gridData); TableLayout tableLayout = new TableLayout(); tableLayout.addColumnData(new ColumnWeightData(60)); tableLayout.addColumnData(new ColumnWeightData(40)); table.setLayout(tableLayout); conflictViewer.setContentProvider(new IStructuredContentProvider() { public Object[] getElements(Object inputElement) { if (inputElement instanceof Collection) { return ((Collection) inputElement).toArray(); } return new Object[0]; } public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } }); conflictViewer.setLabelProvider(new BindingElementLabelProvider() { public String getColumnText(Object o, int index) { BindingElement element = (BindingElement) o; if (index == 0) { return element.getName(); } return element.getContext().getName(); } }); conflictViewer.addSelectionChangedListener(new ISelectionChangedListener() { // When the conflict viewer's selection changes, update the // model's current selection public void selectionChanged(SelectionChangedEvent event) { ModelElement binding = (ModelElement) ((IStructuredSelection) event.getSelection()) .getFirstElement(); BindingModel bindingModel = keyController.getBindingModel(); if (binding != null && binding != bindingModel.getSelectedElement()) { StructuredSelection selection = new StructuredSelection(binding); bindingModel.setSelectedElement(binding); conflictViewer.setSelection(selection); boolean selectionVisible = false; TreeItem[] items = fFilteredTree.getViewer().getTree().getItems(); for (int i = 0; i < items.length; i++) { if (items[i].getData().equals(binding)) { selectionVisible = true; break; } } if (!selectionVisible) { fFilteredTree.getFilterControl().setText(""); //$NON-NLS-1$ fFilteredTree.getViewer().refresh(); bindingModel.setSelectedElement(binding); conflictViewer.setSelection(selection); } } } }); IPropertyChangeListener conflictsListener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { if (event.getSource() == keyController.getConflictModel() && CommonModel.PROP_SELECTED_ELEMENT.equals(event.getProperty())) { if (keyController.getConflictModel().getConflicts() != null) { Object newVal = event.getNewValue(); StructuredSelection structuredSelection = newVal == null ? null : new StructuredSelection(newVal); conflictViewer.setSelection(structuredSelection, true); } } else if (ConflictModel.PROP_CONFLICTS.equals(event.getProperty())) { conflictViewer.setInput(event.getNewValue()); } else if (ConflictModel.PROP_CONFLICTS_ADD.equals(event.getProperty())) { conflictViewer.add(event.getNewValue()); } else if (ConflictModel.PROP_CONFLICTS_REMOVE.equals(event.getProperty())) { conflictViewer.remove(event.getNewValue()); } } }; keyController.addPropertyChangeListener(conflictsListener); IPropertyChangeListener dataUpdateListener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { BindingElement bindingElement = null; boolean weCare = false; if (event.getSource() == keyController.getBindingModel() && CommonModel.PROP_SELECTED_ELEMENT.equals(event.getProperty())) { bindingElement = (BindingElement) event.getNewValue(); weCare = true; } else if (event.getSource() == keyController.getBindingModel().getSelectedElement() && ModelElement.PROP_MODEL_OBJECT.equals(event.getProperty())) { bindingElement = (BindingElement) event.getSource(); weCare = true; } if (bindingElement == null && weCare) { commandNameValueLabel.setText(""); //$NON-NLS-1$ fDescriptionText.setText(""); //$NON-NLS-1$ fBindingText.setText(""); //$NON-NLS-1$ } else if (bindingElement != null) { commandNameValueLabel.setText(bindingElement.getName()); String desc = bindingElement.getDescription(); fDescriptionText.setText(desc == null ? "" : desc); //$NON-NLS-1$ KeySequence trigger = (KeySequence) bindingElement.getTrigger(); fKeySequenceText.setKeySequence(trigger); } } }; keyController.addPropertyChangeListener(dataUpdateListener); }