]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/contentassist/NamedObjectContentProposalProvider.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / contentassist / NamedObjectContentProposalProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.contentassist;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
18
19 import org.eclipse.jface.fieldassist.IContentProposal;
20 import org.eclipse.jface.fieldassist.IContentProposalProvider;
21
22 /**
23  * @author Tuukka Lehtonen
24  *
25  * @param <T>
26  */
27 public class NamedObjectContentProposalProvider<T extends INamedObject> implements IContentProposalProvider {
28
29     private final Collection<T> content;
30
31     public NamedObjectContentProposalProvider(T[] content) {
32         this(Arrays.asList(content));
33     }
34
35     public NamedObjectContentProposalProvider(Collection<T> content) {
36         this.content = content;
37     }
38
39     @Override
40     public IContentProposal[] getProposals(String contents, int position) {
41         String prefix = contents.substring(0, position).toLowerCase();
42
43         List<INamedObject> result = new ArrayList<INamedObject>(content.size());
44         for (INamedObject obj : content) {
45             if (obj.getName().toLowerCase().startsWith(prefix))
46                 result.add(obj);
47         }
48
49         IContentProposal[] proposals = new IContentProposal[result.size()];
50         for (int i = 0; i < result.size(); ++i)
51             proposals[i] = createProposal(result.get(i), position);
52
53         return proposals;
54     }
55
56     public INamedObjectContentProposal createProposal(INamedObject object, int position) {
57         return new NamedObjectContentProposal((NamedObject<?>) object, position);
58     }
59
60 }