Mercurial > projects > dwt-addons
annotate dwtx/jface/fieldassist/SimpleContentProposalProvider.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 46a6e0e6ccd4 |
children |
rev | line source |
---|---|
29 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
2 * Copyright (c) 2005, 2007 IBM Corporation and others. |
29 | 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
10 * Amir Kouchekinia <amir@pyrus.us> - bug 200762 |
29 | 11 * Port to the D programming language: |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.fieldassist.SimpleContentProposalProvider; | |
15 | |
16 import dwtx.jface.fieldassist.IContentProposalProvider; | |
17 import dwtx.jface.fieldassist.IContentProposal; | |
18 | |
19 | |
20 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
21 import dwtx.dwtxhelper.Collection; |
29 | 22 |
23 /** | |
24 * SimpleContentProposalProvider is a class designed to map a static list of | |
25 * Strings to content proposals. | |
26 * | |
27 * @see IContentProposalProvider | |
28 * @since 3.2 | |
29 * | |
30 */ | |
31 public class SimpleContentProposalProvider : IContentProposalProvider { | |
32 | |
33 /* | |
34 * The proposals provided. | |
35 */ | |
36 private String[] proposals; | |
37 | |
38 /* | |
39 * The proposals mapped to IContentProposal. Cached for speed in the case | |
40 * where filtering is not used. | |
41 */ | |
42 private IContentProposal[] contentProposals; | |
43 | |
44 /* | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
45 * Boolean that tracks whether filtering is used. |
29 | 46 */ |
47 private bool filterProposals = false; | |
48 | |
49 /** | |
50 * Construct a SimpleContentProposalProvider whose content proposals are | |
51 * always the specified array of Objects. | |
52 * | |
53 * @param proposals | |
54 * the array of Strings to be returned whenever proposals are | |
55 * requested. | |
56 */ | |
57 public this(String[] proposals) { | |
58 //super(); | |
59 this.proposals = proposals; | |
60 } | |
61 | |
62 /** | |
63 * Return an array of Objects representing the valid content proposals for a | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
64 * field. |
29 | 65 * |
66 * @param contents | |
67 * the current contents of the field (only consulted if filtering | |
68 * is set to <code>true</code>) | |
69 * @param position | |
70 * the current cursor position within the field (ignored) | |
71 * @return the array of Objects that represent valid proposals for the field | |
72 * given its current content. | |
73 */ | |
74 public IContentProposal[] getProposals(String contents, int position) { | |
75 if (filterProposals) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
76 ArrayList list = new ArrayList(); |
29 | 77 for (int i = 0; i < proposals.length; i++) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
78 if (proposals[i].length >= contents.length |
29 | 79 && proposals[i].substring(0, contents.length) |
80 .equalsIgnoreCase(contents)) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
81 list.add(cast(Object)makeContentProposal(proposals[i])); |
29 | 82 } |
83 } | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
84 return arraycast!(IContentProposal)(list.toArray()); |
29 | 85 } |
86 if (contentProposals is null) { | |
87 contentProposals = new IContentProposal[proposals.length]; | |
88 for (int i = 0; i < proposals.length; i++) { | |
89 contentProposals[i] = makeContentProposal(proposals[i]); | |
90 } | |
91 } | |
92 return contentProposals; | |
93 } | |
94 | |
95 /** | |
96 * Set the Strings to be used as content proposals. | |
97 * | |
98 * @param items | |
99 * the array of Strings to be used as proposals. | |
100 */ | |
101 public void setProposals(String[] items) { | |
102 this.proposals = items; | |
103 contentProposals = null; | |
104 } | |
105 | |
106 /** | |
107 * Set the bool that controls whether proposals are filtered according to | |
108 * the current field content. | |
109 * | |
110 * @param filterProposals | |
111 * <code>true</code> if the proposals should be filtered to | |
112 * show only those that match the current contents of the field, | |
113 * and <code>false</code> if the proposals should remain the | |
114 * same, ignoring the field content. | |
115 * @since 3.3 | |
116 */ | |
117 public void setFiltering(bool filterProposals) { | |
118 this.filterProposals = filterProposals; | |
119 // Clear any cached proposals. | |
120 contentProposals = null; | |
121 } | |
122 | |
123 /* | |
124 * Make an IContentProposal for showing the specified String. | |
125 */ | |
126 private IContentProposal makeContentProposal( String proposal) { | |
127 return new class IContentProposal { | |
128 String proposal_; | |
129 this(){proposal_=proposal;} | |
130 public String getContent() { | |
131 return proposal_; | |
132 } | |
133 | |
134 public String getDescription() { | |
135 return null; | |
136 } | |
137 | |
138 public String getLabel() { | |
139 return null; | |
140 } | |
141 | |
142 public int getCursorPosition() { | |
143 return proposal_.length; | |
144 } | |
145 }; | |
146 } | |
147 } |