Example usage for org.eclipse.jface.bindings.keys KeyLookupFactory getDefault

List of usage examples for org.eclipse.jface.bindings.keys KeyLookupFactory getDefault

Introduction

In this page you can find the example usage for org.eclipse.jface.bindings.keys KeyLookupFactory getDefault.

Prototype

public static final IKeyLookup getDefault() 

Source Link

Document

An accessor for the current default look-up.

Usage

From source file:com.cisco.yangide.ext.refactoring.ui.RenameInformationPopup.java

License:Open Source License

private static String getEnterBinding() {
    return KeyStroke.getInstance(KeyLookupFactory.getDefault().formalKeyLookup(IKeyLookup.CR_NAME)).format();
}

From source file:com.github.haixing_hu.swt.action.ActionContributionItemEx.java

License:Open Source License

/**
 * Synchronizes the UI with the given property.
 *
 * @param propertyName/*from  ww  w  .  j a  v  a 2s .c  o m*/
 *          the name of the property, or <code>null</code> meaning all
 *          applicable properties
 */
@Override
public void update(String propertyName) {
    if (widget != null) {
        // determine what to do
        final boolean textChanged = (propertyName == null) || propertyName.equals(IAction.TEXT);
        boolean imageChanged = (propertyName == null) || propertyName.equals(IAction.IMAGE);
        final boolean tooltipTextChanged = (propertyName == null) || propertyName.equals(IAction.TOOL_TIP_TEXT);
        final boolean enableStateChanged = (propertyName == null) || propertyName.equals(IAction.ENABLED)
                || propertyName.equals(IContributionManagerOverrides.P_ENABLED);
        final boolean checkChanged = ((action.getStyle() == IAction.AS_CHECK_BOX)
                || (action.getStyle() == IAction.AS_RADIO_BUTTON))
                && ((propertyName == null) || propertyName.equals(IAction.CHECKED));

        if (!showImage) {
            //  do not update the image if not show image
            imageChanged = false;
        }
        if (widget instanceof ToolItem) {
            final ToolItem ti = (ToolItem) widget;
            String text = action.getText();
            // the set text is shown only if there is no image or if forced
            // by MODE_FORCE_TEXT
            final boolean showText = (text != null)
                    && (((getMode() & MODE_FORCE_TEXT) != 0) || !hasImages(action));

            // only do the trimming if the text will be used
            if (showText && (text != null)) {
                text = Action.removeAcceleratorText(text);
                text = Action.removeMnemonics(text);
            }

            if (textChanged) {
                final String textToSet = showText ? text : ""; //$NON-NLS-1$
                final boolean rightStyle = (ti.getParent().getStyle() & SWT.RIGHT) != 0;
                if (rightStyle || !ti.getText().equals(textToSet)) {
                    // In addition to being required to update the text if
                    // it
                    // gets nulled out in the action, this is also a
                    // workaround
                    // for bug 50151: Using SWT.RIGHT on a ToolBar leaves
                    // blank space
                    ti.setText(textToSet);
                }
            }

            if (imageChanged) {
                // only substitute a missing image if it has no text
                updateImages(!showText);
            }

            if (tooltipTextChanged || textChanged) {
                String toolTip = action.getToolTipText();
                if ((toolTip == null) || (toolTip.length() == 0)) {
                    toolTip = text;
                }

                final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance()
                        .getCallback();
                final String commandId = action.getActionDefinitionId();
                if ((callback != null) && (commandId != null) && (toolTip != null)) {
                    final String acceleratorText = callback.getAcceleratorText(commandId);
                    if ((acceleratorText != null) && (acceleratorText.length() != 0)) {
                        toolTip = JFaceResources.format("Toolbar_Tooltip_Accelerator", //$NON-NLS-1$
                                new Object[] { toolTip, acceleratorText });
                    }
                }

                // if the text is showing, then only set the tooltip if
                // different
                if (!showText || ((toolTip != null) && !toolTip.equals(text))) {
                    ti.setToolTipText(toolTip);
                } else {
                    ti.setToolTipText(null);
                }
            }

            if (enableStateChanged) {
                final boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

                if (ti.getEnabled() != shouldBeEnabled) {
                    ti.setEnabled(shouldBeEnabled);
                }
            }

            if (checkChanged) {
                final boolean bv = action.isChecked();

                if (ti.getSelection() != bv) {
                    ti.setSelection(bv);
                }
            }
            return;
        }

        if (widget instanceof MenuItem) {
            final MenuItem mi = (MenuItem) widget;

            if (textChanged) {
                int accelerator = 0;
                String acceleratorText = null;
                final ActionEx updatedAction = getAction();
                String text = null;
                accelerator = updatedAction.getAccelerator();
                final ExternalActionManager.ICallback callback = ExternalActionManager.getInstance()
                        .getCallback();

                // Block accelerators that are already in use.
                if ((accelerator != 0) && (callback != null) && (callback.isAcceleratorInUse(accelerator))) {
                    accelerator = 0;
                }

                /*
                 * Process accelerators on GTK in a special way to avoid Bug 42009. We
                 * will override the native input method by allowing these reserved
                 * accelerators to be placed on the menu. We will only do this for
                 * "Ctrl+Shift+[0-9A-FU]".
                 */
                final String commandId = updatedAction.getActionDefinitionId();
                if ((Util.isGtk()) && (callback instanceof IBindingManagerCallback) && (commandId != null)) {
                    final IBindingManagerCallback bindingManagerCallback = (IBindingManagerCallback) callback;
                    final IKeyLookup lookup = KeyLookupFactory.getDefault();
                    final TriggerSequence[] triggerSequences = bindingManagerCallback
                            .getActiveBindingsFor(commandId);
                    for (final TriggerSequence triggerSequence : triggerSequences) {
                        final Trigger[] triggers = triggerSequence.getTriggers();
                        if (triggers.length == 1) {
                            final Trigger trigger = triggers[0];
                            if (trigger instanceof KeyStroke) {
                                final KeyStroke currentKeyStroke = (KeyStroke) trigger;
                                final int currentNaturalKey = currentKeyStroke.getNaturalKey();
                                if ((currentKeyStroke
                                        .getModifierKeys() == (lookup.getCtrl() | lookup.getShift()))
                                        && (((currentNaturalKey >= '0') && (currentNaturalKey <= '9'))
                                                || ((currentNaturalKey >= 'A') && (currentNaturalKey <= 'F'))
                                                || (currentNaturalKey == 'U'))) {
                                    accelerator = currentKeyStroke.getModifierKeys() | currentNaturalKey;
                                    acceleratorText = triggerSequence.format();
                                    break;
                                }
                            }
                        }
                    }
                }

                if (accelerator == 0) {
                    if ((callback != null) && (commandId != null)) {
                        acceleratorText = callback.getAcceleratorText(commandId);
                    }
                }

                IContributionManagerOverrides overrides = null;

                if (getParent() != null) {
                    overrides = getParent().getOverrides();
                }

                if (overrides != null) {
                    text = getParent().getOverrides().getText(this);
                }

                mi.setAccelerator(accelerator);

                if (text == null) {
                    text = updatedAction.getText();
                }

                if ((text != null) && (acceleratorText == null)) {
                    // use extracted accelerator text in case accelerator
                    // cannot be fully represented in one int (e.g.
                    // multi-stroke keys)
                    acceleratorText = LegacyActionTools.extractAcceleratorText(text);
                    if ((acceleratorText == null) && (accelerator != 0)) {
                        acceleratorText = Action.convertAccelerator(accelerator);
                    }
                }

                if (text == null) {
                    text = ""; //$NON-NLS-1$
                } else {
                    text = Action.removeAcceleratorText(text);
                }

                // add "..." if the action will show a dialog
                if (updatedAction.isShowDialog()) {
                    text = text + dialogIndicator;
                }

                if (acceleratorText == null) {
                    mi.setText(text);
                } else {
                    mi.setText(text + '\t' + acceleratorText);
                }
            }

            if (imageChanged) {
                updateImages(false);
            }

            if (enableStateChanged) {
                final boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

                if (mi.getEnabled() != shouldBeEnabled) {
                    mi.setEnabled(shouldBeEnabled);
                }
            }

            if (checkChanged) {
                final boolean bv = action.isChecked();

                if (mi.getSelection() != bv) {
                    mi.setSelection(bv);
                }
            }

            return;
        }

        if (widget instanceof Button) {
            final Button button = (Button) widget;

            if (imageChanged) {
                updateImages(false);
            }

            if (textChanged) {
                String text = action.getText();
                final boolean showText = (text != null)
                        && (((getMode() & MODE_FORCE_TEXT) != 0) || !hasImages(action));
                // only do the trimming if the text will be used
                if (showText) {
                    text = Action.removeAcceleratorText(text);
                }
                final String textToSet = showText ? text : ""; //$NON-NLS-1$
                button.setText(textToSet);
            }

            if (tooltipTextChanged) {
                button.setToolTipText(action.getToolTipText());
            }

            if (enableStateChanged) {
                final boolean shouldBeEnabled = action.isEnabled() && isEnabledAllowed();

                if (button.getEnabled() != shouldBeEnabled) {
                    button.setEnabled(shouldBeEnabled);
                }
            }

            if (checkChanged) {
                final boolean bv = action.isChecked();

                if (button.getSelection() != bv) {
                    button.setSelection(bv);
                }
            }
            return;
        }
    }
}

From source file:de.unistuttgart.ipvs.pmp.editor.ui.editors.internals.localetable.LocaleTable.java

License:Apache License

/**
 * Creates a new locale table. This table shows all available names,
 * descriptions or change descriptions and their locale.
 * // ww w.  j  a  va  2  s  . co m
 * @param parent
 *            Parent widget, to which this table should be added
 * @param data
 *            Data, from which the information will be loaded and written
 *            into
 * @param type
 *            Type of data, that should be shown.<br />
 *            <b>Warning:</b> If type is set to {@code CHANGE_DESCRIPTION}, {@code data} has to of type
 *            {@code RGISPrivacySetting}.
 *            Otherwise this table will throw {@code ClassCastException}s
 *            while user interaction!
 * @param tableAction
 *            Action, that will be initated when the data in this table was
 *            changed by the user
 * @param toolkit
 *            SWT-Toolkit
 */
public LocaleTable(Composite parent, IBasicIS data, Type type, ILocaleTableAction tableAction,
        FormToolkit toolkit) {
    this.data = data;
    this.type = type;
    this.tableAction = tableAction;

    // Create grid for storing the table and buttons
    this.outerCompo = toolkit.createComposite(parent);
    this.outerCompo.setLayout(new GridLayout(2, false));

    // Set layout so that the table uses the whole width
    GridData tableLayout = new GridData();
    tableLayout.horizontalAlignment = GridData.FILL;
    tableLayout.verticalAlignment = GridData.FILL;
    tableLayout.grabExcessHorizontalSpace = true;
    tableLayout.grabExcessVerticalSpace = true;

    // Workaround for SWT-Bug
    // (https://bugs.eclipse.org/bugs/show_bug.cgi?id=215997)
    tableLayout.widthHint = 1;

    Composite tableCompo = toolkit.createComposite(this.outerCompo);
    tableCompo.setLayoutData(tableLayout);
    TableColumnLayout columnLayout = new TableColumnLayout();
    tableCompo.setLayout(columnLayout);

    this.tableViewer = new TableViewer(tableCompo, SWT.BORDER | SWT.FULL_SELECTION);
    Table table = this.tableViewer.getTable();
    table.setLayoutData(tableLayout);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    createColumns(columnLayout);

    // Add decoration
    this.tableDec = new ControlDecoration(this.tableViewer.getControl(), SWT.TOP | SWT.LEFT);
    this.tableDec.setImage(Images.IMG_DEC_FIELD_ERROR);
    if (data != null) {
        validate();
    }

    // Add keyboard navigation
    ColumnViewerEditorActivationStrategy activationStrategy = new ColumnViewerEditorActivationStrategy(
            this.tableViewer) {

        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            // Editing is enabled as before but also by pressing enter
            boolean activateByKey = event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                    && event.keyCode == KeyLookupFactory.getDefault().formalKeyLookup(IKeyLookup.ENTER_NAME);
            return super.isEditorActivationEvent(event) || activateByKey;
        }

    };

    activationStrategy.setEnableEditorActivationWithKeyboard(true);

    FocusCellHighlighter highlighter = new FocusCellOwnerDrawHighlighter(this.tableViewer);
    TableViewerFocusCellManager focusManager = new TableViewerFocusCellManager(this.tableViewer, highlighter);

    TableViewerEditor.create(this.tableViewer, focusManager, activationStrategy,
            ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                    | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    // Add table sorter
    this.tableViewer.setSorter(new ViewerSorter());

    // But data into table if set
    this.tableViewer.setContentProvider(new ListContentProvider());
    setData(data);

    // Add tooltip listener
    TooltipTableListener tooltipListener = new TooltipTableListener(this.tableViewer, parent.getShell());
    this.tableViewer.getTable().addListener(SWT.Dispose, tooltipListener);
    this.tableViewer.getTable().addListener(SWT.KeyDown, tooltipListener);
    this.tableViewer.getTable().addListener(SWT.MouseMove, tooltipListener);
    this.tableViewer.getTable().addListener(SWT.MouseHover, tooltipListener);

    // Add buttons
    createButtons(toolkit);

}

From source file:de.xirp.util.XirpKeyFormatter.java

License:Open Source License

/**
 * Formats an individual key into a human readable format. It uses
 * the Application I18n bundle./*from w ww.  java  2  s. co  m*/
 * 
 * @see org.eclipse.jface.bindings.keys.formatting.AbstractKeyFormatter#format(org.eclipse.jface.bindings.keys.KeySequence)
 */
@Override
public String format(final int key) {
    final IKeyLookup lookup = KeyLookupFactory.getDefault();
    final String name = lookup.formalNameLookup(key);

    // NL issue. Possible problem with 4-byte characters.
    if (name.length() == 1) {
        return name;
    }

    return I18n.getString(name);
}

From source file:de.xirp.util.XirpKeyFormatter.java

License:Open Source License

/**
 * Separates the modifier keys from each other, and then places
 * them in an array in some sorted order. The sort order is
 * dependent on the type of formatter./*from ww  w.  j ava  2 s.co m*/
 * 
 * @see org.eclipse.jface.bindings.keys.formatting.NativeKeyFormatter
 */
@Override
protected int[] sortModifierKeys(final int modifierKeys) {
    final IKeyLookup lookup = KeyLookupFactory.getDefault();
    final String platform = SWT.getPlatform();
    final int[] sortedKeys = new int[4];
    int index = 0;

    if ("win32".equals(platform)) { //$NON-NLS-1$
        if ((modifierKeys & lookup.getCtrl()) != 0) {
            sortedKeys[index++] = lookup.getCtrl();
        }
        if ((modifierKeys & lookup.getAlt()) != 0) {
            sortedKeys[index++] = lookup.getAlt();
        }
        if ((modifierKeys & lookup.getShift()) != 0) {
            sortedKeys[index++] = lookup.getShift();
        }

    } else if ("gtk".equals(platform) || "motif".equals(platform)) { //$NON-NLS-1$ //$NON-NLS-2$
        if ((modifierKeys & lookup.getShift()) != 0) {
            sortedKeys[index++] = lookup.getShift();
        }
        if ((modifierKeys & lookup.getCtrl()) != 0) {
            sortedKeys[index++] = lookup.getCtrl();
        }
        if ((modifierKeys & lookup.getAlt()) != 0) {
            sortedKeys[index++] = lookup.getAlt();
        }

    } else if ("carbon".equals(platform)) { //$NON-NLS-1$
        if ((modifierKeys & lookup.getShift()) != 0) {
            sortedKeys[index++] = lookup.getShift();
        }
        if ((modifierKeys & lookup.getCtrl()) != 0) {
            sortedKeys[index++] = lookup.getCtrl();
        }
        if ((modifierKeys & lookup.getAlt()) != 0) {
            sortedKeys[index++] = lookup.getAlt();
        }
        if ((modifierKeys & lookup.getCommand()) != 0) {
            sortedKeys[index++] = lookup.getCommand();
        }

    }

    return sortedKeys;
}

From source file:org.dawnsci.common.widgets.gda.function.FunctionTreeViewer.java

License:Open Source License

public FunctionTreeViewer(final Composite parent, IFunctionDescriptorProvider functionDescriptorProvider) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FillLayout());
    treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);

    treeViewer.getTree().setLinesVisible(true);
    treeViewer.getTree().setHeaderVisible(true);
    TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(treeViewer,
            new FocusCellOwnerDrawHighlighter(treeViewer));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(treeViewer) {
        @Override/*from   www  .j  a v  a 2s. c  om*/
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            // TODO see AbstractComboBoxCellEditor for how list is made visible
            return super.isEditorActivationEvent(event)
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && (event.keyCode == KeyLookupFactory.getDefault()
                                    .formalKeyLookup(IKeyLookup.ENTER_NAME)));
        }
    };

    TreeViewerEditor.create(treeViewer, focusCellManager, actSupport,
            ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                    | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    createFunctionNameColumn();
    createParameterValueColumn("Value", COLUMN.VALUE, false, new IGetSetValueOnParameterModel() {
        @Override
        public void setValue(ParameterModel param, String value) {
            param.setParameterValue(value);
        }

        @Override
        public double getValue(ParameterModel param) {
            return param.getParameterValue();
        }

        @Override
        public String getErrorValue(ParameterModel param) {
            return param.getParameterValueError();
        }
    });
    createParameterValueColumn("Lower Limit", COLUMN.LOWERLIMIT, true, new IGetSetValueOnParameterModel() {
        @Override
        public void setValue(ParameterModel param, String value) {
            param.setParameterLowerLimit(value);
        }

        @Override
        public double getValue(ParameterModel param) {
            return param.getParameterLowerLimit();
        }

        @Override
        public String getErrorValue(ParameterModel param) {
            return param.getParameterLowerLimitError();
        }
    });
    createParameterValueColumn("Upper Limit", COLUMN.UPPERLIMIT, true, new IGetSetValueOnParameterModel() {
        @Override
        public void setValue(ParameterModel param, String value) {
            param.setParameterUpperLimit(value);
        }

        @Override
        public double getValue(ParameterModel param) {
            return param.getParameterUpperLimit();
        }

        @Override
        public String getErrorValue(ParameterModel param) {
            return param.getParameterUpperLimitError();
        }
    });

    createFittedParamsColumn();

    treeViewer.setContentProvider(new FunctionTreeViewerContentProvider());
    treeViewer.expandToLevel(2);
    // IFunctions are not safely hashable for this use (despite their having
    // a custom hashCode)
    treeViewer.setUseHashlookup(false);
    setInput(null);
    this.functionDescriptorProvider = functionDescriptorProvider;
    functionSelectionEditingSupport.setFunctionDesciptorProvider(functionDescriptorProvider);

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            if (event.getSelection() instanceof IStructuredSelection) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                ISelection realSelection = convertSelection(selection);
                fireSelectionChanged(new SelectionChangedEvent(FunctionTreeViewer.this, realSelection));
            }

        }
    });
}

From source file:org.dawnsci.common.widgets.table.SeriesTable.java

License:Open Source License

/**
 * Create the control for the table. The icon provider is checked for label and
 * icon for the first, name column in the table. It must provide at least an
 * icon for a given SeriesItem implementation. If it does not provide a label 
 * then the 'getName()' method of SeriesItem is used.
 * //from w w w  .j a va 2  s . com
 * @param parent       - the SWT composite to add the table to.
 * @param iconProvider - a provider which must at least give the icon for a given SeriesItem
 */
public void createControl(Composite parent, SeriesItemLabelProvider iconProvider) {

    tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.SINGLE) {
        protected void inputChanged(Object input, Object oldInput) {
            super.inputChanged(input, oldInput);
            checkValid((List<ISeriesItemDescriptor>) input);
        }
    };

    tableViewer.getTable().setLinesVisible(true);
    tableViewer.getTable().setHeaderVisible(true);
    tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));

    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tableViewer,
            new FocusCellOwnerDrawHighlighter(tableViewer));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(tableViewer) {
        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            // TODO see AbstractComboBoxCellEditor for how list is made visible
            return super.isEditorActivationEvent(event)
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && (event.keyCode == KeyLookupFactory.getDefault()
                                    .formalKeyLookup(IKeyLookup.ENTER_NAME)));
        }
    };

    TableViewerEditor.create(tableViewer, focusCellManager, actSupport,
            ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                    | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    tableViewer.setContentProvider(new SeriesContentProvider());

    this.error = new Composite(parent, SWT.NONE);
    error.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    error.setLayout(new GridLayout(2, false));
    error.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));

    Label errorIcon = new Label(error, SWT.NONE);
    errorIcon.setImage(Activator.getImageDescriptor("icons/error.png").createImage());
    errorIcon.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
    errorIcon.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

    this.errorLabel = new Label(error, SWT.WRAP);
    errorLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
    errorLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    GridUtils.setVisible(error, false);

    createColumns(iconProvider);

    //hook up delete key to remove from list
    tableViewer.getTable().addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == SWT.DEL) {
                delete();
            }
        }
    });

}

From source file:org.eclipse.birt.chart.ui.swt.fieldassist.preferences.CustomKeyRadioGroupFieldEditor.java

License:Open Source License

/**
 * Returns this field editor's radio group control.
 * @param parent The parent to create the radioBox in
 * @return the radio group control//ww w .j a  va 2 s .c om
 */
public Composite getRadioBoxControl(Composite parent) {
    if (radioBox == null) {

        Font font = parent.getFont();

        if (useGroup) {
            Group group = new Group(parent, SWT.NONE);
            group.setFont(font);
            String text = getLabelText();
            if (text != null) {
                group.setText(text);
            }
            radioBox = group;
            GridLayout layout = new GridLayout();
            layout.horizontalSpacing = HORIZONTAL_GAP;
            layout.numColumns = customKeyName == null ? 1 : 2;
            radioBox.setLayout(layout);
        } else {
            radioBox = new Composite(parent, SWT.NONE);
            GridLayout layout = new GridLayout();
            layout.marginWidth = 0;
            layout.marginHeight = 0;
            layout.horizontalSpacing = HORIZONTAL_GAP;
            layout.numColumns = customKeyName == null ? 1 : 2;
            ;
            radioBox.setLayout(layout);
            radioBox.setFont(font);
        }

        radioBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        radioButtons = new Button[labelsAndValues.length];
        for (int i = 0; i < labelsAndValues.length; i++) {
            Button radio = new Button(radioBox, SWT.RADIO | SWT.LEFT);
            if (hasCustomKeyName() && i != (labelsAndValues.length - 1)) {
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
                gd.horizontalSpan = 2;
                radio.setLayoutData(gd);
            }

            radioButtons[i] = radio;
            String[] labelAndValue = labelsAndValues[i];
            radio.setText(labelAndValue[0]);
            radio.setData(labelAndValue[1]);
            radio.setFont(font);
            radio.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent event) {
                    String oldValue = value;
                    value = (String) event.widget.getData();
                    setPresentsDefaultValue(false);
                    fireValueChanged(VALUE, oldValue, value);
                }
            });
        }

        if (hasCustomKeyName()) {
            customKeyText = new Text(radioBox, SWT.BORDER);
            GridData gd = new GridData(GridData.FILL_HORIZONTAL);
            customKeyText.setLayoutData(gd);
            customKeyText.addKeyListener(new KeyListener() {

                public void keyPressed(KeyEvent e) {

                }

                public void keyReleased(KeyEvent e) {
                    if (e.keyCode == SWT.CTRL || e.keyCode == SWT.ALT || e.keyCode == SWT.SHIFT) {
                        return;
                    }
                    final IKeyLookup lookup = KeyLookupFactory.getDefault();
                    String key = lookup.formalNameLookup(e.keyCode);

                    if (key == null) {
                        return;
                    }

                    StringBuffer txt = new StringBuffer();
                    if ((e.stateMask & SWT.CTRL) != 0) {
                        txt.append("Ctrl"); //$NON-NLS-1$
                    }
                    if ((e.stateMask & SWT.ALT) != 0) {
                        if (txt.length() > 0)
                            txt.append("+"); //$NON-NLS-1$
                        txt.append("Alt"); //$NON-NLS-1$
                    } else if ((e.stateMask & SWT.SHIFT) != 0) {
                        if (txt.length() > 0)
                            txt.append("+"); //$NON-NLS-1$
                        txt.append("Shift"); //$NON-NLS-1$
                    }
                    if (txt.length() > 0) {
                        txt.append("+"); //$NON-NLS-1$
                        txt.append(key);
                        customKeyText.setText(txt.toString());
                    }
                }
            });
        }

        radioButtons[labelsAndValues.length - 1].addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(SelectionEvent e) {
                // TODO Auto-generated method stub
            }

            public void widgetSelected(SelectionEvent e) {
                customKeyText.setEnabled(radioButtons[labelsAndValues.length - 1].getSelection());
            }
        });
        radioBox.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent event) {
                radioBox = null;
                radioButtons = null;
            }
        });
    } else {
        checkParent(radioBox, parent);
    }
    return radioBox;
}

From source file:org.eclipse.debug.internal.ui.actions.expressions.WatchExpressionDialog.java

License:Open Source License

/**
 * Returns a string representation of the "Ctrl+Return" key sequence.
 * //w  w  w  .  ja va 2 s .c om
 * @return a string representation of the "Ctrl+Return" key sequence.
 */
private String getCtrlReturnText() {
    IKeyLookup keyLookup = KeyLookupFactory.getDefault();
    int ctrlKey = keyLookup.getCtrl();
    int returnKey = keyLookup.formalKeyLookup(IKeyLookup.RETURN_NAME);
    KeyStroke ctrlReturnKeyStroke = KeyStroke.getInstance(ctrlKey, returnKey);
    KeySequence ctrltReturnKeySequence = KeySequence.getInstance(ctrlReturnKeyStroke);
    return SWTKeySupport.getKeyFormatterForPlatform().format(ctrltReturnKeySequence);
}

From source file:org.eclipse.jface.snippets.viewers.Snippet060TextCellEditorWithContentProposal.java

License:Open Source License

public Snippet060TextCellEditorWithContentProposal(Shell shell) {
    final TableViewer viewer = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
    final Table table = viewer.getTable();
    table.setLinesVisible(true);/*from w w  w .j  a v  a  2 s.c  o m*/
    table.setHeaderVisible(true);

    final TableViewerColumn colorColumn = new TableViewerColumn(viewer, SWT.LEFT);
    colorColumn.getColumn().setText("Color name");
    colorColumn.getColumn().setWidth(200);
    colorColumn.setLabelProvider(new ColumnLabelProvider());
    colorColumn.setEditingSupport(new ColorNameEditingSupport(viewer));

    viewer.setContentProvider(new ArrayContentProvider());

    ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(viewer) {
        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && event.keyCode == KeyLookupFactory.getDefault()
                                    .formalKeyLookup(IKeyLookup.ENTER_NAME));
        }
    };
    activationSupport.setEnableEditorActivationWithKeyboard(true);

    /*
     * Without focus highlighter, keyboard events will not be delivered to
     * ColumnViewerEditorActivationStragety#isEditorActivationEvent(...) (see above)
     */
    FocusCellHighlighter focusCellHighlighter = new FocusCellOwnerDrawHighlighter(viewer);
    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer,
            focusCellHighlighter);

    TableViewerEditor.create(viewer, focusCellManager, activationSupport,
            ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    viewer.setInput(createModel());
}