KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > ListContentProposalProvider


1 package org.apache.directory.ldapstudio.browser.common.widgets;
2
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.eclipse.jface.fieldassist.IContentProposal;
10 import org.eclipse.jface.fieldassist.IContentProposalProvider;
11
12
13 /**
14  * ListContentProposalProvider is a class designed to map a dynamic list of
15  * Strings to content proposals.
16  *
17  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
18  * @version $Rev$, $Date$
19  */

20 public class ListContentProposalProvider implements IContentProposalProvider
21 {
22
23     /** The dynamic list of proposals */
24     private List JavaDoc<String JavaDoc> proposals;
25
26
27     /**
28      * Creates a new instance of ListContentProposalProvider.
29      *
30      * @param proposals the dynamic list of proposals
31      */

32     public ListContentProposalProvider( List JavaDoc<String JavaDoc> proposals )
33     {
34         setProposals( proposals );
35     }
36
37
38     /**
39      * Creates a new instance of ListContentProposalProvider.
40      *
41      * @param proposals the proposals
42      */

43     public ListContentProposalProvider( String JavaDoc[] proposals )
44     {
45         setProposals( new ArrayList JavaDoc<String JavaDoc>( Arrays.asList( proposals ) ) );
46     }
47
48
49     /**
50      * {@inheritDoc}
51      */

52     public IContentProposal[] getProposals( String JavaDoc contents, int position )
53     {
54         String JavaDoc string = contents.substring( 0, position );
55
56         Collections.sort( proposals );
57
58         List JavaDoc<IContentProposal> proposalList = new ArrayList JavaDoc<IContentProposal>();
59         for ( int k = 0; k < proposals.size(); k++ )
60         {
61             final String JavaDoc proposal = proposals.get( k );
62             if ( proposal.toUpperCase().startsWith( string.toUpperCase() ) && !proposal.equalsIgnoreCase( string )
63                 && !"".equals( string ) )
64             {
65                 IContentProposal p = new IContentProposal()
66                 {
67                     public String JavaDoc getContent()
68                     {
69                         return proposal;
70                     }
71
72
73                     public String JavaDoc getDescription()
74                     {
75                         return proposal;
76                     }
77
78
79                     public String JavaDoc getLabel()
80                     {
81                         return proposal;
82                     }
83
84
85                     public int getCursorPosition()
86                     {
87                         return proposal.length();
88                     }
89                 };
90                 proposalList.add( p );
91             }
92         }
93         return proposalList.toArray( new IContentProposal[proposalList.size()] );
94     }
95
96
97     /**
98      * Sets the possible strings.
99      *
100      * @param proposals the possible strings
101      */

102     public void setProposals( List JavaDoc<String JavaDoc> proposals )
103     {
104         if ( proposals == null )
105         {
106             proposals = new ArrayList JavaDoc<String JavaDoc>();
107         }
108
109         this.proposals = proposals;
110     }
111
112 }
113
Popular Tags