29
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.fieldassist.SimpleContentProposalProvider;
|
|
14
|
|
15 import dwtx.jface.fieldassist.IContentProposalProvider;
|
|
16 import dwtx.jface.fieldassist.IContentProposal;
|
|
17
|
|
18 import tango.util.collection.ArraySeq;
|
|
19
|
|
20 import dwt.dwthelper.utils;
|
|
21
|
|
22 /**
|
|
23 * SimpleContentProposalProvider is a class designed to map a static list of
|
|
24 * Strings to content proposals.
|
|
25 *
|
|
26 * @see IContentProposalProvider
|
|
27 * @since 3.2
|
|
28 *
|
|
29 */
|
|
30 public class SimpleContentProposalProvider : IContentProposalProvider {
|
|
31
|
|
32 /*
|
|
33 * The proposals provided.
|
|
34 */
|
|
35 private String[] proposals;
|
|
36
|
|
37 /*
|
|
38 * The proposals mapped to IContentProposal. Cached for speed in the case
|
|
39 * where filtering is not used.
|
|
40 */
|
|
41 private IContentProposal[] contentProposals;
|
|
42
|
|
43 /*
|
|
44 * bool that tracks whether filtering is used.
|
|
45 */
|
|
46 private bool filterProposals = false;
|
|
47
|
|
48 /**
|
|
49 * Construct a SimpleContentProposalProvider whose content proposals are
|
|
50 * always the specified array of Objects.
|
|
51 *
|
|
52 * @param proposals
|
|
53 * the array of Strings to be returned whenever proposals are
|
|
54 * requested.
|
|
55 */
|
|
56 public this(String[] proposals) {
|
|
57 //super();
|
|
58 this.proposals = proposals;
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * Return an array of Objects representing the valid content proposals for a
|
|
63 * field. Ignore the current contents of the field.
|
|
64 *
|
|
65 * @param contents
|
|
66 * the current contents of the field (only consulted if filtering
|
|
67 * is set to <code>true</code>)
|
|
68 * @param position
|
|
69 * the current cursor position within the field (ignored)
|
|
70 * @return the array of Objects that represent valid proposals for the field
|
|
71 * given its current content.
|
|
72 */
|
|
73 public IContentProposal[] getProposals(String contents, int position) {
|
|
74 if (filterProposals) {
|
|
75 auto list = new ArraySeq!(IContentProposal);
|
|
76 for (int i = 0; i < proposals.length; i++) {
|
|
77 if (proposals[i].length > contents.length
|
|
78 && proposals[i].substring(0, contents.length)
|
|
79 .equalsIgnoreCase(contents)) {
|
|
80 list.append(makeContentProposal(proposals[i]));
|
|
81 }
|
|
82 }
|
|
83 return list.toArray();
|
|
84 }
|
|
85 if (contentProposals is null) {
|
|
86 contentProposals = new IContentProposal[proposals.length];
|
|
87 for (int i = 0; i < proposals.length; i++) {
|
|
88 contentProposals[i] = makeContentProposal(proposals[i]);
|
|
89 }
|
|
90 }
|
|
91 return contentProposals;
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Set the Strings to be used as content proposals.
|
|
96 *
|
|
97 * @param items
|
|
98 * the array of Strings to be used as proposals.
|
|
99 */
|
|
100 public void setProposals(String[] items) {
|
|
101 this.proposals = items;
|
|
102 contentProposals = null;
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Set the bool that controls whether proposals are filtered according to
|
|
107 * the current field content.
|
|
108 *
|
|
109 * @param filterProposals
|
|
110 * <code>true</code> if the proposals should be filtered to
|
|
111 * show only those that match the current contents of the field,
|
|
112 * and <code>false</code> if the proposals should remain the
|
|
113 * same, ignoring the field content.
|
|
114 * @since 3.3
|
|
115 */
|
|
116 public void setFiltering(bool filterProposals) {
|
|
117 this.filterProposals = filterProposals;
|
|
118 // Clear any cached proposals.
|
|
119 contentProposals = null;
|
|
120 }
|
|
121
|
|
122 /*
|
|
123 * Make an IContentProposal for showing the specified String.
|
|
124 */
|
|
125 private IContentProposal makeContentProposal( String proposal) {
|
|
126 return new class IContentProposal {
|
|
127 String proposal_;
|
|
128 this(){proposal_=proposal;}
|
|
129 public String getContent() {
|
|
130 return proposal_;
|
|
131 }
|
|
132
|
|
133 public String getDescription() {
|
|
134 return null;
|
|
135 }
|
|
136
|
|
137 public String getLabel() {
|
|
138 return null;
|
|
139 }
|
|
140
|
|
141 public int getCursorPosition() {
|
|
142 return proposal_.length;
|
|
143 }
|
|
144 };
|
|
145 }
|
|
146 }
|