Search code examples
comboboxautocompletercp

AutoComplete in combo box rcp


I want to provide the auto complete functionality in the combobox.

final String [] ComboDataArr = ComboData.toArray(new String[ComboData.size()]);
        editor = new TableEditor(Table);
        Combo combo = new Combo(Table, SWT.DROP_DOWN);
        combo.setText("Choose the signal");
        Arrays.sort(ComboDataArr);
        for(int j= 0;j < ComboDataArr.length;j++){
            combo.add(ComboDataArr[j]);
        }
        ComboContentAdapter comboAdapter = new ComboContentAdapter(); 
        IContentProposalProvider proposalProvider = new IContentProposalProvider() {

            @Override
            public IContentProposal[] getProposals(String contents, int position) {
                List<IContentProposal> validProposals = new ArrayList<IContentProposal>();
                for (String comboAutoProposals : ComboDataArr) {
                    contents = contents.substring(0, position);
                    for (byte b : contents.getBytes()) {
                        char c = (char) (b & 0xFF);
                        if (comboAutoProposals.indexOf(c) != -1) { // This is where it checks if the proposal contains the chars.
                            validProposals.add(new ContentProposal(comboAutoProposals));
                            break;
                        }
                    }
                }
                return validProposals.toArray(new IContentProposal[validProposals.size()]);
            }
        };
        ContentProposalAdapter adapter = new ContentProposalAdapter(combo, comboAdapter, proposalProvider, null, null);
        adapter.setPropagateKeys(true);
        adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
        editor.grabHorizontal = true;
        editor.setEditor(combo, Items[i], 1);

But its showing all the possibilities which contains the character I type. If i type BH its shows all the entries which contains B,BH and H. I want it to show only the entries which start with the character i type like if i type BH it should show entries starting with BH.


Solution

  • For autocomplete functionality for a ComboBox in rcp

    private static final String LCL = "abcdefghijklmnopqrstuvwxyz";
    private static final String UCL = LCL.toUpperCase();
    private static final String NUMS = "0123456789";
    
     static  void enableContentProposal(Control control)
    {
    
        SimpleContentProposalProvider proposalProvider = null;
        ContentProposalAdapter proposalAdapter = null;
        if (control instanceof Combo)
        {
            Combo combo = (Combo) control;
            proposalProvider = new SimpleContentProposalProvider(combo.getItems());
            proposalAdapter = new ContentProposalAdapter(
                    combo,
                    new ComboContentAdapter(),
                    proposalProvider,
                    getActivationKeystroke(),
                    getAutoactivationChars());
        }
        proposalProvider.setFiltering(true);
        proposalAdapter.setPropagateKeys(true);
        proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
    
    }
    
    
    static char[] getAutoactivationChars() {
    
        String delete = new String(new char[] { 8 });
        String allChars = LCL + UCL + NUMS + delete;
        return allChars.toCharArray();
    }
    
    static KeyStroke getActivationKeystroke() {
        KeyStroke instance = KeyStroke.getInstance(
                new Integer(SWT.CTRL).intValue(), new Integer(' ').intValue());
        return instance;
    }
    

    Call

    enableContentProposal(tableComboBox) 
    

    after ComboBox creation.