blob: ea428e24a153fce4b7e3ac403d2ed5a1b2edf107 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*******************************************************************************
* Copyright (c) 2009, 2018 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
******************************************************************************/
package org.eclipse.equinox.internal.p2.ui.dialogs;
import java.util.ArrayList;
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
import org.eclipse.jface.fieldassist.*;
import org.eclipse.swt.widgets.Combo;
/**
* ComboAutoCompleteField is an auto complete field appropriate for
* pattern matching the text in a combo to the contents of the combo.
* If the proposals should include items outside of the combo, then
* clients can set their own proposal strings.
*
* @since 3.5
*/
public class ComboAutoCompleteField {
ContentProposalAdapter adapter;
Combo combo;
String[] proposalStrings = null;
public ComboAutoCompleteField(Combo c) {
this.combo = c;
adapter = new ContentProposalAdapter(combo, new ComboContentAdapter(), getProposalProvider(), null, null);
adapter.setPropagateKeys(true);
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}
public void setProposalStrings(String[] proposals) {
proposalStrings = proposals;
}
String[] getStringItems() {
if (proposalStrings == null)
return combo.getItems();
return proposalStrings;
}
IContentProposalProvider getProposalProvider() {
return (contents, position) -> {
String[] items = getStringItems();
if (contents.length() == 0 || items.length == 0)
return new IContentProposal[0];
StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
ArrayList<String> matches = new ArrayList<>();
for (String item : items)
if (matcher.match(item))
matches.add(item);
// 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 i2 = 0; i2 < matches.size(); i2++) {
final String proposal = matches.get(i2);
proposals[i2] = 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;
};
}
}
|