Search in sources :

Example 1 with ComboContentAdapter

use of org.eclipse.jface.fieldassist.ComboContentAdapter in project sling by apache.

the class NewNodeDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    Control[] children = composite.getChildren();
    Control errorMessageText = children[children.length - 1];
    GridData errorMessageGridData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
    errorMessageGridData.heightHint = convertHeightInCharsToPixels(2);
    errorMessageText.setLayoutData(errorMessageGridData);
    // now add the node type dropbox-combo
    Label label = new Label(composite, SWT.WRAP);
    label.moveAbove(errorMessageText);
    label.setText("Define node type");
    GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
    data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
    label.setLayoutData(data);
    label.setFont(parent.getFont());
    combo = new Combo(composite, SWT.DROP_DOWN);
    combo.moveAbove(errorMessageText);
    if (allowedChildren != null) {
        combo.setItems(allowedChildren.toArray(new String[0]));
    }
    combo.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
    combo.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            comboSelection = combo.getText();
            validateInput();
        }
    });
    combo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            comboSelection = combo.getText();
            validateInput();
        }
    });
    SimpleContentProposalProvider proposalProvider = new SimpleContentProposalProvider(combo.getItems());
    proposalProvider.setFiltering(true);
    final ComboContentAdapter controlContentAdapter = new ComboContentAdapter() {

        @Override
        public void insertControlContents(Control control, String text, int cursorPosition) {
            Point selection = combo.getSelection();
            combo.setText(text);
            selection.x = selection.x + cursorPosition;
            selection.y = selection.x;
            combo.setSelection(selection);
        }

        @Override
        public Rectangle getInsertionBounds(Control control) {
            final Rectangle insertionBounds = super.getInsertionBounds(control);
            // always insert at start
            insertionBounds.x = 0;
            insertionBounds.y = 0;
            return insertionBounds;
        }
    };
    // this variant opens auto-complete on each character
    proposalAdapter = new ContentProposalAdapter(combo, controlContentAdapter, proposalProvider, null, null);
    // this variant opens auto-complete only when invoking the auto-complete hotkey
    if (allowedChildren != null && allowedChildren.size() == 1) {
        combo.setText(allowedChildren.iterator().next());
    } else if (allowedChildren != null) {
        if (allowedChildren.contains(lastChosenNodeType)) {
            combo.setText(lastChosenNodeType);
        }
    }
    return composite;
}
Also used : ComboContentAdapter(org.eclipse.jface.fieldassist.ComboContentAdapter) Composite(org.eclipse.swt.widgets.Composite) ModifyListener(org.eclipse.swt.events.ModifyListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) Rectangle(org.eclipse.swt.graphics.Rectangle) Combo(org.eclipse.swt.widgets.Combo) Point(org.eclipse.swt.graphics.Point) ContentProposalAdapter(org.eclipse.jface.fieldassist.ContentProposalAdapter) SimpleContentProposalProvider(org.eclipse.jface.fieldassist.SimpleContentProposalProvider) Control(org.eclipse.swt.widgets.Control) ModifyEvent(org.eclipse.swt.events.ModifyEvent) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Aggregations

ComboContentAdapter (org.eclipse.jface.fieldassist.ComboContentAdapter)1 ContentProposalAdapter (org.eclipse.jface.fieldassist.ContentProposalAdapter)1 SimpleContentProposalProvider (org.eclipse.jface.fieldassist.SimpleContentProposalProvider)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 GridData (org.eclipse.swt.layout.GridData)1 Combo (org.eclipse.swt.widgets.Combo)1 Composite (org.eclipse.swt.widgets.Composite)1 Control (org.eclipse.swt.widgets.Control)1 Label (org.eclipse.swt.widgets.Label)1