Unterschiede
Hier werden die Unterschiede zwischen der gewählten und der aktuellen Version gezeigt.
| swt-jface:autocompletefield 2007/09/17 11:10 | swt-jface:autocompletefield 2020/01/22 20:59 aktuell | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | [[swt-jface:swt-jface|Java - SWT/JFace]]\\ | ||
| ====== AutoCompleteField ====== | ====== AutoCompleteField ====== | ||
| - | Die JFace-Methode AutoCompleteField(..) für ein einfaches Textfield kann beispielsweise so verwendet werden: | + | //org.eclipse.jface.fieldassist.AutoCompleteField//\\ |
| + | Die JFace-Klasse AutoCompleteField(..) für ein einfaches Textfield kann beispielsweise so verwendet werden: | ||
| <code java> | <code java> | ||
| Text textField = new Text(); | Text textField = new Text(); | ||
| - | String[] str = {"Ananas", "Apfel", "Apfelsine", "Banane", "Birne", "Kiwi", "Mandarine", "Pflaume", "Weintraube"}; | + | String[] str = {"Ananas", "Apfel", "Banane", "Birne", "Kiwi", "Pflaume" }; |
| new AutoCompleteField(textField, new TextContentAdapter(), str); | new AutoCompleteField(textField, new TextContentAdapter(), str); | ||
| + | </code> | ||
| + | Sollte das TextField über ein Binding verfügen, so muss das Binding **nach** Erstellung des AutoCompleteField's durchgeführt werden.\\ | ||
| + | \\ | ||
| + | <html> | ||
| + | <script type="text/javascript"><!-- | ||
| + | google_ad_client="pub-9681858985507948"; | ||
| + | google_ad_width = 468; | ||
| + | google_ad_height = 60; | ||
| + | google_ad_format = "468x60_as"; | ||
| + | google_ad_type = "text"; | ||
| + | google_ad_channel = ""; | ||
| + | google_color_border = "cccccc"; | ||
| + | google_color_bg = "FFFFFF"; | ||
| + | google_color_link = "1d2d8c"; | ||
| + | google_color_text = "000000"; | ||
| + | google_color_url = "1d2d8c"; | ||
| + | //--> | ||
| + | </script> | ||
| + | <script type="text/javascript" | ||
| + | src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> | ||
| + | </script> | ||
| + | </html> | ||
| + | \\ | ||
| + | \\ | ||
| + | //org.eclipse.jface.fieldassist.IContentProposal//\\ | ||
| + | //org.eclipse.jface.fieldassist.IContentProposalProvider//\\ | ||
| + | Möchte man in der Auswahlliste einen anderen Text anzeigen, als hinterher übernommen wird, so muss eine eigene Klasse MyAutoCompleteField erstellt werden, welche eine eigene Klasse MyContentProposalProvider instanziert. | ||
| + | Die Klasse MyContenteProposalProvider kann am Einfachsten durch eine Kopie von SimpleContentProposalProvider erzeugt werden. Dort muss dann die Methode makeContentProposal angepasst werden.\\ | ||
| + | Dabei liefert die Methode getContent() den Wert, welcher im Textfeld erscheint. Die Methode getLabel() liefert den Text, welcher in der Auswahl-Liste unter dem Textfeld angezeigt wird und die Methode getDescriptionen() einen zusätzlichen ausführlichen Text, welcher für den jeweils ausgewählten Eintrag der Liste in einem seperaten Fenster daneben angezeigt werden kann.\\ | ||
| + | <code java> | ||
| + | Text textField = new Text(); | ||
| + | String[] str = {"Ananas", "Apfel", "Banane", "Birne", "Kiwi", "Pflaume" }; | ||
| + | String[] str2 = {"besonders lecker", "schön grün", "wunderbar gebogen", | ||
| + | "irgendwie nicht rund", "exotisch", "hmm" }; | ||
| + | new MyAutoCompleteField(textField, new TextContentAdapter(), str, str2); | ||
| + | </code> | ||
| + | \\ | ||
| + | <code java> | ||
| + | package com.sowas.javawiki.autocomplete; | ||
| + | |||
| + | import org.eclipse.jface.fieldassist.ContentProposalAdapter; | ||
| + | import org.eclipse.jface.fieldassist.IControlContentAdapter; | ||
| + | import org.eclipse.swt.widgets.Control; | ||
| + | |||
| + | public class MyAutoCompleteField { | ||
| + | private ContentProposalProvider contentProposalProvider; | ||
| + | private ContentProposalAdapter contentProposalAdapter; | ||
| + | |||
| + | public MyAutoCompleteField(final Control control, | ||
| + | final IControlContentAdapter controlContentAdapter, | ||
| + | final String[] literals, | ||
| + | final String[] labels) { | ||
| + | contentProposalProvider = new ContentProposalProvider(literals, labels); | ||
| + | contentProposalProvider.setFiltering(false); | ||
| + | contentProposalAdapter = new ContentProposalAdapter(control, controlContentAdapter, contentProposalProvider, null, null); | ||
| + | contentProposalAdapter.setPropagateKeys(true); | ||
| + | contentProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE); | ||
| + | } | ||
| + | |||
| + | public void setProposals(final String[] proposals) { | ||
| + | contentProposalProvider.setProposals(proposals); | ||
| + | } | ||
| + | |||
| + | public ContentProposalProvider getContentProposalProvider() { | ||
| + | return contentProposalProvider; | ||
| + | } | ||
| + | |||
| + | public ContentProposalAdapter getContentProposalAdapter() { | ||
| + | return contentProposalAdapter; | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | \\ | ||
| + | [[http://www.fotostacker.de|{{:fotostacker2.png|FotoStacker}}]]FotoStacker, Fotos für Ihre Website\\ | ||
| + | \\ | ||
| + | <code java> | ||
| + | package com.sowas.javawiki.autocomplete; | ||
| + | |||
| + | import java.util.ArrayList; | ||
| + | |||
| + | import org.eclipse.jface.fieldassist.IContentProposal; | ||
| + | import org.eclipse.jface.fieldassist.IContentProposalProvider; | ||
| + | |||
| + | public class ContentProposalProvider implements IContentProposalProvider { | ||
| + | private String[] proposals; | ||
| + | private String[] labels; | ||
| + | private IContentProposal[] contentProposals; | ||
| + | private boolean filterProposals = false; | ||
| + | |||
| + | public ContentProposalProvider(String[] proposals, String[] labels) { | ||
| + | super(); | ||
| + | this.proposals = proposals; | ||
| + | this.labels = labels; | ||
| + | } | ||
| + | |||
| + | public IContentProposal[] getProposals(String contents, int position) { | ||
| + | if (filterProposals) { | ||
| + | ArrayList list = new ArrayList(); | ||
| + | for (int i = 0; i < proposals.length; i++) { | ||
| + | if (proposals[i].length() >= contents.length() | ||
| + | && proposals[i].substring(0, contents.length()).equalsIgnoreCase(contents)) { | ||
| + | list.add(makeContentProposal(proposals[i], labels[i])); | ||
| + | } | ||
| + | } | ||
| + | return (IContentProposal[]) list.toArray(new IContentProposal[list.size()]); | ||
| + | } | ||
| + | if (contentProposals == null) { | ||
| + | contentProposals = new IContentProposal[proposals.length]; | ||
| + | for (int i = 0; i < proposals.length; i++) { | ||
| + | contentProposals[i] = makeContentProposal(proposals[i], labels[i]); | ||
| + | } | ||
| + | } | ||
| + | return contentProposals; | ||
| + | } | ||
| + | |||
| + | public void setProposals(String[] items) { | ||
| + | this.proposals = items; | ||
| + | contentProposals = null; | ||
| + | } | ||
| + | |||
| + | public void setFiltering(boolean filterProposals) { | ||
| + | this.filterProposals = filterProposals; | ||
| + | contentProposals = null; | ||
| + | } | ||
| + | |||
| + | private IContentProposal makeContentProposal(final String proposal, final String label) { | ||
| + | return new IContentProposal() { | ||
| + | |||
| + | public String getContent() { | ||
| + | return proposal; | ||
| + | } | ||
| + | |||
| + | public String getDescription() { | ||
| + | // Wenn hier was zurückgegeben wird, dann erscheint dieser Text in einem seperatem Fenster | ||
| + | return null; | ||
| + | } | ||
| + | |||
| + | public String getLabel() { | ||
| + | return proposal + " - " + label; | ||
| + | } | ||
| + | |||
| + | public int getCursorPosition() { | ||
| + | return proposal.length(); | ||
| + | } | ||
| + | }; | ||
| + | } | ||
| + | } | ||
| </code> | </code> | ||