Project

General

Profile

« Previous | Next » 

Revision a5a9ec69

Added by Katja Luther over 4 years ago

ref #8749: add proposalProvider to combo elements

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/AbstractComboElement.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.ui.combo;
10 10

  
11
import java.util.ArrayList;
12

  
13
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
14
import org.eclipse.jface.fieldassist.ComboContentAdapter;
15
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
16
import org.eclipse.jface.fieldassist.IContentProposal;
17
import org.eclipse.jface.fieldassist.IContentProposalProvider;
11 18
import org.eclipse.swt.SWT;
12 19
import org.eclipse.swt.custom.CCombo;
13 20
import org.eclipse.swt.events.DisposeEvent;
......
47 54
    protected final CCombo combo;
48 55

  
49 56

  
57

  
58

  
50 59
    public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
51 60
        super(formFactory, formElement);
52 61

  
......
61 70
        combo.setLayoutData(fill_HORIZONTALLY);
62 71
        fill_HORIZONTALLY.maxWidth = 50;
63 72
        combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
64

  
73
        ContentProposalAdapter adapter = new ContentProposalAdapter(combo, new ComboContentAdapter(), getProposalProvider(), null, null);
74
        adapter.setPropagateKeys(true);
75
        adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
65 76
        //disable mouse-wheel selection
66 77
        combo.addListener(SWT.MouseWheel, e->e.doit=false);
67 78
    }
......
132 143
    }
133 144

  
134 145
    public abstract void setSelection(T selection);
146

  
147
    IContentProposalProvider getProposalProvider() {
148
        return new IContentProposalProvider() {
149
            @Override
150
            public IContentProposal[] getProposals(String contents, int position) {
151
                String[] items = combo.getItems();
152
                if (contents.length() == 0 || items.length == 0) {
153
                    return new IContentProposal[0];
154
                }
155
                StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
156
                ArrayList<String> matches = new ArrayList<String>();
157
                for (int i = 0; i < items.length; i++) {
158
                    if (matcher.match(items[i])) {
159
                        matches.add(items[i]);
160
                    }
161
                }
162

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

  
171
                if (matches.isEmpty()) {
172
                    return new IContentProposal[0];
173
                }
174

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

  
181
                        @Override
182
                        public String getContent() {
183
                            return proposal;
184
                        }
185

  
186
                        @Override
187
                        public int getCursorPosition() {
188
                            return proposal.length();
189
                        }
190

  
191
                        @Override
192
                        public String getDescription() {
193
                            return null;
194
                        }
195

  
196
                        @Override
197
                        public String getLabel() {
198
                            return null;
199
                        }
200
                    };
201
                }
202
                return proposals;
203
            }
204
        };
205
    }
206

  
135 207
}

Also available in: Unified diff