KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > fieldassist > SimpleContentProposalProvider


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  *******************************************************************************/

11 package org.eclipse.jface.fieldassist;
12
13 import java.util.ArrayList JavaDoc;
14
15 /**
16  * SimpleContentProposalProvider is a class designed to map a static list of
17  * Strings to content proposals.
18  *
19  * @see IContentProposalProvider
20  * @since 3.2
21  *
22  */

23 public class SimpleContentProposalProvider implements IContentProposalProvider {
24
25     /*
26      * The proposals provided.
27      */

28     private String JavaDoc[] proposals;
29
30     /*
31      * The proposals mapped to IContentProposal. Cached for speed in the case
32      * where filtering is not used.
33      */

34     private IContentProposal[] contentProposals;
35
36     /*
37      * Boolean that tracks whether filtering is used.
38      */

39     private boolean filterProposals = false;
40
41     /**
42      * Construct a SimpleContentProposalProvider whose content proposals are
43      * always the specified array of Objects.
44      *
45      * @param proposals
46      * the array of Strings to be returned whenever proposals are
47      * requested.
48      */

49     public SimpleContentProposalProvider(String JavaDoc[] proposals) {
50         super();
51         this.proposals = proposals;
52     }
53
54     /**
55      * Return an array of Objects representing the valid content proposals for a
56      * field. Ignore the current contents of the field.
57      *
58      * @param contents
59      * the current contents of the field (only consulted if filtering
60      * is set to <code>true</code>)
61      * @param position
62      * the current cursor position within the field (ignored)
63      * @return the array of Objects that represent valid proposals for the field
64      * given its current content.
65      */

66     public IContentProposal[] getProposals(String JavaDoc contents, int position) {
67         if (filterProposals) {
68             ArrayList JavaDoc list = new ArrayList JavaDoc();
69             for (int i = 0; i < proposals.length; i++) {
70                 if (proposals[i].length() > contents.length()
71                         && proposals[i].substring(0, contents.length())
72                                 .equalsIgnoreCase(contents)) {
73                     list.add(makeContentProposal(proposals[i]));
74                 }
75             }
76             return (IContentProposal[]) list.toArray(new IContentProposal[list
77                     .size()]);
78         }
79         if (contentProposals == null) {
80             contentProposals = new IContentProposal[proposals.length];
81             for (int i = 0; i < proposals.length; i++) {
82                 contentProposals[i] = makeContentProposal(proposals[i]);
83             }
84         }
85         return contentProposals;
86     }
87
88     /**
89      * Set the Strings to be used as content proposals.
90      *
91      * @param items
92      * the array of Strings to be used as proposals.
93      */

94     public void setProposals(String JavaDoc[] items) {
95         this.proposals = items;
96         contentProposals = null;
97     }
98
99     /**
100      * Set the boolean that controls whether proposals are filtered according to
101      * the current field content.
102      *
103      * @param filterProposals
104      * <code>true</code> if the proposals should be filtered to
105      * show only those that match the current contents of the field,
106      * and <code>false</code> if the proposals should remain the
107      * same, ignoring the field content.
108      * @since 3.3
109      */

110     public void setFiltering(boolean filterProposals) {
111         this.filterProposals = filterProposals;
112         // Clear any cached proposals.
113
contentProposals = null;
114     }
115
116     /*
117      * Make an IContentProposal for showing the specified String.
118      */

119     private IContentProposal makeContentProposal(final String JavaDoc proposal) {
120         return new IContentProposal() {
121             public String JavaDoc getContent() {
122                 return proposal;
123             }
124
125             public String JavaDoc getDescription() {
126                 return null;
127             }
128
129             public String JavaDoc getLabel() {
130                 return null;
131             }
132
133             public int getCursorPosition() {
134                 return proposal.length();
135             }
136         };
137     }
138 }
139
Popular Tags