blob: 2dff508a1559e34ecc3de20f63fed5251019ca2a (
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, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* 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 new IContentProposalProvider() {
@Override
public IContentProposal[] getProposals(String contents, int 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 (int i = 0; i < items.length; i++)
if (matcher.match(items[i]))
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;
}
};
}
}
|