Example usage for org.eclipse.jface.fieldassist IContentProposalProvider IContentProposalProvider

List of usage examples for org.eclipse.jface.fieldassist IContentProposalProvider IContentProposalProvider

Introduction

In this page you can find the example usage for org.eclipse.jface.fieldassist IContentProposalProvider IContentProposalProvider.

Prototype

IContentProposalProvider

Source Link

Usage

From source file:com.codenvy.eclipse.ui.widgets.ComboAutoCompleteField.java

License:Open Source License

private IContentProposalProvider getProposalProvider() {
    return new IContentProposalProvider() {
        @Override// w  w  w .jav  a  2  s  . c  o  m
        public IContentProposal[] getProposals(String contents, int position) {
            String[] items = getStringItems();
            if (contents.length() == 0 || items.length == 0)
                return new IContentProposal[0];
            Pattern matcher = Pattern.compile(Pattern.quote(contents) + ".*");
            ArrayList<String> matches = new ArrayList<String>();
            for (int i = 0; i < items.length; i++) {
                if (matcher.matcher(items[i]).find()) {
                    matches.add(items[i]);
                }
            }

            // We don't want to autoactivate if the only proposal exactly matches
            // what is in the combo. This prevents the popup from
            // opening when the user is merely scrolling through the combo values or
            // has accepted a combo value.
            if (matches.size() == 1 && matches.get(0).equals(combo.getText()))
                return new IContentProposal[0];

            if (matches.isEmpty())
                return new IContentProposal[0];

            // Make the proposals
            IContentProposal[] proposals = new IContentProposal[matches.size()];
            for (int i = 0; i < matches.size(); i++) {
                final String proposal = matches.get(i);
                proposals[i] = new IContentProposal() {

                    @Override
                    public String getContent() {
                        return proposal;
                    }

                    @Override
                    public int getCursorPosition() {
                        return proposal.length();
                    }

                    @Override
                    public String getDescription() {
                        return null;
                    }

                    @Override
                    public String getLabel() {
                        return null;
                    }
                };
            }
            return proposals;
        }
    };
}

From source file:com.nokia.tools.s60.views.SearchViewPage.java

License:Open Source License

private IContentProposalProvider getContentProposalProvider() {
    return new IContentProposalProvider() {
        public IContentProposal[] getProposals(String contents, int position) {
            IContentProposal[] proposals = new IContentProposal[suggestionsAndHistory.size()];
            for (int i = 0; i < suggestionsAndHistory.size(); i++) {
                final String currentItem = suggestionsAndHistory.get(i);
                proposals[i] = new IContentProposal() {
                    public String getContent() {
                        return currentItem;
                    }//from  w w  w  .ja  va  2  s  . c  om

                    public String getLabel() {
                        return currentItem;
                    }

                    public String getDescription() {

                        return MessageFormat.format(
                                "The table will be filtered using following string: \"{0}\". The filter is applied to all columns",
                                new Object[] { currentItem });

                    }

                    public int getCursorPosition() {
                        return currentItem.length();
                    }
                };
            }
            return proposals;
        }
    };
}

From source file:com.nokia.tools.theme.ui.dialogs.ThemeResourceSelectionPage.java

License:Open Source License

private IContentProposalProvider getContentProposalProvider() {
    return new IContentProposalProvider() {
        public IContentProposal[] getProposals(String contents, int position) {
            IContentProposal[] proposals = new IContentProposal[suggestionsAndHistory.size()];
            for (int i = 0; i < suggestionsAndHistory.size(); i++) {
                final String currentItem = suggestionsAndHistory.get(i);
                proposals[i] = new IContentProposal() {
                    public String getContent() {
                        return currentItem;
                    }//from  ww w  .  jav a  2 s.c o  m

                    public String getLabel() {
                        return currentItem;
                    }

                    public String getDescription() {

                        return MessageFormat.format(WizardMessages.ResourceSelectionDialog_Pattern_Info_Text,
                                new Object[] { currentItem });

                    }

                    public int getCursorPosition() {
                        return currentItem.length();
                    }
                };
            }
            return proposals;
        }
    };
}

From source file:com.rcpcompany.uibindings.internal.handlers.QuickFixHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    // LogUtils.debug(this, "hello");
    if (Activator.getDefault().TRACE_HANDLERS) {
        LogUtils.debug(this, "");
    }//from  w  ww  . j a  va  2  s  .c  o  m
    final IValueBinding binding = (IValueBinding) HandlerUtil.getVariable(event,
            Constants.SOURCES_ACTIVE_BINDING);
    if (binding == null) {
        Display.getDefault().beep();
        throw new ExecutionException("Quickfixes not supported for control");
    }

    final ValueBindingMessageImageDecorator decorator = binding
            .getService(ValueBindingMessageImageDecorator.class);
    if (decorator == null) {
        Display.getDefault().beep();
        throw new ExecutionException("Quickfixes not supported for control");
    }

    final List<IQuickfixProposal> quickfixes = decorator.getQuickfixes();

    if (quickfixes == null || quickfixes.size() == 0)
        return null;

    if (quickfixes.size() == 1 && IManager.Factory.getManager().isAutoApplySingleQuickfix()) {
        quickfixes.get(0).apply();
        return null;
    }
    final IQuickfixProposal[] quickfixArray = quickfixes.toArray(new IQuickfixProposal[quickfixes.size()]);

    final IContentProposalProvider proposalProvider = new IContentProposalProvider() {
        @Override
        public IContentProposal[] getProposals(String contents, int position) {
            return quickfixArray;
        }
    };
    final MyContentProposalAdapter proposalAdapter = new MyContentProposalAdapter(binding.getControl(),
            new MyControlContentAdapter(), proposalProvider);
    proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_IGNORE);
    proposalAdapter.addContentProposalListener(new IContentProposalListener() {
        @Override
        public void proposalAccepted(IContentProposal proposal) {
            Assert.isNotNull(proposal);
            final IQuickfixProposal quickfix = (IQuickfixProposal) proposal;
            quickfix.apply();
        }
    });
    proposalAdapter.setLabelProvider(new MyQuickfixLabelProvider());
    proposalAdapter.open();

    return null;
}

From source file:de.quamoco.qm.properties.QIESLSpecificationPropertySectionBase.java

License:Apache License

/** Creates the {@link Text}. */
@Override/*from   ww w .j a v a  2  s  .c  om*/
protected void createValue(Composite composite) {

    Composite subComposite = getWidgetFactory().createComposite(composite);
    GridLayout layout = new GridLayout(1, false);
    layout.marginBottom = layout.marginLeft = layout.marginRight = layout.marginTop = 0;
    layout.marginWidth = layout.marginHeight = 0;
    subComposite.setLayout(layout);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalIndent = ITabbedPropertyConstants.HSPACE;
    data.grabExcessHorizontalSpace = true;
    subComposite.setLayoutData(data);

    super.createValue(subComposite);
    ((GridData) text.getLayoutData()).horizontalIndent = 0;
    try {
        new ContentProposalAdapter(text, new TextContentAdapter(), new IContentProposalProvider() {

            @Override
            public IContentProposal[] getProposals(String contents, int position) {
                List<IContentProposal> proposals = new ArrayList<IContentProposal>();
                for (CharacterizingElement element : getNormalizationMeasures()) {
                    proposals.add(new SpecificationContentProposal(element));
                }
                for (CharacterizingElement element : getMeasuresAndFactors()) {
                    proposals.add(new SpecificationContentProposal(element));
                }
                return proposals.toArray(new IContentProposal[0]);
            }
        }, KeyStroke.getInstance("M1+Space"), null);
    } catch (ParseException e) {
        LoggingUtils.error(Activator.getDefault(), "Key combination could not be parsed", e);
    }

    createButtons(subComposite);
    createTables(subComposite);
}

From source file:net.enilink.komma.edit.ui.util.SearchWidget.java

License:Open Source License

/**
 * Creates the search text and adds listeners. This method calls
 * {@link #doCreateSearchText(Composite)} to create the text control.
 * Subclasses should override {@link #doCreateSearchText(Composite)} instead
 * of overriding this method.// w  w w. j  a v  a2 s. com
 * 
 * @param parent
 *            <code>Composite</code> of the search text
 */
protected void createSearchText(Composite parent) {
    searchText = doCreateSearchText(parent);
    searchText.addFocusListener(new FocusAdapter() {
        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt
         * .events.FocusEvent)
         */
        public void focusGained(FocusEvent e) {
            /*
             * Running in an asyncExec because the selectAll() does not
             * appear to work when using mouse to give focus to text.
             */
            Display display = searchText.getDisplay();
            display.asyncExec(new Runnable() {
                public void run() {
                    if (!searchText.isDisposed()) {
                        if (getInitialText().equals(searchText.getText().trim())) {
                            searchText.selectAll();
                        }
                    }
                }
            });
        }
    });

    searchText.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            doSearch(searchText.getText());
        }
    });
    searchText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    // content proposals for searching
    class ObjectProposal extends ContentProposal {
        Object object;

        ObjectProposal(Object object) {
            super("");
            this.object = object;
        }
    }

    ContentProposalAdapter proposalAdapter = new ContentProposalAdapter(searchText, new TextContentAdapter(),
            new IContentProposalProvider() {
                @Override
                public IContentProposal[] getProposals(String contents, int position) {
                    Collection<Object> results = findElements(contents);
                    List<IContentProposal> proposals = new ArrayList<IContentProposal>();
                    for (Object result : results) {
                        proposals.add(new ObjectProposal(result));
                    }
                    return proposals.toArray(new IContentProposal[proposals.size()]);
                }
            }, null, null);
    proposalAdapter.setAutoActivationDelay(750);
    proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_IGNORE);
    proposalAdapter.addContentProposalListener(new IContentProposalListener() {
        @Override
        public void proposalAccepted(IContentProposal proposal) {
            viewer.setSelection(new StructuredSelection(((ObjectProposal) proposal).object), true);
        }
    });

    proposalAdapter.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            ILabelProvider labelProvider = getLabelProvider();
            return labelProvider != null ? labelProvider.getText(((ObjectProposal) element).object)
                    : super.getText(element);
        }

        @Override
        public Image getImage(Object element) {
            ILabelProvider labelProvider = getLabelProvider();
            return labelProvider != null ? labelProvider.getImage(((ObjectProposal) element).object)
                    : super.getImage(element);
        }
    });
}

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

License:Open Source License

private IContentProposalProvider getContentProposalProvider(final String[] values) {
    return new IContentProposalProvider() {

        public IContentProposal[] getProposals(String contents, int position) {
            IContentProposal[] proposals = new IContentProposal[values.length];
            for (int i = 0; i < values.length; i++) {
                final String user = values[i];
                proposals[i] = new IContentProposal() {

                    public String getContent() {
                        return user;
                    }//w w w  .j av  a 2 s.c o  m

                    public String getLabel() {
                        return user;
                    }

                    public String getDescription() {
                        return null;
                    }

                    public int getCursorPosition() {
                        return user.length();
                    }
                };
            }
            return proposals;
        }
    };
}

From source file:org.eclipse.egit.ui.internal.fetch.FetchGerritChangePage.java

License:Open Source License

private void addRefContentProposalToText(final Text textField) {
    KeyStroke stroke;/*www  . j  a  va  2  s. c o  m*/
    try {
        stroke = KeyStroke.getInstance("CTRL+SPACE"); //$NON-NLS-1$
        UIUtils.addBulbDecorator(textField,
                NLS.bind(UIText.FetchGerritChangePage_ContentAssistTooltip, stroke.format()));
    } catch (ParseException e1) {
        Activator.handleError(e1.getMessage(), e1, false);
        stroke = null;
    }

    IContentProposalProvider cp = new IContentProposalProvider() {
        public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ')
                patternString = patternString.substring(1);

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); //$NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) //$NON-NLS-1$
                patternString = patternString + ".*"; //$NON-NLS-1$

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
                pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
                pattern = null;
            }

            List<Change> proposals;
            try {
                proposals = getRefsForContentAssist();
            } catch (InvocationTargetException e) {
                Activator.handleError(e.getMessage(), e, false);
                return null;
            } catch (InterruptedException e) {
                return null;
            }

            if (proposals != null)
                for (final Change ref : proposals) {
                    if (pattern != null && !pattern.matcher(ref.getChangeNumber().toString()).matches())
                        continue;
                    IContentProposal propsal = new ChangeContentProposal(ref);
                    resultList.add(propsal);
                }

            return resultList.toArray(new IContentProposal[resultList.size()]);
        }
    };

    ContentProposalAdapter adapter = new ContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke,
            null);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}

From source file:org.eclipse.egit.ui.internal.gerrit.GerritConfigurationPage.java

License:Open Source License

private void addRefContentProposalToText(final Text textField) {
    KeyStroke stroke;//from w ww.  ja  v  a2  s . c o  m
    try {
        stroke = KeyStroke.getInstance("CTRL+SPACE"); //$NON-NLS-1$
        UIUtils.addBulbDecorator(textField,
                NLS.bind(UIText.GerritConfigurationPage_BranchTooltipHover, stroke.format()));
    } catch (ParseException e1) {
        Activator.handleError(e1.getMessage(), e1, false);
        stroke = null;
    }

    IContentProposalProvider cp = new IContentProposalProvider() {
        public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ') {
                patternString = patternString.substring(1);
            }

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); //$NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) //$NON-NLS-1$
                patternString = patternString + ".*"; //$NON-NLS-1$

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
                pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
                pattern = null;
            }

            Set<String> proposals = new TreeSet<String>();

            try {
                // propose the names of the remote tracking
                // branches for the given remote
                Set<String> remotes = repository.getRefDatabase()
                        .getRefs(Constants.R_REMOTES + remoteName + "/").keySet(); //$NON-NLS-1$
                proposals.addAll(remotes);
            } catch (IOException e) {
                // simply ignore, no proposals then
            }

            for (final String proposal : proposals) {
                if (pattern != null && !pattern.matcher(proposal).matches())
                    continue;
                IContentProposal propsal = new BranchContentProposal(proposal);
                resultList.add(propsal);
            }

            return resultList.toArray(new IContentProposal[resultList.size()]);
        }
    };

    ContentProposalAdapter adapter = new ContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke,
            null);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}

From source file:org.eclipse.egit.ui.internal.push.PushToGerritPage.java

License:Open Source License

private void addRefContentProposalToText(final Text textField) {
    KeyStroke stroke;/* www.ja  v a2  s .  c o  m*/
    try {
        stroke = KeyStroke.getInstance("CTRL+SPACE"); //$NON-NLS-1$
        UIUtils.addBulbDecorator(textField,
                NLS.bind(UIText.PushToGerritPage_ContentProposalHoverText, stroke.format()));
    } catch (ParseException e1) {
        Activator.handleError(e1.getMessage(), e1, false);
        stroke = null;
    }

    IContentProposalProvider cp = new IContentProposalProvider() {
        public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ') {
                patternString = patternString.substring(1);
            }

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); //$NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) //$NON-NLS-1$
                patternString = patternString + ".*"; //$NON-NLS-1$

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
                pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
                pattern = null;
            }

            Set<String> proposals = new TreeSet<String>();

            try {
                Set<String> remotes = repository.getRefDatabase().getRefs(Constants.R_REMOTES).keySet();
                for (String remote : remotes) {
                    // these are "origin/master", "origin/xxx"...
                    int slashIndex = remote.indexOf('/');
                    if (slashIndex > 0 && slashIndex < remote.length() - 1)
                        proposals.add(remote.substring(remote.indexOf('/') + 1));
                }
            } catch (IOException e) {
                // simply ignore, no proposals then
            }

            for (final String proposal : proposals) {
                if (pattern != null && !pattern.matcher(proposal).matches())
                    continue;
                IContentProposal propsal = new BranchContentProposal(proposal);
                resultList.add(propsal);
            }

            return resultList.toArray(new IContentProposal[resultList.size()]);
        }
    };

    ContentProposalAdapter adapter = new ContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke,
            null);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}